• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* random-del-create.c (GPL)*/
2 /* Hironobu SUZUKI <hironobu@h2np.net> */
3 
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <time.h>
10 #include <stdlib.h>
11 #define FAIL 0
12 #define SUCCESS 1
13 
14 int openlog[2] = { 0, 0 };
15 
16 #define MAXNUM 0x100000
17 
18 #define  MAXERROR 1024
19 
20 extern int box_muler(int, int);
21 extern void create_or_delete(char *);
22 
23 int cfilecount = 0;
24 int dfilecount = 0;
25 int errorcount = 0;
26 
main(int ac,char ** av)27 int main(int ac, char **av)
28 {
29 	int r;
30 	char fname[1024];
31 	time_t t;
32 	int i;
33 	int m;
34 
35 	if (ac != 2) {
36 		printf("%s hex-style-filename \n", av[0]);
37 		printf("ex) %s 00022300\n", av[0]);
38 		exit(1);
39 	}
40 	sscanf(av[1], "%x", &m);
41 	if (m < 1 || m > MAXNUM) {
42 		printf("out of size %d\n", m);
43 		exit(1);
44 	}
45 
46 	time(&t);
47 	srandom((unsigned int)getpid() ^
48 		(((unsigned int)t << 16) | (unsigned int)t >> 16));
49 
50 	/* 00/00/00/00 */
51 	for (i = 0; i < m; i++) {
52 		r = random() % m;
53 		sprintf(fname, "00/%2.2x/%2.2x/00%2.2x%2.2x%2.2x",
54 			((r >> 16) & 0xFF),
55 			((r >> 8) & 0xFF),
56 			((r >> 16) & 0xFF), ((r >> 8) & 0xFF), (r & 0xFF));
57 		create_or_delete(fname);
58 	}
59 	fprintf(stderr, "Total create files: %d\n", cfilecount);
60 	fprintf(stderr, "Total delete files: %d\n", dfilecount);
61 	fprintf(stderr, "Total error       : %d\n", errorcount);
62 	exit(0);
63 }
64 
65 #define MAXFSIZE (192*1024)
66 #define AVEFSIZE (MAXFSIZE/2)
67 #define POOLDISKSPACE (AVEFSIZE*128)
68 
69 static int disk_space_pool = 0;
create_or_delete(char * fname)70 void create_or_delete(char *fname)
71 {
72 	int r;
73 	int fsize;
74 
75 	r = (random() & 1);
76 	if (r && disk_space_pool > POOLDISKSPACE) {
77 		/* create */
78 		create_file(fname);
79 	} else {
80 		delete_file(fname);
81 	}
82 	if ((errorcount > dfilecount || errorcount > cfilecount)
83 	    && (errorcount > MAXERROR)) {
84 		fprintf(stderr, "too much error -- stop\n");
85 		fprintf(stderr, "Total create files: %d\n", cfilecount);
86 		fprintf(stderr, "Total delete files: %d\n", dfilecount);
87 		fprintf(stderr, "Total error       : %d\n", errorcount);
88 		exit(1);
89 	}
90 }
91 
create_file(char * filename)92 int create_file(char *filename)
93 {
94 	int fd;
95 	int randomsize;
96 	char wbuf[MAXFSIZE];
97 	if ((fd = creat(filename, S_IRWXU)) < 0) {
98 		errorcount++;
99 		return (-1);
100 	}
101 	if ((randomsize = box_muler(0, MAXFSIZE)) < 0) {
102 		randomsize = MAXFSIZE;
103 	}
104 	if (write(fd, wbuf, randomsize) < 0) {
105 		errorcount++;
106 		close(fd);
107 		return (-1);
108 	}
109 	cfilecount++;
110 	disk_space_pool -= randomsize;
111 	close(fd);
112 	return 0;
113 }
114 
115 #include <sys/stat.h>
116 #include <unistd.h>
117 
delete_file(char * filename)118 int delete_file(char *filename)
119 {
120 	struct stat buf;
121 	int st;
122 	st = stat(filename, &buf);
123 	if (st < 0) {
124 		errorcount++;
125 		return (-1);
126 	}
127 	disk_space_pool += buf.st_size;
128 	if (unlink(filename) < 0) {
129 		errorcount++;
130 		return (-1);
131 	}
132 	dfilecount++;
133 	return 0;
134 }
135