-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.cpp
More file actions
1912 lines (1774 loc) · 63.2 KB
/
fs.cpp
File metadata and controls
1912 lines (1774 loc) · 63.2 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
// 参考操作系统课本285页的内容,对比本项目的实现和标准实现的差距
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <thread>
#include <mutex>
#include <spinlock>
#include <linux/rcupdate.h>
#define BLOCK_NUM 256
#define BLOCK_SIZE 256
#define INODE_NUM 128
//#define INODE_SIZE 128
#define IS_FILE 1
#define IS_DIR 2
#define IS_EMPTY 0
#define IS_FULL 1
typedef struct
{
int inodes_count;
int blocks_count;
int free_inodes_count;
int free_blocks_count;
int root;
int curDirIndex; // to inode
}SuperBlock;
typedef struct
{
char data[BLOCK_SIZE];
}Block;
typedef struct
{
int file_size;
int file_type;
char file_name[20];
time_t creation_time;
time_t last_modification_time;
int direct_block[4]; // to store the index in blocks
int single_indirect_link; // each link leads to 8 blocks
int double_indirect_link; // each link leads to 8 blocks
int next_available_block; // logical block in inode
int last_directory_index;
int index;
}Inode;
int portno, sockfd, newsockfd, serverPort, client_sockfd; // For server
struct hostent *host; // For client
int cylinders, sector_per_cylinder;
std::mutex mtx;
FILE *log_file;
SuperBlock mySuperBlock;
Inode inodes[INODE_NUM];
Block blocks[BLOCK_NUM];
int emptyInodes[INODE_NUM];
int emptyBlocks[BLOCK_NUM];
void error(char *errorMessage){
printf("%s\n", errorMessage);
return;
}
void make_empty(char *myString){
for(int i=0; i<strlen(myString); i++)
myString[i] = '\0';
return;
}
/*Connect the program to fs.log*/
void connect_to_log();
/*Disconnect the program to fs.log*/
void disconnect_to_log();
/*Write information to fs.log*/
void write_log(char* information_to_log);
/* The initialization of SuperBlock */
void superBlockInit();
/* The initialization of home directory */
void homeDirInit();
/* The initialization of a file */
void fileInit(Inode *curDir, int inode_index, int physical_block_index, int logical_block_index, char *request);
/* The initialization of a directory */
void dirInit(Inode *curDir, int inode_index, int physical_block_index, int logical_block_index, char *request);
/* Look for the file in double_indirect_link in inode, return -1 if not found, return the block index which stores the inode index if found */
int double_indirect_search(Inode *curDir, char *name);
/* Look for the file in single_indirect_link in inode, return -1 if not found, return the block index which stores the inode index if found */
int single_indirect_search(Inode *curDir, char *name);
/* Look for the file in the direct_blocks in inode, return -1 if not found, return the block index which stores the inode index if found */
int direct_search(Inode *curDir, char *name);
/* Display the data of the file destFile */
void display_data(Inode *destFile);
/* Overwrite data with length of dataLen in destFile */
void overwrite(Inode *destFile, int dataLen, char *data);
/* Insert to destFile at the position before the pos-th character(0-indexed), with the dataLen bytes of data */
void insert_data(Inode *destFile, int pos, int dataLen, char *data);
/* Delete the contents in destFile from the pos character(0-indexed), delete l bytes or till the end of the file*/
void delete_data(Inode *destFile, int pos, int dataLen);
/* Shift left every character in data by one spot */
void left_shift(char *data);
/* Format the file system on the disk */
// > f
void format_system();
/* Create a file named f in the file system */
// > mk f
void create_file(char *request);
/* Create a subdirectory named d in the currect directory */
// > mkdir d
void create_directory(char *request);
/* Delete the file named f from current directory*/
// > rm f
void delete_file(char *request);
/* Change the current working directory to the path */
// > cd path
void change_directory(char *request);
/* Delete the directory named d in the current directory */
// > rmdir d
void delete_directory(char *request);
/* Return a listing of the files and directories in the current directory */
// > ls
void directory_listing();
/* Read the file named f, and return the data that came from it */
// > cat f
void catch_file(char *request);
/* Overwrite the contents of the file named f with the l bytes of data */
// > w f l data
void write_file(char *request);
/* Insert to the file at the position before the pos-th character(0-indexed), with the l bytes of data */
// > i f pos l data
void insert_to_file(char *request);
/* Delete the contents from the pos character(0-indexed), delete l bytes or till the end of the file */
// > d f pos l
void delete_in_file(char *request);
/* Doing the work of server */
void serverWork();
/* Doing the work of client */
void clientWork();
int main(int argc, char* argv[])
{
/******************************/
/****Dealing with socket...****/
/******************************/
host = gethostbyname(argv[1]);
serverPort = atoi(argv[2]);
portno = atoi(argv[3]);
// Do the connection to disk system
clientWork();
// Client needs to first get the information about disk
char info[64];
write(client_sockfd, "Disk", 5);
read(client_sockfd, info, 64);
printf("Receive data about disk.c: %s\n", info);
char tmp;
char cylinders_string[64];
char sector_per_cylinder_string[64];
// first read out cylinders
int pointer = 0;
int count = 0;
tmp = info[pointer];
while(tmp!=' '){
cylinders_string[count] = tmp;
pointer++;
count++;
tmp = info[pointer];
}
cylinders = atoi(cylinders_string);
// then read out sector_per_cylinder
count = 0;
pointer++;
tmp = info[pointer];
while(tmp!='\0'){
sector_per_cylinder_string[count] = tmp;
pointer++;
count++;
tmp = info[pointer];
}
sector_per_cylinder = atoi(sector_per_cylinder_string);
char request[256]; // The whole request
char token[20];
int i, lastPos;
while(1){
int sock = serverWork();
// Get the request
make_empty(request);
make_empty(token);
if(read(sock, request, 256) < 0)
error("Error on reading\n");
printf("Receive: %s\n", request);
// Get the token
for(i=0; i<strlen(request); i++){
if(request[i] == ' ' || request[i] == '\0'){
token[i] = '\0';
break;
}
token[i] = request[i];
}
lastPos = i;
for(i=lastPos; i<strlen(token); i++)
token[i] = '\0';
if(strcmp(token, "f") == 0){
printf("Formatting system\n");
std::thread t(format_system, sock);
t.detach();
}
else if(strcmp(token, "mk") == 0){
printf("Creating file\n");
std::thread t(create_file, sock, request);
t.detach();
}
else if(strcmp(token, "mkdir") == 0){
printf("Creating directory\n");
std::thread t(create_directory, sock, request);
t.detach();
}
else if(strcmp(token, "rm") == 0){
printf("Deleting file\n");
std::thread t(delete_file, sock, request);
t.detach();
}
else if(strcmp(token, "cd") == 0){
printf("Changing directory\n");
std::thread t(change_directory, sock, request);
t.detach();
}
else if(strcmp(token, "rmdir") == 0){
printf("Deleting directory\n");
std::thread t(delete_directory, sock, request);
t.detach();
}
else if(strcmp(token, "ls") == 0){
printf("Listing directory\n");
std::thread t(directory_listing, sock);
t.detach();
}
else if(strcmp(token, "cat") == 0){
printf("Catching the file\n");
std::thread t(catch_file, sock, request);
t.detach();
}
else if(strcmp(token, "w") == 0){
printf("Writing file\n");
std::thread t(write_file, sock, request);
t.detach();
}
else if(strcmp(token, "i") == 0){
printf("Inserting to file\n");
std::thread t(insert_to_file, sock, request);
t.detach();
}
else if(strcmp(token, "d") == 0){
printf("Deleting in file\n");
std::thread t(delete_in_file, sock, request);
t.detach();
}
else if(strcmp(token, "e") == 0){
printf("The other side has been closed");
write(sock, "Goodbye!\n", 10);
break;
}
else{
printf("Invalid request\n");
continue;
}
}
close(sockfd);
return 0; // never get here
}
void connect_to_log()
{
log_file = fopen("fs.log", "w");
if(log_file == NULL){
printf("Error: Could not open file fs.log.\n");
exit(-1);
}
return;
}
void disconnect_to_log()
{
fclose(log_file);
return;
}
void write_log(char* information_to_log)
{
int information_len = strlen(information_to_log);
fwrite(information_to_log, 1, information_len, log_file);
return;
}
void superBlockInit()
{
mySuperBlock.inodes_count = INODE_NUM;
mySuperBlock.blocks_count = BLOCK_NUM;
mySuperBlock.free_inodes_count = INODE_NUM;
mySuperBlock.free_blocks_count = BLOCK_NUM;
mySuperBlock.root = 0; // home directory is the first inode
mySuperBlock.curDirIndex = 0;
int i;
for(i=0; i<INODE_NUM; i++)
emptyInodes[i] = IS_EMPTY;
for(i=0; i<BLOCK_NUM; i++)
emptyBlocks[i] = IS_EMPTY;
return;
}
void homeDirInit()
{
mySuperBlock.free_inodes_count--;
emptyInodes[mySuperBlock.root] = IS_FULL;
Inode *homeDirectory = &inodes[mySuperBlock.root];
homeDirectory->file_size = 0;
homeDirectory->file_type = IS_DIR;
time(&(homeDirectory->creation_time));
time(&(homeDirectory->last_modification_time));
homeDirectory->next_available_block = 0;
homeDirectory->last_directory_index = -1;
homeDirectory->index = 0;
return;
}
/* The initialization of a file */
void fileInit(Inode *curDir, int inode_index, int physical_block_index, int logical_block_index, char *request)
{
// State update
mySuperBlock.free_inodes_count--;
mySuperBlock.free_blocks_count--;
emptyInodes[inode_index] = IS_FULL;
emptyBlocks[physical_block_index] = IS_FULL;
curDir->next_available_block++;
// New inode for file created
Inode *fileNode = &inodes[inode_index];
fileNode->file_size = 0;
fileNode->file_type = IS_FILE;
//scanf("%s", fileNode->file_name);
time(&(fileNode->creation_time));
time(&(fileNode->last_modification_time));
fileNode->next_available_block = 0;
fileNode->last_directory_index = curDir->last_directory_index;
fileNode->index = inode_index;
// Get the file name
int i, startPoint, count = 0;
char fileName[20];
for(i=0; i<strlen(request); i++){
if(request[i] == ' '){
startPoint = i + 1;
break;
}
}
for(i=startPoint; i<strlen(request); i++){
fileName[count] = request[i];
count++;
}
strcpy(fileNode->file_name, fileName);
// Create the connection between curDir and fileNode
// Use direct block
if(logical_block_index < 4){
curDir->direct_block[logical_block_index] = physical_block_index;
char string[5];
sprintf(string, "%d", inode_index);
char diskCommand[256];
char indexString[20];
int cylinder_index = physical_block_index / sector_per_cylinder;
int sector_index = physical_block_index % sector_per_cylinder;
strncat(diskCommand, "W", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
strncat(diskCommand, string, strlen(string));
write(client_sockfd, diskCommand, strlen(diskCommand));
return;
}
// Use single indirect link
// First node in single indirect link
else if(logical_block_index == 4){
curDir->single_indirect_link = physical_block_index;
int next_physical_block_index;
int i;
for(i=0; i<BLOCK_NUM; i++)
if(emptyBlocks[i] == IS_EMPTY)
break;
next_physical_block_index = i;
mySuperBlock.free_blocks_count--;
emptyBlocks[next_physical_block_index] = IS_FULL;
char string1[5];
char string2[5];
//itoa(inode_index, string1, 10);
//itoa(next_physical_block_index, string2, 10);
sprintf(string1, "%d", inode_index);
sprintf(string2, "%d", next_physical_block_index);
strcpy(blocks[curDir->single_indirect_link].data, string2);
strncat(blocks[curDir->single_indirect_link].data, "@", 1); // for seperation between different index
strcpy(blocks[next_physical_block_index].data, string1);
return;
}
// Not the first node but still in single indirect link
else if(logical_block_index < 12){
char string1[5];
char string2[5];
//itoa(physical_block_index, string1, 10);
//itoa(inode_index, string2, 10);
sprintf(string1, "%d", physical_block_index);
sprintf(string2, "%d", inode_index);
strncat(blocks[curDir->single_indirect_link].data, string1, strlen(string1));
strncat(blocks[curDir->single_indirect_link].data, "@", 1);
strcpy(blocks[physical_block_index].data, string2);
return;
}
// Use double indirect link
return;
}
/* The initialization of a directory */
void dirInit(Inode *curDir, int inode_index, int physical_block_index, int logical_block_index, char *request)
{
// State update
mySuperBlock.free_inodes_count--;
mySuperBlock.free_blocks_count--;
emptyInodes[inode_index] = IS_FULL;
emptyBlocks[physical_block_index] = IS_FULL;
curDir->next_available_block++;
// New inode for directory created
Inode *fileNode = &inodes[inode_index];
fileNode->file_size = 0;
fileNode->file_type = IS_DIR;
//scanf("%s", fileNode->file_name);
time(&(fileNode->creation_time));
time(&(fileNode->last_modification_time));
fileNode->next_available_block = 0;
fileNode->last_directory_index = curDir->index;
fileNode->index = inode_index;
// Get the file name
int i, startPoint, count = 0;
char fileName[20];
for(i=0; i<strlen(request); i++){
if(request[i] == ' '){
startPoint = i + 1;
break;
}
}
for(i=startPoint; i<strlen(request); i++){
fileName[count] = request[i];
count++;
}
strcpy(fileNode->file_name, fileName);
// debug
printf("Filename: %s\n", fileName);
// Create the connection between curDir and fileNode
// Use direct block
if(logical_block_index < 4){
curDir->direct_block[logical_block_index] = physical_block_index;
char string[5];
sprintf(string, "%d", inode_index);
char diskCommand[256];
char indexString[20];
int cylinder_index = physical_block_index / sector_per_cylinder;
int sector_index = physical_block_index % sector_per_cylinder;
strncat(diskCommand, "W", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
strncat(diskCommand, string, strlen(string));
// debug
printf("diskCommand: %s\n", diskCommand);
write(client_sockfd, diskCommand, strlen(diskCommand));
return;
}
// Use single indirect link
// First node in single indirect link
else if(logical_block_index == 4){
curDir->single_indirect_link = physical_block_index;
int next_physical_block_index;
int i;
for(i=0; i<BLOCK_NUM; i++)
if(emptyBlocks[i] == IS_EMPTY)
break;
next_physical_block_index = i;
mySuperBlock.free_blocks_count--;
emptyBlocks[next_physical_block_index] = IS_FULL;
char string1[5];
char string2[5];
//itoa(inode_index, string1, 10);
//itoa(next_physical_block_index, string2, 10);
sprintf(string1, "%d", inode_index);
sprintf(string2, "%d", next_physical_block_index);
strcpy(blocks[curDir->single_indirect_link].data, string2);
strncat(blocks[curDir->single_indirect_link].data, "@", 1); // for seperation between different index
strcpy(blocks[next_physical_block_index].data, string1);
return;
}
// Not the first node but still in single indirect link
else if(logical_block_index < 12){
char string1[5];
char string2[5];
//itoa(physical_block_index, string1, 10);
//itoa(inode_index, string2, 10);
sprintf(string1, "%d", physical_block_index);
sprintf(string2, "%d", inode_index);
strncat(blocks[curDir->single_indirect_link].data, string1, strlen(string1));
strncat(blocks[curDir->single_indirect_link].data, "@", 1);
strcpy(blocks[physical_block_index].data, string2);
return;
}
// Use double indirect link
return;
}
/* Look for the file in double_indirect_link in inode, return -1 if not found, return the block index which stores the inode index if found */
int double_indirect_search(Inode *curDir, char *name)
{
return -1;
}
/* Look for the file in single_indirect_link in inode, return -1 if not found, return the block index which stores the inode index if found */
int single_indirect_search(Inode *curDir, char *name)
{
Inode *destFile;
int first_level_block_index;
int second_level_block_index;
int inode_index;
first_level_block_index = curDir->single_indirect_link;
char curIndexString[10];
char char_to_load[2];
char *all_index = blocks[first_level_block_index].data;
for(int i=0; i<strlen(all_index); i++){
// Meet seperator, check the current index
if(all_index[i] == '@'){
second_level_block_index = atoi(curIndexString);
inode_index = atoi(blocks[second_level_block_index].data);
destFile = &inodes[inode_index];
if(strcmp(destFile->file_name, name) == 0)
return second_level_block_index;
curIndexString[0] = '\0'; // clear it
}
// Load the curIndexString
else{
char_to_load[0] = all_index[i];
strncat(curIndexString, char_to_load, 1);
}
}
// If the loop finishes, it means the file is not found
return -1;
}
/* Look for the file in the direct_blocks in inode, return -1 if not found, return the block index which stores the inode index if found */
int direct_search(Inode *curDir, char *name)
{
int block_index;
int inode_index;
Inode *destFile;
int ceiling;
char buffer[256];
char diskCommand[256];
int cylinder_index, sector_index;
char indexString[20];
if(curDir->next_available_block > 3) ceiling = 4;
else ceiling = curDir->next_available_block;
for(int i=0; i<ceiling; i++){
block_index = curDir->direct_block[i];
//inode_index = atoi(blocks[block_index].data);
cylinder_index = block_index / sector_per_cylinder;
sector_index = block_index % sector_per_cylinder;
strncat(diskCommand, "R", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
printf("debug1\n");
write(client_sockfd, diskCommand, strlen(diskCommand));
printf("debug2\n");
read(client_sockfd, buffer, 256);
printf("debug3\n");
inode_index = atoi(buffer);
destFile = &inodes[inode_index];
// Find the file
if(strcmp(destFile->file_name, name) == 0)
return block_index;
}
return -1;
}
/* Display the data of the file destFile */
void display_data(Inode *destFile)
{
int direct_index;
int first_level_index, second_level_index;
int i;
int ceiling;
if(destFile->next_available_block > 3) ceiling = 4;
else ceiling = destFile->next_available_block;
// Display in direct blocks
int cylinder_index, sector_index;
char indexString[20];
char diskCommand[256];
char data[256];
for(i=0; i<ceiling; i++){
direct_index = destFile->direct_block[i];
if(emptyBlocks[direct_index] == IS_FULL){
cylinder_index = direct_index / sector_per_cylinder;
sector_index = direct_index % sector_per_cylinder;
strncat(diskCommand, "R", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
write(client_sockfd, diskCommand, strlen(diskCommand));
read(client_sockfd, data, 256);
write(newsockfd, data, 256);
}
}
if(destFile->next_available_block <= 4){
//write_log("\n");
write(newsockfd, "\n", 2);
return;
}
// Display in single indirect link
first_level_index = destFile->single_indirect_link;
char *all_index = blocks[first_level_index].data;
char curIndexString[10];
char char_to_load[2];
for(i=0; i<strlen(all_index); i++){
// Meet seperator, check the current index
if(all_index[i] == '@'){
second_level_index = atoi(curIndexString);
if(emptyBlocks[second_level_index] == IS_FULL)
write_log(blocks[second_level_index].data);
curIndexString[0] = '\0';
}
else{
char_to_load[0] = all_index[i];
strncat(curIndexString, char_to_load, 1);
}
}
// Display in double indirect link
write_log("\n");
return;
}
/* Overwrite data with length of dataLen in destFile */
void overwrite(Inode *destFile, int dataLen, char *data)
{
int i;
int block_index, second_block_index;
// Clear direct blocks
/*for(i=0; i<4; i++){
block_index = destFile->direct_block[i];
emptyBlocks[block_index] = IS_EMPTY;
}*/
// Clear single indirect link
char curIndexString[10];
char all_index[256];
char char_to_load[2];
block_index = destFile->single_indirect_link;
//all_index = blocks[block_index].data;
int cylinder_index = block_index / sector_per_cylinder;
int sector_index = block_index % sector_per_cylinder;
char diskCommand[256];
char indexString[20];
char buffer[256];
strncat(diskCommand, "R", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
write(client_sockfd, diskCommand, strlen(diskCommand));
read(client_sockfd, buffer, 256);
strcpy(all_index, buffer);
for(i=0; i<strlen(all_index); i++){
if(all_index[i] == '@'){
second_block_index = atoi(curIndexString);
emptyBlocks[second_block_index] = IS_EMPTY;
curIndexString[0] = '\0';
}
else{
char_to_load[0] = all_index[i];
strncat(curIndexString, char_to_load, 1);
}
}
// Clear double indirect link
int block_required = dataLen / BLOCK_SIZE + 1; // these lines' problem
int tmp;
for(i=0; i<BLOCK_NUM; i++){
if(emptyBlocks[i] == IS_EMPTY){
tmp = i;
break;
}
}
destFile->direct_block[0] = tmp;
block_index = destFile->direct_block[0];
//strcpy(blocks[block_index].data, data);
cylinder_index = block_index / sector_per_cylinder;
sector_index = block_index % sector_per_cylinder;
diskCommand[0] = '\0';
strncat(diskCommand, "W", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
strncat(diskCommand, data, strlen(data));
write(client_sockfd, diskCommand, strlen(diskCommand));
emptyBlocks[block_index] = IS_FULL;
destFile->next_available_block = 1;
destFile->file_size = strlen(data);
time(&(destFile->last_modification_time));
return;
}
/* Insert to destFile at the position before the pos-th character(0-indexed), with the dataLen bytes of data */
void insert_data(Inode *destFile, int pos, int dataLen, char *data)
{
time(&(destFile->last_modification_time));
destFile->file_size += strlen(data);
int i;
int block_index;
block_index = destFile->direct_block[0];
//char *data_to_modify = blocks[block_index].data;
char data_to_modify[256];
int cylinder_index, sector_index;
char indexString[20];
char diskCommand[256];
char buffer[256];
cylinder_index = block_index / sector_per_cylinder;
sector_index = block_index % sector_per_cylinder;
strncat(diskCommand, "R", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
write(client_sockfd, diskCommand, 256);
read(client_sockfd, buffer, 256);
strcpy(data_to_modify, buffer);
char mybuffer[BLOCK_SIZE];
int count = 0;
for(i=pos; i<strlen(data_to_modify); i++){
mybuffer[count] = data_to_modify[count + pos];
count ++;
}
data_to_modify[pos] = '\0';
strncat(data_to_modify, data, dataLen); // insert new data
strncat(data_to_modify, mybuffer, count); // append original data
return;
}
/* Delete the contents in destFile from the pos character(0-indexed), delete l bytes or till the end of the file*/
void delete_data(Inode *destFile, int pos, int dataLen)
{
time(&(destFile->last_modification_time));
destFile->file_size -= dataLen;
int i;
int block_index;
block_index = destFile->direct_block[0];
//char *data_to_modify = blocks[block_index].data;
char data_to_modify[256];
int cylinder_index, sector_index;
char indexString[20];
char diskCommand[256];
char buffer[256];
cylinder_index = block_index / sector_per_cylinder;
sector_index = block_index % sector_per_cylinder;
strncat(diskCommand, "R", 2);
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
write(client_sockfd, diskCommand, 256);
read(client_sockfd, buffer, 256);
strcpy(data_to_modify, buffer);
char mybuffer[BLOCK_SIZE];
int count = 0;
for(i=pos; i<strlen(data_to_modify); i++){
mybuffer[count] = data_to_modify[count + i];
count ++;
}
data_to_modify[pos] = '\0';
strncat(data_to_modify, mybuffer, count);
return;
}
/* Shift left every character in data by one spot */
void left_shift(char *data)
{
int i;
int len = strlen(data);
for(i=0; i<len-1; i++)
data[i] = data[i+1];
data[len-1] = '\0';
return;
}
/* Format the file system on the disk */
// > f
void format_system(int sock)
{
mtx.lock();
superBlockInit();
homeDirInit();
mtx.unlock();s
write(sock, "Done\n", 10);
return;
}
/* Create a file named f in the file system */
// > mk f
void create_file(int sock, char *request)
{
int cylinder_index, sector_index;
Inode *curDir = &inodes[mySuperBlock.curDirIndex];
int inode_index;
int physical_block_index;
int logical_block_index = curDir->next_available_block;
int i;
rcu_read_lock();
// get inode_index
for(i=0; i<INODE_NUM; i++)
if(emptyInodes[i] == IS_EMPTY)
break;
if(i == INODE_NUM){
//write_log("No\n"); // no available inode
write(newsockfd, "No\n", 10);
return;
}
else inode_index = i;
// get physical_block_index
for(i=0; i<BLOCK_NUM; i++)
if(emptyBlocks[i] == IS_EMPTY)
break;
if(i == BLOCK_NUM){
//write_log("No\n"); // no available block
write(newsockfd, "No\n", 10);
return;
}
else physical_block_index = i;
rcu_read_unlock();
fileInit(curDir, inode_index, physical_block_index, logical_block_index, request);
cylinder_index = physical_block_index / sector_per_cylinder;
sector_index = physical_block_index % sector_per_cylinder;
/*char diskCommand[256];
char indexString[10];
strncat(diskCommand, "W ", 3);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", inode_index);
strncat(diskCommand, indexString, strlen(indexString));
write(client_sockfd, diskCommand, strlen(diskCommand));*/
//write_log("Yes\n");
write(newsockfd, "Yes\n", 10);
return;
}
/* Create a subdirectory named d in the currect directory */
// > mkdir d
void create_directory(int sock, char *request)
{
int cylinder_index, sector_index;
Inode *curDir = &inodes[mySuperBlock.curDirIndex];
int inode_index;
int physical_block_index;
int logical_block_index = curDir->next_available_block;
int i;
rcu_read_lock();
// get inode_index
for(i=0; i<INODE_NUM; i++)
if(emptyInodes[i] == IS_EMPTY)
break;
if(i == INODE_NUM){
//write_log("No\n");
write(newsockfd, "No\n", 10);
return;
}
else inode_index = i;
// get physical_block_index
for(i=0; i<BLOCK_NUM; i++)
if(emptyBlocks[i] == IS_EMPTY)
break;
if(i == BLOCK_NUM){
//write_log("No\n");
write(newsockfd, "No\n", 10);
return;
}
else physical_block_index = i;
rcu_read_unlock();
dirInit(curDir, inode_index, physical_block_index, logical_block_index, request);
/*cylinder_index = physical_block_index / sector_per_cylinder;
sector_index = physical_block_index % sector_per_cylinder;
char diskCommand[256];
char indexString[10];
strncat(diskCommand, "W ", 3);
sprintf(indexString, "%d", cylinder_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", sector_index);
strncat(diskCommand, indexString, strlen(indexString));
strncat(diskCommand, " ", 2);
sprintf(indexString, "%d", inode_index);
strncat(diskCommand, indexString, strlen(indexString));
write(client_sockfd, diskCommand, strlen(diskCommand));*/
//write_log("Yes\n");
write(sock, "Yes\n", 10);
return;
}
/* Delete the file named f from current directory*/
// > rm f
void delete_file(int sock, char *request)
{
Inode *curDir = &inodes[mySuperBlock.curDirIndex];
char name[20];
int file_inode_index = -1; // The inode of file
int file_block_index = -1;
//scanf("%s", name);
// Get the file name
int i, startPoint, count = 0;