snapraid/cmdline/mktest.c

787 lines
16 KiB
C
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
2026-04-02 16:25:49 +02:00
// Copyright (C) 2011 Andrea Mazzoleni
2011-03-12 23:21:32 +01:00
#include "os/portable.h"
2011-03-12 23:21:32 +01:00
#include "support.h"
2014-07-11 19:57:58 +02:00
/****************************************************************************/
/* random */
2011-03-12 23:21:32 +01:00
/**
* Pseudo random number generator.
*/
unsigned long long seed = 0;
2026-06-04 00:34:51 +02:00
size_t rnd(size_t max)
2011-03-12 23:21:32 +01:00
{
seed = seed * 6364136223846793005LL + 1442695040888963407LL;
return (seed >> 32) % max;
}
2026-06-04 00:34:51 +02:00
size_t rndnz(size_t max)
{
if (max <= 1)
return 1;
else
return rnd(max - 1) + 1;
}
2026-06-04 00:34:51 +02:00
void rndnz_range(unsigned char* data, ssize_t size)
2014-07-11 19:57:58 +02:00
{
2026-06-04 00:34:51 +02:00
ssize_t i;
2014-09-27 00:32:39 +02:00
for (i = 0; i < size; ++i)
2014-07-11 19:57:58 +02:00
data[i] = rndnz(256);
}
2026-06-04 00:34:51 +02:00
void rndnz_damage(unsigned char* data, ssize_t size)
2014-07-14 08:24:23 +02:00
{
2026-06-04 00:34:51 +02:00
ssize_t i;
2014-07-14 08:24:23 +02:00
/* corrupt ensuring always different data */
2014-09-27 00:32:39 +02:00
for (i = 0; i < size; ++i) {
2014-07-14 08:24:23 +02:00
unsigned char c;
do {
c = rndnz(256);
} while (c == data[i]);
data[i] = c;
}
}
2011-03-22 23:21:19 +01:00
char CHARSET[] = "qwertyuiopasdfghjklzxcvbnm1234567890 .-+";
2014-09-27 00:32:39 +02:00
#define CHARSET_LEN (sizeof(CHARSET) - 1)
2011-03-22 23:21:19 +01:00
void rnd_name(char* file)
{
2026-06-04 00:34:51 +02:00
ssize_t l = 1 + rnd(20);
2014-09-27 00:32:39 +02:00
while (l) {
*file++ = CHARSET[rnd(CHARSET_LEN)];
--l;
}
*file = 0;
}
2014-07-11 19:57:58 +02:00
/****************************************************************************/
/* file */
int file_cmp(const void* a, const void* b)
{
return strcmp(a, b);
}
2014-07-14 08:24:23 +02:00
int fallback(int f, struct stat* st)
2014-07-11 19:57:58 +02:00
{
#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
2014-07-14 08:24:23 +02:00
return ret;
2014-07-11 19:57:58 +02:00
}
/****************************************************************************/
/* cmd */
/**
2015-10-07 22:07:20 +02:00
* Create a file with random content.
2014-07-11 19:57:58 +02:00
* - If the file exists it's rewritten, but avoiding to truncating it to 0.
*/
2026-06-04 00:34:51 +02:00
void cmd_generate_file(const char* path, ssize_t size)
2014-07-11 19:57:58 +02:00
{
unsigned char* data;
2014-07-14 08:24:23 +02:00
int f;
2014-07-11 19:57:58 +02:00
/* remove the existing file/symlink if any */
2014-01-15 19:57:02 +01:00
if (remove(path) != 0) {
if (errno != ENOENT) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error removing file %s\n", path);
2014-01-15 19:57:02 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2014-01-15 19:57:02 +01:00
}
} else {
/* don't truncate files to 0 size to avoid ZERO file size protection */
2014-01-15 19:57:02 +01:00
++size;
}
2014-07-11 19:57:58 +02:00
data = malloc(size);
2014-01-15 19:57:02 +01:00
2026-04-27 15:43:06 +02:00
/*
* 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
*/
2014-07-11 19:57:58 +02:00
rndnz_range(data, size);
2014-01-15 19:57:02 +01:00
2014-07-14 08:24:23 +02:00
f = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_NOFOLLOW, 0600);
if (f < 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error creating file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (write(f, data, size) != size) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error writing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (close(f) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error closing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-11 19:57:58 +02:00
free(data);
2014-01-15 19:57:02 +01:00
}
2014-07-11 19:57:58 +02:00
/**
* Create a symlink.
* - If the file already exists, it's removed.
*/
void cmd_generate_symlink(const char* path, const char* linkto)
2014-01-15 19:57:02 +01:00
{
2014-07-11 19:57:58 +02:00
/* remove the existing file/symlink if any */
2014-01-15 19:57:02 +01:00
if (remove(path) != 0) {
if (errno != ENOENT) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error removing file %s\n", path);
2014-01-15 19:57:02 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2014-01-15 19:57:02 +01:00
}
}
if (symlink(linkto, path) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error writing symlink %s\n", path);
2014-01-15 19:57:02 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2014-01-15 19:57:02 +01:00
}
}
2014-07-11 19:57:58 +02:00
/**
* Create a file or a symlink with a random name.
*/
2026-06-04 00:34:51 +02:00
void cmd_generate(const char* tag, int disk, ssize_t size)
2011-03-12 23:21:32 +01:00
{
char path[PATH_MAX];
2011-03-22 23:21:19 +01:00
char* file;
2011-03-12 23:21:32 +01:00
snprintf(path, sizeof(path), "bench/%s%d/", tag, disk);
file = path + strlen(path);
2011-03-22 23:21:19 +01:00
/* add a directory */
2014-01-15 19:57:02 +01:00
*file++ = 'a' + rnd(2);
*file = 0;
2011-03-22 23:21:19 +01:00
2014-01-15 19:57:02 +01:00
/* create it */
if (mkdir(path, 0777) != 0) {
if (errno != EEXIST) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error creating directory %s\n", path);
2014-01-15 19:57:02 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2011-03-22 23:21:19 +01:00
}
}
2011-03-12 23:21:32 +01:00
2014-01-15 19:57:02 +01:00
*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] == ' '
2014-09-27 00:32:39 +02:00
|| file[strlen(file) - 1] == ' '
|| file[strlen(file) - 1] == '.'
) {
continue;
}
break;
2011-03-12 23:21:32 +01:00
}
#ifndef WIN32 /* Windows XP doesn't support symlinks */
if (rnd(32) == 0) {
/* symlink */
char linkto[PATH_MAX];
rnd_name(linkto);
2014-07-11 19:57:58 +02:00
cmd_generate_symlink(path, linkto);
} else
#endif
{
/* file */
2014-07-11 19:57:58 +02:00
cmd_generate_file(path, size);
}
}
2014-07-11 19:57:58 +02:00
/**
* 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.
2014-09-27 00:32:39 +02:00
* - If it's a symlink nothing is done.
2014-07-11 19:57:58 +02:00
*/
2026-06-04 00:34:51 +02:00
void cmd_write(const char* path, ssize_t size)
2011-03-12 23:21:32 +01:00
{
struct stat st;
if (lstat(path, &st) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error accessing %s\n", path);
2011-03-12 23:21:32 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2011-03-12 23:21:32 +01:00
}
2014-01-15 19:57:02 +01:00
if (S_ISREG(st.st_mode)) {
2014-07-11 19:57:58 +02:00
unsigned char* data;
2014-07-14 08:24:23 +02:00
off_t off;
int f;
2014-07-11 19:57:58 +02:00
/* not over the end */
if (size > st.st_size)
size = st.st_size;
2011-03-12 23:21:32 +01:00
/* start at random position inside the file */
2014-07-11 19:57:58 +02:00
if (size < st.st_size)
2014-07-14 08:24:23 +02:00
off = rnd(st.st_size - size);
else
2014-07-14 08:24:23 +02:00
off = 0;
2014-07-14 08:24:23 +02:00
data = malloc(size);
2011-03-12 23:21:32 +01:00
2014-07-14 08:24:23 +02:00
rndnz_range(data, size);
f = open(path, O_WRONLY | O_BINARY | O_NOFOLLOW);
if (f < 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error creating file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (lseek(f, off, SEEK_SET) != off) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error seeking file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (write(f, data, size) != size) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error writing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (close(f) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error closing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-11 19:57:58 +02:00
free(data);
}
}
2014-07-11 19:57:58 +02:00
/**
* 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.
*/
2026-06-04 00:34:51 +02:00
void cmd_damage(const char* path, ssize_t size)
{
struct stat st;
2026-04-27 15:43:06 +02:00
/*
* 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 */
log_fatal(errno, "Error accessing %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (st.st_size == 0)
return;
if (S_ISREG(st.st_mode)) {
2014-07-14 08:24:23 +02:00
off_t off;
unsigned char* data;
2014-07-14 08:24:23 +02:00
int f;
/* 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)
2014-07-14 08:24:23 +02:00
off = rnd(st.st_size - size);
else
2014-07-14 08:24:23 +02:00
off = 0;
data = malloc(size);
2014-07-14 08:24:23 +02:00
f = open(path, O_RDWR | O_BINARY | O_NOFOLLOW);
if (f < 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error creating file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-14 08:24:23 +02:00
if (lseek(f, off, SEEK_SET) != off) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error seeking file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-14 08:24:23 +02:00
if (read(f, data, size) != size) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error writing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-14 08:24:23 +02:00
rndnz_damage(data, size);
if (lseek(f, off, SEEK_SET) != off) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error seeking file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (write(f, data, size) != size) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error writing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (fallback(f, &st) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error setting time for file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (close(f) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error closing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-03 21:51:16 +02:00
free(data);
}
2011-03-12 23:21:32 +01:00
}
2014-07-11 19:57:58 +02:00
/**
* Append to a file.
* - The file must exist.
* - If it's a symlink nothing is done.
*/
2026-06-04 00:34:51 +02:00
void cmd_append(const char* path, ssize_t size)
{
struct stat st;
if (lstat(path, &st) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error accessing %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-01-15 19:57:02 +01:00
if (S_ISREG(st.st_mode)) {
2014-07-11 19:57:58 +02:00
unsigned char* data;
2014-07-14 08:24:23 +02:00
int f;
data = malloc(size);
2014-07-14 08:24:23 +02:00
rndnz_range(data, size);
2014-07-14 08:24:23 +02:00
f = open(path, O_WRONLY | O_APPEND | O_BINARY | O_NOFOLLOW);
if (f < 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error opening file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (write(f, data, size) != size) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error writing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-11 19:57:58 +02:00
2014-07-14 08:24:23 +02:00
if (close(f) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error closing file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-07-11 19:57:58 +02:00
free(data);
}
}
2014-07-11 19:57:58 +02:00
/**
* Truncate a file.
* - The file must exist.
* - The file is NEVER truncated to 0.
* - If it's a symlink nothing is done.
*/
2026-06-04 00:34:51 +02:00
void cmd_truncate(const char* path, ssize_t size)
{
struct stat st;
if (lstat(path, &st) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error accessing %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
2014-01-15 19:57:02 +01:00
if (S_ISREG(st.st_mode)) {
2014-07-14 08:24:23 +02:00
off_t off;
int f;
2014-07-11 19:57:58 +02:00
/* if file is empty, just rewrite it */
if (st.st_size == 0) {
size = 0;
} else {
/* don't truncate files to 0 size to avoid ZERO file size protection */
if (size >= st.st_size)
size = st.st_size - 1;
}
2014-07-14 08:24:23 +02:00
off = st.st_size - size;
2014-07-11 19:57:58 +02:00
f = open(path, O_WRONLY | O_BINARY | O_NOFOLLOW);
if (f < 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error opening file %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (ftruncate(f, off) != 0) {
2014-07-14 08:24:23 +02:00
/* LCOV_EXCL_START */
log_fatal(errno, "Error truncating file %s\n", path);
2014-07-14 08:24:23 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
if (close(f) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error closing file %s\n", path);
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
}
}
2014-07-11 19:57:58 +02:00
/**
* Delete a file.
* - The file must exist.
*/
void cmd_delete(const char* path)
{
if (remove(path) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error removing %s\n", path);
2014-07-11 19:57:58 +02:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
}
/**
* Change a file. Or deleted/truncated/append/created.
* - The file must exist.
*/
2026-06-04 00:34:51 +02:00
void cmd_change(const char* path, ssize_t size)
2014-01-15 19:57:02 +01:00
{
struct stat st;
if (!size)
return;
if (lstat(path, &st) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error accessing %s\n", path);
2014-01-15 19:57:02 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2014-01-15 19:57:02 +01:00
}
if (S_ISLNK(st.st_mode)) {
/* symlink */
if (rnd(2) == 0) {
/* delete */
if (remove(path) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error removing %s\n", path);
2014-01-15 19:57:02 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2014-01-15 19:57:02 +01:00
}
} else {
/* recreate */
char linkto[PATH_MAX];
if (remove(path) != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error removing %s\n", path);
2014-01-15 19:57:02 +01:00
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2014-01-15 19:57:02 +01:00
}
rnd_name(linkto);
2014-07-11 19:57:58 +02:00
cmd_generate_symlink(path, linkto);
2014-01-15 19:57:02 +01:00
}
} else if (S_ISREG(st.st_mode)) {
2026-06-04 00:34:51 +02:00
size_t r;
2014-01-15 19:57:02 +01:00
r = rnd(4);
if (r == 0) {
2014-07-11 19:57:58 +02:00
cmd_write(path, size);
2014-01-15 19:57:02 +01:00
} else if (r == 1) {
2014-07-11 19:57:58 +02:00
cmd_append(path, size);
2014-01-15 19:57:02 +01:00
} else if (r == 2) {
2014-07-11 19:57:58 +02:00
cmd_truncate(path, size);
2014-01-15 19:57:02 +01:00
} else {
2014-07-11 19:57:58 +02:00
cmd_delete(path);
2014-01-15 19:57:02 +01:00
}
}
}
2011-03-12 23:21:32 +01:00
void help(void)
{
2015-02-22 14:31:03 +01:00
printf("Test for " PACKAGE " v" VERSION " by Andrea Mazzoleni, " PACKAGE_URL "\n");
printf("Usage:\n");
printf("\tmktest generate SEED DISK_TAG DISK_NUM FILE_NUM FILE_SIZE\n");
printf("\tmktest damage SEED COUNT SIZE FILE\n");
printf("\tmktest write SEED COUNT SIZE FILE\n");
2015-02-22 14:31:03 +01:00
printf("\tmktest change SEED SIZE FILE\n");
printf("\tmktest append SEED SIZE FILE\n");
printf("\tmktest truncate SEED SIZE FILE\n");
2011-03-12 23:21:32 +01:00
}
int main(int argc, char* argv[])
{
int i, j, b;
2011-03-12 23:21:32 +01:00
2016-01-05 16:37:29 +01:00
lock_init();
2011-03-12 23:21:32 +01:00
if (argc < 2) {
help();
2014-02-05 00:00:29 +01:00
exit(EXIT_SUCCESS);
2011-03-12 23:21:32 +01:00
}
if (strcmp(argv[1], "generate") == 0) {
int disk, file, size;
const char* tag;
2011-03-12 23:21:32 +01:00
if (argc != 7) {
/* LCOV_EXCL_START */
2011-03-12 23:21:32 +01:00
help();
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2011-03-12 23:21:32 +01:00
}
seed = atoi(argv[2]);
tag = argv[3];
disk = atoi(argv[4]);
file = atoi(argv[5]);
size = atoi(argv[6]);
2011-03-12 23:21:32 +01:00
2014-09-27 00:32:39 +02:00
for (i = 0; i < disk; ++i) {
for (j = 0; j < file; ++j) {
if (j == 0)
2014-07-11 19:57:58 +02:00
/* create at least a big one */
cmd_generate(tag, i + 1, size);
else if (j == 1)
2014-07-11 19:57:58 +02:00
/* create at least an empty one */
cmd_generate(tag, i + 1, 0);
else
cmd_generate(tag, i + 1, rnd(size));
2011-03-12 23:21:32 +01:00
}
}
} else if (strcmp(argv[1], "write") == 0) {
int fail, size;
if (argc < 6) {
/* LCOV_EXCL_START */
help();
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
seed = atoi(argv[2]);
fail = atoi(argv[3]);
size = atoi(argv[4]);
b = 5;
/* sort the file names */
2014-09-27 00:32:39 +02:00
qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);
2014-09-27 00:32:39 +02:00
for (i = b; i < argc; ++i)
for (j = 0; j < fail; ++j)
cmd_write(argv[i], rndnz(size));
} else if (strcmp(argv[1], "damage") == 0) {
2011-03-12 23:21:32 +01:00
int fail, size;
if (argc < 6) {
/* LCOV_EXCL_START */
2011-03-12 23:21:32 +01:00
help();
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2011-03-12 23:21:32 +01:00
}
seed = atoi(argv[2]);
fail = atoi(argv[3]);
size = atoi(argv[4]);
b = 5;
2011-03-12 23:21:32 +01:00
/* sort the file names */
2014-09-27 00:32:39 +02:00
qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);
2014-09-27 00:32:39 +02:00
for (i = b; i < argc; ++i)
for (j = 0; j < fail; ++j)
2014-07-11 19:57:58 +02:00
cmd_damage(argv[i], rndnz(size)); /* at least one byte */
} else if (strcmp(argv[1], "append") == 0) {
int size;
if (argc < 5) {
/* LCOV_EXCL_START */
help();
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
seed = atoi(argv[2]);
size = atoi(argv[3]);
b = 4;
/* sort the file names */
2014-09-27 00:32:39 +02:00
qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);
2014-09-27 00:32:39 +02:00
for (i = b; i < argc; ++i)
cmd_append(argv[i], rndnz(size)); /* at least one byte */
} else if (strcmp(argv[1], "truncate") == 0) {
int size;
if (argc < 5) {
/* LCOV_EXCL_START */
help();
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
seed = atoi(argv[2]);
size = atoi(argv[3]);
b = 4;
/* sort the file names */
2014-09-27 00:32:39 +02:00
qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);
2014-09-27 00:32:39 +02:00
for (i = b; i < argc; ++i)
2014-07-11 19:57:58 +02:00
cmd_truncate(argv[i], rnd(size));
2014-01-15 19:57:02 +01:00
} else if (strcmp(argv[1], "change") == 0) {
int size;
if (argc < 5) {
/* LCOV_EXCL_START */
2014-01-15 19:57:02 +01:00
help();
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2014-01-15 19:57:02 +01:00
}
seed = atoi(argv[2]);
size = atoi(argv[3]);
b = 4;
/* sort the file names */
2014-09-27 00:32:39 +02:00
qsort(&argv[b], argc - b, sizeof(argv[b]), file_cmp);
2014-01-15 19:57:02 +01:00
2014-09-27 00:32:39 +02:00
for (i = b; i < argc; ++i)
2014-07-11 19:57:58 +02:00
cmd_change(argv[i], rnd(size));
2011-03-12 23:21:32 +01:00
} else {
/* LCOV_EXCL_START */
2011-03-12 23:21:32 +01:00
help();
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
2011-03-12 23:21:32 +01:00
}
2016-01-05 16:37:29 +01:00
lock_done();
2011-03-12 23:21:32 +01:00
return 0;
}