-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpr4.c
More file actions
1507 lines (1189 loc) · 50.1 KB
/
pr4.c
File metadata and controls
1507 lines (1189 loc) · 50.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
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* CMPSC 473, Spring 2014, Project 4
*
* Authors: Gabe Harms
Luke Uliana
* John Guda
*
*
* 24 April 2014
* Project 4
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
/*--------------------------------------------------------------------------------*/
int debug = 0; // extra output; 1 = on, 0 = off
/*--------------------------------------------------------------------------------*/
/* The input file (stdin) represents a sequence of file-system commands,
* which all look like cmd filename filesize
*
* command action
* ------- ------
* root initialize root directory
* print print current working directory and all descendants
* chdir change current working directory
* (.. refers to parent directory, as in Unix)
* mkdir sub-directory create (mk = make)
* rmdir delete (rm = delete)
* mvdir rename (mv = move)
* mkfil file create
* rmfil delete
* mvfil rename
* szfil resize (sz = size)
* exit quit the program immediately
*/
/* The size argument is usually ignored.
* The return value is 0 (success) or -1 (failure).
*/
int do_root (char *name, char *size);
int do_print(char *name, char *size);
int do_chdir(char *name, char *size);
int do_mkdir(char *name, char *size);
int do_rmdir(char *name, char *size);
int do_mvdir(char *name, char *size);
int do_mkfil(char *name, char *size);
int do_rmfil(char *name, char *size);
int do_mvfil(char *name, char *size);
int do_szfil(char *name, char *size);
int do_exit (char *name, char *size);
struct action {
char *cmd; // pointer to string
int (*action)(char *name, char *size); // pointer to function
} table[] = {
{ "root" , do_root },
{ "print", do_print },
{ "chdir", do_chdir },
{ "mkdir", do_mkdir },
{ "rmdir", do_rmdir },
{ "mvdir", do_mvdir },
{ "mkfil", do_mkfil },
{ "rmfil", do_rmfil },
{ "mvfil", do_mvfil },
{ "szfil", do_szfil },
{ "exit" , do_exit },
{ NULL, NULL } // end marker, do not remove
};
/*--------------------------------------------------------------------------------*/
void printing(char *name);
void print_descriptor ( );
void parse(char *buf, int *argc, char *argv[]);
int allocate_block (char *name, bool directory ) ;
void unallocate_block ( int offset );
int find_block ( char* name, bool directory );
int add_descriptor ( char * name );
int edit_descriptor ( int free_index, bool free, int name_index, char * name );
int edit_descriptor_name (int index, char* new_name);
int add_directory( char * name );
int remove_directory( char * name );
int rename_directory( char *name, char *new_name );
int edit_directory ( char * name, char*subitem_name, char *new_name, bool name_change, bool directory );
int add_file( char * name, int size );
int edit_file ( char * name, int size, char *new_name );
int remove_file (char* name);
int edit_directory_subitem (char* name, char* sub_name, char* new_sub_name);
void print_directory ( char *name);
char * get_directory_name ( char*name );
char * get_directory_top_level ( char*name);
char * get_directory_subitem ( char*name, int subitem_index, char * subitem );
int get_directory_subitem_count ( char*name);
char * get_file_name ( char*name );
char * get_file_top_level ( char*name);
int get_file_size( char*name);
void print_file ( char *name);
#define LINESIZE 128
#define DISK_PARTITION 4000000
#define BLOCK_SIZE 5000
#define BLOCKS 4000000/5000
#define MAX_STRING_LENGTH 20
#define MAX_FILE_DATA_BLOCKS (BLOCK_SIZE-64*59)
#define MAX_SUBDIRECTORIES (BLOCK_SIZE - 136)/MAX_STRING_LENGTH
typedef struct {
char directory[MAX_STRING_LENGTH];
int directory_index;
char parent[MAX_STRING_LENGTH];
int parent_index;
} working_directory;
typedef struct dir_type {
char name[MAX_STRING_LENGTH]; //Name of file or dir
char top_level[MAX_STRING_LENGTH]; //Name of dir one level up
char (*subitem)[MAX_STRING_LENGTH];
bool subitem_type[MAX_SUBDIRECTORIES]; //true if directory, false if file
int subitem_count;
struct dir_type *next;
} dir_type;
typedef struct file_type {
char name[MAX_STRING_LENGTH]; //Name of file or dir
char top_level[MAX_STRING_LENGTH]; //Name of dir one level up
int data_block_index[MAX_FILE_DATA_BLOCKS];
int data_block_count;
int size;
struct file_type *next;
} file_type;
typedef struct {
bool free[BLOCKS];
bool directory[BLOCKS];
char (*name)[MAX_STRING_LENGTH];
} descriptor_block;
char *disk;
working_directory current;
bool disk_allocated = false; // makes sure that root is first thing being called and only called once
/*--------------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
(void)argc;
(void)*argv;
char in[LINESIZE];
char *cmd, *fnm, *fsz;
char dummy[] = "";
//printf("sizeof file_type = %d\nsizeof dir_type = %d\nsize of descriptor_block = %d\nMAX_FILE_SIZE %d\n", sizeof(file_type), sizeof(dir_type), sizeof(descriptor_block), MAX_FILE_DATA_BLOCKS );
printf("Welcome to your file system\n");
int n;
char *a[LINESIZE];
while (fgets(in, LINESIZE, stdin) != NULL)
{
// commands are all of form "cmd filename filesize\n" with whitespace between
// parse in
parse(in, &n, a);
cmd = (n > 0) ? a[0] : dummy;
fnm = (n > 1) ? a[1] : dummy;
fsz = (n > 2) ? a[2] : dummy;
if (debug) printf(":%s:%s:%s:\n", cmd, fnm, fsz);
if (n == 0) continue; // blank line
int found = 0;
for (struct action *ptr = table; ptr->cmd != NULL; ptr++)
{
if (strcmp(ptr->cmd, cmd) == 0)
{
found = 1;
int ret = (ptr->action)(fnm, fsz);
if (ret == -1)
{ printf(" %s %s %s: failed\n", cmd, fnm, fsz); }
break;
}
}
if (!found) { printf("command not found: %s\n", cmd);}
}
return 0;
}
/*--------------------------------------------------------------------------------*/
//This function initializes the disk, descriptor within the disk, as
//well as the root directory.
int do_root(char *name, char *size)
{
(void)*name;
(void)*size;
//Don't want to call the root more than once
if ( disk_allocated == true )
return 0;
//Initialize disk
disk = (char*)malloc ( DISK_PARTITION );
if ( debug ) printf("\t[%s]::Allocating [%d] Bytes of memory to the disk\n", __func__, DISK_PARTITION );
//Add descriptor and root directory to disk
add_descriptor("descriptor");
if ( debug ) printf("\t[%s]::Creating Descriptor Block\n", __func__ );
add_directory( "root");
if ( debug ) printf("\t[%s]::Creating Root Directory\n", __func__ );
//Set up the working_directory structure
strcpy(current.directory, "root");
current.directory_index = 3;
strcpy(current.parent, "" );
current.parent_index = -1;
if ( debug ) printf("\t[%s]::Set Current Directory to [%s], with Parent Directory [%s]\n", __func__, "root", "" );
//Set global variable to indicate disk has been allocated
if ( debug ) printf("\t[%s]::Disk Successfully Allocated\n", __func__ );
disk_allocated = true;
return 0;
}
/*--------------------------------------------------------------------------------*/
int do_print(char *name, char *size)
{
(void)*name;
(void)*size;
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
//Start with the root directory, which is directory type (true)
printing("root");
if (debug) if ( debug ) printf("\n\t[%s]::Finished printing\n", __func__);
return 0;
}
/*--------------------------------------------------------------------------------*/
int do_chdir(char *name, char *size)
{
(void)*size;
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
//Case ".." is the argument to "chdir"
if ( strcmp(name, ".." ) == 0 ) {
//If we are in root directory then we can't go back
//so return and do nothing
if ( strcmp(current.directory, "root") == 0 )
return 0;
//Adjust the working_directory struct, which is doing
//the actual chdir action
strcpy ( current.directory, current.parent );
strcpy (current.parent, get_directory_top_level( current.parent) );
if ( debug ) printf ("\t[%s]::Current Directory is now [%s], Parent Directory is [%s]\n", __func__, current.directory, current.parent);
return 0;
}
else
{
char tmp[20];
//Check to make sure it is a subdirectory that is to be changed
//If name is not in the current directory then returns -1, else return 0
if ( (strcmp(get_directory_subitem(current.directory, -1, name), "-1") == 0) && strcmp( current.parent, name ) != 0 ) {
if ( debug ) printf( "\t\t\t[%s]::Cannot Change to Directory [%s]\n", __func__, name );
if (!debug ) printf( "%s: %s: No such file or directory\n", "chdir", name );
return 0;
}
//These actions help ensure that the folder does actually exist,
//and returns with no actions done if it does not
strcpy( tmp, get_directory_name(name));
if ( strcmp(tmp, "") == 0 )
return 0;
//If they match then our disk directory has accurate information
if ( strcmp( tmp, name ) == 0 ) {
//Adjust the working_directory struct, which is doing
//the actual chdir action
strcpy ( current.directory, tmp);
strcpy(current.parent, get_directory_top_level(name) );
if ( debug ) printf ("\t[%s]::Current Directory is now [%s], Parent Directory is [%s]\n", __func__, current.directory, current.parent);
return 1;
}
return -1;
}
return 0;
}
/*--------------------------------------------------------------------------------*/
int do_mkdir(char *name, char *size)
{
(void)*size;
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
//If it returns 0, there is a subitem with that name already
if ( get_directory_subitem(current.directory, -1, name) == 0 ) {
if ( debug ) printf( "\t\t\t[%s]::Cannot Make Directory [%s]\n", __func__, name );
if (!debug ) printf( "%s: cannot create directory '%s': Folder exists\n", "mkdir", name );
return 0;
}
//Call add directory
if ( debug ) printf("\t[%s]::Creating Directory: [%s]\n", __func__, name );
if ( add_directory( name ) != 0 ) {
if (!debug ) printf("%s: missing operand\n", "mkdir");
return 0;
}
//Edit the current directory to add our new directory
//to the current directory's "subdirectory" member. NULL
//at the end tells the function that we are just editing the
//subdirectory member and nothing else. See "edit_directory"
//to understand
edit_directory( current.directory, name, NULL, false, true );
if ( debug ) printf("\t[%s]::Updating Parents Subitem content\n", __func__ );
if ( debug ) printf("\t[%s]::Directory Created Successfully\n", __func__ );
if( debug ) print_directory(name);
return 0;
}
/*--------------------------------------------------------------------------------*/
int do_rmdir(char *name, char *size)
{
(void)*size;
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
//Do some checking before starting to remove
if ( strcmp(name,"") == 0 ) {
if ( debug ) printf("\t[%s]::Invalid Command\n", __func__ );
if (!debug ) printf("%s: missing operand\n", "rmdir");
return 0;
}
//Does not take commands "rmdir ." or "rmdir .."
if( (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0) ) {
if ( debug ) printf("\t[%s]::Invalid command [%s] Will not remove directory\n", __func__, name);
if (!debug ) printf( "%s: %s: No such file or directory\n", "rmdir", name );
return 0;
}
//Check to make sure it is a subdirectory that is to be changed
//If name is not in the current directory then returns -1, else return 0
if ( strcmp(get_directory_subitem(current.directory, -1, name), "-1") == 0 ) {
if ( debug ) printf( "\t[%s]::Cannot Remove Directory [%s]\n", __func__, name );
if (!debug ) printf( "%s: %s: No such file or directory\n", "rmdir", name );
return 0;
}
//Remove directory from the parent's subitems.
dir_type *folder = malloc ( BLOCK_SIZE );
int block_index = find_block(name, true);
memcpy( folder, disk + block_index*BLOCK_SIZE, BLOCK_SIZE );
dir_type *top_folder = malloc ( BLOCK_SIZE );
//The top_level is created based off the folder
int top_block_index = find_block(folder->top_level, true);
memcpy( disk + block_index*BLOCK_SIZE, folder, BLOCK_SIZE );
memcpy( top_folder, disk + top_block_index*BLOCK_SIZE, BLOCK_SIZE );
char subitem_name[MAX_STRING_LENGTH]; // holds the current subitem in the parent directory array
const int subcnt = top_folder->subitem_count; // # of subitems
int j;
int k=0;
//iterate through the subitem count
for(j = 0; j<subcnt; j++) {
strcpy(subitem_name, top_folder->subitem[j]);
if (strcmp(subitem_name, name) != 0) // if this element is not the one we are removing, copy it back
{
strcpy(top_folder->subitem[k],subitem_name);
//printf("------ Subitem [%s] copied ------\n", subitem_name);
k++;
}
}
//Remove the directory subitem from the parent
strcpy(top_folder->subitem[k], "");
top_folder->subitem_count--;
memcpy( disk + top_block_index*BLOCK_SIZE, top_folder, BLOCK_SIZE );
free(top_folder);
//Remove the directory with its contents
if ( debug ) printf("\t[%s]::Removing Directory: [%s]\n", __func__, name );
if( remove_directory( name ) == -1 ) {
return 0;
}
if (debug) printf("\t[%s]::Directory Removed Successfully\n", __func__);
return 0;
}
/*--------------------------------------------------------------------------------*/
int do_mvdir(char *name, char *size) //"size" is actually the new name
{
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
//Rename the directory
if ( debug ) printf("\t[%s]::Renaming Directory: [%s]\n", __func__, name );
//Get the directory based off the "name" and change to the new name "size"
//if the directory "name" is not found, rename_directory returns a -1
if( edit_directory( name, "", size, true, true ) == -1 ) {
if (!debug ) printf( "%s: cannot rename file or directory '%s'\n", "mvdir", name );
return 0;
}
//else the directory is renamed
if (debug) printf( "\t[%s]::Directory Renamed Successfully: [%s]\n", __func__, size );
if (debug) print_directory(size);
return 0;
}
/*--------------------------------------------------------------------------------*/
int do_mkfil(char *name, char *size)
{
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
//Add file to disk, and convert size argument to
//an integer value
if ( debug ) printf("\t[%s]::Creating File: [%s], with Size: [%s]\n", __func__, name, size );
//If it returns 0, there is a subitem with that name already
if ( get_directory_subitem(current.directory, -1, name) == 0 ) {
if ( debug ) printf( "\t\t\t[%s]::Cannot make file [%s], a file or directory [%s] already exists\n", __func__, name, name );
if (!debug ) printf( "%s: cannot create file '%s': File exists\n", "mkfil", name );
return 0;
}
if ( add_file ( name, atoi(size)) != 0 )
return 0;
//Edit the current directory to add our new file
//to the current directory's "subdirectory" member. NULL
//at the end tells the function that we are just editing the
//subdirectory member and nothing else. See "edit_directory"
//to understand
edit_directory( current.directory, name, NULL, false, false);
if ( debug ) printf("\t[%s]::Updating Parents Subitem content\n", __func__ );
if ( debug ) print_file(name);
return 0;
}
/*--------------------------------------------------------------------------------*/
// Remove a file
int do_rmfil(char *name, char *size)
{
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
(void)*size;
if ( debug ) printf("\t[%s]::Removing File: [%s]\n", __func__, name);
//If the file to be removed actually exists in current directory, remove it
if ( get_directory_subitem(current.directory, -1, name) == 0 ) {
remove_file(name);
return 0;
}
else{ // If it doesn't exist, print error and return
if ( debug ) printf( "\t\t\t[%s]::Cannot remove file [%s], it does not exist in this directory\n", __func__, name );
if (!debug ) printf( "%s: %s: No such file or directory\n", "rmfil", name );
return 0;
}
}
/*--------------------------------------------------------------------------------*/
// Rename a file
int do_mvfil(char *name, char *size)
{
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
if ( debug ) printf("\t[%s]::Renaming File: [%s], to: [%s]\n", __func__, name, size );
//If it returns 0, there is a subitem with that name already
if ( get_directory_subitem(current.directory, -1, size) == 0 ) {
if ( debug ) printf( "\t\t\t[%s]::Cannot rename file [%s], a file or directory [%s] already exists\n", __func__, name, size );
if (!debug ) printf( "%s: cannot rename file or directory '%s'\n", "mvfil", name );
return 0;
}
int er = edit_file( name, 0, size);
if (er == -1) return -1;
if (debug) print_file(size);
return 0;
}
/*--------------------------------------------------------------------------------*/
// Resize a file
int do_szfil(char *name, char *size)
{
//The root must be called first
if ( disk_allocated == false ) {
printf("Error: Disk not allocated\n");
return 0;
}
if ( debug ) printf("\t[%s]::Resizing File: [%s], to: [%s]\n", __func__, name, size );
// To resize the file, first we will remove it, then we will make a
// new file with the updated size
if (remove_file(name) != -1) do_mkfil(name, size);
else {
if ( debug ) printf("\t[%s]::File: [%s] does not exist. Cannot resize.\n", __func__, name);
if (!debug ) printf( "%s: cannot resize '%s': No such file or directory\n", "szfil", name );
}
return 0;
}
/*--------------------------------------------------------------------------------*/
int do_exit(char *name, char *size)
{
(void)*name;
(void)*size;
if (debug) printf("\t[%s]::Exiting\n", __func__);
exit(0);
return 0;
}
/*--------------------------------------------------------------------------------*/
//Prints the information of directories and files starting at the root
//Works similar to the ls -R function. Uses recursion
void printing(char *name) {
//Allocate memory to a dir_type so that we can copy
//the folder from memory into this variable.
dir_type *folder = malloc (BLOCK_SIZE);
int block_index = find_block(name, true);
memcpy( folder, disk + block_index*BLOCK_SIZE, BLOCK_SIZE);
printf("%s:\n", folder->name);
for( int i = 0; i < folder->subitem_count; i++ ) {
printf("\t%s\n", folder->subitem[i]);
}
//Go through again if there is a subdirectory
for( int i = 0; i < folder->subitem_count; i++ ) {
if( folder->subitem_type[i] == true ) {
//Recursively call the function
printing(folder->subitem[i]);
}
}
}
/*--------------------------------------------------------------------------------*/
//Displays the content of the descriptor block and free block table.
//Prints a list of it for debugging purposes when needed.
void print_descriptor ( ) {
//Allocate memory to a descriptor_block type so that we
//can read the descriptor on the disk
descriptor_block *descriptor = malloc( BLOCK_SIZE*2 );
//Copy descriptor on disk to our descriptor_block type, making
//our variable readable and writeable
memcpy ( descriptor, disk, BLOCK_SIZE*2 );
printf("Disk Descriptor Free Table:\n");
for ( int i = 0; i < BLOCKS ; i++ ) {
printf("\tIndex %d : %d\n", i, descriptor->free[i]);
}
free(descriptor);
}
/*--------------------------------------------------------------------------------*/
//Function searches through the free member of our descriptor and finds
//the first free block on the disk. Once a free block is found, the free member
//is updated to reflect that the block is no longer free. The name of the item in the
//block is also updated. The free block index is returned
int allocate_block ( char *name, bool directory ) {
//Allocate memory to a descriptor_block type so that we
//can read the descriptor on the disk
descriptor_block *descriptor = malloc( BLOCK_SIZE*2);
//Copy descriptor on disk to our descriptor_block type, making
//our variable readable and writeable
memcpy ( descriptor, disk, BLOCK_SIZE*2 );
//Goes through every block until free one is found
if ( debug ) printf("\t\t\t[%s]::Finding Free Memory Block in the Descriptor\n", __func__ );
for ( int i = 0; i < BLOCKS; i++ ) {
if ( descriptor->free[i] ) {
//Once free block is found, update descriptor information
descriptor->free[i] = false;
descriptor->directory[i] = directory;
strcpy(descriptor->name[i], name);
//Before we return, we need to make sure that we write the new
//updated descriptor back to the beginning of the disk
memcpy(disk, descriptor, BLOCK_SIZE*2);
if ( debug ) printf("\t\t\t[%s]::Allocated [%s] at Memory Block [%d]\n", __func__, name, i );
free(descriptor);
return i;
}
}
free(descriptor);
if ( debug ) printf("\t\t\t[%s]::No Free Space Found: Returning -1\n", __func__);
return -1;
}
/*--------------------------------------------------------------------------------*/
//This function updates the descriptor block on disk to reflect that
//the block holding "name" is no longer in use. This will make it show that
//it is free when "allocate_block" runs
void unallocate_block ( int offset ) {
//Allocate memory to a descriptor_block type so that we
//can read the descriptor on the disk
descriptor_block *descriptor = malloc( BLOCK_SIZE*2 );
//Copy descriptor on disk to our descriptor_block type, making
//our variable readable and writeable
memcpy ( descriptor, disk, BLOCK_SIZE*2 );
// DON'T NEED.Find the block index where the "name" item is held
//int offset = find_block ( name );
//Make descriptor reflect that the block is free.
//NOTE:further development should check if the block
// holds a file, and then unallocate all its sub-block
if ( debug ) printf("\t\t\t[%s]::Unallocating Memory Block [%d]\n", __func__, offset );
descriptor->free[offset] = true;
strcpy( descriptor->name[offset], "" );
//Before we return, we need to make sure that we write the new
//updated descriptor back to the beginning of the disk
memcpy ( disk, descriptor, BLOCK_SIZE*2 );
free(descriptor);
}
/*--------------------------------------------------------------------------------*/
//Takes in a name, and searches through descriptor block to find the
//block that contains the item with "name"
int find_block ( char *name, bool directory ) {
//Allocate memory to a descriptor_block type so that we
//can read the descriptor on the disk
descriptor_block *descriptor = malloc( BLOCK_SIZE*2 );
//Copy descriptor on disk to our descriptor_block type, making
//our variable readable and writable
memcpy ( descriptor, disk, BLOCK_SIZE*2 );
//Run through every block in the descriptor table to see
//if the item exists in memory
if ( debug ) printf("\t\t\t[%s]::Searching Descriptor for [%s], which is a [%s]\n", __func__, name, directory == true ? "Folder": "File" );
for ( int i = 0; i < BLOCKS; i++ ) {
if ( strcmp(descriptor->name[i], name) ==0 ){
//Make sure it is of the type that we are searching for
if ( descriptor->directory[i] == directory ) {
if ( debug ) printf("\t\t\t[%s]::Found [%s] at Memory Block [%d]\n", __func__, name, i );
free(descriptor);
//Return the block index where the item resides in memory
return i;
}
}
}
free(descriptor);
if ( debug ) printf("\t\t\t[%s]::Block Not Found: Returning -1\n", __func__);
return -1;
}
/*--------------------------------------------------------------------------------*/
int add_descriptor ( char * name ) {
//Allocate memory to a descriptor_block type so that we start
//assigning values to its members. BLOCK_SIZE*2 is the hard-coded
//size of the descriptor, and should most likely be changed in final version
descriptor_block *descriptor = malloc( BLOCK_SIZE*2);
if ( debug ) printf("\t\t[%s]::Allocating Space for Descriptor Block\n", __func__);
//Allocate memory to the array of strings within the descriptor block,
//which holds the name of each block
descriptor->name = malloc ( sizeof*name*BLOCKS );
if ( debug ) printf("\t\t[%s]::Allocating Space for Descriptor's Name Member\n", __func__);
//initialize each block to indicate in the descriptor, that it is free
if ( debug ) printf("\t\t[%s]::Initializing Descriptor to Have All of Memory Available\n", __func__);
for (int i = 0; i < BLOCKS; i++ ) {
descriptor->free[i] = true;
descriptor->directory[i] = false;
}
//Find the amount of space, in blocks, that the descriptor is going to take up
//on the disk
int limit = (int)(sizeof(descriptor_block)/BLOCK_SIZE) + 1;
//Now update the descriptor to show that these blocks are now in use
if ( debug ) printf("\t\t[%s]::Updating Descriptor to Show that first [%d] Memory Blocks Are Taken\n", __func__, limit+1);
for ( int i = 0; i < limit; i ++ ) {
descriptor->free[i]= false;
}
//Update descriptor to show that the first block in the disk
//is occupied by the descriptor
strcpy(descriptor->name[0], "descriptor");
//Before we return, we need to make sure that we write the new
//updated descriptor to the beginning of the disk
memcpy ( disk, descriptor, (BLOCK_SIZE*(limit+1)));
return 0;
}
/*--------------------------------------------------------------------------------*/
//Allows us to directly update values in the descriptor block. Won't be used in
//normal cases, might be useful if an error is detected in order to correct the issue.
//Free array will be updated at index "free_index", to the value of the bool "free". Same
//goes for the name array, and name_index values
int edit_descriptor ( int free_index, bool free, int name_index, char * name ) {
//Allocate memory to a descriptor_block type so that we
//can read the descriptor on the disk
descriptor_block *descriptor = malloc( BLOCK_SIZE*2 );
//Copy descriptor on disk to our descriptor_block type, making
//our variable readable and writeable
memcpy ( descriptor, disk, BLOCK_SIZE*2 );
//Each array in the descriptor block will be updated, as long as
//the index provided for each is valid. If you wish to only change on
//value using the function, set the other index to -1
if ( free_index > 0 ) {
descriptor->free[free_index] = free;
if ( debug ) printf("\t\t[%s]::Descriptor Free Member now shows Memory Block [%d] is [%s]\n", __func__, free_index, free == true ? "Free": "Used");
}
if ( name_index > 0 ) {
strcpy(descriptor->name[name_index], name );
if ( debug ) printf("\t\t[%s]::Descriptor Name Member now shows Memory Block [%d] has Name [%s]\n", __func__, name_index, name);
}
//Before we return, we need to make sure that we write the new
//updated descriptor back to the beginning of the disk
memcpy(disk, descriptor, BLOCK_SIZE*2);
//Getting compile error when the following line
//is uncommented. Weird. ?
//free(descriptor);
return 0;
}
/*--------------------------------------------------------------------------------*/
// This changes the name of a file in the descriptor; used for the files
int edit_descriptor_name (int index, char* new_name)
{
descriptor_block *descriptor = malloc( BLOCK_SIZE*2 );
//Copy descriptor on disk to our descriptor_block type, making
//our variable readable and writeable
memcpy ( descriptor, disk, BLOCK_SIZE*2 );
// Change the name of the file at "index" to the new_name
// Right now this is being used for mvfil
strcpy(descriptor->name[index], new_name);
memcpy(disk, descriptor, BLOCK_SIZE*2);
free(descriptor);
return 0;
}
/*--------------------------------------------------------------------------------*/
//Allows us to add a folder called "name" to the disk.
int add_directory( char * name ) {
if ( strcmp(name,"") == 0 ) {
if ( debug ) printf("\t\t[%s]::Invalid Command\n", __func__ );
return -1;
}
//Allocate memory to a dir_type so that we start
//assigning values to the folder's members. BLOCK_SIZE is hard-coded and
//will need to be changed to accommodate different situations
dir_type *folder = malloc ( BLOCK_SIZE);
if ( debug ) printf("\t\t[%s]::Allocating Space for New Folder\n", __func__);
//Initialize all the members of our new folder
strcpy(folder->name, name); //Copy name to folder
strcpy(folder->top_level, current.directory); //Copy current directory to parent member of folder
folder->subitem = malloc ( sizeof*(folder->subitem)*MAX_SUBDIRECTORIES); //Allocate memory to the subitem array holding all elements beneath it
if ( debug ) printf("\t\t[%s]::Allocating Space for Descriptor's Name Member\n", __func__);
folder->subitem_count = 0; //Initialize subitem array to have 0 elements
//Find free block in disk to store our folder in. The true argument
//merely tells the function to mark this used block as a directory
int index = allocate_block(name, true);
if ( debug ) printf("\t\t[%s]::Assigning New Folder to Memory Block [%d]\n", __func__, index);
//Copy our folder to the disk at the free block that we have just found
memcpy( disk + index*BLOCK_SIZE, folder, BLOCK_SIZE);
if ( debug ) printf("\t\t[%s]::Folder [%s] Successfully Added\n", __func__, name);
free(folder);
return 0;
}
/*--------------------------------------------------------------------------------*/
//Allows to remove a directory folder of name "name" from the disk.
int remove_directory( char * name ) {
//Allocate memory to a dir_type so that we can copy
//the folder from memory into this variable.
dir_type *folder = malloc (BLOCK_SIZE);
int block_index = find_block(name, true);
//If there was no subdirectory found, then return
if( block_index == -1 ) {
if ( debug ) printf("\t\t[%s]::Directory [%s] does not exist in the current folder [%s]\n", __func__, name, current.directory);
return -1;
}
memcpy( folder, disk + block_index*BLOCK_SIZE, BLOCK_SIZE );
//Go through again if there is a subdirectory
for( int i = 0; i < folder->subitem_count; i++ ) {
if( folder->subitem_type[i] == true ) {
//Recursively call the function to remove the subitem
remove_directory(folder->subitem[i]);
}
else {
//Remove the subitem that is a file
remove_file(folder->subitem[i]);
}
}
//Deallocate memory of the removed directory
unallocate_block(block_index);
free(folder);
return 0;
}
/*--------------------------------------------------------------------------------*/
//Allows you to directly add items to a folders subitem array, or change the folder's name
int edit_directory ( char * name, char*subitem_name, char *new_name, bool name_change, bool directory ) {
if( strcmp(name,"") == 0 ) {
if( debug ) printf("\t\t[%s]::Invalid Command\n", __func__ );
return -1;
}
//Allocate memory to a dir_type so that we can copy
//the folder from memory into this variable.
dir_type *folder = malloc ( BLOCK_SIZE);
//Find where the folder is on disk
int block_index = find_block(name, true);
//If the directory is not found, should return
if( block_index == -1 ) {
if ( debug ) printf("\t\t[%s]::Directory [%s] does not exist\n", __func__, name);
return -1;
}
if ( debug ) printf("\t\t[%s]::Folder [%s] Found At Memory Block [%d]\n", __func__, name, block_index);
//Copy the folder from disk into our variable. This allows us to read
//and write to the folder
memcpy( folder, disk + block_index*BLOCK_SIZE, BLOCK_SIZE);
//If "subitem_name" isn't empty, then we add the item to the folder's
//subdirectory array. Otherwise we are changing the folder's name
if ( strcmp(subitem_name, "") != 0 ) { //Case that we are adding subitem to the descriptor block
if ( !name_change ) { //Case adding subitem
if ( debug ) printf("\t\t[%s]::Added Subitem [%s] at Subitem index [%d] to directory [%s]\n", __func__, subitem_name, folder->subitem_count, folder->name );
strcpy (folder->subitem[folder->subitem_count], subitem_name );
folder->subitem_type[folder->subitem_count] = directory;
folder->subitem_count++;
if ( debug ) printf("\t\t[%s]::Folder [%s] Now Has [%d] Subitems\n", __func__, name, folder->subitem_count);
//Write updated version of the folder back to its current location on disk
memcpy( disk + block_index*BLOCK_SIZE, folder, BLOCK_SIZE);
free(folder);
return 0;
}
else { //Case editing a subitem's name
//iterate through the subitems to see which to edit
for ( int i =0; i < folder->subitem_count; i++ ) {
if ( strcmp(folder->subitem[i], subitem_name) == 0 ) {
strcpy( folder->subitem[i], new_name);
if ( debug ) printf("\t\t[%s]::Edited Subitem [%s] to [%s] at Subitem index [%d] for directory [%s]\n", __func__, subitem_name, new_name, i, folder->name );
//Write updated version of the folder back to its current location on disk
memcpy( disk + block_index*BLOCK_SIZE, folder, BLOCK_SIZE);
free(folder);
return 0;
}
}
if ( debug ) printf("\t\t[%s]::Subitem Does Not Exist in Directory [%s]\n", __func__, folder->name );
free(folder);
return -1;
}
}
else { //Case we are changing the folders name
//Make sure the new_name directory does not already exist
int block_index2 = find_block(new_name, true);
//If the directory for the new name already exists, should return
if( block_index2 != -1 ) {
if ( debug ) printf("\t\t[%s]::Directory [%s] already exists. Choose a different name\n", __func__, new_name);
return -1;
}
strcpy(folder->name, new_name );
if ( debug ) printf("\t\t[%s]::Folder [%s] Now Has Name [%s]\n", __func__, name, folder->name);