diff --git a/cmdline/mktest.c b/cmdline/mktest.c index 30406143..5b25a6e1 100644 --- a/cmdline/mktest.c +++ b/cmdline/mktest.c @@ -47,6 +47,22 @@ void rndnz_range(unsigned char* data, int size) data[i] = rndnz(256); } +void rndnz_damage(unsigned char* data, int size) +{ + int i; + + /* corrupt ensuring always different data */ + for(i=0;i st.st_size) @@ -338,13 +306,42 @@ void cmd_write(const char* path, int size) /* start at random position inside the file */ if (size < st.st_size) - start = rnd(st.st_size - size); + off = rnd(st.st_size - size); else - start = 0; + off = 0; - rndnz_range(data + start, size); + data = malloc(size); - file_write(path, data, st.st_size, 0); + rndnz_range(data, size); + + f = open(path, O_WRONLY | O_BINARY | O_NOFOLLOW); + if (f < 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error creating file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (lseek(f, off, SEEK_SET) != off) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error seeking file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (write(f, data, size) != size) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error writing file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (close(f) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error closing file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } free(data); } @@ -378,11 +375,9 @@ void cmd_damage(const char* path, int size) return; if (S_ISREG(st.st_mode)) { - off_t start; + off_t off; unsigned char* data; - int i; - - data = file_read(path, st.st_size, 0); + int f; /* not over the end */ if (size > st.st_size) @@ -390,22 +385,63 @@ void cmd_damage(const char* path, int size) /* start at random position inside the file */ if (size < st.st_size) - start = rnd(st.st_size - size); + off = rnd(st.st_size - size); else - start = 0; + off = 0; - /* corrupt ensuring always different data */ - for(i=0;i