More efficient test avoiding rewrite.

This commit is contained in:
Andrea Mazzoleni 2014-07-14 08:24:23 +02:00
parent ffb928d6bb
commit c8df7d4a07

View file

@ -47,6 +47,22 @@ void rndnz_range(unsigned char* data, int size)
data[i] = rndnz(256); data[i] = rndnz(256);
} }
void rndnz_damage(unsigned char* data, int size)
{
int i;
/* corrupt ensuring always different data */
for(i=0;i<size;++i) {
unsigned char c;
do {
c = rndnz(256);
} while (c == data[i]);
data[i] = c;
}
}
char CHARSET[] = "qwertyuiopasdfghjklzxcvbnm1234567890 .-+"; char CHARSET[] = "qwertyuiopasdfghjklzxcvbnm1234567890 .-+";
#define CHARSET_LEN (sizeof(CHARSET)-1) #define CHARSET_LEN (sizeof(CHARSET)-1)
@ -68,7 +84,7 @@ int file_cmp(const void* a, const void* b)
return strcmp(a, b); return strcmp(a, b);
} }
void file_fallback(int f, struct stat* st) int fallback(int f, struct stat* st)
{ {
#if HAVE_FUTIMENS #if HAVE_FUTIMENS
struct timespec tv[2]; struct timespec tv[2];
@ -110,75 +126,7 @@ void file_fallback(int f, struct stat* st)
#else #else
#error No function available to set file timestamps #error No function available to set file timestamps
#endif #endif
if (ret != 0) { return ret;
/* 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 | O_BINARY);
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 = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_NOFOLLOW, 0600);
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 */
}
} }
/****************************************************************************/ /****************************************************************************/
@ -191,6 +139,7 @@ void file_write(const char* path, const unsigned char* data, int size, struct st
void cmd_generate_file(const char* path, int size) void cmd_generate_file(const char* path, int size)
{ {
unsigned char* data; unsigned char* data;
int f;
/* remove the existing file/symlink if any */ /* remove the existing file/symlink if any */
if (remove(path) != 0) { if (remove(path) != 0) {
@ -215,7 +164,27 @@ void cmd_generate_file(const char* path, int size)
/* and a file filled with 0 */ /* and a file filled with 0 */
rndnz_range(data, size); rndnz_range(data, size);
file_write(path, data, size, 0); f = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_NOFOLLOW, 0600);
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 (close(f) != 0) {
/* LCOV_EXCL_START */
fprintf(stderr, "Error closing file %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
free(data); free(data);
} }
@ -328,9 +297,8 @@ void cmd_write(const char* path, int size)
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
unsigned char* data; unsigned char* data;
off_t start; off_t off;
int f;
data = file_read(path, st.st_size, 0);
/* not over the end */ /* not over the end */
if (size > st.st_size) if (size > st.st_size)
@ -338,13 +306,42 @@ void cmd_write(const char* path, int size)
/* start at random position inside the file */ /* start at random position inside the file */
if (size < st.st_size) if (size < st.st_size)
start = rnd(st.st_size - size); off = rnd(st.st_size - size);
else 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); free(data);
} }
@ -378,11 +375,9 @@ void cmd_damage(const char* path, int size)
return; return;
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
off_t start; off_t off;
unsigned char* data; unsigned char* data;
int i; int f;
data = file_read(path, st.st_size, 0);
/* not over the end */ /* not over the end */
if (size > st.st_size) if (size > st.st_size)
@ -390,22 +385,63 @@ void cmd_damage(const char* path, int size)
/* start at random position inside the file */ /* start at random position inside the file */
if (size < st.st_size) if (size < st.st_size)
start = rnd(st.st_size - size); off = rnd(st.st_size - size);
else else
start = 0; off = 0;
/* corrupt ensuring always different data */ data = malloc(size);
for(i=0;i<size;++i) {
unsigned char c;
do { f = open(path, O_RDWR | O_BINARY | O_NOFOLLOW);
c = rndnz(256); if (f < 0) {
} while (c == data[start + i]); /* LCOV_EXCL_START */
fprintf(stderr, "Error creating file %s\n", path);
data[start + i] = c; exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
} }
file_write(path, data, st.st_size, &st); 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 (read(f, data, size) != size) {
/* LCOV_EXCL_START */
fprintf(stderr, "Error writing file %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
rndnz_damage(data, size);
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 (fallback(f, &st) != 0) {
/* LCOV_EXCL_START */
fprintf(stderr, "Error setting time for 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); free(data);
} }
@ -429,12 +465,33 @@ void cmd_append(const char* path, int size)
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
unsigned char* data; unsigned char* data;
int f;
data = file_read(path, st.st_size, size); data = malloc(size);
rndnz_range(data + st.st_size, size); rndnz_range(data, size);
file_write(path, data, st.st_size + size, 0); f = open(path, O_WRONLY | O_APPEND | O_BINARY | O_NOFOLLOW);
if (f < 0) {
/* LCOV_EXCL_START */
fprintf(stderr, "Error opening 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); free(data);
} }
@ -458,9 +515,7 @@ void cmd_truncate(const char* path, int size)
} }
if (S_ISREG(st.st_mode)) { if (S_ISREG(st.st_mode)) {
unsigned char* data; off_t off;
data = file_read(path, st.st_size, size);
/* if file is empty, just rewrite it */ /* if file is empty, just rewrite it */
if (st.st_size == 0) { if (st.st_size == 0) {
@ -471,9 +526,14 @@ void cmd_truncate(const char* path, int size)
size = st.st_size - 1; size = st.st_size - 1;
} }
file_write(path, data, st.st_size - size, 0); off = st.st_size - size;
free(data); if (truncate(path, off) != 0) {
/* LCOV_EXCL_START */
fprintf(stderr, "Error truncating file %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
} }
} }