aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Liodden <[email protected]>2017-07-15 16:42:42 +0200
committerErik Liodden <[email protected]>2017-07-15 16:42:42 +0200
commit738b3d1a7b6b8df80d8c71861479c063672bb2c7 (patch)
treed557f88629051ecef757ee2ad0af917120825a2b
parent6a57108df0e3d622a78559e42be76caf5ada5fce (diff)
downloadbinary-chopper-738b3d1a7b6b8df80d8c71861479c063672bb2c7.tar.gz
remove unnecessary *bpcHEADmaster
found and easier way to calculate bytes per chunk, and removed the corresponding function. this commit also take advantage of the `error` module.
-rw-r--r--include/cut.h1
-rw-r--r--include/file.h2
-rw-r--r--src/file/cut.c63
-rw-r--r--src/file/file.c21
-rw-r--r--src/file/merge.c22
5 files changed, 49 insertions, 60 deletions
diff --git a/include/cut.h b/include/cut.h
index 420e0e7..3ee202b 100644
--- a/include/cut.h
+++ b/include/cut.h
@@ -1,7 +1,6 @@
#ifndef _CUT_H
#define _CUT_H
-uint64_t *bytes_per_chunk(char *file_name, int parts);
int write_chunks(char *file_name, int parts);
#endif
diff --git a/include/file.h b/include/file.h
index ddf9172..dc79b24 100644
--- a/include/file.h
+++ b/include/file.h
@@ -4,7 +4,7 @@
#define HEADER_SIZE 256
#define BUF_SIZE 8388608
-uint64_t get_file_size(char *name);
FILE *open_file(char *name, char *mode);
+struct stat *get_file_info(char *name);
#endif
diff --git a/src/file/cut.c b/src/file/cut.c
index 7219b31..506b9a4 100644
--- a/src/file/cut.c
+++ b/src/file/cut.c
@@ -8,46 +8,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
-#include <inttypes.h>
+#include <stdint.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include "file.h"
#include "cut.h"
-
-/* bytes_per_chunk: return an array of number of bytes per chunk
- * TODO: This one could/should be written better.
- */
-uint64_t *bytes_per_chunk(char *file_name, int parts)
-{
- uint64_t fsize, base;
- int rest, i;
- uint64_t *bytes_per_chunk = malloc(parts * sizeof(uint64_t));
-
- fsize = get_file_size(file_name);
- rest = fsize % parts;
- fsize -= rest;
- base = fsize / parts;
-
- for (i = 0; i < parts; i++) {
- bytes_per_chunk[i] = base;
- }
- bytes_per_chunk[parts-1] += rest;
-
- return bytes_per_chunk;
-}
+#include "error.h"
/* write_chunks: write bytes to .bin files of size given in bpc */
int write_chunks(char *file_name, int parts)
{
- int i, bytes;
- FILE *fp_out, *fp_in;
+ int i, n;
+ FILE *fp_in, *fp_out;
+ uint64_t psize, tmp;
char *buffer = malloc(BUF_SIZE);
- uint64_t *bpc;
+ struct stat *stbuf = get_file_info(file_name);
fp_in = open_file(file_name, "rb");
- bpc = bytes_per_chunk(file_name, parts);
- bytes = 0;
+ psize = (stbuf->st_size - (stbuf->st_size % parts)) / (uint64_t) parts;
for (i = 0; i < parts; i++) {
+ if (parts - i == 1)
+ psize += stbuf->st_size % parts;
+
snprintf(buffer, BUF_SIZE, "data%d.bin", i);
fp_out = open_file(buffer, "wb"); /* open file to write to */
@@ -56,25 +40,30 @@ int write_chunks(char *file_name, int parts)
*/
memset(buffer, 0, BUF_SIZE);
snprintf(buffer, HEADER_SIZE, "%d %llu %s", (i+1) % parts,
- bpc[i], file_name);
+ psize, file_name);
fwrite(buffer, 1, HEADER_SIZE, fp_out);
/* Read to buffer and write to chunk.
* Read and write one full buffer at a time.
*/
- while (bpc[i] > BUF_SIZE) {
- bytes += fread(buffer, 1, BUF_SIZE, fp_in);
- bytes -= fwrite(buffer, 1, BUF_SIZE, fp_out);
- bpc[i] -= BUF_SIZE;
+ tmp = psize;
+ while (tmp > BUF_SIZE) {
+ n = fread(buffer, 1, BUF_SIZE, fp_in);
+ if (fwrite(buffer, 1, BUF_SIZE, fp_out) != n)
+ error(errno, "error writing file data%d.bin", i);
+ tmp -= n;
}
/* read and write the rest that did not fill buffer */
- bytes += fread(buffer, 1, bpc[i], fp_in);
- bytes -= fwrite(buffer, 1, bpc[i], fp_out);
+ n = fread(buffer, 1, tmp, fp_in);
+ if (fwrite(buffer, 1, n, fp_out) != n)
+ error(errno, "error writing file \
+ data%d.bin, tmp: %d", i, tmp);
+
fclose(fp_out);
}
fclose(fp_in);
free(buffer);
- free(bpc);
+ free(stbuf);
- return bytes;
+ return 0;
}
diff --git a/src/file/file.c b/src/file/file.c
index 862c1ea..db37b25 100644
--- a/src/file/file.c
+++ b/src/file/file.c
@@ -6,20 +6,13 @@
*/
#include <sys/stat.h>
+#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "file.h"
+#include "error.h"
-/* get_file_size: return size of file with filename 'name' */
-uint64_t get_file_size(char *name)
-{
- uint64_t size;
- struct stat st;
- stat(name, &st);
- size = st.st_size;
- return size;
-}
/* open_file: open file and return pointer to the file if it exists. */
FILE *open_file(char *name, char *mode)
@@ -27,8 +20,16 @@ FILE *open_file(char *name, char *mode)
FILE *fp;
fp = fopen(name, mode);
if (fp == NULL) {
- perror("cant open file");
+ error(errno, "cant open file");
exit(1);
}
return fp;
}
+
+/* get_file_info: return struct with info about file */
+struct stat *get_file_info(char *name)
+{
+ struct stat *stbuf = malloc(sizeof(struct stat));
+ stat(name, stbuf);
+ return stbuf;
+}
diff --git a/src/file/merge.c b/src/file/merge.c
index e88cd0b..e00ba53 100644
--- a/src/file/merge.c
+++ b/src/file/merge.c
@@ -12,11 +12,12 @@
#include <string.h>
#include "merge.h"
#include "file.h"
+#include "error.h"
int merge_chunks(char *file_name)
{
- int id_next, bytes=0;
+ int id_next, n;
FILE *fp_out = NULL;
FILE *fp_chunk;
uint64_t size;
@@ -26,7 +27,8 @@ int merge_chunks(char *file_name)
do {
fp_chunk = open_file(buffer, "rb");
- fread(buffer, 1, HEADER_SIZE, fp_chunk);
+ if (fread(buffer, 1, HEADER_SIZE, fp_chunk) != HEADER_SIZE)
+ error(errno, "Could not read header");
sscanf(buffer, "%d %llu %s", &id_next, &size, out_file);
if (file_name != NULL && strcmp(out_file, file_name) != 0)
strcpy(out_file, file_name);
@@ -37,17 +39,15 @@ int merge_chunks(char *file_name)
* Read and write one full buffer at a time.
*/
while (size > BUF_SIZE) {
- bytes += fread(buffer, 1, BUF_SIZE, fp_chunk);
- bytes -= fwrite(buffer, 1, BUF_SIZE, fp_out);
+ n = fread(buffer, 1, BUF_SIZE, fp_chunk);
+ if (fwrite(buffer, 1, BUF_SIZE, fp_out) != n)
+ error(errno, "error writing to file");
size -= BUF_SIZE;
}
/* read and write the rest that did not fill buffer */
- bytes += fread(buffer, 1, size, fp_chunk);
- bytes -= fwrite(buffer, 1, size, fp_out);
- if (bytes != 0) {
- fprintf(stderr, "bytes in != bytes out. Hmm\n");
- exit(1);
- }
+ n = fread(buffer, 1, size, fp_chunk);
+ if (fwrite(buffer, 1, size, fp_out) != n)
+ error(errno, "error writing to file");
fclose(fp_chunk);
snprintf(buffer, BUF_SIZE, "data%d.bin", id_next);
} while (id_next);
@@ -55,5 +55,5 @@ int merge_chunks(char *file_name)
fclose(fp_out);
free(buffer);
- return bytes;
+ return 0;
}