From 6c88c4497086d9f3eb1ee2b3143e0be7123110cf Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Fri, 11 Jul 2014 19:57:58 +0200 Subject: [PATCH] Reworks the test implementation. --- cmdline/mktest.c | 606 ++++++++++++++++++++++++----------------------- 1 file changed, 306 insertions(+), 300 deletions(-) diff --git a/cmdline/mktest.c b/cmdline/mktest.c index e1f9077d..9454fe60 100644 --- a/cmdline/mktest.c +++ b/cmdline/mktest.c @@ -17,6 +17,9 @@ #include "portable.h" +/****************************************************************************/ +/* random */ + /** * Pseudo random number generator. */ @@ -29,7 +32,7 @@ unsigned rnd(unsigned max) return (seed >> 32) % max; } -unsigned rndnotzero(unsigned max) +unsigned rndnz(unsigned max) { if (max <= 1) return 1; @@ -37,6 +40,13 @@ unsigned rndnotzero(unsigned max) return rnd(max - 1) + 1; } +void rndnz_range(unsigned char* data, int size) +{ + int i; + for(i=0;ist_mtime; + if (STAT_NSEC(st) != STAT_NSEC_INVALID) + tv[0].tv_nsec = STAT_NSEC(st); + else + tv[0].tv_nsec = 0; + tv[1].tv_sec = tv[0].tv_sec; + tv[1].tv_nsec = tv[0].tv_nsec; + + ret = futimens(f, tv); +#elif HAVE_FUTIMES /* fallback to futimes() if nanosecond precision is not available */ + tv[0].tv_sec = st->st_mtime; + if (STAT_NSEC(st) != STAT_NSEC_INVALID) + tv[0].tv_usec = STAT_NSEC(st) / 1000; + else + tv[0].tv_usec = 0; + tv[1].tv_sec = tv[0].tv_sec; + tv[1].tv_usec = tv[0].tv_usec; + + ret = futimes(f, tv); +#elif HAVE_FUTIMESAT /* fallback to futimesat() for Solaris, it only has futimesat() */ + tv[0].tv_sec = st->st_mtime; + if (STAT_NSEC(st) != STAT_NSEC_INVALID) + tv[0].tv_usec = STAT_NSEC(st) / 1000; + else + tv[0].tv_usec = 0; + tv[1].tv_sec = tv[0].tv_sec; + tv[1].tv_usec = tv[0].tv_usec; + + ret = futimesat(f, 0, tv); +#else +#error No function available to set file timestamps +#endif + if (ret != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error restoring time\n"); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } +} + +unsigned char* file_read(const char* path, int size, int extra_alloc) +{ + unsigned char* data; + int f; + + data = malloc(size + extra_alloc); + + f = open(path, O_RDONLY); + if (f < 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error opening file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (read(f, data, size) != size) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error reading 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 */ + } + + return data; +} + +void file_write(const char* path, const unsigned char* data, int size, struct stat* st) +{ + int f; + + f = creat(path, S_IRUSR | S_IWUSR); + if (f < 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error creating 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 (st != 0) { + file_fallback(f, st); + } + + if (close(f) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error closing file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } +} + +/****************************************************************************/ +/* cmd */ + +/** + * Creates a file with random content. + * - If the file exists it's rewritten, but avoiding to truncating it to 0. + */ +void cmd_generate_file(const char* path, int size) +{ + unsigned char* data; + + /* remove the existing file/symlink if any */ if (remove(path) != 0) { if (errno != ENOENT) { /* LCOV_EXCL_START */ @@ -68,32 +205,28 @@ void create_file(const char* path, int size) ++size; } - f = fopen(path, "wb"); - if (!f) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error writing file %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } + data = malloc(size); - count = size; - while (count) { - /* We don't write zero bytes because we want to test */ - /* the recovering of new files, after an aborted sync */ - /* If the files contains full blocks at zero */ - /* this is an impossible condition to recover */ - /* because we cannot differentiate between an unused block */ - /* and a file filled with 0 */ - fputc(rndnotzero(256), f); - --count; - } + /* We don't write zero bytes because we want to test */ + /* the recovering of new files, after an aborted sync */ + /* If the files contains full blocks at zero */ + /* this is an impossible condition to recover */ + /* because we cannot differentiate between an unused block */ + /* and a file filled with 0 */ + rndnz_range(data, size); - fclose(f); + file_write(path, data, size, 0); + + free(data); } -void create_symlink(const char* path, const char* linkto) +/** + * Create a symlink. + * - If the file already exists, it's removed. + */ +void cmd_generate_symlink(const char* path, const char* linkto) { - /* remove the existing file if any */ + /* remove the existing file/symlink if any */ if (remove(path) != 0) { if (errno != ENOENT) { /* LCOV_EXCL_START */ @@ -111,7 +244,10 @@ void create_symlink(const char* path, const char* linkto) } } -void generate(int disk, int size) +/** + * Create a file or a symlink with a random name. + */ +void cmd_generate(int disk, int size) { char path[PATH_MAX]; char* file; @@ -163,75 +299,27 @@ void generate(int disk, int size) rnd_name(linkto); - create_symlink(path, linkto); + cmd_generate_symlink(path, linkto); } else #endif { /* file */ - create_file(path, size); + cmd_generate_file(path, size); } } -void fallback(int f, struct stat* st) -{ -#if HAVE_FUTIMENS - struct timespec tv[2]; -#else - struct timeval tv[2]; -#endif - int ret; - -#if HAVE_FUTIMENS /* futimens() is preferred because it gives nanosecond precision */ - tv[0].tv_sec = st->st_mtime; - if (STAT_NSEC(st) != STAT_NSEC_INVALID) - tv[0].tv_nsec = STAT_NSEC(st); - else - tv[0].tv_nsec = 0; - tv[1].tv_sec = tv[0].tv_sec; - tv[1].tv_nsec = tv[0].tv_nsec; - - ret = futimens(f, tv); -#elif HAVE_FUTIMES /* fallback to futimes() if nanosecond precision is not available */ - tv[0].tv_sec = st->st_mtime; - if (STAT_NSEC(st) != STAT_NSEC_INVALID) - tv[0].tv_usec = STAT_NSEC(st) / 1000; - else - tv[0].tv_usec = 0; - tv[1].tv_sec = tv[0].tv_sec; - tv[1].tv_usec = tv[0].tv_usec; - - ret = futimes(f, tv); -#elif HAVE_FUTIMESAT /* fallback to futimesat() for Solaris, it only has futimesat() */ - tv[0].tv_sec = st->st_mtime; - if (STAT_NSEC(st) != STAT_NSEC_INVALID) - tv[0].tv_usec = STAT_NSEC(st) / 1000; - else - tv[0].tv_usec = 0; - tv[1].tv_sec = tv[0].tv_sec; - tv[1].tv_usec = tv[0].tv_usec; - - ret = futimesat(f, 0, tv); -#else -#error No function available to set file timestamps -#endif - if (ret != 0) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error restoring time\n"); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } -} - -void writ(const char* path, int size) +/** + * Write a partially a file. + * - The file must exist. + * - The file size is not changed. + * - The written data may be equal or not at the already existing one. + * - If it's a symlink nothing is done. + */ +void cmd_write(const char* path, int size) { struct stat st; - if (!size) - return; - if (lstat(path, &st) != 0) { - if (errno == ENOENT) - return; /* it may be already deleted */ /* LCOV_EXCL_START */ fprintf(stderr, "Error accessing %s\n", path); exit(EXIT_FAILURE); @@ -239,76 +327,10 @@ void writ(const char* path, int size) } if (S_ISREG(st.st_mode)) { - /* file */ - FILE* f; - off_t start; - int count; - - f = fopen(path, "r+b"); - if (!f) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error writing %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } - - /* start at random position inside the file */ - if (st.st_size) - start = rnd(st.st_size); - else - start = 0; - - if (fseek(f, start, SEEK_SET) != 0) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error seeking %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } - - /* write garbage, in case also over the end */ - count = size; - while (count) { - fputc(rndnotzero(256), f); - --count; - } - - fclose(f); - } -} - -void damage(const char* path, int size) -{ - struct stat st; - - if (!size) - return; - - if (lstat(path, &st) != 0) { - if (errno == ENOENT) - return; /* it may be already deleted */ - /* LCOV_EXCL_START */ - fprintf(stderr, "Error accessing %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } - - if (st.st_size == 0) - return; - - if (S_ISREG(st.st_mode)) { - /* file */ - FILE* f; - off_t start; unsigned char* data; - int i; + off_t start; - f = fopen(path, "r+b"); - if (!f) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error writing %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } + data = file_read(path, st.st_size, 0); /* not over the end */ if (size > st.st_size) @@ -320,75 +342,85 @@ void damage(const char* path, int size) else start = 0; - data = malloc(size); - if (!data) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Low memoru %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } + rndnz_range(data + start, size); - if (fseek(f, start, SEEK_SET) != 0) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error seeking %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } + file_write(path, data, st.st_size, 0); - if (fread(data, size, 1, f) != 1) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error reading %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } + free(data); + } +} + +/** + * Damage a file. + * - The file must exist. + * - The file size is not changed. + * - The written data is SURELY different than the already existing one. + * - The file timestamp is NOT modified. + * - If it's a symlink nothing is done. + */ +void cmd_damage(const char* path, int size) +{ + struct stat st; + + /* here a 0 size means to change nothing */ + /* as also the timestamp should not be changed */ + if (!size) + return; + + if (lstat(path, &st) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error accessing %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (st.st_size == 0) + return; + + if (S_ISREG(st.st_mode)) { + off_t start; + unsigned char* data; + int i; + + data = file_read(path, st.st_size, 0); + + /* not over the end */ + if (size > st.st_size) + size = st.st_size; + + /* start at random position inside the file */ + if (size < st.st_size) + start = rnd(st.st_size - size); + else + start = 0; /* corrupt ensuring always different data */ for(i=0;i= st.st_size) + size = st.st_size - 1; } - /* truncate at random position inside the file */ - count = rnd(size); - if (count > st.st_size) - count = st.st_size; - start = st.st_size - count; + file_write(path, data, st.st_size - size, 0); - /* don't truncate to 0 to avoid ZERO protection */ - if (start == 0) - start = 1; - - if (ftruncate(fileno(f), start) != 0) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error truncating %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } - - fclose(f); + free(data); } } -void change(const char* path, int size) +/** + * Delete a file. + * - The file must exist. + */ +void cmd_delete(const char* path) +{ + if (remove(path) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error removing %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } +} + +/** + * Change a file. Or deleted/truncated/append/created. + * - The file must exist. + */ +void cmd_change(const char* path, int size) { struct stat st; @@ -476,8 +503,6 @@ void change(const char* path, int size) return; if (lstat(path, &st) != 0) { - if (errno == ENOENT) - return; /* it may be already deleted */ /* LCOV_EXCL_START */ fprintf(stderr, "Error accessing %s\n", path); exit(EXIT_FAILURE); @@ -507,7 +532,7 @@ void change(const char* path, int size) rnd_name(linkto); - create_symlink(path, linkto); + cmd_generate_symlink(path, linkto); } } else if (S_ISREG(st.st_mode)) { int r; @@ -515,22 +540,13 @@ void change(const char* path, int size) r = rnd(4); if (r == 0) { - /* write */ - writ(path, size); + cmd_write(path, size); } else if (r == 1) { - /* append */ - append(path, size); + cmd_append(path, size); } else if (r == 2) { - /* truncate */ - truncat(path, size); + cmd_truncate(path, size); } else { - /* delete */ - if (remove(path) != 0) { - /* LCOV_EXCL_START */ - fprintf(stderr, "Error removing %s\n", path); - exit(EXIT_FAILURE); - /* LCOV_EXCL_STOP */ - } + cmd_delete(path); } } } @@ -547,11 +563,6 @@ void help(void) printf("\tmktest truncate SEED FAIL_SIZE FILE\n"); } -int qsort_strcmp(const void* a, const void* b) -{ - return strcmp(a, b); -} - int main(int argc, char* argv[]) { int i, j, b; @@ -579,11 +590,13 @@ int main(int argc, char* argv[]) for(i=0;i