Home | History | Annotate | Line # | Download | only in ckckp
cleanalot_async.c revision 1.1
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <signal.h>
      4 #include <unistd.h>
      5 
      6 void dirname(int n, char *s)
      7 {
      8 	if (n == 0) {
      9 		strcat(s, "/0");
     10 		mkdir(s);
     11 	}
     12         while (n) {
     13                 sprintf(s + strlen(s), "/%x", n & 0xf);
     14                 n >>= 4;
     15 		mkdir(s);
     16         }
     17 }
     18 
     19 /*
     20  * Write a file, creating the directory if necessary.
     21  */
     22 int write_file(int gen, int n, int plex, char *buf, int size)
     23 {
     24         FILE *fp;
     25         char s[1024], *t;
     26         int r;
     27 
     28         sprintf(s, "dir_%x_%x", plex, gen);
     29         dirname(n, s);
     30 	strcat(s, ".file");
     31 
     32         // printf("write file %d.%d.%x: %s\n", gen, plex, n, s);
     33 
     34         fp = fopen(s, "wb");
     35         if (fp == NULL)
     36                 return 0;
     37         r = fwrite(buf, size, 1, fp);
     38         fclose(fp);
     39 
     40         return r;
     41 }
     42 
     43 int write_dirs(int gen, int size, int plex)
     44 {
     45         int i, j, tot;
     46         char s[1024];
     47         char *buf;
     48 
     49         /* Create all base dirs */
     50         for (i = 0; i < plex; i++) {
     51                 sprintf(s, "dir_%x_%x", i, gen);
     52                 if (mkdir(s, 0700) != 0)
     53                         return 0;
     54         }
     55 
     56         /* Write files */
     57         buf = malloc(size);
     58         if (buf == NULL)
     59                 return 0;
     60         tot = 0;
     61         for (i = 0; ; i++) {
     62                 for (j = 0; j < plex; j++) {
     63                         if (write_file(gen, i, j, buf, size) == 0) {
     64                                 free(buf);
     65                                 return tot;
     66                         }
     67                         ++tot;
     68                 }
     69         }
     70         /* NOTREACHED */
     71 }
     72 
     73 int main(int argc, char **argv)
     74 {
     75         int c, i, j;
     76         int bs = 0;
     77         int count = 0;
     78         int plex = 2;
     79         char cmd[1024];
     80 
     81         while((c = getopt(argc, argv, "b:n:p:")) != -1) {
     82                 switch(c) {
     83                     case 'b':
     84                             bs = atoi(optarg);
     85                             break;
     86                     case 'n':
     87                             count = atoi(optarg);
     88                             break;
     89                     case 'p':
     90                             plex = atoi(optarg);
     91                             break;
     92 		    default:
     93                             exit(1);
     94                 }
     95         }
     96 
     97         /*
     98          * Process old-style, non-flag parameters
     99          */
    100         if (count == 0) {
    101                 if (argv[optind] != NULL)
    102                         count = atoi(argv[optind]);
    103         }
    104         if (bs == 0 && getenv("BS") != NULL)
    105                 bs = atoi(getenv("BS"));
    106         if (bs == 0)
    107                 bs = 16384;
    108         if (plex == 0)
    109                 plex = 2;
    110 
    111         for (i = 0; count == 0 || i < count; i++) {
    112                 if (count)
    113                         printf("::: begin iteration %d/%d\n", i, count);
    114                 else
    115                         printf("::: begin iteration %d\n", i);
    116 
    117                 for (j = 0; ; j++) {
    118                         if ((c = write_dirs(j, bs, plex)) == 0)
    119                                 break;
    120                 	printf("%d: %d files of size %d\n", j, c, bs);
    121                         sprintf(cmd, "rm -rf dir_%x_%x", plex - 1, j);
    122                         system(cmd);
    123 			sync();
    124                 }
    125                 printf("%d files of size %d\n", j * plex, bs);
    126 		system("df -k .");
    127         }
    128 }
    129