use 64bit counter
[tridge/junkcode.git] / dirtest2.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/wait.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/mman.h>
11 #include <dirent.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
15
16
17 static int create_files(int dir, int nfiles)
18 {
19         int i, fd;
20         char *dname;
21         asprintf(&dname, "dir%d", dir);
22
23         if (mkdir(dname, 0755) != 0 && errno != EEXIST) {
24                 perror(dname);
25                 exit(1);
26         }
27
28         for (i=0;i<nfiles;i++) {
29                 char *fname;
30                 asprintf(&fname, "%s/File%d", dname, i);
31                 fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0644);
32                 if (fd == -1) {
33                         perror(fname);
34                         exit(1);
35                 }
36                 write(fd, fname, strlen(fname)+1);
37                 close(fd);
38                 free(fname);
39         }
40
41         return 0;
42 }
43
44 static int check_files(int dir, int nfiles)
45 {
46         int i, fd;
47         char *dname;
48         asprintf(&dname, "dir%d", dir);
49
50         for (i=0;i<nfiles;i++) {
51                 char *fname;
52                 char s[100];
53                 asprintf(&fname, "%s/File%d", dname, i);
54                 fd = open(fname, O_RDONLY, 0644);
55                 if (fd == -1) {
56                         perror(fname);
57                         exit(1);
58                 }
59                 read(fd, s, sizeof(s));
60                 close(fd);
61
62                 if (strcmp(fname, s)) {
63                         printf("Name mismatch! %s %s\n", fname, s);
64                 }
65
66                 unlink(fname);
67                 free(fname);
68         }
69
70         if (rmdir(dname) != 0) {
71                 perror(dname);
72         }
73
74         return 0;
75 }
76
77 int main(int argc, char *argv[])
78 {
79         int nprocs, nfiles, i, ret=0, status;
80
81         nprocs = atoi(argv[1]);
82         nfiles = atoi(argv[2]);
83
84         printf("Creating %d files in %d dirs\n", nfiles, nprocs);
85
86         for (i=0;i<nprocs;i++) {
87                 if (fork() == 0) {
88                         exit(create_files(i, nfiles));
89                 }
90         }
91         
92         while (waitpid(0, &status, 0) > 0 || errno != ECHILD) {
93                 if (WEXITSTATUS(status) != 0) {
94                         ret = WEXITSTATUS(status);
95                         printf("Child exited with status %d\n", ret);
96                 }
97         }
98
99         for (i=0;i<nprocs;i++) {
100                 if (fork() == 0) {
101                         exit(check_files(i, nfiles));
102                 }
103         }
104         
105         while (waitpid(0, &status, 0) > 0 || errno != ECHILD) {
106                 if (WEXITSTATUS(status) != 0) {
107                         ret = WEXITSTATUS(status);
108                         printf("Child exited with status %d\n", ret);
109                 }
110         }
111
112         return ret;
113 }