-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.c
224 lines (177 loc) · 4.39 KB
/
generator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "generator.h"
// struct link *fds;
// fds = malloc((sizeof(struct link));
//
// int determine_num()
// {
//
// }
// helper function to determine whether a file is a regular file or a directory
int determine_dir(char direct[])
{
struct stat status;
stat(direct, &status);
if (status.st_mode & S_IFDIR) // is a directory file.
return 0;
if (status.st_mode & S_IFREG) // is a regular file.
return 1;
return -1;
}
//recursively loop a absolute path, open all files and returns file descriptors to pool->fd_pool.
void generator_fd_dir(struct para_pool *pool, char *direct, int *index)
{
int regular = 0;
int count, i;
char *abs_dir;
DIR *d;
struct dirent *dir;
d = opendir(direct);
if (d != NULL)
{
while ((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
{
count = 0;
for(i = 0; dir->d_name[i] != '\0'; i++)
count++;
for(i = 0; direct[i] != '\0'; i++)
count++;
abs_dir = (char*)malloc((count + 2) * sizeof(char));
pool->dirs_pool[*index] = (char*)malloc((count + 2) * sizeof(char));
strcpy(abs_dir, direct);
strcat(abs_dir, "/");
strcat(abs_dir, dir->d_name);
pool->fd_pool[*index] = open(abs_dir, O_RDONLY);
strcpy(pool->dirs_pool[*index], abs_dir);
(*index)++;
regular = determine_dir(abs_dir);
//printf("Dir: %s \n regular = %d \n\n", abs_dir, regular);
if (regular == 0)
generator_fd_dir(pool, abs_dir, index);
free(abs_dir);
}
}
closedir(d);
}
}
/* Generate an array of directories for testing. */
/*void generator_dir(struct para_pool *pool, char *direct, int *index)
{
int regular;
int count, i;
char *abs_dir;
DIR *d;
struct dirent *dir;
d = opendir(direct);
if(d != NULL)
{
while((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
{
count = 0;
for(i = 0; dir->d_name[i] != '\0'; i++)
count++;
abs_dir = (char*)malloc((count + 2) * sizeof(char));
strcpy(abs_dir, direct);
strcat(abs_dir, "/");
strcat(abs_dir, dir->d_name);
pool->dirs_pool[*index] = abs_dir;
(*index)++;
regular = determine_dir(abs_dir);
if (regular == 0)
generator_dir(pool, abs_dir, index);
free(abs_dir);
}
}
}
closedir(d);
}
*/
//recursively loop a absolute path, count all files.
void generator_init(char *direct, int *files_number)
{
int regular = 0;
char *abs_dir;
int count, i;
DIR *d;
struct dirent *dir;
d = opendir(direct);
if (d != NULL)
{
while ((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
{
count = 0;
for(i = 0; dir->d_name[i] != '\0'; i++)
count++;
for(i = 0; direct[i] != '\0'; i++)
count++;
abs_dir = (char*)malloc((count + 2) * sizeof(char));
strcpy(abs_dir, direct);
strcat(abs_dir, "/");
strcat(abs_dir, dir->d_name);
(*files_number)++;
regular = determine_dir(abs_dir);
//printf("Good \n");
if (regular == 0)
generator_init_fd(abs_dir, files_number);
free(abs_dir);
}
}
}
closedir(d);
}
void generator_init_fd(char *direct, int *index)
{
int regular = 0;
int count, i;
char *abs_dir;
DIR *d;
struct dirent *dir;
d = opendir(direct);
if (d != NULL)
{
while ((dir = readdir(d)) != NULL)
{
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0)
{
count = 0;
for(i = 0; dir->d_name[i] != '\0'; i++)
count++;
for(i = 0; direct[i] != '\0'; i++)
count++;
abs_dir = (char*)malloc((count + 2) * sizeof(char));
strcpy(abs_dir, direct);
strcat(abs_dir, "/");
strcat(abs_dir, dir->d_name);
//pool->fd_pool[*index] = open(abs_dir, O_RDONLY);
(*index)++;
regular = determine_dir(abs_dir);
if (regular == 0)
generator_init_fd(abs_dir, index);
free(abs_dir);
}
}
}
closedir(d);
}
void generator_mod(struct para_pool *pool)
{
mode_t tmp[] = {S_ISUID, S_ISGID, S_IRUSR, S_IWUSR, S_IXUSR, S_IREAD, S_IWRITE, S_IEXEC, S_IRWXG, S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO,
S_IROTH, S_IWOTH, S_IXOTH, S_IFMT, S_IFIFO, S_IFCHR, S_IFDIR, S_IFBLK, S_IFREG, S_IFLNK, S_IFSOCK, S_ISVTX, S_BLKSIZE, DEFFILEMODE};
int i;
for(i = 0; i < 27; i++)
{
pool->mode_pool[i] = tmp[i];
}
}