-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.c
More file actions
2137 lines (1724 loc) · 58.9 KB
/
functions.c
File metadata and controls
2137 lines (1724 loc) · 58.9 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "functions.h"
#include "functions2.h"
#include "functions_util.h"
#include "util.h"
int cfs_create(char* cfs_filename, size_t bs, size_t fns, size_t cfs, uint mdfn)
{
/* cfs directories have to able to at least contain the ./ and ../
directories, therefore the data block size has to at least as big as the
space that is needed to store the information for these 2 directories error oc*/
if (bs - sizeof(Block) < 2 * (fns + sizeof(off_t)))
{
printf("block size if too small: %lu, or filename_size is too big: %lu\nA directory data block can't contain the basic information for the ./ and ../ directories.\n", bs, fns);
return 0;
}
/* create the new cfs file */
int fd = open(cfs_filename, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
/* check is an error occured */
if (fd < 0)
{
/* ERROR: the file already existed */
if (errno == EEXIST)
{
printf("File already exists, give another name for the cfs file.\n");
return 0;
}
/* some other error occured */
else
{
perror("Error occured");
return -1;
}
}
/* calculate the size of every struct */
/* the size of the superblock is the size of its struct */
size_t superblock_size = sizeof(superblock);
/* the size of the hole map is the the size of its struct */
size_t hole_map_size = sizeof(hole_map);
/* the size of the /root is determined by the size of the structs MDS */
size_t root_header_size = sizeof(MDS);
/* the size of the data block used by the root directory */
size_t block_size = bs;
/* construct the superblock */
/* create the struct */
superblock* my_superblock = NULL;
MALLOC_OR_DIE(my_superblock, superblock_size, fd);
/* initialize its values */
initialize_superblock(my_superblock, cfs_filename, fd, superblock_size + hole_map_size, superblock_size + hole_map_size + root_header_size + bs, bs, fns, cfs, mdfn);
/* write to the cfs file */
WRITE_OR_DIE(fd, my_superblock, superblock_size);
/* construct the hole map */
/* create the struct */
hole_map* holes = NULL;
MALLOC_OR_DIE(holes, hole_map_size, fd);
/* initialize its values */
initialize_holes(holes, MAX_HOLES, 1, superblock_size + hole_map_size + root_header_size + bs);
/* write to the cfs file */
WRITE_OR_DIE(fd, holes, hole_map_size);
/* construct the header for the /root directory:
the only directory that will have "infinite" space */
/* create the struct */
MDS* root_header = NULL;
MALLOC_OR_DIE(root_header, root_header_size, fd);
/* initialize its values */
initialize_MDS(root_header, 0, DIRECTORY, 1, 1, 2 * (fns + sizeof(off_t)), superblock_size + hole_map_size, superblock_size + hole_map_size + root_header_size);
/* write to the cfs file */
WRITE_OR_DIE(fd, root_header, root_header_size);
/* construct the data block of the /root directory */
/* create the struct */
Block* root_data = NULL;
MALLOC_OR_DIE(root_data, block_size, fd);
/* initialize its values */
initialize_Directory_Data_Block(root_data, block_size, fns, superblock_size + hole_map_size, superblock_size + hole_map_size);
/* write to the cfs file */
WRITE_OR_DIE(fd, root_data, block_size);
/* free the allocated memory */
free(root_data);
free(root_header);
free(my_superblock);
free(holes);
/* close the cfs file because we are not working with it */
CLOSE_OR_DIE(fd);
return 1;
}
int cfs_workwith(char* cfs_filename, superblock** my_superblock, hole_map** holes, Stack_List** list)
{
/* open file just for reading to see if it exists */
int fd = open(cfs_filename, O_RDONLY, READ_WRITE_USER_GROUP_PERMS);
/* check for errors */
if (fd == -1)
{
if (errno == ENOENT)
{
printf("Error input: cfs file \"%s\" does not exist.\n", cfs_filename);
return 0;
}
else
{
perror("open() error in cfs_workwith()");
return -1;
}
}
/* close the file opened only for reading */
CLOSE_OR_DIE(fd);
/* now open it for both reading and writing, to work with it */
fd = open(cfs_filename, O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
/* check for errors */
if (fd == -1)
{
perror("open() error in correct opening of cfs_workwith()");
return -1;
}
/* get the superblock so that we don't have to read it from the file every
time we use a cfs_function */
*my_superblock = get_superblock(fd);
/* check for errors */
if (*my_superblock == NULL)
{
perror("get_superblock() returned NULL in cfs_workwith()");
CLOSE_OR_DIE(fd);
return -1;
}
/* same as above, just for the holes map */
*holes = get_hole_map(fd);
/* check for errors */
if (*holes == NULL)
{
perror("get_hole_map() returned NULL in cfs_workwith()");
free(my_superblock);
CLOSE_OR_DIE(fd);
return -1;
}
/* create the list that will be used to manage the paths */
*list = create_List();
/* check if the creation of the list fails */
if (*list == NULL)
{
printf("Unexpected error in create_List().\n");
free(my_superblock);
free(holes);
CLOSE_OR_DIE(fd);
return -1;
}
/* name of the root directory */
char* root_name = malloc(5 * sizeof(char));
if (root_name == NULL)
{
printf("Unexpected malloc() error in create_List().\n");
free(my_superblock);
free(holes);
Stack_List_Destroy(list);
CLOSE_OR_DIE(fd);
return -1;
}
strcpy(root_name, "root");
/* calculate the position of the root MDS */
off_t root_position = sizeof(superblock) + sizeof(hole_map);
/* push it into the stack list, while also checking for any failure */
int ret = Stack_List_Push(*list, root_name, root_position);
/* check for errors */
if (ret == -1)
{
/* pushing failed for some reason, so free() the allocated memory and exit */
Stack_List_Destroy(list);
perror("Unexpected error");
CLOSE_OR_DIE(fd);
return -1;
}
/* return the appropriate file descriptor of the cfs to be worked with after
everything succeded */
return fd;
}
int cfs_mkdir(int fd, superblock* my_superblock, hole_map* holes, MDS* current_directory, off_t parent_offset, char* insert_name)
{
/* get some important parameters */
size_t block_size = my_superblock->block_size;
size_t fns = my_superblock->filename_size;
uint total_entities = my_superblock->total_entities;
/* calculate the size of the struct MDS */
size_t size_of_mds = sizeof(MDS);
/* find where to place the MDS and its first data block */
off_t mds_position = find_hole(holes, size_of_mds);
off_t block_position = find_hole(holes, block_size);
/* if there are no more holes */
if (mds_position == 0 || block_position == 0)
{
printf("No more holes are available. Make the hole map bigger in the next cfs file you make\n");
return 0;
}
/* create the struct */
MDS* new_mds = NULL;
MALLOC_OR_DIE_3(new_mds, sizeof(MDS));
/* initialize its values */
initialize_MDS(new_mds, total_entities, DIRECTORY, 1, 1, 2 * (fns + sizeof(off_t)), parent_offset, block_position);
/* write to the cfs file */
int retval = set_MDS(new_mds, fd, mds_position);
if (retval == 0)
{
perror("Error occured in set_MDS() when called from cfs_mkdir()");
free(new_mds);
return 0;
}
/* free the new MDS because we don't need it anymore */
free(new_mds);
/* initialize data block */
Block* data_block = NULL;
MALLOC_OR_DIE_3(data_block, block_size);
/* initialize the new directory data block */
initialize_Directory_Data_Block(data_block, block_size, fns, mds_position, parent_offset);
/* write the block in the cfs file */
retval = set_Block(data_block, fd, block_size, block_position);
if (retval == 0)
{
perror("Error occured in set_Block() when called from cfs_mkdir()");
free(data_block);
return 0;
}
/* free the data block because we don't need it anymore */
free(data_block);
/* insert the pair */
retval = insert_pair(fd, holes, current_directory, insert_name, mds_position, block_size, fns);
if (retval == 0)
{
printf("insert_pair() error when called from cfs_mkdir()\n");
return 0;
}
/* if retval == 1, it means that we allocated a new block to place the pair <name, offset> */
else if (retval == 1)
{
current_directory->blocks_using++;
my_superblock->current_size += block_size;
}
/* calculate the attributes that will be added to the current directory */
size_t size_of_pair = fns + sizeof(off_t);
current_directory->size += size_of_pair;
/* update the current_directory */
retval = set_MDS(current_directory, fd, parent_offset);
if (retval == 0)
{
perror("Error occured in set_MDS() when called from cfs_mkdir() before finishing the function");
return 0;
}
/* inform the superblock */
my_superblock->total_entities += 1;
/* the size of the new directory plus its first block */
my_superblock->current_size += sizeof(MDS) + block_size;
/* update the superblock */
retval = set_superblock(my_superblock, fd);
if (retval == 0)
{
perror("Error occured in set_superblock() when called from cfs_mkdir() before finishing the function");
return 0;
}
/* update the hole map */
retval = set_hole_map(holes, fd);
if (!retval)
{
return 0;
}
return 1;
}
int cfs_touch(int fd, superblock* my_superblock, hole_map* holes, MDS* current_directory, off_t parent_offset, char* insert_name, off_t file_offset, int flag_a, int flag_m)
{
/* first check if any flag is true, in order to perform its operation */
if ((flag_a || flag_m) && file_offset != (off_t) -1)
{
/* get the file */
MDS* target_file = get_MDS(fd, file_offset);
if (target_file == NULL)
{
printf("get_MDS() error in cfs_touch().\n");
return 0;
}
if (target_file->type != FILE)
{
printf("Error: entity with name %s is not a file.\n", insert_name);
free(target_file);
return -1;
}
time_t my_time = time(NULL);
if (flag_a)
{
target_file->access_time = my_time;
}
else
{
target_file->modification_time = my_time;
}
/* update the file that was modified */
int retval = set_MDS(target_file, fd, file_offset);
if (retval == 0)
{
perror("Error occured in set_MDS()");
FREE_IF_NOT_NULL(target_file);
return 0;
}
free(target_file);
return 1;
}
/* calculate some useful sizes and variables */
size_t block_size = my_superblock->block_size;
size_t fns = my_superblock->filename_size;
uint total_entities = my_superblock->total_entities;
/* get info about the new file */
size_t size_of_mds = sizeof(MDS);
off_t mds_position = find_hole(holes, size_of_mds);
/* if no hole is found */
if (mds_position == 0)
{
printf("No more holes are available. Make the hole map bigger in the next cfs file you make\n");
return 0;
}
/* create the struct */
MDS* new_mds = NULL;
MALLOC_OR_DIE_3(new_mds, sizeof(MDS));
/* initialize its values */
initialize_MDS(new_mds, total_entities, FILE, 1, 0, 0, parent_offset, 0);
/* write to the cfs file */
int retval = set_MDS(new_mds, fd, mds_position);
if (retval == 0)
{
perror("Error occured in set_MDS() when called from cfs_touch()");
free(new_mds);
return 0;
}
/* free up the used space */
free(new_mds);
/* insert the pair in the current directory */
retval = insert_pair(fd, holes, current_directory, insert_name, mds_position, block_size, fns);
if (retval == 0)
{
printf("insert_pair() error when called from cfs_touch()\n");
return 0;
}
/* if retval == 1, it means that we allocated a new block to place the pair <name, offset> */
else if (retval == 1)
{
current_directory->blocks_using++;
my_superblock->current_size += block_size;
}
/* calculate the attributes that will be added to the current directory */
size_t size_of_pair = fns + sizeof(off_t);
current_directory->size += size_of_pair;
/* update the current_directory */
retval = set_MDS(current_directory, fd, parent_offset);
if (retval == 0)
{
perror("Error occured in set_MDS() when called from cfs_touch() before finishing the function");
return 0;
}
/* inform the superblock */
my_superblock->total_entities += 1;
my_superblock->current_size += sizeof(MDS);
/* update the superblock */
retval = set_superblock(my_superblock, fd);
if (retval == 0)
{
perror("Error occured in set_superblock() when called from cfs_touch() before finishing the function");
return 0;
}
/* update the hole map */
retval = set_hole_map(holes, fd);
if (!retval)
{
return 0;
}
/* return 1 if everything goes smoothly */
return 1;
}
int cfs_pwd(int fd, superblock* my_superblock, Stack_List* list)
{
/* just call a function that prints the current working directory */
Stack_List_Print_Path(list);
return 1;
}
int cfs_cd(int fd, superblock* my_superblock, Stack_List* list, const char path[])
{
/* this should never print */
if (is_Empty(list))
{
printf("Bug: list is empty.\n");
return 0;
}
/* if the command "cfs_cd" is given or the path is absolute, just go to the
root directory */
if (path[0] == 0 || path_is_absolute(path))
{
while (!is_in_Root(list))
{
Stack_List_Pop(list);
}
return 1;
}
size_t fns = my_superblock->filename_size;
size_t block_size = my_superblock->block_size;
/* temp variable to store the name of the current directory */
char* temp_directory = NULL;
MALLOC_OR_DIE_3(temp_directory, fns);
int is_finished = 0;
int path_index = 0;
while (!is_finished)
{
/* while the character '/' is being read, ignore it */
while (path[path_index] == '/')
{
path_index++;
}
/* if the string ends, break */
if ((path[path_index] == 0) || (path[path_index] == '\n') || (path[path_index] == '\t'))
{
break;
}
int name_index = 0;
/* get the next directory */
while ((path[path_index] != 0) && (path[path_index] != '\n') && (path[path_index] != '\t') && (path[path_index] != '/'))
{
temp_directory[name_index] = path[path_index];
name_index++;
path_index++;
if (path_index == MAX_BUFFER_SIZE)
{
printf("Error: the path exceeds the max number of availabe characters (%d) for a path. This should have been checked in main.\n", MAX_BUFFER_SIZE);
free(temp_directory);
return 0;
}
if (name_index == fns)
{
printf("The name of a directory is too big. The maximum number of characters that a name of an entity can have, is fns: %ld.\n", fns);
free(temp_directory);
return 0;
}
}
if (!strcmp(temp_directory, ".."))
{
if (!is_in_Root(list))
{
Stack_List_Pop(list);
}
}
else if (!strcmp(temp_directory, "."))
{
/* do nothing */
}
else
{
/* get the info of the current directory that we are in */
char* current_directory_name = malloc(fns * sizeof(char));
if (current_directory_name == NULL)
{
perror("malloc() error");
free(temp_directory);
return 0;
}
off_t current_directory_offset = (off_t) 0;
/* get the location of the MDS of the directory */
Stack_List_Peek(list, ¤t_directory_name, ¤t_directory_offset);
/* free the name of the current directory because we don't need it anymore */
free(current_directory_name);
/* get the current directory */
MDS* current_directory = get_MDS(fd, current_directory_offset);
if (current_directory == NULL)
{
printf("get_MDS() error in cfs_cd().\n");
free(temp_directory);
return 0;
}
off_t directory_offset = directory_get_offset(fd, current_directory, block_size, fns, temp_directory);
if (directory_offset == (off_t) -1)
{
char wrong_directory[MAX_BUFFER_SIZE] = {0};
memcpy(wrong_directory, path, path_index + 1);
printf("Error: directory %s does not exist.\n", wrong_directory);
free(current_directory);
free(temp_directory);
return 0;
}
else if (directory_offset == (off_t) 0)
{
printf("Unexpected directory_offset() error.\n");
free(current_directory);
free(temp_directory);
return 0;
}
free(current_directory);
char* address_for_list = malloc((strlen(temp_directory) + 1) * sizeof(char));
if (address_for_list == NULL)
{
perror("malloc() error");
free(temp_directory);
return 0;
}
/* copy the name inside the address */
strcpy(address_for_list, temp_directory);
/* put the new pair inside the directory */
Stack_List_Push(list, address_for_list, directory_offset);
}
/* reset the memory for the temporary directory */
memset(temp_directory, 0, fns);
}
free(temp_directory);
return 1;
}
int cfs_cp(int fd, superblock* my_superblock, hole_map* holes, MDS* source, char* source_name, MDS* destination_directory, off_t destination_offset, int flag_R, int flag_i, int flag_r, char* source_path, char* destination_path)
{
/* get some important sizes */
size_t block_size = my_superblock->block_size;
size_t fns = my_superblock->filename_size;
size_t size_of_pair = fns + sizeof(off_t);
/* is the entity to be copied is a file */
if (source->type == FILE)
{
MDS* new_file_to_be_copied = NULL;
MALLOC_OR_DIE_3(new_file_to_be_copied, sizeof(MDS));
/* find where to place the new file */
off_t position_of_new_file = find_hole(holes, sizeof(MDS));
/* find where to place the first block */
off_t position_of_block = 0;
if (source->size > 0)
{
/* find where to place the first block */
position_of_block = find_hole(holes, block_size);
}
/* initialize the attribues of the copied file */
initialize_MDS(new_file_to_be_copied, my_superblock->total_entities, FILE, 1, source->blocks_using, source->size, destination_offset, position_of_block);
/* write the file header to the cfs */
int retval = set_MDS(new_file_to_be_copied, fd, position_of_new_file);
free(new_file_to_be_copied);
if (!retval)
{
return 0;
}
/* if there are data blocks to be copied */
if (source->size > 0)
{
off_t source_block_position = source->first_block;
off_t next_block_position = position_of_block;
/* copy all the data blocks one by one */
while (69 * 0 == 0 * 69)
{
/* get the source block */
Block* source_block = get_Block(fd, block_size, source_block_position);
DIE_IF_NULL(source_block);
/* get a block for the destination */
Block* destination_block = malloc(block_size);
if (destination_block == NULL)
{
free(source_block);
return 0;
}
/* copy the data */
memcpy(destination_block, source_block, block_size);
/* if this is not the last data block, find where to place the next */
if (source_block->next_block != 0)
{
/* where the next block will be placed */
next_block_position = find_hole(holes, block_size);
destination_block->next_block = next_block_position;
}
/* get the position of the next source block */
source_block_position = source_block->next_block;
/* free the current source block because we don't need it anymore */
free(source_block);
retval = set_Block(destination_block, fd, block_size, position_of_block);
free(destination_block);
if (!retval)
{
return 0;
}
/* break if all the blocks have been copied */
if (source_block_position == 0)
{
break;
}
/* keep track of the position of the next block so that we know where
to set it */
position_of_block = next_block_position;
}
}
/* insert pair <source_name, offset> to the destination directory */
retval = insert_pair(fd, holes, destination_directory, source_name, position_of_new_file, block_size, fns);
/* if insert_pair() fails */
if (retval == 0)
{
return 0;
}
else if (retval == 1)
{
/* if insert_pair() returns 1, it means that we allocated a new block to insert the pair */
my_superblock->current_size += block_size;
destination_directory->blocks_using++;
}
/* inform the size field */
destination_directory->size += size_of_pair;
/* inform the superblock */
my_superblock->total_entities++;
my_superblock->current_size += sizeof(MDS) + block_size * source->blocks_using;
}
/* if the entity to be copied is a directory */
else if (source->type == DIRECTORY)
{
/* copy the directory */
int retval = cfs_mkdir(fd, my_superblock, holes, destination_directory, destination_offset, source_name);
if (!retval)
{
printf("Error in cfs_mkdir() when called from cfs_cp().\n");
return 0;
}
/* get the position of the directory we just copied */
off_t copy_directory_offset = directory_get_offset(fd, destination_directory, block_size, fns, source_name);
/* check for errors */
if (copy_directory_offset == (off_t) 0)
{
return 0;
}
else if (copy_directory_offset == (off_t) -1)
{
printf("Error in cfs_cp(). This message should have never been printed because the insertion above worked, therefore the entity must exist. Congratulations, ELOUSES.\n");
return 0;
}
/* if we are to copy the directories recursively */
if (flag_r)
{
/* initialize useful variables */
size_t size_of_pair = fns + sizeof(off_t);
/* get the copy directory */
MDS* copy_directory = get_MDS(fd, copy_directory_offset);
if (copy_directory == NULL)
{
return 0;
}
/* get the position of the data blocks of the source directory */
off_t directory_data_block_position = source->first_block;
/* iterate through all the directory data blocks */
while (directory_data_block_position != (off_t) 0)
{
Block* directory_data_block = get_Block(fd, block_size, directory_data_block_position);
if (directory_data_block == NULL)
{
free(copy_directory);
return 0;
}
char* name = (char *) directory_data_block->data;
uint number_of_pairs = directory_data_block->bytes_used / size_of_pair;
int i = 0;
for (; i < number_of_pairs; i++)
{
/* skip the hidden directories */
if (!strcmp(name, ".") || !strcmp(name, ".."))
{
/* point to the next name */
name = pointer_to_next_name(name, fns);
/* go to the next entry */
continue;
}
/* fix the path for the source */
char temp_source_buffer[MAX_BUFFER_SIZE] = {0};
strcpy(temp_source_buffer, source_path);
if (temp_source_buffer[strlen(temp_source_buffer) - 1] != '/')
{
strcat(temp_source_buffer, "/");
}
strcat(temp_source_buffer, name);
/* fix the path for the destination */
char temp_destination_buffer[MAX_BUFFER_SIZE] = {0};
strcpy(temp_destination_buffer, destination_path);
if (temp_destination_buffer[strlen(temp_destination_buffer) - 1] != '/')
{
strcat(temp_destination_buffer, "/");
}
strcat(temp_destination_buffer, source_name);
/* ask for persmission if -i parameter has been given */
if (flag_i)
{
if (!get_approval(temp_source_buffer, temp_destination_buffer, "copy"))
{
/* point to the next name */
name = pointer_to_next_name(name, fns);
/* go to the next entry */
continue;
}
}
/* find the new source that will be copied */
off_t* entity_offset = pointer_to_offset(name, fns);
/* get the new source that will be copied */
MDS* new_source = get_MDS(fd, *entity_offset);
if (new_source == NULL)
{
free(directory_data_block);
free(copy_directory);
return 0;
}
/* copy the entity recursively */
retval = cfs_cp(fd, my_superblock, holes, new_source, name, copy_directory, copy_directory_offset, flag_R, flag_i, flag_r, temp_source_buffer, temp_destination_buffer);
if (!retval)
{
free(directory_data_block);
free(copy_directory);
free(new_source);
return 0;
}
/* free the allocated memory */
free(new_source);
/* point to the next name */
name = pointer_to_next_name(name, fns);
}
/* get the position of the next directory data block */
directory_data_block_position = directory_data_block->next_block;
/* free the current block because we finished with it */
free(directory_data_block);
}
/* update the destination directory */
retval = set_MDS(copy_directory, fd, copy_directory_offset);
/* free with the copy directory because we finished with it */
free(copy_directory);
if (!retval)
{
return 0;
}
}
}
/* update the superblock */
int retval = set_superblock(my_superblock, fd);
if (!retval)
{
return 0;
}
/* update the hole map */
retval = set_hole_map(holes, fd);
if (!retval)
{
return 0;
}
/* update the destination directory */
retval = set_MDS(destination_directory, fd, destination_offset);
if (!retval)
{
return 0;
}
return 1;
}
int cfs_cat(int fd, superblock* my_superblock, hole_map* holes, MDS* destination_file, off_t destination_file_offset, MDS* source_file)
{
/* useful attributes */
size_t block_size = my_superblock->block_size;
/* how much data a block can contain */
size_t bytes_for_data = block_size - sizeof(Block);
/* counter used to update the different fields of the structs */
uint number_of_new_blocks_created = 0;
/* if this is the first time we write to this file */
if (destination_file->blocks_using == 0)
{
/* allocate and initialize the new block */
Block* new_block = NULL;
MALLOC_OR_DIE_3(new_block, block_size);
initialize_data_Block(new_block, block_size);
/* find a spot for the new block */
off_t block_position = find_hole(holes, block_size);
/* write the block in the cfs file */
int retval = set_Block(new_block, fd, block_size, block_position);
if (!retval)
{
printf("set_Block() error.\n");
free(new_block);
return 0;
}
/* update the destination file */
destination_file->first_block = block_position;
free(new_block);
number_of_new_blocks_created++;
}
/* get the position of the first block */
off_t block_position = destination_file->first_block;
Block* block = NULL;
/* go to the last block in order to concatenate */
while (6 + 9 != 69)
{
/* get the next block */
block = get_Block(fd, block_size, block_position);