-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.c
More file actions
489 lines (428 loc) · 16.1 KB
/
Copy pathtree.c
File metadata and controls
489 lines (428 loc) · 16.1 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
489
// Copyright Spinochi Andreea && Chesches Iulia, 312CA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tree.h"
#include "utils.h"
#define TREE_CMD_INDENT_SIZE 4
#define NO_ARG ""
#define PARENT_DIR ".."
// create the tree structure
FileTree createFileTree(char* rootFolderName) {
FileTree fileTree;
TreeNode *root = malloc(sizeof(TreeNode));
DIE(!root, "Malloc failed\n");
root->parent = NULL;
root->name = rootFolderName;
root->type = FOLDER_NODE;
root->content = malloc(sizeof(FolderContent));
DIE(!root->content, "Malloc failed\n");
((FolderContent *)root->content)->children = list_create();
fileTree.root = root;
return fileTree;
}
// destroy the tree structure
void freeTree(FileTree fileTree) {
// create a tree node for the root parent in order to free the root
TreeNode *root_parent = malloc(sizeof(TreeNode));
DIE(!root_parent, "Malloc failed\n");
root_parent->parent = NULL;
root_parent->name = malloc(sizeof(char) * 20);
DIE(!root_parent->name, "Malloc failed\n");
strcpy(root_parent->name, "root_parent");
root_parent->type = 1;
root_parent->content = malloc(sizeof(FolderContent));
DIE(!root_parent->content, "Malloc failed\n");
((FolderContent *)root_parent->content)->children = list_create();
list_add_node(((FolderContent *)root_parent->content)
->children, fileTree.root->name);
free(((FolderContent *)root_parent->content)->children->head->info);
((FolderContent *)root_parent->content)->children->head
->info = fileTree.root;
fileTree.root->parent = root_parent;
// destroy the tree from the root parent down
rmrec(root_parent, fileTree.root->name);
// free the root parent
free(((FolderContent *)root_parent->content)->children);
free((FolderContent *)root_parent->content);
free(root_parent->name);
free(root_parent);
}
// display the files and folders from the current directory
void ls(TreeNode* currentNode, char* arg) {
if (arg[0] == 0) {
ListNode *aux = ((FolderContent *)currentNode->content)
->children->head;
while (aux) {
if (aux->info->name)
printf("%s\n", aux->info->name);
aux = aux->next;
}
} else {
// if the content inside the directory is anoter directory, then
// go inside it and display the files and folders
TreeNode *aux = get_dir(currentNode, arg);
if (aux) {
ListNode *curr = ((FolderContent *)aux->content)->children
->head;
while (curr) {
printf("%s\n", curr->info->name);
curr = curr->next;
}
} else {
// if the content inside the directory is a file, then display
// only its name
TreeNode *aux = get_file(currentNode, arg);
if (aux) {
if (aux->name != NULL) {
printf("%s", aux->name);
if (((FileContent *)aux->content)->text)
printf(": %s\n",
((FileContent *)aux->content)->text);
}
} else {
printf("ls: cannot access '%s': ", arg);
printf("No such file or directory\n");
return;
}
}
}
}
// display the current directory
void pwd(TreeNode* treeNode) {
char **pwd_name = malloc(50 * sizeof(char *));
DIE(!pwd_name, "Malloc failed\n");
TreeNode *aux = treeNode;
// make a vector of words to store the path of the current directory
int i = 0;
while (aux) {
pwd_name[i] = aux->name;
aux = aux->parent;
i++;
}
// print the path of the current directory
int j;
for (j = i - 1; j >= 0; j--) {
printf("%s", pwd_name[j]);
if (j != 0) {
printf("/");
}
}
}
// go to the specified folder and return a pointer to it
TreeNode* cd(TreeNode* currentNode, char* path) {
TreeNode *aux = generic_cd(currentNode, path);
if (aux) {
return aux;
} else {
printf("cd: ");
printf("no such file or directory: %s\n", path);
}
return currentNode;
}
// display all the files and folders in the tree
void tree(TreeNode* currentNode, char* arg) {
int files = 0, directories = 0;
TreeNode *aux = currentNode;
aux = generic_cd(aux, arg);
if (!aux) {
printf("%s [error opening dir]\n\n0 directories, 0 files\n", arg);
return;
}
int ok = 0;
if (arg[0] == 0) {
print_rec(currentNode, 0, &files, &directories, &ok);
} else {
print_rec(aux, 0, &files, &directories, &ok);
}
printf("%d directories, %d files\n", directories, files);
}
// create a new directory
void mkdir(TreeNode* currentNode, char* folderName) {
// parse the current directory to verify if the folder already exists
ListNode * aux = ((FolderContent *)currentNode->content)->children->head;
while (aux)
{
if (aux->info->type == 1)
{
if (strcmp(folderName, aux->info->name) == 0)
{
printf("mkdir: cannot create directory '%s': ", folderName);
printf("File exists\n");
return;
}
}
aux = aux->next;
}
// add the new folder to the current directory
list_add_node(((FolderContent *)currentNode->content)->children,
folderName);
((FolderContent *)currentNode->content)->children->head->info
->type = 1;
((FolderContent *)currentNode->content)->children->head->info
->parent = currentNode;
((FolderContent *)currentNode->content)->children->head->info
->content = malloc(sizeof(FolderContent));
DIE(!((FolderContent *)currentNode->content)->children->head->info
->content, "Malloc failed\n");
((FolderContent *)((FolderContent *)currentNode->content)->children
->head->info->content)->children = list_create();
}
// delete the files and folders recursively
void rmrec(TreeNode* currentNode, char* resourceName) {
// check if the resource is a file or a directory
TreeNode *aux = get_dir(currentNode, resourceName);
ListNode *prev = NULL;
if (aux) {
// parse the directory to delete all the files and folders inside
ListNode *aux2 = ((FolderContent *)aux->content)->children->head;
prev = aux2;
while (aux2) {
prev = aux2;
aux2 = aux2->next;
rmrec(aux, prev->info->name);
}
free(list_remove_dir(((FolderContent *)currentNode->content)->children,
aux->name));
} else {
aux = get_file(currentNode, resourceName);
if (aux) {
free(list_remove_file(((FolderContent *)currentNode
->content)->children, resourceName));
} else {
printf("rmrec: failed to remove '%s': ", resourceName);
printf("No such file or directory\n");
}
}
}
// delete a file
void rm(TreeNode* currentNode, char* fileName) {
// check if the file we want to delete exists
TreeNode *aux = get_file(currentNode, fileName);
if (aux) {
free(list_remove_file(((FolderContent *)currentNode->content)->children,
fileName));
} else {
aux = get_dir(currentNode, fileName);
if (aux) {
printf("rm: cannot remove '%s': Is a directory\n", fileName);
} else {
printf("rm: failed to remove '%s': ", fileName);
printf("No such file or directory\n");
}
}
}
// delete a directory
void rmdir(TreeNode* currentNode, char* folderName) {
// check if the folder we want to delete exists
TreeNode *aux = get_dir(currentNode, folderName);
// only delete the folder if it is empty
if (aux) {
if (!((FolderContent*)aux->content)->children->head) {
free(list_remove_dir(((FolderContent *)currentNode->content)
->children, folderName));
} else {
printf("rmdir: failed to remove '%s': ", folderName);
printf("Directory not empty\n");
}
} else {
aux = get_file(currentNode, folderName);
if (aux) {
printf("rmdir: failed to remove '%s': ", folderName);
printf("Not a directory\n");
} else {
printf("rmdir: failed to remove '%s': ", folderName);
printf("No such file or directory\n");
}
}
}
// create a file and add it to the current directory
void touch(TreeNode* currentNode, char* fileName, char* fileContent) {
// parse the current directory to verify if the file already exists
ListNode * aux = ((FolderContent *)currentNode->content)->children->head;
while (aux)
{
if (aux->info->type == 0)
{
if (strcmp(fileName, aux->info->name) == 0)
{
return;
}
}
aux = aux->next;
}
// add the new file to the current directory
list_add_node(((FolderContent *)currentNode->content)->children,
fileName);
((FolderContent *)currentNode->content)->children->head->info
->type = 0;
((FolderContent *)currentNode->content)->children->head->info
->parent = currentNode;
((FolderContent *)currentNode->content)->children->head->info
->content = malloc(sizeof(FileContent *));
DIE(!((FolderContent *)currentNode->content)->children->head->info
->content, "Malloc failed\n");
((FileContent *)((FolderContent *)currentNode->content)->children
->head->info->content)->text = fileContent;
}
// copy the content of a file to another file
void cp(TreeNode* currentNode, char* source, char* destination) {
// find the path of the source file
char *source_copy = malloc(strlen(source) + 1);
DIE(!source_copy, "Malloc failed\n");
memcpy(source_copy, source, strlen(source) + 1);
strcat(source_copy, "/");
char *token = strtok(source_copy , "/");
char *token_source = malloc(sizeof(char) * 2);
DIE(!token_source, "Malloc failed\n");
while (token) {
strcpy(token_source, token);
token = strtok(NULL, "/");
}
// find the path of the destination file
char *destination_copy = malloc(strlen(destination) + 1);
DIE(!destination_copy, "Malloc failed\n");
memcpy(destination_copy, destination, strlen(destination) + 1);
strcat(destination_copy, "/");
token = strtok(destination_copy , "/");
char *token_destination = malloc(sizeof(char) * 2);
DIE(!token_destination, "Malloc failed\n");
while (token) {
strcpy(token_destination, token);
token = strtok(NULL, "/");
}
memcpy(destination_copy, destination, strlen(destination) + 1);
TreeNode* aux2;
// if the source is inside a directory we need to go to that directory
// to get the file
if (strcmp(token_source, source_copy) != 0) {
strcat(source_copy, "/");
TreeNode *token_source_dir = generic_cd(currentNode, source_copy);
aux2 = get_file(token_source_dir, token_source);
} else {
aux2 = get_file(currentNode, token_source);
}
TreeNode* aux = generic_cd(currentNode, destination_copy);
TreeNode* aux3 = generic_cd2(currentNode, destination_copy);
if (aux && aux2) {
touch(aux, strdup(aux2->name), strdup(((FileContent *)aux2->
content)->text));
} else {
if (!aux2) {
printf("cp: -r not specified; omitting directory '%s'\n",
source);
free(destination_copy);
free(source_copy);
return;
}
if (!aux && aux2) {
aux = get_file(aux3, token_destination);
if (aux) {
free(((FileContent *)aux->content)->text);
((FileContent *)aux->content)
->text = strdup(((FileContent *)aux2->content)->text);
} else {
printf("cp: failed to access '%s': Not a directory\n",
destination);
}
}
}
free(destination_copy);
free(source_copy);
}
// move the content of a file or directory to another file or directory
void mv(TreeNode* currentNode, char* source, char* destination) {
// find the path of the source file
char *source_copy = malloc(strlen(source) + 1);
DIE(!source_copy, "Malloc failed\n");
memcpy(source_copy, source, strlen(source) + 1);
strcat(source_copy, "/");
char *token = strtok(source_copy , "/");
char *token_source = malloc(sizeof(char) * 2);
DIE(!token, "Mallloc failed");
while (token) {
strcpy(token_source, token);
token = strtok(NULL, "/");
}
// find the path of the destination file
char *destination_copy = malloc(strlen(destination) + 1);
DIE(!destination_copy, "Malloc failed");
memcpy(destination_copy, destination, strlen(destination) + 1);
strcat(destination_copy, "/");
token = strtok(destination_copy , "/");
char *token_destination = malloc(sizeof(char) * 2);
DIE(!token_destination, "Malloc failed");
while (token) {
strcpy(token_destination, token);
token = strtok(NULL, "/");
}
memcpy(destination_copy, destination, strlen(destination) + 1);
TreeNode* is_file;
TreeNode *token_source_dir = NULL;
// if the source is inside a directory we need to go to that directory
// to get the file
if (strcmp(token_source, source_copy) != 0) {
strcat(source_copy, "/");
token_source_dir = generic_cd(currentNode, source_copy);
is_file = get_file(token_source_dir, token_source);
} else {
is_file = get_file(currentNode, source);
}
if (is_file) {
if (!token_source_dir) {
TreeNode *is_dir = generic_cd(currentNode, destination_copy);
if (!is_dir) {
printf("mv: failed to access '%s': Not a directory\n",
destination);
return;
}
cp(currentNode, source, destination);
rm(currentNode, source);
} else {
cp(currentNode, source, destination);
rm(token_source_dir, token_source);
}
} else {
TreeNode *is_dir = get_dir(currentNode, source);
if (is_dir) {
TreeNode *aux = generic_cd(currentNode, destination);
if (aux) {
TreeNode *source_dir = generic_cd2(currentNode, source);
TreeNode *aux_source = source_dir;
TreeNode *destination_dir = generic_cd(currentNode,
destination_copy);
list_add_node(((FolderContent *)destination_dir->content)
->children, aux_source->name);
((FolderContent *)destination_dir->content)->children->head
->info->type = 1;
((FolderContent *)destination_dir->content)->children->head->
info->parent = destination_dir;
free(((FolderContent *)destination_dir->content)->children->
head->info);
((FolderContent *)destination_dir->content)->children->head
->info = aux_source;
TreeNode *parent = aux_source->parent;
ListNode *aux_list = ((FolderContent *)parent->content)
->children->head;
// change the parent of the node
int i = 0;
ListNode *prev;
prev = aux_list;
while (aux_list) {
if (strcmp(aux_list->info->name,
aux_source->name) == 0) {
if (i == 0) {
((FolderContent *)parent->content)
->children->head = aux_list->next;
} else {
prev->next = aux_list->next;
}
}
i++;
prev = aux_list;
aux_list = aux_list->next;
}
}
}
}
}