-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.c
More file actions
325 lines (185 loc) · 5.88 KB
/
watcher.c
File metadata and controls
325 lines (185 loc) · 5.88 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "watcher.h"
void *mmalloc(size_t size){
void *mem = malloc(size);
if(mem == NULL){
printf("memory cant be allocated.\n");
return NULL;
}
return mem;
}
/*int f_o_d(char *fullpath){
struct stat st;
if (stat(fullpath, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
return IS_D;
} else if (S_ISREG(st.st_mode)) {
return IS_F;
}
}
}*/
void read_to_fnode(struct fnode *f){
FILE *fi = fopen(f->location,"r");
if(fi == NULL){
return;
}
int flag = 0;
f->text = (struct str_blk *)malloc(sizeof(struct str_blk));
while(1){
int blk_sz = BLK_SIZE-1;
char *buffer = (char*)malloc(sizeof(char)*BLK_SIZE);
int read = fread(buffer,sizeof(char),blk_sz,fi);
if(read == 0){
if(flag == 0){
f->text->content=strdup("");
f->text->next=NULL;
}
else{
add_str(f->text,strdup(""));
}
free(buffer);
break;
}
if(flag == 0){
f->text->content = buffer;
f->text->next = NULL;
flag = 1;
}
else{
add_str(f->text,buffer);
}
if(read<blk_sz){
break;
}
}
fclose(fi);
}
void add_to_dnode_l(struct dnode_list *head, struct dnode *dval){
if(head->val == NULL){
head->val=dval;
head->next=NULL;
}
else{
struct dnode_list *trv =head;
while(trv->next!=NULL){
trv=trv->next;
}
trv->next=(struct dnode_list *)mmalloc(sizeof(struct dnode_list));
trv->next->val=dval;
trv->next->next=NULL;
}
}
void add_to_fnode_l(struct fnode_list *head, struct fnode *fval){
if(head->val == NULL){
head->val=fval;
head->next=NULL;
}
else{
struct fnode_list *trv =head;
while(trv->next!=NULL){
trv=trv->next;
}
trv->next=(struct fnode_list *)mmalloc(sizeof(struct fnode_list));
trv->next->val=fval;
trv->next->next=NULL;
}
}
struct dnode *init_dnode(char *dloc){
struct dnode *root_dir=(struct dnode *)mmalloc(sizeof(struct dnode));
root_dir->location=dloc;
root_dir->dlist=(struct dnode_list *)mmalloc(sizeof(struct dnode_list));
root_dir->dlist->val=NULL;
root_dir->flist=(struct fnode_list *)mmalloc(sizeof(struct fnode_list));
root_dir->flist->val=NULL;
return root_dir;
}
void construct_base_tree(struct dnode *root_dir, struct dnode *parent){
char *dloc = root_dir->location;
DIR *root = opendir(dloc);
// printf("here1\n");
if(root == NULL){
printf("%s can't be opened\n", dloc);
}
else{
struct dirent *trv;
while(1){
trv = readdir(root);
// printf("here2");
if(trv == NULL){
break;
}
else{
if(strcmp(trv->d_name, ".") !=0 && strcmp(trv->d_name,"..") !=0){
// directory
if(trv->d_type == DT_DIR){
int size = strlen(root_dir->location) + 2 + strlen(trv->d_name);
char *new_loc =(char*)mmalloc(sizeof(char)*size);
snprintf(new_loc,size,"%s/%s",root_dir->location, trv->d_name);
struct dnode *add_d = init_dnode(new_loc);
struct stat sta;
if (stat(new_loc, &sta) == -1) {
perror("stat");
}
else{
add_d->st=sta;
}
add_to_dnode_l(root_dir->dlist,add_d);
construct_base_tree(add_d,root_dir);
}
else{
struct fnode *add_f=(struct fnode *)mmalloc(sizeof(struct fnode));
int size = strlen(root_dir->location) + 2 + strlen(trv->d_name);
char *new_loc =(char*)mmalloc(sizeof(char)*size);
snprintf(new_loc,size,"%s/%s",root_dir->location, trv->d_name);
add_f->location=new_loc;
read_to_fnode(add_f);
struct stat sta;
if (stat(new_loc, &sta) == -1) {
perror("stat");
}
else{
add_f->st=sta;
}
add_to_fnode_l(root_dir->flist,add_f);
}
}
}
}
}
}
void trv_tree_test(struct dnode *root){
printf("Location: %s\n\n",root->location);
// files
struct fnode_list *trv=root->flist;
while(trv!=NULL){
// below condition added for directories with no files.
if(trv->val!=NULL){
printf("%s\n",trv->val->location); // this
trv = trv->next;
}
else{
break;
}
}
// directories
struct dnode_list *dtrv= root->dlist;
while(dtrv!=NULL){
if(dtrv->val!=NULL){
trv_tree_test(dtrv->val);
dtrv = dtrv->next;
}
else{
break;
}
}
}
struct dnode *get_tree(char *location){
struct dnode *loc = init_dnode(location);
construct_base_tree(loc, NULL);
return loc;
}
/*int main(){
struct dnode *loc = init_dnode("/home/dfmaaa1/Samex");
construct_base_tree(loc, NULL);
//trv_tree_test(loc);
return 0;
}*/