-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdirtree.c
More file actions
executable file
·491 lines (364 loc) · 11.6 KB
/
dirtree.c
File metadata and controls
executable file
·491 lines (364 loc) · 11.6 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "linkedlist.h"
#include "dirtree.h"
#include "cmds.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
struct filedata {
long size;
LList blocks;
}; typedef struct filedata* FileData;
struct dirdata {
LList files;
}; typedef struct filedata* DirData;
struct dirtree {
/* Is the node a file or directory */
int is_file;
/* The name of the node */
char *name;
/* The parent directory */
DirTree parent_dir;
/* Timestamp of when the node was last changed. */
time_t timestamp;
union {
struct filedata file_dta;
struct dirdata dir_dta;
} nodedata;
};
/**
* Creates a directory node. Duplicates the name w/ strdup().
*/
DirTree makeDirTree(char *name, int is_file) {
DirTree node = (DirTree) malloc(sizeof(struct dirtree));
node->name = (char*) malloc((1 + strlen(name)) * sizeof(char));
strcpy(node->name, name);
node->is_file = is_file;
if (is_file) {
/* Starts as 0 byte file */
node->nodedata.file_dta.size = 0;
/* Create block list */
node->nodedata.file_dta.blocks = makeLL();
} else {
node->nodedata.dir_dta.files = makeLL();
node->parent_dir = node;
}
/* Update the timestamp */
updateTimestamp(node);
return node;
}
void flushDirTree(DirTree tree) {
if (tree->is_file) {
/* Remove and free every value from the block allocation. */
while (!isEmptyLL(tree->nodedata.file_dta.blocks))
free(remFromLL(tree->nodedata.file_dta.blocks, 0));
/* Then, free the list */
free(tree->nodedata.file_dta.blocks);
tree->nodedata.file_dta.blocks = NULL;
} else {
/* Flush every subtree */
while (!isEmptyLL(tree->nodedata.dir_dta.files)) {
DirTree subtree = (DirTree) remFromLL(tree->nodedata.dir_dta.files, 0);
flushDirTree(subtree);
}
/* Free and zero */
free(tree->nodedata.dir_dta.files);
tree->nodedata.dir_dta.files = NULL;
tree->parent_dir = NULL;
}
/* Final frees */
free(tree->name);
tree->is_file = 0;
free(tree);
}
/**
* Gets the directory node associated with the given path.
* path - The tokenized path
*
* return - The requested node, or NULL if it doesn't exist.
*/
DirTree getDirSubtree(DirTree tree, char *path[]) {
LLiter iter;
if (!path || !path[0])
return tree; /* Found the file */
else if (tree->is_file)
return NULL; /* Files do not have subdirectories. */
else if (!path[0][0] || !strcmp(path[0], "."))
return getDirSubtree(tree, &path[1]); /* Stay in current dir */
else if (!strcmp(path[0], ".."))
return getDirSubtree(tree->parent_dir, &path[1]); /* Go back one directory */
/* Search the subfiles for the next recursive step */
iter = makeLLiter(tree->nodedata.dir_dta.files);
while (iterHasNextLL(iter)) {
/* Get the next item from the iterator */
DirTree child = (DirTree) iterNextLL(iter);
if (!strcmp(path[0], child->name)) {
disposeIterLL(iter);
/* The child is should be searched through. */
return getDirSubtree(child, &path[1]);
}
}
disposeIterLL(iter);
return NULL;
}
DirTree getTreeParent(DirTree tree) {
if (tree)
return tree->parent_dir ? tree->parent_dir : tree;
else
return NULL;
}
int addNodeToTree(DirTree tree, char *path[], int is_file) {
int i;
DirTree tgtDir;
char **subpath;
char *filename;
/* Error check */
if (!path || !path[0])
return 3;
/* Make memory space for subpath */
i = 0;
while (path[i]) i++;
subpath = (char**) malloc(i * sizeof(char*));
/* Get the filename */
filename = path[--i];
/* Build the subpath */
subpath[i--] = NULL;
for (; i >= 0; i--)
subpath[i] = path[i];
/* Get the destination */
tgtDir = getDirSubtree(tree, subpath);
free(subpath);
if (tgtDir == NULL) {
/* Directory not found */
return 1;
} else if (tgtDir->is_file) {
/* Cannot add node to file */
return 2;
} else {
/* Make the file */
DirTree file = makeDirTree(filename, is_file);
/* Set the parent directory */
file->parent_dir = tgtDir;
/* Add to the file list */
addToLL(tgtDir->nodedata.dir_dta.files, 0, file);
/* Update the parent's timestamp to reflect the change. */
updateTimestamp(tgtDir);
/* No error */
return 0;
}
}
int addDirToTree(DirTree tree, char *path[]) {
return addNodeToTree(tree, path, 0);
}
int addFileToTree(DirTree tree, char *path[]) {
return addNodeToTree(tree, path, 1);
}
long filesizeOfDirTree(DirTree tree, char *path[]) {
if (!tree)
return 0;
else if (!path || !path[0]) {
/* Current node is the root. */
long size = 0; /* Size of the directory node */
/* File check */
if (tree->is_file)
return tree->nodedata.file_dta.size;
/* Node is a directory, so it is a combo of sizes. */
for (int i = sizeOfLL(tree->nodedata.dir_dta.files) - 1; i >= 0; i--)
size += filesizeOfDirTree((DirTree) getFromLL(tree->nodedata.dir_dta.files, i), NULL);
return size;
} else {
/* Find subtree and get size */
return filesizeOfDirTree(getDirSubtree(tree, path), NULL);
}
}
long numFilesInTreeDir(DirTree tree, char *dir[], int rec) {
if (!tree)
return 0;
else if (dir && dir[0]) /* Recursive initial condition */
return numFilesInTreeDir(getDirSubtree(tree, dir), NULL, rec);
else {
int i;
long count = 0;
/* Check each subfile */
for (i = sizeOfLL(tree->nodedata.dir_dta.files) - 1; i >= 0; i--) {
DirTree sub = getFromLL(tree->nodedata.dir_dta.files, i);
/* Only count directories if recursively checking */
if (sub->is_file)
count += sub->nodedata.file_dta.size;
else
count += rec ? numFilesInTreeDir(sub, NULL, rec) : 0;
}
return count;
}
}
long treeFileSize(DirTree tree, char *path[]) {
DirTree file = path ? getDirSubtree(tree, path) : tree;
if (file->is_file)
return file->nodedata.file_dta.size;
else
return 0;
}
int rmfileFromTree(DirTree tree, char *path[]) {
if (!tree)
return 1;
else if (!path || !path[0]) {
/* This file is the target */
DirTree parent = tree->parent_dir;
if (!(tree->is_file)) {
/* Node is not for a file */
return 2;
}
/* Remove the linking from the tree */
if (parent) {
/* Update the parent with the change */
updateTimestamp(parent);
remFromLL(parent->nodedata.dir_dta.files, indexOfLL(parent->nodedata.dir_dta.files, tree));
tree->parent_dir = NULL;
}
/* Handle the destruction of the file */
while (sizeOfLL(tree->nodedata.file_dta.blocks))
remFromLL(tree->nodedata.file_dta.blocks, 0);
/* Zero the file data */
tree->nodedata.file_dta.size = 0;
free(tree->nodedata.file_dta.blocks);
tree->nodedata.file_dta.blocks = NULL;
free(tree);
return 0;
} else
return rmfileFromTree(getDirSubtree(tree, path), NULL);
}
int rmdirFromTree(DirTree tree, char *path[]) {
if (!tree)
return 1;
else if (!path || !path[0]) {
/* This file is the target */
DirTree parent = tree->parent_dir;
if (tree->is_file) {
/* Node is a file */
return 2;
} else if (sizeOfLL(tree->nodedata.file_dta.blocks)) {
/* Do not allow a directory with contents to be destroyed */
return 3;
}
if (parent) {
/* Update the parent's timestamp */
updateTimestamp(parent);
/* Remove linking with parent */
remFromLL(parent->nodedata.dir_dta.files, indexOfLL(parent->nodedata.dir_dta.files, tree));
tree->parent_dir = NULL;
}
/* Zero the file data */
free(tree->nodedata.dir_dta.files);
tree->nodedata.dir_dta.files = NULL;
free(tree);
return 0;
} else
return rmfileFromTree(getDirSubtree(tree, path), NULL);
}
int isTreeFile(DirTree tree) {
return tree->is_file;
}
char* getTreeFilename(DirTree tree) {
return tree->name;
}
LList getDirTreeChildren(DirTree tree, int alphabetize) {
LList list = makeLL();
int i;
if (!tree || tree->is_file)
return list;
else if (!alphabetize) /* Simply clone if not adjusting order */
return cloneLL(tree->nodedata.dir_dta.files);
/* Add each item in alphabetical order */
for (i = sizeOfLL(tree->nodedata.dir_dta.files) - 1; i >= 0; i--) {
/* Get an item */
DirTree elem = (DirTree) getFromLL(tree->nodedata.dir_dta.files, i);
/* Create a new iterator */
LLiter iter = makeLLiter(list);
int j;
/* Iterate until the needed item is found. */
for (j = 0; iterHasNextLL(iter); j++) {
DirTree tst = (DirTree) iterNextLL(iter);
if (strcmp(tst->name, elem->name) < 0) {
break;
}
}
disposeIterLL(iter);
addToLL(list, j+1, elem);
}
return list;
}
char** pathVecOfTree(DirTree tree) {
char *name;
char **vec;
if (!tree)
return NULL;
name = tree->name;
if (tree->parent_dir == NULL || tree->parent_dir == tree) {
/* Tree is root */
vec = (char**) malloc(2*sizeof(char*));
vec[0] = (char*) malloc((1 + strlen(name)) * sizeof(char));
strcpy(vec[0], name);
vec[1] = NULL;
} else {
/* Tree is not root */
char **par_vec = pathVecOfTree(tree->parent_dir);
int i;
for (i = 0; par_vec[i]; i++);
par_vec[i] = (char*) malloc((1 + strlen(name)) * sizeof(char));
strcpy(par_vec[i], name);
/* Add to the path */
vec = realloc(par_vec, (i+2) * sizeof(char*));
if (!vec) {
vec = (char**) malloc((i+2) * sizeof(char*));
memcpy(vec, par_vec, (i+1) * sizeof(char*));
free_str_vec(par_vec);
}
vec[i+1] = NULL;
return vec;
}
return vec;
}
time_t getTreeTimestamp(DirTree tree) {
if (tree)
return tree->timestamp;
else
return time(NULL);
}
LList getTreeFileBlocks(DirTree file) {
if (!file || !(file->is_file))
return NULL;
else
return cloneLL(file->nodedata.file_dta.blocks);
}
void updateFileSize(DirTree tree, long newSize) {
if (tree && tree->is_file)
tree->nodedata.file_dta.size = newSize;
}
void updateTimestamp(DirTree tree) {
if (tree)
tree->timestamp = time(NULL);
}
void setTimestamp(DirTree tree, time_t t) {
if (tree)
tree->timestamp = t;
}
void assignMemoryBlock(DirTree tree, long blk) {
long *tmp;
/* Edge case checks */
if (!tree)
return;
else if (!(tree->is_file))
return;
tmp = (long*) malloc(sizeof(long));
*tmp = blk;
addToLL(tree->nodedata.file_dta.blocks, 0, tmp);
}
long releaseMemoryBlock(DirTree tree) {
long *res;
long val;
if (!tree || !(tree->is_file))
return -1;
res = (long*) remFromLL(tree->nodedata.file_dta.blocks, 0);
val = *res;
free(res);
return val;
}