-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnix_File_System_Simulation.c
More file actions
810 lines (737 loc) · 21.7 KB
/
Unix_File_System_Simulation.c
File metadata and controls
810 lines (737 loc) · 21.7 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_NAME 256
#define MAX_CONTENT 1024
#define MAX_PATH_LENGTH 4096
#define MAX_QUEUE 1000
typedef enum
{
TYPE_FILE = 0,
TYPE_FOLDER = 1
} NodeType;
typedef struct FileNode
{
char fileName[MAX_NAME];
char fileContent[MAX_CONTENT];
NodeType type;
time_t createdTime;
time_t modifiedTime;
struct FileNode *parent;
struct FileNode *fChild;
struct FileNode *nSibling;
} FileNode;
// Function prototypes
FileNode *createNode(const char *name, const char *content, NodeType type);
FileNode *findNode(FileNode *root, const char *name);
FileNode *findParent(FileNode *parentFolder, const char *node);
int insertNode(FileNode *parent, FileNode *newNode);
int deleteNode(FileNode *root, FileNode *parent, const char *name);
void freeTree(FileNode *node);
void listDirectory(FileNode *node, int showDetails);
void printPath(FileNode *node);
void displayHelp(void);
char *getCurrentTime(time_t t);
int isValidName(const char *name);
// Create a new file/folder node
FileNode *createNode(const char *name, const char *content, NodeType type)
{
if (!isValidName(name))
{
printf("Error: Invalid name '%s'\n", name);
return NULL;
}
FileNode *node = (FileNode *)malloc(sizeof(FileNode));
if (!node)
{
printf("Error: Memory allocation failed\n");
return NULL;
}
strncpy(node->fileName, name, MAX_NAME - 1);
node->fileName[MAX_NAME - 1] = '\0';
if (content)
{
strncpy(node->fileContent, content, MAX_CONTENT - 1);
node->fileContent[MAX_CONTENT - 1] = '\0';
}
else
{
node->fileContent[0] = '\0';
}
node->type = type;
node->createdTime = time(NULL);
node->modifiedTime = node->createdTime;
node->parent = NULL;
node->fChild = NULL;
node->nSibling = NULL;
return node;
}
// Validate filename
int isValidName(const char *name)
{
if (!name || strlen(name) == 0 || strlen(name) >= MAX_NAME)
{
return 0;
}
// Check for invalid characters
const char *invalid = "/\\:*?\"<>|";
for (int i = 0; name[i]; i++)
{
if (strchr(invalid, name[i]))
{
return 0;
}
}
// Check for reserved names
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
{
return 0;
}
return 1;
}
// Find node by name using BFS
FileNode *findNode(FileNode *root, const char *name)
{
if (!root || !name)
return NULL;
FileNode *queue[MAX_QUEUE];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear)
{
FileNode *curr = queue[front++];
if (strcmp(curr->fileName, name) == 0)
{
return curr;
}
// Add first child
if (curr->fChild)
{
if (rear < MAX_QUEUE)
queue[rear++] = curr->fChild;
}
// Add all siblings
FileNode *sibling = curr->nSibling;
while (sibling)
{
if (rear < MAX_QUEUE)
queue[rear++] = sibling;
sibling = sibling->nSibling;
}
}
return NULL;
}
// Find parent of a specific node
FileNode *findParent(FileNode *parentFolder, const char *name)
{
if (!parentFolder || !name)
return NULL;
// Check direct children
FileNode *child = parentFolder->fChild;
FileNode *prev = NULL;
while (child)
{
if (strcmp(child->fileName, name) == 0)
{
return prev ? prev : parentFolder;
}
prev = child;
child = child->nSibling;
}
return NULL;
}
// Insert node into parent directory
int insertNode(FileNode *parent, FileNode *newNode)
{
if (!parent || !newNode)
return 0;
if (parent->type != TYPE_FOLDER)
{
printf("Error: '%s' is not a directory\n", parent->fileName);
return 0;
}
// Check for duplicate names
FileNode *child = parent->fChild;
while (child)
{
if (strcmp(child->fileName, newNode->fileName) == 0)
{
printf("Error: '%s' already exists\n", newNode->fileName);
return 0;
}
child = child->nSibling;
}
newNode->parent = parent;
if (!parent->fChild)
{
parent->fChild = newNode;
}
else
{
FileNode *temp = parent->fChild;
while (temp->nSibling)
{
temp = temp->nSibling;
}
temp->nSibling = newNode;
}
parent->modifiedTime = time(NULL);
return 1;
}
// Delete node from tree
int deleteNode(FileNode *root, FileNode *parent, const char *name)
{
if (!parent || !name)
return 0;
FileNode *child = parent->fChild;
FileNode *prev = NULL;
while (child)
{
if (strcmp(child->fileName, name) == 0)
{
// Check if directory is empty
if (child->type == TYPE_FOLDER && child->fChild)
{
printf("Error: Directory '%s' is not empty\n", name);
return 0;
}
// Remove from linked list
if (prev)
{
prev->nSibling = child->nSibling;
}
else
{
parent->fChild = child->nSibling;
}
free(child);
parent->modifiedTime = time(NULL);
return 1;
}
prev = child;
child = child->nSibling;
}
printf("Error: '%s' not found\n", name);
return 0;
}
// Free entire tree
void freeTree(FileNode *node)
{
if (!node)
return;
// Free all children recursively
FileNode *child = node->fChild;
while (child)
{
FileNode *next = child->nSibling;
freeTree(child);
child = next;
}
free(node);
}
// List directory contents
void listDirectory(FileNode *node, int showDetails)
{
if (!node)
return;
FileNode *child = node->fChild;
if (!child)
{
return;
}
while (child)
{
if (showDetails)
{
char typeChar = (child->type == TYPE_FOLDER) ? 'd' : '-';
char *modTime = getCurrentTime(child->modifiedTime);
printf("%c %s %s\n", typeChar, modTime, child->fileName);
free(modTime);
}
else
{
if (child->type == TYPE_FOLDER)
{
printf("%s/\n", child->fileName);
}
else
{
printf("%s\n", child->fileName);
}
}
child = child->nSibling;
}
}
// Print full path from root
void printPath(FileNode *node)
{
if (!node)
return;
char path[MAX_PATH_LENGTH] = "";
FileNode *stack[100];
int top = -1;
// Build stack of nodes from current to root
FileNode *temp = node;
while (temp)
{
stack[++top] = temp;
temp = temp->parent;
}
// Print path from root to current
while (top >= 0)
{
strcat(path, stack[top]->fileName);
if (top > 0)
strcat(path, "/");
top--;
}
printf("%s\n", path);
}
// Get formatted time string
char *getCurrentTime(time_t t)
{
char *timeStr = (char *)malloc(20);
struct tm *tm_info = localtime(&t);
strftime(timeStr, 20, "%b %d %H:%M", tm_info);
return timeStr;
}
// Display help information
void displayHelp(void)
{
printf("\n=== File System Commands ===\n");
printf(" man - Display this help message\n");
printf(" ls [-l] - List directory contents (-l for details)\n");
printf(" pwd - Print working directory\n");
printf(" cd <dir> - Change directory\n");
printf(" mkdir <name> - Create directory\n");
printf(" touch <name> - Create file\n");
printf(" rm <name> - Remove file/empty directory\n");
printf(" cat <file> - Display file content\n");
printf(" echo > <file> - Write to file\n");
printf(" cp <src> <dst> - Copy file\n");
printf(" mv <src> <dst> - Move file/directory\n");
printf(" rename <old> <new> - Rename file/directory\n");
printf(" find <name> - Find file/directory path\n");
printf(" tree - Display directory tree\n");
printf(" clear - Clear screen\n");
printf(" exit - Exit program\n");
printf("============================\n\n");
}
// Display tree structure
void displayTree(FileNode *node, int depth)
{
if (!node)
return;
for (int i = 0; i < depth; i++)
{
printf(" ");
}
if (node->type == TYPE_FOLDER)
{
printf("[DIR] %s/\n", node->fileName);
}
else
{
printf(" %s\n", node->fileName);
}
FileNode *child = node->fChild;
while (child)
{
displayTree(child, depth + 1);
child = child->nSibling;
}
}
int main(void)
{
// Create root directory
FileNode *root = createNode("root", "", TYPE_FOLDER);
if (!root)
{
printf("Failed to create root directory\n");
return 1;
}
FileNode *current = root;
char prompt[MAX_PATH_LENGTH];
char input[MAX_PATH_LENGTH];
printf("Welcome to Enhanced File System Simulator\n");
printf("Type 'man' for help, 'exit' to quit\n\n");
while (1)
{
// Build prompt
snprintf(prompt, MAX_PATH_LENGTH, "user@filesystem:~/%s$ ", current->fileName);
printf("%s", prompt);
fflush(stdout);
if (!fgets(input, MAX_PATH_LENGTH, stdin))
{
break;
}
// Remove newline
input[strcspn(input, "\n")] = 0;
if (strlen(input) == 0)
continue;
char *cmd = strtok(input, " ");
if (strcmp(cmd, "exit") == 0)
{
break;
}
else if (strcmp(cmd, "man") == 0 || strcmp(cmd, "help") == 0)
{
displayHelp();
}
else if (strcmp(cmd, "clear") == 0)
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
else if (strcmp(cmd, "ls") == 0)
{
char *flag = strtok(NULL, " ");
listDirectory(current, flag && strcmp(flag, "-l") == 0);
}
else if (strcmp(cmd, "pwd") == 0)
{
printPath(current);
}
else if (strcmp(cmd, "cd") == 0)
{
char *dir = strtok(NULL, " ");
if (!dir)
{
printf("Usage: cd <directory>\n");
}
else if (strcmp(dir, "..") == 0)
{
if (current->parent)
{
current = current->parent;
}
}
else if (strcmp(dir, "~") == 0 || strcmp(dir, "/") == 0)
{
current = root;
}
else
{
FileNode *target = NULL;
FileNode *child = current->fChild;
while (child)
{
if (strcmp(child->fileName, dir) == 0)
{
target = child;
break;
}
child = child->nSibling;
}
if (target && target->type == TYPE_FOLDER)
{
current = target;
}
else if (target)
{
printf("Error: '%s' is not a directory\n", dir);
}
else
{
printf("Error: '%s' not found\n", dir);
}
}
}
else if (strcmp(cmd, "mkdir") == 0)
{
char *name = strtok(NULL, " ");
if (!name)
{
printf("Usage: mkdir <directory_name>\n");
}
else
{
FileNode *newDir = createNode(name, "", TYPE_FOLDER);
if (newDir)
{
if (!insertNode(current, newDir))
{
free(newDir);
}
}
}
}
else if (strcmp(cmd, "touch") == 0)
{
char *name = strtok(NULL, " ");
if (!name)
{
printf("Usage: touch <filename>\n");
}
else
{
FileNode *newFile = createNode(name, "", TYPE_FILE);
if (newFile)
{
if (!insertNode(current, newFile))
{
free(newFile);
}
}
}
}
else if (strcmp(cmd, "rm") == 0)
{
char *name = strtok(NULL, " ");
if (!name)
{
printf("Usage: rm <name>\n");
}
else
{
deleteNode(root, current, name);
}
}
else if (strcmp(cmd, "cat") == 0)
{
char *name = strtok(NULL, " ");
if (!name)
{
printf("Usage: cat <filename>\n");
}
else
{
FileNode *child = current->fChild;
while (child)
{
if (strcmp(child->fileName, name) == 0)
{
if (child->type == TYPE_FILE)
{
printf("%s\n", child->fileContent);
}
else
{
printf("Error: '%s' is a directory\n", name);
}
break;
}
child = child->nSibling;
}
if (!child)
{
printf("Error: '%s' not found\n", name);
}
}
}
else if (strcmp(cmd, "echo") == 0)
{
char *arrow = strtok(NULL, " ");
if (arrow && strcmp(arrow, ">") == 0)
{
char *name = strtok(NULL, " ");
if (!name)
{
printf("Usage: echo > <filename>\n");
}
else
{
printf("Enter content (press Enter to finish):\n");
char content[MAX_CONTENT];
if (fgets(content, MAX_CONTENT, stdin))
{
content[strcspn(content, "\n")] = 0;
FileNode *child = current->fChild;
while (child)
{
if (strcmp(child->fileName, name) == 0)
{
if (child->type == TYPE_FILE)
{
strncpy(child->fileContent, content, MAX_CONTENT - 1);
child->modifiedTime = time(NULL);
}
else
{
printf("Error: '%s' is a directory\n", name);
}
break;
}
child = child->nSibling;
}
if (!child)
{
FileNode *newFile = createNode(name, content, TYPE_FILE);
if (newFile)
{
if (!insertNode(current, newFile))
{
free(newFile);
}
}
}
}
}
}
else
{
printf("Usage: echo > <filename>\n");
}
}
else if (strcmp(cmd, "find") == 0)
{
char *name = strtok(NULL, " ");
if (!name)
{
printf("Usage: find <name>\n");
}
else
{
FileNode *found = findNode(root, name);
if (found)
{
printPath(found);
}
else
{
printf("'%s' not found\n", name);
}
}
}
else if (strcmp(cmd, "tree") == 0)
{
displayTree(current, 0);
}
else if (strcmp(cmd, "rename") == 0)
{
char *oldName = strtok(NULL, " ");
char *newName = strtok(NULL, " ");
if (!oldName || !newName)
{
printf("Usage: rename <old_name> <new_name>\n");
}
else if (!isValidName(newName))
{
printf("Error: Invalid name '%s'\n", newName);
}
else
{
FileNode *child = current->fChild;
while (child)
{
if (strcmp(child->fileName, oldName) == 0)
{
strncpy(child->fileName, newName, MAX_NAME - 1);
child->modifiedTime = time(NULL);
break;
}
child = child->nSibling;
}
if (!child)
{
printf("Error: '%s' not found\n", oldName);
}
}
}
else if (strcmp(cmd, "mv") == 0)
{
char *src = strtok(NULL, " ");
char *dst = strtok(NULL, " ");
if (!src || !dst)
{
printf("Usage: mv <source> <destination>\n");
}
else
{
FileNode *srcNode = findNode(root, src);
FileNode *dstNode = findNode(root, dst);
if (!srcNode)
{
printf("Error: '%s' not found\n", src);
}
else if (!dstNode || dstNode->type != TYPE_FOLDER)
{
printf("Error: '%s' is not a valid directory\n", dst);
}
else
{
// Remove from old location
FileNode *srcParent = srcNode->parent;
if (srcParent)
{
FileNode *child = srcParent->fChild;
FileNode *prev = NULL;
while (child)
{
if (child == srcNode)
{
if (prev)
{
prev->nSibling = child->nSibling;
}
else
{
srcParent->fChild = child->nSibling;
}
child->nSibling = NULL;
break;
}
prev = child;
child = child->nSibling;
}
}
// Insert in new location
insertNode(dstNode, srcNode);
}
}
}
else if (strcmp(cmd, "cp") == 0)
{
char *src = strtok(NULL, " ");
char *dst = strtok(NULL, " ");
if (!src || !dst)
{
printf("Usage: cp <source> <destination>\n");
}
else
{
FileNode *child = current->fChild;
while (child)
{
if (strcmp(child->fileName, src) == 0)
{
if (child->type == TYPE_FILE)
{
FileNode *copy = createNode(dst, child->fileContent, TYPE_FILE);
if (copy)
{
if (!insertNode(current, copy))
{
free(copy);
}
}
}
else
{
printf("Error: Cannot copy directories\n");
}
break;
}
child = child->nSibling;
}
if (!child)
{
printf("Error: '%s' not found\n", src);
}
}
}
else
{
printf("Command not found: %s\n", cmd);
printf("Type 'man' for help\n");
}
}
printf("\nCleaning up...\n");
freeTree(root);
printf("Goodbye!\n");
return 0;
}