-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.c
More file actions
executable file
·433 lines (343 loc) · 10.3 KB
/
operations.c
File metadata and controls
executable file
·433 lines (343 loc) · 10.3 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
#include "operations.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../er/error.h"
#include "../thr/threads.h"
/* Given a path, fills pointers with strings for the parent path and child
* file name
* Input:
* - path: the path to split. ATENTION: the function may alter this parameter
* - parent: reference to a char*, to store parent path
* - child: reference to a char*, to store child file name
*/
void split_parent_child_from_path(char * path, char ** parent, char ** child) {
int n_slashes = 0, last_slash_location = 0;
int len = strlen(path);
// deal with trailing slash ( a/x vs a/x/ )
if (path[len-1] == '/') {
path[len-1] = '\0';
}
for (int i=0; i < len; ++i) {
if (path[i] == '/' && path[i+1] != '\0') {
last_slash_location = i;
n_slashes++;
}
}
if (n_slashes == 0) { // root directory
*parent = "";
*child = path;
return;
}
path[last_slash_location] = '\0';
*parent = path;
*child = path + last_slash_location + 1;
}
/*
* Initializes tecnicofs and creates root node.
*/
void init_fs() {
inode_table_init();
int root = inode_create(T_DIRECTORY);
if (root != FS_ROOT)
errorParse("Error: failed to create node for tecnicofs root\n");
}
/*
* Destroy tecnicofs and inode table.
*/
void destroy_fs() {
inode_table_destroy();
}
/*
* Checks if content of directory is not empty.
* Input:
* - entries: entries of directory
* Returns: SUCCESS or FAIL
*/
int is_dir_empty(DirEntry *dirEntries) {
if (dirEntries == NULL) {
return FAIL;
}
for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
if (dirEntries[i].inumber != FREE_INODE) {
return FAIL;
}
}
return SUCCESS;
}
/*
* Looks for node in directory entry from name.
* Input:
* - name: path of node
* - entries: entries of directory
* Returns:
* - inumber: found node's inumber
* - FAIL: if not found
*/
int lookup_sub_node(char *name, DirEntry *entries) {
if (entries == NULL) {
return FAIL;
}
for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
if (entries[i].inumber != FREE_INODE && strcmp(entries[i].name, name) == 0) {
return entries[i].inumber;
}
}
return FAIL;
}
/*
* Creates a new node given a path.
* Input:
* - name: path of node
* - nodeType: type of node
* Returns: SUCCESS or FAIL
*/
int create(char *name, type nodeType, list *List){
int parent_inumber, child_inumber;
char *parent_name, *child_name, name_copy[MAX_FILE_NAME];
/* use for copy */
type pType;
union Data pdata;
strcpy(name_copy, name);
split_parent_child_from_path(name_copy, &parent_name, &child_name);
parent_inumber = lookup(parent_name, List, 1);
if (parent_inumber == FAIL) {
printf("failed to create %s, invalid parent dir %s\n",
name, parent_name);
return FAIL;
}
inode_get(parent_inumber, &pType, &pdata);
if(pType != T_DIRECTORY) {
printf("failed to create %s, parent %s is not a dir\n",
name, parent_name);
return FAIL;
}
if (lookup_sub_node(child_name, pdata.dirEntries) != FAIL) {
printf("failed to create %s, already exists in dir %s\n",
child_name, parent_name);
return FAIL;
}
/* create node and add entry to folder that contains new node */
child_inumber = inode_create(nodeType);
if (child_inumber == FAIL) {
printf("failed to create %s in %s, couldn't allocate inode\n",
child_name, parent_name);
return FAIL;
}
if (dir_add_entry(parent_inumber, child_inumber, child_name) == FAIL) {
printf("could not add entry %s in dir %s\n",
child_name, parent_name);
return FAIL;
}
return SUCCESS;
}
/*
* moves a node given a path to another path.
* Input:
* - nodeOrigin: the original path of the node
* - nodeDestination: the final path of the node
* Returns: SUCCESS or FAIL
*/
int move(char* nodeOrigin, char* nodeDestination, list *List){
int parent_inumber_orig, child_inumber_orig;
int parent_inumber_dest, child_inumber_dest;
char *parent_name_orig, *child_name_orig, name_copy_orig[MAX_FILE_NAME];
char *parent_name_dest, *child_name_dest, name_copy_dest[MAX_FILE_NAME];
type pType_orig, cType_orig;
union Data pdata_orig, cdata_orig;
type pType_dest;
union Data pdata_dest;
// Split child from path
strcpy(name_copy_orig, nodeOrigin);
split_parent_child_from_path(name_copy_orig, &parent_name_orig, &child_name_orig);
// Splits Child From Path
strcpy(name_copy_dest, nodeDestination);
split_parent_child_from_path(name_copy_dest, &parent_name_dest, &child_name_dest);
if(strcmp(parent_name_orig, parent_name_dest) < 0){
parent_inumber_orig = lookup(parent_name_orig, List, 1);
parent_inumber_dest = lookup(parent_name_dest, List, 1);
}
else{
parent_inumber_dest = lookup(parent_name_dest, List, 1);
parent_inumber_orig = lookup(parent_name_orig, List, 1);
}
inode_get(parent_inumber_orig, &pType_orig, &pdata_orig);
inode_get(parent_inumber_dest, &pType_dest, &pdata_dest);
child_inumber_orig = lookup_sub_node(child_name_orig, pdata_orig.dirEntries);
inode_get(child_inumber_orig, &cType_orig, &cdata_orig);
/* Invalid Parent Name */
if (parent_inumber_dest == FAIL) {
printf("failed to move %s, invalid parent dir %s\n",
child_name_dest, parent_name_dest);
return FAIL;
}
// Verify if parent is directory
if(pType_orig != T_DIRECTORY) {
printf("failed to move %s, parent %s is not a dir\n",
child_name_orig, parent_name_orig);
return FAIL;
}
// Verify if child origin exists
if (child_inumber_orig == FAIL) {
printf("could not move %s, does not exist in dir %s\n",
child_name_orig, parent_name_orig);
return FAIL;
}
//Verify is the one to move if its a dir is empty
if (cType_orig == T_DIRECTORY && is_dir_empty(cdata_orig.dirEntries) == FAIL) {
printf("could not move %s: is a directory and not empty\n",
name_copy_orig);
return FAIL;
}
/* Origin And Destiny name need to be the same */
if(strcmp(child_name_orig, child_name_dest)){
printf("failed to move %s, invalid destiny path %s\n ",
child_name_orig, child_name_dest);
return FAIL;
}
/* Destination cant exist */
if (lookup_sub_node(child_name_dest, pdata_dest.dirEntries) != FAIL) {
printf("failed to move %s, already exists in dir %s\n",
child_name_orig, parent_name_dest);
return FAIL;
}
/* Delete Node */
/* remove entry from folder that contained deleted node */
if (dir_reset_entry(parent_inumber_orig, child_inumber_orig) == FAIL) {
printf("failed to delete %s from dir %s\n",
child_name_orig, parent_name_orig);
return FAIL;
}
/* Delete node */
if (inode_delete(child_inumber_orig) == FAIL) {
printf("could not delete inode number %d from dir %s\n",
child_inumber_orig, parent_name_orig);
return FAIL;
}
/* Create Node */
child_inumber_dest = inode_create(pType_orig);
if (child_inumber_dest == FAIL) {
printf("failed to create %s in %s, couldn't allocate inode\n",
child_name_dest, parent_name_dest);
return FAIL;
}
/* Add Entry */
if (dir_add_entry(parent_inumber_dest, child_inumber_dest, child_name_dest) == FAIL) {
printf("could not add entry %s in dir %s\n",
child_name_dest, parent_name_dest);
return FAIL;
}
return SUCCESS;
}
/*
* Deletes a node given a path.
* Input:
* - name: path of node
* Returns: SUCCESS or FAIL
*/
int delete(char *name, list *List){
int parent_inumber, child_inumber;
char *parent_name, *child_name, name_copy[MAX_FILE_NAME];
/* use for copy */
type pType, cType;
union Data pdata, cdata;
strcpy(name_copy, name);
split_parent_child_from_path(name_copy, &parent_name, &child_name);
parent_inumber = lookup(parent_name, List, 1);
if (parent_inumber == FAIL) {
printf("failed to delete %s, invalid parent dir %s\n",
child_name, parent_name);
return FAIL;
}
inode_get(parent_inumber, &pType, &pdata);
if(pType != T_DIRECTORY) {
printf("failed to delete %s, parent %s is not a dir\n",
child_name, parent_name);
return FAIL;
}
child_inumber = lookup_sub_node(child_name, pdata.dirEntries);
if (child_inumber == FAIL) {
printf("could not delete %s, does not exist in dir %s\n",
name, parent_name);
return FAIL;
}
inode_get(child_inumber, &cType, &cdata);
if (cType == T_DIRECTORY && is_dir_empty(cdata.dirEntries) == FAIL) {
printf("could not delete %s: is a directory and not empty\n",
name);
return FAIL;
}
/* remove entry from folder that contained deleted node */
if (dir_reset_entry(parent_inumber, child_inumber) == FAIL) {
printf("failed to delete %s from dir %s\n",
child_name, parent_name);
return FAIL;
}
if (inode_delete(child_inumber) == FAIL) {
printf("could not delete inode number %d from dir %s\n",
child_inumber, parent_name);
return FAIL;
}
return SUCCESS;
}
/*
* Lookup for a given path.
* Input:
* - name: path of node
* Returns:
* inumber: identifier of the i-node, if found
* FAIL: otherwise
*/
int lookup(char *name, list* List, int doLockWrite) {
char full_path[MAX_FILE_NAME];
char test_path[MAX_FILE_NAME];
char delim[] = "/";
char *parent_name, *child_name, *saveptr;
strcpy(full_path, name);
strcpy(test_path, name);
split_parent_child_from_path(test_path, &parent_name, &child_name);
/* start at root node */
int current_inumber = FS_ROOT;
/* use for copy */
type nType;
union Data data;
/* Lock Root */
if(!searchList(getLockInumber(current_inumber), List)){
if(!strcmp(child_name, "") && doLockWrite){
lockInumberWrite(current_inumber);
addList(List, getLockInumber(current_inumber));
}
else{
lockInumberRead(current_inumber);
addList(List, getLockInumber(current_inumber));
}
}
/* get root inode data */
inode_get(current_inumber, &nType, &data);
char *path = strtok_r(full_path, delim, &saveptr);
/* search for all sub nodes */
while (path != NULL && (current_inumber = lookup_sub_node(path, data.dirEntries)) != FAIL) {
/* Lock node */
if(!searchList(getLockInumber(current_inumber), List)){
if(!strcmp(child_name, path) && doLockWrite){
lockInumberWrite(current_inumber);
addList(List, getLockInumber(current_inumber));
}
else{
lockInumberRead(current_inumber);
addList(List, getLockInumber(current_inumber));
}
}
inode_get(current_inumber, &nType, &data);
path = strtok_r(NULL, delim, &saveptr);
}
return current_inumber;
}
/*
* Prints tecnicofs tree.
* Input:
* - fp: pointer to output file
*/
void print_tecnicofs_tree(FILE *fp){
inode_print_tree(fp, FS_ROOT, "");
}