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