fix: ext2 directory insertion; racing on inode create
[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 int main()
12 {
13     unsigned int buf[4096];
14     char name[8];
15     int fd = open("/dev/rand", O_RDONLY);
16
17     if (mkdir("testdir") && errno != EEXIST)
18     {
19         printf("Unable to mkdir %d\n", errno);
20         _exit(1);
21     }
22
23     if (chdir("testdir"))
24     {
25         printf("Unable to chdir %d\n", errno);
26         _exit(1);
27     }
28
29     int cnt = 0;
30     for (int i = 0; i < 1; i++)
31     {
32         int n = read(fd, buf, 4096 * sizeof(int));
33         int j = 0, k = 0;
34         while (j < 4096) {
35             name[k++] = alphabets[buf[j++] % 63];
36
37             if (k < 7) {
38                 continue;
39             }
40
41             k = 0;
42             cnt++;
43             name[7] = 0;
44
45             printf("[%03d] creating: %s\n", cnt, name);
46             int fd2 = open(name, O_RDONLY | O_CREAT);
47             if (fd2 < 0) {
48                 printf("Unable to open %d\n", errno);
49                 continue;
50             }
51
52             close(fd2);
53         }
54     }
55     
56     return 0;
57 }