scale up the fragfile sample size
[lunaix-os.git] / lunaix-os / usr / fragfile.c
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <lunaix/status.h>
6
7 static char alphabets[] = "abcdefghijklmnopqrstuvwxyz"
8                           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9                           "01234567890";
10
11 #define NR_BUFSIZE   4096
12 #define NR_NAME_LEN  8
13 #define NR_REPEAT    5
14
15 int main()
16 {
17     unsigned int buf[NR_BUFSIZE];
18     char name[NR_NAME_LEN + 1];
19     int fd = open("/dev/rand", O_RDONLY);
20
21     if (mkdir("testdir") && errno != EEXIST)
22     {
23         printf("Unable to mkdir %d\n", errno);
24         _exit(1);
25     }
26
27     if (chdir("testdir"))
28     {
29         printf("Unable to chdir %d\n", errno);
30         _exit(1);
31     }
32
33     int nr_total = NR_REPEAT * NR_BUFSIZE / NR_NAME_LEN;
34
35     int cnt = 0;
36     for (int i = 0; i < NR_REPEAT; i++)
37     {
38         int n = read(fd, buf, 4096 * sizeof(int));
39         int j = 0, k = 0;
40         while (j < 4096) {
41             name[k++] = alphabets[buf[j++] % 63];
42
43             if (k < NR_NAME_LEN) {
44                 continue;
45             }
46
47             k = 0;
48             cnt++;
49             name[NR_NAME_LEN] = 0;
50
51             printf("[%04d/%04d] creating: %s\r", cnt, nr_total, name);
52             int fd2 = open(name, O_RDONLY | O_CREAT);
53             
54             if (fd2 < 0) 
55             {
56                 printf("\n");
57                 if (errno == EDQUOT) {
58                     printf("Out of quota\n");
59                     return 0;
60                 }
61
62                 printf("Unable to open %d\n", errno);
63                 continue;
64             }
65
66             close(fd2);
67         }
68     }
69     printf("\n");
70     return 0;
71 }