From 7efd5b6f8305f7607cc20d81e434734794504562 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Tue, 1 Jul 2014 22:55:35 +0200 Subject: [PATCH] Ensures that the "datage" command writes always different data. --- cmdline/mktest.c | 128 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 121 insertions(+), 7 deletions(-) diff --git a/cmdline/mktest.c b/cmdline/mktest.c index e230bbe7..fd3893dd 100644 --- a/cmdline/mktest.c +++ b/cmdline/mktest.c @@ -222,7 +222,7 @@ void fallback(int f, struct stat* st) } } -void writ(const char* path, int size, int silent_error) +void writ(const char* path, int size) { struct stat st; @@ -257,6 +257,7 @@ void writ(const char* path, int size, int silent_error) 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); @@ -271,12 +272,103 @@ void writ(const char* path, int size, int silent_error) --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 (S_ISREG(st.st_mode)) { + /* file */ + FILE* f; + off_t start; + unsigned char* data; + int i; + + f = fopen(path, "r+b"); + if (!f) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error writing %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + /* 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; + + data = malloc(size); + if (!data) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Low memoru %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (fseek(f, start, SEEK_SET) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error seeking %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (fread(data, size, 1, f) != 1) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error reading %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + /* corrupt ensuring always different data */ + for(i=0;i