plan to cleanup the fd generation code
[trinity.git] / children / read-all-files.c
blob49852ee4a1d7cb7cce36d931edb97dc69ce19df4
1 /*
2 * Simple child to iterate over the entire fd list, opening/reading/closing as we go.
3 */
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include "arch.h" // page_size
14 #include "child.h"
15 #include "files.h"
16 #include "log.h"
17 #include "random.h"
18 #include "trinity.h" // __unused__
20 int child_read_all_files(__unused__ int childno)
22 struct stat sb;
23 char *buffer;
24 unsigned int i;
25 int fd;
27 for (i = 0; i < files_in_index; i++) {
28 int ret;
29 const char *filename;
31 filename = fileindex[i];
33 ret = (lstat(filename, &sb));
34 if (ret == -1)
35 continue;
37 if (sb.st_size == 0)
38 sb.st_size = page_size;
40 buffer = malloc(sb.st_size);
41 if (!buffer)
42 continue;
44 memset(buffer, 0, sb.st_size);
46 fd = open(filename, O_RDONLY | O_NONBLOCK);
47 if (!fd) {
48 free(buffer);
49 continue;
52 ret = read(fd, buffer, sb.st_size);
53 // if (ret != -1)
54 // output(0, "%s:%s\n", filename, buffer);
56 if (rand_bool())
57 sleep(1);
59 free(buffer);
61 close(fd);
63 return 0;