-
Notifications
You must be signed in to change notification settings - Fork 0
/
libTinyFS.c
833 lines (741 loc) · 18.8 KB
/
libTinyFS.c
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
//make linked list of free blocks
#include "libTinyFS.h"
#include "TinyFS_errno.h"
int mountedDisk = -1, totalDiskBytes;
fileTableEntry *fileTable;
diskInfo dInfo;
unsigned char *blockBuffer;
fdList *openFDs;
int numOpenFiles = 0;
char finalPass[BLOCKSIZE];
int setBit(int block){
int err;
unsigned char mask = 1, temp;
mask = mask << 7;
temp = mask;
unsigned char* buff = calloc(BLOCKSIZE, 1);
if(err = readBlock(dInfo.disk,0,buff) < 0){
free(buff);
return err;
}
mask = mask >> block % 8;
if(buff[4 + (block / 8)] & mask == 1)
printf("warning, this block is already set\n");
buff[4 +(block / 8)] |= mask;
if(err = writeBlock(dInfo.disk,0,buff) < 0){
free(buff);
return err;
}
free(buff);
return 0;
}
int clearBit(int block){
int err;
unsigned char mask = 1, temp;
mask = mask << 7;
temp = mask;
unsigned char* buff = calloc(BLOCKSIZE, 1);
if(err = readBlock(dInfo.disk,0,buff) < 0){
free(buff);
return err;
}
mask = mask >> block % 8;
if(buff[4 + (block / 8)] & mask == 0)
printf("warning, this block is already clear\n");
buff[4 +(block / 8)] &= ~mask;
if(err = writeBlock(dInfo.disk,0,buff) < 0){
free(buff);
return err;
}
free(buff);
return 0;
}
int tfs_mkfs(char *filename, int nBytes){
char password1[BLOCKSIZE];
char password2[BLOCKSIZE];
totalDiskBytes = nBytes;
char strBuff[MAX_FILE_NAME_LENGTH + 20];
sprintf(strBuff,"%s.enc",filename);
int err;
//set empty blocks
//set superblock
int disk = openDisk(filename, nBytes);
if(disk < 0){
return disk; //some error occurred
}
//new file if nBytes > 0
unsigned char* initBytes = calloc(sizeof(char),BLOCKSIZE);
if(nBytes > 0){
int x;
unsigned char mask = 0;
//initialize superblock
initBytes[0] = 1; //set block type
initBytes[1] = 0x45;
initBytes[2] = 0; //pointer to inode, will be set when first file created
//int x;
for(x = 1; x < nBytes / BLOCKSIZE; x++){
if(x % 8 == 0){
mask = 1;
mask = mask << 7;
}
initBytes[4+ (x / 8)] |= mask;
mask = mask >> 1;
}
mask = 1;
mask = ~(mask << 7);
initBytes[4] |= mask;
if((err = writeBlock(disk,0,initBytes)) < 0){
return err;
}
for(x = 4; x < 9; x++){
initBytes[x] = 0;
}
//initBytes[4] = //block number of root inode
//number free blocks
//nBytes/blockSize
initBytes[0] = 4; //set block type
for(x= 1; x < nBytes / BLOCKSIZE; x++){
if(err = writeBlock(disk,x,initBytes) < 0){
return err;
}
}
}else{
return 0;
}
free(initBytes);
//create and delete encryption
password1[0] = 'p';
password1[1] = 'a';
password1[2] = 's';
password1[3] = 's';
password1[4] = ':';
password2[0] = 'p';
password2[1] = 'a';
password2[2] = 's';
password2[3] = 's';
password2[4] = ':';
while(1){
printf("enter password to encrypt disk\n");
scanf("%s",password1 + 5);
printf("confirm password\n");
scanf("%s",password2 + 5);
if(strcmp(password1,password2))
printf("passwords do not match\n");
else{
break;
}
}
int pid;
if((pid = fork())){//parent
int tempStatus;
waitpid(pid, &tempStatus,0);
remove(filename);
}else {//child
execl("/usr/bin/openssl", "enc", "-aes-256-cbc", "-salt", "-in", filename, "-out", strBuff, "-pass", password1, NULL);
}
return 0;
}
int tfs_mount(char *filename){
if(mountedDisk != -1){
return MAXDISKS;
}
char strBuff[MAX_FILE_NAME_LENGTH + 20];
sprintf(strBuff,"%s.enc",filename);
if(PLACEHOLDER == openDisk(strBuff, -1)){
//decrypt
char password1[BLOCKSIZE];
password1[0] = 'p';
password1[1] = 'a';
password1[2] = 's';
password1[3] = 's';
password1[4] = ':';
printf("enter password to mount disk\n");
scanf("%s",password1 + 5);
strcpy(finalPass,password1);
int pid;
if((pid = fork())){//parent
int tempStatus;
waitpid(pid, &tempStatus,0);
}else {//child
execl("/usr/bin/openssl", "enc", "-d", "-aes-256-cbc", "-in", strBuff, "-out", filename, "-pass", password1, NULL);
}
//finish decrypt
}else{
return NODISK;
}
int disk = openDisk(filename,0);
if(disk < 0){
return NODISK;
}
fileTable = calloc(sizeof(fileTableEntry), DEFAULT_DISK_SIZE / BLOCKSIZE);
blockBuffer = calloc(BLOCKSIZE, sizeof(char));
//dInfo = *(diskInfo*)calloc(sizeof(diskInfo),1);
dInfo.disk = disk;
dInfo.filename = calloc(sizeof(char), MAX_FILE_NAME_LENGTH + 20);
strcpy(dInfo.filename, strBuff);
dInfo.realName = filename;
dInfo.size = totalDiskBytes / BLOCKSIZE;
//checks to see if file is mountable
int temp = 0;
int x, y, mask;
unsigned char *freeBlock = calloc(sizeof(char), BLOCKSIZE);
unsigned char vector;
int rdRtn = readBlock(disk,0,blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
if(blockBuffer[0] != 1 && blockBuffer[1] != 0x45){
free(freeBlock);
return DISKCORRUPT; // disk corrupted
}
for(x = 1; x < dInfo.size; x++){
rdRtn = readBlock(disk,x,freeBlock);
if (rdRtn < 0) {
return rdRtn;
}
if(freeBlock[1] != 0x45){
free(freeBlock);
return DISKCORRUPT; //disk corrupted
}
y = 4+ (x / 8);
vector = blockBuffer[y];
mask = 1 << (7 - (x % 8));
if(freeBlock[0] == 4){//empty block
if(vector & mask == 0){
free(freeBlock);
return DISKCORRUPT;
}
}else{
if(vector & mask != 0){
free(freeBlock);
return DISKCORRUPT;
}
}
}
free(freeBlock);
mountedDisk = dInfo.disk;
return 0;
}
int tfs_unmount(void){
int pid;
if((pid = fork())){//parent
int tempStatus;
waitpid(pid, &tempStatus,0);
}else {//child
execl("/usr/bin/openssl", "enc", "-aes-256-cbc", "-salt", "-in", dInfo.realName, "-out", dInfo.filename, "-pass", finalPass, NULL);
}
remove(dInfo.realName);
free(fileTable);
//free(&dInfo);
free(blockBuffer);
dInfo.disk = mountedDisk = -1;
}
fileDescriptor tfs_openFile(char *name) {
fileDescriptor myFD;
int iNode;
//check strlen
if (strlen(name) > MAX_FILE_NAME_LENGTH) {
myFD = FILENAMETOOLONG;
}
//check if open
else if (myFD = fileIsOpen(name)) {
myFD = FILEOPEN;
}
//check if exists
else if (iNode = fileOnFS(name)) {
myFD = openFile(iNode, name);
}
//create it
else {
if (iNode = spaceOnFS()) {
myFD = openFile(iNode, name);
int nextINode = 0;
int rdRtn = readBlock(dInfo.disk, 0, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
superBlockFormat *super = (superBlockFormat *)blockBuffer;
nextINode = super->firstINode;
super->firstINode = iNode;
int wtRtn = writeBlock(dInfo.disk, 0, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
//set up iNode
free(blockBuffer);
blockBuffer = calloc(BLOCKSIZE, sizeof(char));
iNodeFormat *format = (iNodeFormat *)blockBuffer;
format->blockType = 2;
format->magicNumber = 0x45;
format->nextINode = nextINode;
format->nextFileExtent = 0;
format->fileSize = 0;
strcpy(format->filename, name);
wtRtn = writeBlock(dInfo.disk, iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
setCreated(myFD);
}
else {
myFD = NODISKMEM;
}
}
return myFD;
}
int tfs_closeFile(fileDescriptor FD){
int rtn = 0;
if (!(fileTable[FD].valid)) {
rtn = FSBADFD;
}
else {
fileTable[FD].valid = 0;
fdList *newOpenFD = malloc(sizeof(fdList));
newOpenFD->next = openFDs;
newOpenFD->fd = FD;
numOpenFiles--;
rtn = 1;
}
return rtn;
}
int tfs_writeFile(fileDescriptor FD, char *buffer, int size){
int wtRtn = 0;
int rtn = 0;
if (!(fileTable[FD].valid)) {
rtn = FSBADFD;
}
else {
int rdRtn = readBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iNode = (iNodeFormat*)blockBuffer;
if (iNode->fileSize != 0) {
int next = iNode->nextFileExtent;
while (next) {
rdRtn = readBlock(dInfo.disk, next, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
fileExtentFormat *fileExtent = (fileExtentFormat *)blockBuffer;
setBit(next);
next = fileExtent->nextFileExtent;
}
}
rdRtn = readBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNode = (iNodeFormat*)blockBuffer;
iNode->fileSize = size;
wtRtn = writeBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
int blocks = 0;
int fileSize = size;
int bufferOffset = 0;
size += sizeof(iNodeFormat); //iNode header size in bytes
while(size >= BLOCKSIZE) {
size -= (BLOCKSIZE - 4); //fileExtent header size in bytes
blocks++;
}
int *blocksUsed = calloc(blocks, sizeof(int));
int i, j, check;
for (i = 0; i < blocks; i++) {
check = spaceOnFS();
if (!check) {
rtn = FSBIG;
//NOT ENOUGH SPACE FOR FILE, WHAT ERROR?
//clear the ones I've "taken" so far
for (j = 0; j < i; j++ ) {
setBit(blocksUsed[j]);
}
}
else {
blocksUsed[i] = check;
}
}
rdRtn = readBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iNodef = (iNodeFormat*) blockBuffer;
iNodef->nextFileExtent = blocksUsed[0];
if (blocks == 0) { //fits in iNode
memcpy(blockBuffer + sizeof(iNodeFormat), buffer, fileSize);
wtRtn = writeBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
}
else {
memcpy(blockBuffer + sizeof(iNodeFormat), buffer, BLOCKSIZE - sizeof(iNodeFormat));
wtRtn = writeBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
bufferOffset += BLOCKSIZE - sizeof(iNodeFormat);
fileExtentFormat *fileExtent;
for (i = 0; i < blocks-1; i++) {
free(blockBuffer);
blockBuffer = calloc(BLOCKSIZE,sizeof(char));
fileExtent = (fileExtentFormat *)blockBuffer;
fileExtent->blockType = 3;
fileExtent->magicNumber = 0x45;
fileExtent->nextFileExtent = blocksUsed[i+1];
fileExtent->filler = 0;
memcpy(blockBuffer + sizeof(fileExtentFormat), buffer + bufferOffset, BLOCKSIZE - sizeof(fileExtentFormat));
bufferOffset += BLOCKSIZE - sizeof(fileExtentFormat);
wtRtn = writeBlock(dInfo.disk, blocksUsed[i], blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
}
free(blockBuffer);
blockBuffer = calloc(BLOCKSIZE,sizeof(char));
fileExtent = (fileExtentFormat *)blockBuffer;
fileExtent->blockType = 3;
fileExtent->magicNumber = 0x45;
fileExtent->nextFileExtent = 0;
fileExtent->filler = 0;
memcpy(blockBuffer + sizeof(fileExtentFormat), buffer + bufferOffset, fileSize - bufferOffset);
wtRtn = writeBlock(dInfo.disk, blocksUsed[i], blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
}
rtn = 1;
}
setLastModified(FD);
return rtn;
}
int tfs_deleteFile(fileDescriptor FD){
int rtn = 0;
if (!(fileTable[FD].valid)) {
rtn = FSBADFD;
}
else {
//mark its blocks as Free
//fix iNode pointers
fileTableEntry file = fileTable[FD];
int rdRtn = readBlock(dInfo.disk, file.iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iFormat = (iNodeFormat*)blockBuffer;
int nextINode = iFormat->nextINode;
int next = iFormat->nextFileExtent;
iFormat->blockType = 4;
int wtRtn = writeBlock(dInfo.disk, file.iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
setBit(file.iNode);
while (next) {
rdRtn = readBlock(dInfo.disk, next, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
fileExtentFormat *fileExtent = (fileExtentFormat *)blockBuffer;
fileExtent->blockType = 4;
int wtRtn = writeBlock(dInfo.disk, next, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
setBit(next);
next = fileExtent->nextFileExtent;
}
rdRtn = readBlock(dInfo.disk, 0, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
superBlockFormat *super = (superBlockFormat *)blockBuffer;
next = super->firstINode;
int block = 0;
while (next != file.iNode) {
rdRtn = readBlock(dInfo.disk, next, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iFormat = (iNodeFormat*)blockBuffer;
block = next;
next = iFormat->nextINode;
}
if (block) {
iFormat->nextINode = nextINode;
int wtRtn = writeBlock(dInfo.disk, block, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
}
else {
super->firstINode = nextINode;
int wtRtn = writeBlock(dInfo.disk, 0, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
}
rtn = tfs_closeFile(FD);
}
return rtn;
}
int tfs_readByte(fileDescriptor FD, char *buffer){
int rtn = 0;
fileTableEntry file = fileTable[FD];
if (!file.valid) {
rtn = FSBADFD;
}
else {
int readLoc = file.offset;
int blocks = 0;
int rdRtn = readBlock(dInfo.disk, file.iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iNode = (iNodeFormat *)blockBuffer;
if(iNode->fileSize == 0){
return FILENOTWRIT;
}
if (iNode->fileSize <= readLoc) {
return rtn = EOFREACH;
}
if(readLoc + sizeof(iNodeFormat) < BLOCKSIZE)
readLoc += sizeof(iNodeFormat);
else{
readLoc -= (BLOCKSIZE - sizeof(iNodeFormat));
//readLoc += sizeof(fileExtentFormat);
blocks = readLoc / (BLOCKSIZE - sizeof(fileExtentFormat));
blocks++;
readLoc = readLoc % (BLOCKSIZE - sizeof(fileExtentFormat));
}
if (blocks) {
readLoc += sizeof(fileExtentFormat); //fileExtent header size in bytes
rdRtn = readBlock(dInfo.disk, file.iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNode = (iNodeFormat *)blockBuffer;
int i, next = iNode->nextFileExtent;
fileExtentFormat *fileExtent;
for (i = 0; i < (blocks); i++) {
rdRtn = readBlock(dInfo.disk, next, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
fileExtent = (fileExtentFormat *)blockBuffer;
next = fileExtent->nextFileExtent;
}
memcpy(buffer,blockBuffer + readLoc, 1);
}
//data is in the iNode block
else {
rdRtn = readBlock(dInfo.disk, file.iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
memcpy(buffer,blockBuffer + readLoc, 1);
}
file.offset++;
rtn = 1;
}
fileTable[FD] = file;
setLastAccess(FD);
return rtn;
}
int tfs_seek(fileDescriptor FD, int offset){
int rtn = 0;
fileTableEntry file = fileTable[FD];
if (!file.valid) {
rtn = FSBADFD;
}
else {
file.offset = offset;
rtn = 1;
}
return rtn;
}
int openFile(int iNode, char *name) {
fileDescriptor fd = 0;
//check if no open space in table, then make new one
if (openFDs == NULL) {
fd = numOpenFiles;
}
else {
fd = openFDs->fd;
openFDs = openFDs->next;
}
(fileTable[fd]).iNode = iNode;
(fileTable[fd]).offset = 0;
(fileTable[fd]).valid = 1;
strcpy((fileTable[fd]).filename, name);
numOpenFiles++;
return fd;
}
int fileIsOpen(char *name) {
int fd = 0, i = 0;
//check through fileTable
while (i != numOpenFiles) {
if (fileTable[i].valid == 1) {
if (!strcmp(name, fileTable[i].filename)) {
fd = i;
break;
}
i++;
}
}
return fd;
}
int fileOnFS(char *name) {
int iNode = 0, next = 0;
//check through iNodes
//return the iNode it is on
//get the superblock
int rdRtn = readBlock(dInfo.disk, 0, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
superBlockFormat *superBlock = (superBlockFormat *)blockBuffer;
next = (int)(superBlock->firstINode);
while (next) {
rdRtn = readBlock(dInfo.disk, next, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iFormat = (iNodeFormat *)blockBuffer;
if (!strcmp(iFormat->filename, name)) {
iNode = next;
break;
}
next = (int)(iFormat->nextINode);
}
return iNode;
}
int spaceOnFS() {
int iNode = 0, i, j;
int numBlocks = DEFAULT_DISK_SIZE / BLOCKSIZE;
int numBytes = 5; //numBlocks / 8;
int rdRtn = readBlock(dInfo.disk, 0, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
superBlockFormat *superBlock = (superBlockFormat *)blockBuffer;
//first open on freeListBitVector, or zero
for (i = 0; i < numBytes; i++) { // <= instead of < ?
//skip through if all full
for(j = 0; j < 8; j++) {
if (((superBlock->freeListBitVector[i] >> (7-j)) & 1)) {
iNode = (i * 8) + j;
clearBit(iNode);
return iNode;
}
}
}
return iNode;
}
/* renames a file. New name should be passed in. */
int tfs_rename(fileDescriptor FD, char *newName) {
if (strlen(newName) > MAX_FILE_NAME_LENGTH) {
return FILENAMETOOLONG;
}
int iNodeLoc = fileTable[FD].iNode;
int rdRtn = readBlock(dInfo.disk, iNodeLoc, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iFormat = (iNodeFormat*)blockBuffer;
strcpy((fileTable[FD]).filename, newName);
strcpy(iFormat->filename, newName);
int wtRtn = writeBlock(dInfo.disk, iNodeLoc, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
return 1;
}
/* lists all the files and directories on the disk */
int tfs_readdir() {
int rdRtn = readBlock(dInfo.disk, 0, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
superBlockFormat *super = (superBlockFormat *)blockBuffer;
int next = super->firstINode;
while(next) {
int rdRtn = readBlock(dInfo.disk, next, blockBuffer);
iNodeFormat *iFormat = (iNodeFormat *)blockBuffer;
next = iFormat->nextINode;
fprintf(stdout, "File name: %-8s \tFile Size: %d\n", iFormat->filename, iFormat->fileSize);
}
}
struct timeval *tfs_readFileInfo(fileDescriptor FD) {
int rdRtn = readBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (rdRtn < 0) {
return NULL;
}
iNodeFormat *iFormat = (iNodeFormat *)blockBuffer;
struct timeval *timestamps = calloc(3, sizeof(struct timeval));
timestamps[0] = iFormat->created;
timestamps[1] = iFormat->lastModified;
timestamps[2] = iFormat->lastAccess;
return timestamps;
}
int setCreated(fileDescriptor FD) {
int rdRtn = readBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iFormat = (iNodeFormat *)blockBuffer;
struct timeval timestamp;
gettimeofday(×tamp, NULL);
iFormat->created = timestamp;
int wtRtn = writeBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
return 1;
}
int setLastModified(fileDescriptor FD) {
int rdRtn = readBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iFormat = (iNodeFormat *)blockBuffer;
struct timeval timestamp;
gettimeofday(×tamp, NULL);
iFormat->lastModified = timestamp;
//printf("Last Modified: %ld.%06ld\n", iFormat->lastModified.tv_sec,iFormat->lastModified.tv_usec);
int wtRtn = writeBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
return 1;
}
int setLastAccess(fileDescriptor FD) {
int rdRtn = readBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (rdRtn < 0) {
return rdRtn;
}
iNodeFormat *iFormat = (iNodeFormat *)blockBuffer;
struct timeval timestamp;
gettimeofday(×tamp, NULL);
iFormat->lastAccess = timestamp;
int wtRtn = writeBlock(dInfo.disk, fileTable[FD].iNode, blockBuffer);
if (wtRtn < 0) {
return wtRtn;
}
return 1;
}
/* to-do list
what to do with error codes? -> print
file descriptor table (contains offset pointer)
*/
/*questions:
how to implement error codes? -> print?
is it unmountable if superblock does
not have magic number? -> superblock has
magic number, number free blocks is correct/where
they are supposed to be
max number of blocks per disk? yes
//should unmount close the disk?
//max number of open disks?
*/