Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #ifndef WIN32
- #include <unistd.h>
- #include <getopt.h>
- #else
- #include <io.h>
- #include "win/getopt/getopt.h"
- #endif
- #define MAX_FNAME 128
- struct flist {
- char filename[MAX_FNAME];
- struct flist *next;
- };
- static int add_flist(struct flist **f, const char *filename)
- {
- struct flist *p, *c;
- c = malloc(sizeof(*c));
- if (!c)
- return -1;
- strcpy(c->filename, filename);
- c->next = NULL;
- if (!(*f)) {
- *f = c;
- return 0;
- }
- for (p=*f;p->next;p=p->next)
- ;
- p->next = c;
- return 0;
- }
- struct option longopts[] = {
- { "minsize", required_argument, 0, 'm' },
- { "maxsize", required_argument, 0, 'M' },
- { "maxfiles", required_argument, 0, 'f' },
- { "seed", required_argument, 0, 's' },
- { NULL, 0, 0, 0 }
- };
- #define DEFAULT_MINSIZE 0
- #define DEFAULT_MAXSIZE (128 * 1024)
- int main(int argc, char **argv)
- {
- struct flist *f = NULL, *p, *prev;
- char *buffer;
- int c, count;
- int i;
- int err;
- int minsize = DEFAULT_MINSIZE, maxsize = DEFAULT_MAXSIZE, maxfiles = 0;
- int seed = time(0)|1;
- while ((c = getopt_long(argc, argv, "f:s:m:M:", longopts, NULL)) != EOF) {
- switch (c) {
- case 'm' : minsize = strtol(optarg,0,0); break;
- case 'M' : maxsize = strtol(optarg,0,0); break;
- case 's' : seed = strtol(optarg,0,0); break;
- case 'f' : maxfiles = strtol(optarg,0,0); break;
- case '?' :
- printf("arguments:\n"
- " --minsize|-m <bytes> -- minimum file size to create (default %d)\n"
- " --maxsize|-M <bytes> -- maximum file size to create (default %d)\n"
- " --maxfiles|-f <files> -- limit the maximum number of files to create\n"
- " --seed|-s <value> -- random number seed to use\n",
- DEFAULT_MINSIZE, DEFAULT_MAXSIZE);
- return 1;
- }
- }
- if (optind != argc) {
- printf("Extra arguments found. Aborting.\n");
- return 1;
- }
- if (maxsize < minsize) {
- printf("Maximum file size (0x%x) < Minimum file size (0x%x)!\n",
- maxsize, minsize);
- return 1;
- }
- buffer = calloc(maxsize, 1);
- if (!buffer) {
- printf("Out of memory allocating write buffer\n");
- return 1;
- }
- srand(seed);
- printf("Seed = 0x%x, minsize = %d, maxsize = %d, ",
- seed, minsize, maxsize);
- if (maxfiles)
- printf("maxfiles = %d\n", maxfiles);
- else
- printf("no maxfiles\n");
- while(1) {
- printf("Creating files\n");
- count = 0;
- while ((maxfiles == 0) || (count < maxfiles)) {
- char fname[MAX_FNAME];
- int fd;
- int size;
- sprintf(fname, "f%8.8x.%8.8x", rand(), rand());
- fd = open(fname, O_RDWR | O_CREAT, 0666);
- if (fd < 0)
- break;
- printf("Created file '%s'\n", fname);
- #ifdef WIN32
- size = (((int)rand() + ((int)rand() << 16)) % (maxsize - minsize + 1)) + minsize;
- #else
- size = rand() % (maxsize - minsize + 1) + minsize;
- #endif
- for(i=0; i<size; i++) {
- if((rand() % 100) >= 75) buffer[i] = rand();
- else buffer[i] = 0;
- }
- err = write(fd, buffer, size);
- close(fd);
- if(err < 0) {
- printf("err writing to file, unlinking it\n");
- unlink(fname);
- break;
- }
- if (add_flist(&f, fname) < 0) {
- printf("Error adding filename to list (Out of memory?)\n");
- return 1;
- }
- if ((++count % 100) == 0) {
- printf("%d files created\r", count); fflush(stdout);
- }
- }
- printf("%d files created. Deleting 50%% of files.\n", count);
- prev = NULL;
- p = f;
- count = 0;
- while (p) {
- if((rand() % 100) >= 50) {
- unlink(p->filename);
- printf("Unlinked file '%s'\n", p->filename);
- if ((++count % 100) == 0) {
- printf("%d files deleted\r", count); fflush(stdout);
- }
- if(prev == NULL) {
- f = p->next;
- free(p);
- p = f;
- } else {
- prev->next = p->next;
- free(p);
- p = prev->next;
- }
- } else {
- prev = p;
- p = p->next;
- }
- }
- }
- free(buffer);
- printf("done\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement