-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.c
More file actions
1439 lines (1313 loc) · 48.3 KB
/
Copy pathfilesystem.c
File metadata and controls
1439 lines (1313 loc) · 48.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
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 "filesystem.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define PATH_CURRENT 0
#define PATH_ROOT 1
#define FILE_TYPE 0
#define DIRECTORY_TYPE 1
#define MAX_INPUT_SIZE 64
static int inode_num;
static int data_num;
static struct User* current_user;
enum Mode {
None = -1,
READ,
WRITE,
APPEND,
READ_WRITE, //file must exist
WRITE_READ,
APPEND_READ
};
struct Tokenizer{
int flag;
int length;
char* tokens[MAX_INPUT_SIZE];
};
// helper methods:
static struct inode* getInode(int index){
return inode_data + index;
}
static char* getData(int index){
return (char*)block_data + block_size * index;
}
int f_changeMod(int inode, int permission) {
struct inode* changeInode = getInode(inode);
int creatorid = changeInode->uid;
if (creatorid != current_user->uid) {
printf("You do not have permission to change the mode of this file\n");
return -1;
}
if (changeInode == NULL || (permission < 0 && permission > 7)) {
printf("Invalid inode or permission\n");
return -1;
} else {
changeInode->permissions = permission;
printf("The permission of the inode %d is now %d\n", inode, permission);
return 0;
}
}
void freeBlock(int blockindex) {
memset(getData(blockindex),0,block_size);
// Retrieve the first block in the free list.
int first_free_block = sb->free_block;
if (first_free_block == -1) {
// If the free list is currently empty, just set this block as the first free block
// This case may not be typically allowed based on your policy but is here for completeness
sb->free_block = blockindex;
// Assuming blocknum needs to point to 0 to indicate no next block
int* block_data = (int*)getData(blockindex);
*block_data = -1; // Set the next pointer to 0
return;
}
// Get the data from the first free block, which contains the pointer to the next free block
int* first_block_data = (int*)getData(first_free_block);
int second_free_block = *first_block_data;
// Set the new block's next pointer to the current second block
int* new_block_data = (int*)getData(blockindex);
*new_block_data = second_free_block;
// Update the first block's pointer to point to the new block
*first_block_data = blockindex;
}
static void freeTokenizer(struct Tokenizer* tokenizer) {
if (tokenizer == NULL) {
return; // Nothing to free
}
// Free memory for each token
for (int i = 0; i < tokenizer->length; i++) {
free(tokenizer->tokens[i]);
}
// Free memory for the Tokenizer structure itself
free(tokenizer);
}
static void cleaninode(struct inode* inode, int all) {
// Assuming there is a function to free blocks and add them to free list
// void freeBlock(int blocknum);
// Free direct data blocks
for (int i = 0; i < N_DBLOCKS; i++) {
if (inode->dblocks[i] != 0) { // Check if the block is in use
freeBlock(inode->dblocks[i]); // Free the block
inode->dblocks[i] = 0; // Reset the pointer in the inode
}
}
// Free indirect data blocks
for (int i = 0; i < N_IBLOCKS; i++) {
if (inode->iblocks[i] != 0) { // Check if the block is in use
int* block_data = (int*)getData(inode->iblocks[i]); // Get the block containing pointers
for (int j = 0; j < sb->size / sizeof(int); j++) {
if (block_data[j] != 0) {
freeBlock(block_data[j]); // Free each data block pointed by the indirect block
block_data[j] = 0; // Reset the pointer in the data block
}
}
freeBlock(inode->iblocks[i]); // Finally, free the indirect block itself
inode->iblocks[i] = 0; // Reset the indirect block pointer in the inode
}
}
// Free doubly indirect block
if (inode->i2block != 0) {
int* first_level_block = (int*)getData(inode->i2block); // Get first level block
for (int i = 0; i < sb->size / sizeof(int); i++) {
if (first_level_block[i] != 0) {
int* second_level_block = (int*)getData(first_level_block[i]); // Get second level block
for (int j = 0; j < sb->size / sizeof(int); j++) {
if (second_level_block[j] != 0) {
freeBlock(second_level_block[j]); // Free each block pointed by second level block
second_level_block[j] = 0; // Reset pointer
}
}
freeBlock(first_level_block[i]); // Free second level block itself
first_level_block[i] = 0; // Reset pointer in first level block
}
}
freeBlock(inode->i2block); // Free the first level doubly indirect block
inode->i2block = 0; // Reset the doubly indirect block pointer in the inode
}
if(all==1){
inode->permissions = NONE;
int next_free = getInode(sb->free_inode)->parent;
getInode(sb->free_inode)->parent = inode->index;
inode->parent = next_free;
inode->type = 0;
inode->mtime = 0;
inode->size = 0;
inode->nlink = 0;
return;
}
// Additionally, reset inode metadata (optional based on requirement)
inode->size = 0; // Reset file size
inode->mtime = time(NULL); // Reset modification time
inode->nlink = 0; // Reset number of links
}
static struct dirent* findAllByIndex(struct inode inode, int INDEX) {
int read_num = 0;
int count = 0;
if(INDEX>inode.size/sizeof(struct dirent)-2){return NULL;}
// Direct blocks
for (int i = 0; i < N_DBLOCKS && read_num < inode.size; i++) {
char* data = getData(inode.dblocks[i]);
for (int block_num = 0; block_num < block_size; block_num += sizeof(struct dirent)) {
if (read_num >= inode.size) return NULL;
struct dirent* cur_dirent = (struct dirent*)(data + block_num);
if (strcmp(cur_dirent->name, ".") != 0&& strcmp(cur_dirent->name, "..") != 0) {
if (count == INDEX) {
return cur_dirent;
}
count++;
}
read_num += sizeof(struct dirent);
}
}
// Indirect blocks
for (int i = 0; i < N_IBLOCKS && read_num < inode.size; i++) {
int* index_data = (int*)getData(inode.iblocks[i]);
for (int index = 0; index < block_size / sizeof(int); index++) {
char* data = getData(index_data[index]);
for (int block_num = 0; block_num < block_size; block_num += sizeof(struct dirent)) {
if (read_num >= inode.size) return NULL;
struct dirent* cur_dirent = (struct dirent*)(data + block_num);
if (count == INDEX) {
return cur_dirent;
}
count++;
read_num += sizeof(struct dirent);
}
}
}
// Doubly indirect blocks
int* doubly_indirect = (int*)getData(inode.i2block);
for (int d_index = 0; d_index < block_size / sizeof(int) && read_num < inode.size; d_index++) {
int* index_data = (int*)getData(doubly_indirect[d_index]);
for (int index = 0; index < block_size / sizeof(int); index++) {
char* data = getData(index_data[index]);
for (int block_num = 0; block_num < block_size; block_num += sizeof(struct dirent)) {
if (read_num >= inode.size) return NULL;
struct dirent* cur_dirent = (struct dirent*)(data + block_num);
if (count == INDEX) {
return cur_dirent;
}
count++;
read_num += sizeof(struct dirent);
}
}
}
return NULL; // If no dirent was found at the specified index
}
static int assignBlock(){
if(sb->free_block==-1){printf("No more memory\n");return -1;}
int head_free = *(int*)getData(sb->free_block);
int data_file;
if(head_free==-1){
data_file = sb->free_block;
sb->free_block = -1;
}else{
data_file = head_free;
int next_free = *(int*)getData(head_free);
int* ptr = (int*)getData(sb->free_block);
*ptr = next_free;
}
// clean the data inside the new data block
char* new_data_block = getData(data_file);
memset(new_data_block,0,block_size);
return data_file;
}
static void* appendPosition(struct inode* inode,int block_index, int position, int request){
// request: write = 1, read = 0
// calculate the offset
int last_block = inode->size/block_size;
int num_index = block_size/sizeof(int);
int calculated_size = block_index*block_size+position;
// if it exceeds the size limit
if(block_index>(N_DBLOCKS+N_IBLOCKS*num_index+num_index*num_index)){return NULL;}
// if it reach an unassigned position
if(calculated_size>=inode->size){
if(request==0){
return NULL;
}else{
if(block_index==last_block&&position!=0){
if(block_index<N_DBLOCKS){
return getData(inode->dblocks[block_index])+position;
}
block_index = block_index - N_DBLOCKS;
if(block_index<N_IBLOCKS*num_index){
int a = block_index/num_index;
int b = block_index%num_index;
return getData(*((int*)getData(inode->iblocks[a])+b))+position;
}
block_index = block_index - N_IBLOCKS*num_index;
if(block_index<num_index*num_index){
int a = block_index/num_index;
int b = block_index%num_index;
int index = *((int*)getData(inode->i2block)+a);
return getData(*((int*)getData(index)+b))+position;
}else{
return NULL;
}
}else{
if(block_index==last_block){last_block--;}
// assign block in betweem last_block and block_index
if(block_index < N_DBLOCKS){
int index = 0;
for(int i=last_block+1;i<=block_index;i++){
index = assignBlock();
inode->dblocks[i] = index;
}
return getData(index)+ position;
}else{
for(int i=last_block+1;i<N_DBLOCKS;i++){
last_block++;
inode->dblocks[i] = assignBlock();
}
}
block_index = block_index - N_DBLOCKS;
last_block = last_block - N_DBLOCKS;
int c = last_block/num_index;
int d = last_block%num_index;
if(block_index<N_IBLOCKS*num_index){
int a = block_index/num_index;
int b = block_index%num_index;
int index;
for(int i=c;i<=a;i++){
int start = 0;
int end = num_index-1;
if(i==c){start=d+1;}
if(i==a){end=b;}
for(int j=start;j<=end;j++){
int* ptr = (int*)getData(inode->iblocks[i])+j;
index = assignBlock();
*ptr = index;
}
}
return getData(index)+ position;
}else{
for(int i=c;i<num_index;i++){
int start = 0;
if(i==c){start=d+1;}
for(int j=start;j<num_index;j++){
int* ptr = (int*)getData(inode->iblocks[i])+j;
*ptr = assignBlock();
last_block++;
}
}
}
block_index = block_index - N_IBLOCKS*num_index;
last_block = last_block - N_IBLOCKS*num_index;
c = last_block/num_index;
d = last_block%num_index;
if(block_index<num_index*num_index){
int a = block_index/num_index;
int b = block_index%num_index;
int index;
for(int i=c;i<=a;i++){
int start = 0;
int end = num_index-1;
if(i==c){start=d+1;}
if(i==a){end=b;}
for(int j=start;j<=end;j++){
int* ptr = (int*)getData(*((int*)getData(inode->i2block)+i))+j;
index = assignBlock();
*ptr = index;
}
}
return getData(index)+ position;
}
}
}
}else{
if(block_index<N_DBLOCKS){
return getData(inode->dblocks[block_index])+position;
}
block_index = block_index - N_DBLOCKS;
if(block_index<N_IBLOCKS*num_index){
int a = block_index/num_index;
int b = block_index%num_index;
return getData(*((int*)getData(inode->iblocks[a])+b))+position;
}
block_index = block_index - N_IBLOCKS*num_index;
if(block_index<num_index*num_index){
int a = block_index/num_index;
int b = block_index%num_index;
int index = *((int*)getData(inode->i2block)+a);
return getData(*((int*)getData(index)+b))+position;
}else{
return NULL;
}
}
return NULL;
}
static struct dirent* createFile(struct dirent* cur_dirent, char* name){
// we don't assign any data block for a new file
// rearrange the free inode
if(sb->free_inode==-1){printf("No more memory\n"); return NULL;}
int free_i = getInode(sb->free_inode)->parent;
struct inode* file_inode;
if(free_i!=-1){
int next_i = getInode(free_i)->parent;
file_inode = getInode(free_i);
getInode(sb->free_inode)->parent = next_i;
}else{
free_i = sb->free_inode;
file_inode = getInode(free_i);
sb->free_inode = -1;
}
// modify the inode to store all the information
file_inode -> type = FILE_TYPE;
file_inode -> permissions = RWx;
file_inode -> uid = current_user->uid;
file_inode -> parent = cur_dirent->inode;
file_inode -> nlink = 0;
file_inode -> size = 0;
file_inode -> mtime = time(NULL);
struct inode* inode = getInode(cur_dirent->inode);;
struct dirent* direct = (struct dirent*)appendPosition(inode,inode->size/block_size,inode->size%block_size,1);
inode->size += sizeof(struct dirent);
if(direct==NULL){return NULL;}
direct->inode = free_i;
direct->type = FILE_TYPE;
direct->offset = -1;
strcpy(direct->name,name);
return direct;
}
static struct dirent* findDirent(struct inode inode, char* target) {
int read_num = 0;
// Loop through direct blocks
for (int i = 0; i < N_DBLOCKS; i++) {
char* data = getData(inode.dblocks[i]);
int block_num = 0;
while (block_num < block_size) {
struct dirent* cur_dirent = (struct dirent*)data;
if (strcmp(target, cur_dirent->name) == 0) {
return cur_dirent;
}
read_num += sizeof(struct dirent);
block_num += sizeof(struct dirent);
if (read_num >= inode.size) {
return NULL;
}
data += sizeof(struct dirent);
}
}
// Loop through indirect blocks
for (int i = 0; i < N_IBLOCKS; i++) {
int* index_data = (int*)getData(inode.iblocks[i]);
for (int index = 0; index < block_size / sizeof(int); index++) {
char* data = getData(index_data[index]);
int block_num = 0;
while (block_num < block_size) {
struct dirent* cur_dirent = (struct dirent*)data;
if (strcmp(target, cur_dirent->name) == 0 ) {
return cur_dirent;
}
read_num += sizeof(struct dirent);
block_num += sizeof(struct dirent);
if (read_num >= inode.size) {
return NULL;
}
data += sizeof(struct dirent);
}
}
}
// Loop through doubly indirect blocks
int* doubly_indirect = (int*)getData(inode.i2block);
for (int d_index = 0; d_index < block_size / sizeof(int); d_index++) {
int* index_data = (int*)getData(doubly_indirect[d_index]);
for (int index = 0; index < block_size / sizeof(int); index++) {
char* data = getData(index_data[index]);
int block_num = 0;
while (block_num < block_size) {
struct dirent* cur_dirent = (struct dirent*)data;
if (strcmp(target, cur_dirent->name) == 0) {
return cur_dirent;
}
read_num += sizeof(struct dirent);
block_num += sizeof(struct dirent);
if (read_num >= inode.size) {
return NULL;
}
data += sizeof(struct dirent);
}
}
}
return NULL; // If no dirent was found
}
static struct dirent* findDirentByInode(struct inode inode, int target) {
int read_num = 0;
// Loop through direct blocks
for (int i = 0; i < N_DBLOCKS; i++) {
char* data = getData(inode.dblocks[i]);
int block_num = 0;
while (block_num < block_size) {
struct dirent* cur_dirent = (struct dirent*)data;
if (target== cur_dirent->inode&& cur_dirent->type == DIRECTORY_TYPE) {
return cur_dirent;
}
read_num += sizeof(struct dirent);
block_num += sizeof(struct dirent);
if (read_num >= inode.size) {
return NULL;
}
data += sizeof(struct dirent);
}
}
// Loop through indirect blocks
for (int i = 0; i < N_IBLOCKS; i++) {
int* index_data = (int*)getData(inode.iblocks[i]);
for (int index = 0; index < block_size / sizeof(int); index++) {
char* data = getData(index_data[index]);
int block_num = 0;
while (block_num < block_size) {
struct dirent* cur_dirent = (struct dirent*)data;
if (target== cur_dirent->inode&& cur_dirent->type == DIRECTORY_TYPE) {
return cur_dirent;
}
read_num += sizeof(struct dirent);
block_num += sizeof(struct dirent);
if (read_num >= inode.size) {
return NULL;
}
data += sizeof(struct dirent);
}
}
}
// Loop through doubly indirect blocks
int* doubly_indirect = (int*)getData(inode.i2block);
for (int d_index = 0; d_index < block_size / sizeof(int); d_index++) {
int* index_data = (int*)getData(doubly_indirect[d_index]);
for (int index = 0; index < block_size / sizeof(int); index++) {
char* data = getData(index_data[index]);
int block_num = 0;
while (block_num < block_size) {
struct dirent* cur_dirent = (struct dirent*)data;
if (target== cur_dirent->inode&& cur_dirent->type == DIRECTORY_TYPE) {
return cur_dirent;
}
read_num += sizeof(struct dirent);
block_num += sizeof(struct dirent);
if (read_num >= inode.size) {
return NULL;
}
data += sizeof(struct dirent);
}
}
}
return NULL; // If no dirent was found
}
static struct Tokenizer* tokenize(char* arg){
if(arg==NULL){
return NULL;
}
// Initialize a tokenizer
struct Tokenizer* path = malloc(sizeof(struct Tokenizer));
if (path == NULL) {
return NULL; // Failed to allocate memory for Tokenizer
}
int i = 0;
if(arg[i]=='/' || arg[i]=='~'){
path->flag = PATH_ROOT;
i++;
} else {
path->flag = PATH_CURRENT;
}
char* arg_copy = strdup(arg + i);
if (arg_copy == NULL) {
free(path);
return NULL; // Failed to allocate memory for arg_copy
}
char* token = strtok(arg_copy, "/");
int arg_count = 0;
// Tokenize input
while (token != NULL) {
if(arg_count >= MAX_INPUT_SIZE){
free(arg_copy);
free(path);
return NULL; // Reached maximum input size
}
path->tokens[arg_count] = strdup(token); // Allocate memory and copy token string
if (path->tokens[arg_count] == NULL) {
// Failed to allocate memory for token string
// Clean up allocated memory before returning NULL
for (int j = 0; j < arg_count; j++) {
free(path->tokens[j]);
}
free(arg_copy);
free(path);
return NULL;
}
arg_count++;
token = strtok(NULL, "/");
}
free(arg_copy); // Free the memory allocated for arg_copy
path->length = arg_count;
return path;
}
// Function to authenticate user directly from data blocks
int f_userAuthen(char* username, char* password) {
for (int i = 0; i < N_DBLOCKS; i++) {
if (user_inode->dblocks[i] != 0 || (i == 0 && user_inode->dblocks[i] == 0)) {
char* data = getData(user_inode->dblocks[i]);
if (!data) continue; // Handle NULL data
for (int j = 0; j < block_size; j += sizeof(struct User)) {
struct User* user = (struct User*)(data + j);
if (user->uid == -1) {
break; // Stop processing this block if sentinel value found
}
// Perform authentication check directly
if (strcmp(user->username, username) == 0 && strcmp(user->password, password) == 0) {
printf("User %s has been authenticated\n", username);
strcpy(current_user->username, username);
strcpy(current_user->password, password);
current_user->uid = user->uid;
return 0; // Authentication successful
}
}
}
}
return -1; // Authentication failed
}
int disk_open(char *diskname){
// open the disk image
FILE* file = fopen(diskname,"r+b");
if (file == NULL) {
printf("Disk not found. Please run ./format diskimage to create a disk.\n");
return 0;
}
diskimage = file;
sb = (struct Superblock*)malloc(sizeof(struct Superblock));
if(fread(sb,sizeof(struct Superblock),1,diskimage) == 0){
printf("Can't read superblock\n");
free(sb);
fclose(diskimage);
return 0;
}
// read the user inode
user_inode = (struct inode*)malloc(sizeof(struct inode));
if(fread(user_inode,sizeof(struct inode),1,diskimage) <1){
printf("Can't read user inode\n");
free(sb);free(user_inode);
fclose(diskimage);
return 0;
}
current_user = malloc(sizeof(struct User));
if (fseek(diskimage, 512, SEEK_SET) != 0) {
perror("Error seeking file");
free(sb); free(user_inode); free(current_user);
fclose(diskimage); // Close the file
return -1; // Exit program with an error
}
block_size = sb->size;
inode_num = block_size*(sb->data_offset-sb->inode_offset);
inode_data = (struct inode*)malloc(inode_num);
if(fread(inode_data,inode_num,1,diskimage)<1){
printf("Can't read inode region\n");
return 0;
}
root_direct = malloc(sizeof(struct dirent));
root_direct->inode = 0;
root_direct->offset = -1;
root_direct->type = DIRECTORY_TYPE;
strcpy(root_direct->name,"root");
//printf("inode %d\n",inode_data->size);
current_direct = malloc(sizeof(struct dirent));
current_direct->inode = 0;
current_direct->type = DIRECTORY_TYPE;
strcpy(current_direct->name,"root");
if (fseek(diskimage, 0, SEEK_END) != 0) {
perror("Error seeking file");
fclose(diskimage); // Close the file
return -1; // Exit program with an error
}
int num_bytes = ftell(diskimage);
if (num_bytes == -1) {
perror("Error getting file size");
fclose(diskimage); // Close the file
return 1; // Exit program with an error
}
fseek(diskimage,512+(sb->data_offset*512),SEEK_SET);
//printf("num of bytes %d\n",num_bytes); //好像少了128bytes?
data_num = num_bytes-512-inode_num;
block_data = (char*)malloc(data_num);
if(fread(block_data,data_num,1,diskimage)==0){
printf("Can't read data block region\n");
return 0;
}
//printf("%s\n",((struct dirent*)block_data+2)->name);
return 1;
}
int disk_close(){
free(current_direct);
free(root_direct);
free(current_user);
fseek(diskimage,0,SEEK_SET);
if(fwrite(sb,sizeof(struct Superblock),1,diskimage)<1){
free(sb);
free(user_inode);
free(inode_data);
free(block_data);
fclose(diskimage);
return 0;
}
free(sb);
if(fwrite(user_inode,sizeof(struct inode),1,diskimage)<1){
free(user_inode);
free(inode_data);
free(block_data);
fclose(diskimage);
return 0;
}
free(user_inode);
fseek(diskimage,512,SEEK_SET);
if(fwrite(inode_data,inode_num,1,diskimage)<1){
free(inode_data);
free(block_data);
fclose(diskimage);
return 0;
}
free(inode_data);
if(fwrite(block_data,data_num,1,diskimage)<1){
free(block_data);
fclose(diskimage);
return 0;
}
free(block_data);
fclose(diskimage);
return 1;
}
File* f_open(char* filename, char* mode){
if(filename[strlen(filename)-1]=='/'){
return NULL;
}
// do the path standardizing and permission check
struct Tokenizer* path = tokenize(filename);
// if the path is invalid (exceeds MAX_INPUT_SIZE)
if(path==NULL){
return NULL;
}
// check if there is a directory needed to open
struct dirent* target = current_direct;
if(path->length>1){
int len = strlen(filename)-strlen(path->tokens[path->length-1]);
char* pathname = malloc(len+1);
for (int i = 0; i < len; i++) {
pathname[i] = filename[i];
}
pathname[len] = '\0';
target = f_opendir(pathname);
free(pathname);
}
if(target == NULL){freeTokenizer(path);return NULL;}
char* name = NULL;
name = path->tokens[path->length-1];
if(strcmp(name,"..")==0||strcmp(name,".")==0){freeTokenizer(path);return NULL;}
struct inode* temp_inode = getInode(target->inode);
struct dirent* target_file = findDirent(*temp_inode,name);
int permission = NONE;
if(target_file!=NULL){permission = getInode(target_file->inode)->permissions;}
if(target_file!=NULL){if(target_file->type==DIRECTORY_TYPE){printf("cannot open '%s': is a directory\n",name); return NULL;}}
// mode
int int_mode = -1;
if(strcmp("r",mode)==0){
if(target_file==NULL){
printf("%s:No such file or directory\n",filename);
freeTokenizer(path);
return NULL;
}
if(current_user->uid ==0){
//superuser
}else if(permission!=Rwx&&permission!=RWx&&permission!=RWX&&permission!=RwX){
printf("Wrong permission\n");
freeTokenizer(path);
return NULL;
}
int_mode = READ;
}
else if(strcmp("r+",mode)==0){
if(target_file==NULL){
printf("%s:No such file or directory\n",filename);
freeTokenizer(path);
return NULL;
}
if(current_user->uid ==0){
//superuser
}else if(permission!=RWX&&permission!=RWx){
printf("Wrong permission\n");
freeTokenizer(path);
return NULL;
}
int_mode = READ_WRITE;
}
else if(strcmp("w",mode)==0){int_mode = WRITE;}
else if(strcmp("w+",mode)==0){int_mode = WRITE_READ;}
else if(strcmp("a",mode)==0){int_mode = APPEND;}
else if(strcmp("a+",mode)==0){int_mode = APPEND_READ;}
else{
printf("Invalid request\n");
freeTokenizer(path);
return NULL;
}
File* file = (File*)malloc(sizeof(File));
file->block_index = 0;
file->position = 0;
if(int_mode==READ || int_mode==READ_WRITE){
if(target_file==NULL){
printf("%s:No such file or directory\n",filename);
free(file);
freeTokenizer(path);
return NULL;
}else{
file->inode = target_file->inode;
}
}
else if(int_mode==WRITE || int_mode== WRITE_READ){
if(target_file==NULL){
file->inode = createFile(target,name)->inode;
}else{
file->inode = target_file->inode;
cleaninode(getInode(target_file->inode),0);
}
}
else if(int_mode==APPEND || int_mode== APPEND_READ){
if(target_file==NULL){
file->inode = createFile(target,name)->inode;
}else{
if(current_user->uid==0|| //superuser
(int_mode==APPEND&&(permission==rWx||permission==rWX||permission==RWX||permission==RWx))||
(int_mode==APPEND_READ&&(permission==RWx||permission==RWX))){
file->inode = target_file->inode;
int size = getInode(file->inode)->size;
file->block_index = size / block_size;
file->position = size % block_size;
}else{
free(file);
printf("Wrong Permission\n");
freeTokenizer(path);
return NULL;
}
}
}
getInode(file->inode)->nlink++;
file->mode = int_mode;
strcpy(file->name,filename);
freeTokenizer(path);
return file;
}
// return null if not found, return pointer to the dirent if found
struct dirent* f_opendir(char* directory) {
// Tokenize the input path
struct Tokenizer* path = tokenize(directory);
if (path == NULL) {
return NULL;
}
struct dirent* cur = NULL;
// Decide starting directory based on path flag
if (path->flag == PATH_CURRENT) {
cur = current_direct;
} else {
// Assuming the only other option is to start from root
cur = root_direct;
}
// Iterate over each token based on the number of tokens
for (int count = 0; count < path->length; count++) {
if (cur == NULL) {
printf("%s: Not a directory",directory);
return NULL;
}
if (cur->type == FILE_TYPE){
printf("%s: Not a directory",directory);
return NULL;
}
// Perform directory matching or traversal
if (strcmp(cur->name, path->tokens[count]) == 0) {
// If the directory name matches the current token, continue to next token
continue;
}
// Move to the next directory in the path
cur = findDirent(inode_data[cur->inode], path->tokens[count]);
}
freeTokenizer(path);
if(cur == NULL){
printf("%s: Not a directory",directory);
return NULL;
}else if (cur->type == FILE_TYPE){
printf("%s: Not a directory",directory);
}
cur->offset = 0;
// Return the final directory entry found, or NULL if not found
return cur;
}
int f_close(File* file){
if(file==NULL){return 0;}
//getInode(file->inode)->nlink--;
free(file);
return 1;
}
int f_rewind(File* file){
if(file==NULL){return 0;}
file->position = 0;
file->block_index = 0;
return 1;
}
struct dirent* f_readdir(struct dirent* directory){
// read the current sub-directory according to the offset
if(directory->offset==-1){
return NULL;
}
// update the offset;
struct dirent* cur = findAllByIndex(*getInode(directory->inode), directory->offset);
if (cur != NULL) {
directory->offset ++;
}else{
directory->offset = 0;
}
return cur;
}
int f_closedir(struct dirent* directory) {
// if directory doesn’t exists
if (directory == NULL) {
return -1;
}
// remove the dirent from the open file table
// set the offset of the directory to 0
directory->offset = -1;
return 0;
}
static void path_recur(char* path,struct inode* inode){
//printf("%s\n",findDirentByInode(*getInode(0),3)->name);
if(inode->parent!=-1){
path_recur(path,getInode(inode->parent));
strcat(path,"/");
strcat(path,findDirentByInode(*getInode(inode->parent), inode->index)->name);
}else{
strcat(path,"/root");
}
}
void f_path(char* path) {
path_recur(path, getInode(current_direct->inode));
}
void ls_helper(char* content,struct dirent* direct, int flag){
struct inode* temp = getInode(direct->inode);
if(flag==0){
strcat(content,direct->name);
strcat(content," ");
}else if(flag==1){
char tempStr[40];
if(temp->permissions==0){strcat(content,"--- ");}
else if(temp->permissions==Rwx){strcat(content,"r-- ");}
else if(temp->permissions==RWx){strcat(content,"rw- ");}
else if(temp->permissions==RwX){strcat(content,"r-x ");}
else if(temp->permissions==RWX){strcat(content,"rwx ");}
else if(temp->permissions==rWx){strcat(content,"-w- ");}
else if(temp->permissions==rWX){strcat(content,"-wx ");}
else if(temp->permissions==rwX){strcat(content,"--x ");}
sprintf(tempStr, " %-3d %-3d %-10d ", temp->nlink,temp->uid,temp->size);
//strcat(content,temp->mtime);
strcat(content,tempStr);
strcat(content,direct->name);
strcat(content,"\n");
}else{
strcat(content,direct->name);
if(temp->permissions%2==1){strcat(content,"*");}
strcat(content," ");
}
}
static void clearDirect(struct dirent* direct){
struct inode* inode = getInode(direct->inode);