/*
* Copyright (C) 2011 Andrea Mazzoleni
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "portable.h"
/**
* Pseudo random number generator.
*/
unsigned long long seed = 0;
unsigned rnd(unsigned max)
{
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
return (seed >> 32) % max;
}
unsigned rndnotzero(unsigned max)
{
if (max <= 1)
return 1;
else
return rnd(max - 1) + 1;
}
char CHARSET[] = "qwertyuiopasdfghjklzxcvbnm1234567890 .-+";
#define CHARSET_LEN (sizeof(CHARSET)-1)
void rnd_name(char* file)
{
int l = 1 + rnd(20);
while (l) {
*file++ = CHARSET[rnd(CHARSET_LEN)];
--l;
}
*file = 0;
}
void generate(int disk, int size)
{
char path[PATH_MAX];
char* file;
snprintf(path, sizeof(path), "test/disk%d/", disk);
file = path + strlen(path);
/* add a directory */
if (rnd(2) == 0) {
*file++ = '_';
*file++ = 'a' + rnd(2);
*file = 0;
/* create it */
if (mkdir(path, 0777) != 0) {
if (errno != EEXIST) {
fprintf(stderr, "Error creating directory %s\n", path);
exit(EXIT_FAILURE);
}
}
*file++ = '/';
}
/* add another directory */
if (rnd(2) == 0) {
*file++ = '_';
*file++ = 'a' + rnd(2);
*file = 0;
/* create it */
if (mkdir(path, 0777) != 0) {
if (errno != EEXIST) {
fprintf(stderr, "Error creating directory %s\n", path);
exit(EXIT_FAILURE);
}
}
*file++ = '/';
}
while (1) {
/* add a random file */
rnd_name(file);
/* skip some invalid file name, see http://en.wikipedia.org/wiki/Filename */
if (strcmp(file, ".") == 0
|| strcmp(file, "..") == 0
|| strcmp(file, "prn") == 0
|| strcmp(file, "con") == 0
|| strcmp(file, "nul") == 0
|| strcmp(file, "aux") == 0
|| file[0] == ' '
|| file[strlen(file)-1] == ' '
|| file[strlen(file)-1] == '.'
) {
continue;
}
break;
}
/* remove the existing file if any */
if (remove(path) != 0) {
if (errno != ENOENT) {
fprintf(stderr, "Error removing file %s\n", path);
exit(EXIT_FAILURE);
}
}
#ifndef WIN32 /* Windows XP doesn't support symlinks */
if (rnd(32) == 0) {
/* symlink */
char linkto[PATH_MAX];
int ret;
rnd_name(linkto);
ret = symlink(linkto, path);
if (ret != 0) {
fprintf(stderr, "Error writing symlink %s\n", path);
exit(EXIT_FAILURE);
}
} else
#endif
{
/* file */
FILE* f;
int count;
f = fopen(path, "wb");
if (!f) {
fprintf(stderr, "Error writing file %s\n", path);
exit(EXIT_FAILURE);
}
count = size;
while (count) {
fputc(rnd(256), f);
--count;
}
fclose(f);
}
}
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) {
fprintf(stderr, "Error restoring time\n");
exit(EXIT_FAILURE);
}
}
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 */
fprintf(stderr, "Error accessing %s\n", path);
exit(EXIT_FAILURE);
}
if (S_ISLNK(st.st_mode)) {
/* symlink */
int ret;
ret = remove(path);
if (ret != 0) {
fprintf(stderr, "Error removing %s\n", path);
exit(EXIT_FAILURE);
}
} else {
/* file */
FILE* f;
off_t start;
int count;
f = fopen(path, "r+b");
if (!f) {
fprintf(stderr, "Error writing %s\n", path);
exit(EXIT_FAILURE);
}
/* 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) {
fprintf(stderr, "Error seeking %s\n", path);
exit(EXIT_FAILURE);
}
/* write garbage, in case also over the end */
count = size;
while (count) {
fputc(rnd(256), f);
--count;
}
/* flush changes before restoring the time */
fflush(f);
/* restore the previous modification time */
fallback(fileno(f), &st);
fclose(f);
}
}
void append(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 */
fprintf(stderr, "Error accessing %s\n", path);
exit(EXIT_FAILURE);
}
if (S_ISLNK(st.st_mode)) {
/* symlink */
int ret;
ret = remove(path);
if (ret != 0) {
fprintf(stderr, "Error removing %s\n", path);
exit(EXIT_FAILURE);
}
} else {
/* file */
FILE* f;
int count;
f = fopen(path, "ab");
if (!f) {
fprintf(stderr, "Error appending %s\n", path);
exit(EXIT_FAILURE);
}
/* write garbage, in case also over the end */
count = rnd(size);
while (count) {
fputc(rnd(256), f);
--count;
}
fclose(f);
}
}
void truncat(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 */
fprintf(stderr, "Error accessing %s\n", path);
exit(EXIT_FAILURE);
}
if (S_ISLNK(st.st_mode)) {
/* symlink */
int ret;
ret = remove(path);
if (ret != 0) {
fprintf(stderr, "Error removing %s\n", path);
exit(EXIT_FAILURE);
}
} else {
/* file */
FILE* f;
off_t start;
int count;
f = fopen(path, "r+b");
if (!f) {
fprintf(stderr, "Error writing %s\n", path);
exit(EXIT_FAILURE);
}
/* truncate at random position inside the file */
count = rnd(size);
if (count > st.st_size)
count = st.st_size;
start = st.st_size - count;
if (ftruncate(fileno(f), start) != 0) {
fprintf(stderr, "Error truncating %s\n", path);
exit(EXIT_FAILURE);
}
fclose(f);
}
}
void help(void)
{
printf("Test for " PACKAGE " v" VERSION " by Andrea Mazzoleni, " PACKAGE_URL "\n");
printf("Usage:\n");
printf("\tmktest generate SEED DISK_NUM FILE_NUM FILE_SIZE\n");
printf("\tmktest damage SEED FAIL_NUM FAIL_SIZE FILE\n");
printf("\tmktest append SEED FAIL_SIZE FILE\n");
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;
if (argc < 2) {
help();
exit(EXIT_FAILURE);
}
if (strcmp(argv[1], "generate") == 0) {
int disk, file, size;
if (argc != 6) {
help();
exit(EXIT_FAILURE);
}
seed = atoi(argv[2]);
disk = atoi(argv[3]);
file = atoi(argv[4]);
size = atoi(argv[5]);
for(i=0;i