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