-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework.c
executable file
·2576 lines (1867 loc) · 63.2 KB
/
homework.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
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
/*
* file: homework.c
* description: skeleton file for CS 7600 homework 3
*
* CS 7600, Intensive Computer Systems, Northeastern CCIS
* Peter Desnoyers, November 2015
*/
#define FUSE_USE_VERSION 27
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <fuse.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "fs7600.h"
#include "blkdev.h"
extern int homework_part; /* set by '-part n' command-line option */
/*
* disk access - the global variable 'disk' points to a blkdev
* structure which has been initialized to access the image file.
*
* NOTE - blkdev access is in terms of 1024-byte blocks
*/
extern struct blkdev *disk;
/*
* cache - you'll need to create a blkdev which "wraps" this one
* and performs LRU caching with write-back.
*/
uint32_t clean_valid [30];
int clean_b_num[30];
int clean_age[30];
char * clean_b_data[30];
uint32_t dirty_valid[10];
int dirty_b_num[10];
int dirty_age[10];
char * dirty_b_data[10];
void b_cache_initialize(){
printf("------ in b_cache_initialize function\n");
int i = 0 ;
for(i = 0 ; i < 30; i++){
clean_b_num[i] = -1;
clean_valid[i] = 0;
clean_age[i] = 0;
}
for (i = 0 ; i < 10 ; i++){
dirty_b_num[i] = -1;
dirty_valid[i] = 0;
dirty_age[i] = 0;
}
}
void clean_update_age(int cache_index){
printf("------ in clean_update_age, cache_index is %d\n", cache_index);
int i;
for (i = 0 ; i < 30; i++){
if (clean_valid[i] == 1){
if (i == cache_index){
clean_age[i] = 1;
}else{
clean_age[i]++;
}
}
}
}
void dirty_update_age(int cache_index){
printf("------ in dirty_update_age, cache_index is %d\n", cache_index);
int i;
for (i = 0 ; i < 10; i++){
if (dirty_valid[i] == 1){
if (i == cache_index){
dirty_age[i] = 1;
}else{
dirty_age[i]++;
}
}
}
}
int clean_get_cache_index(int block_index){
printf("------ in clean_get_cache_index, block_index is %d\n", block_index);
int i;
for(i = 0 ; i < 30; i ++ ){
if (clean_valid[i] == 1 && clean_b_num[i] == block_index){
clean_update_age(i);
return i ;
}
}
return -1;
}
int dirty_get_cache_index(int block_index){
printf("------ in dirty_get_cache_index, block_index is %d\n", block_index);
int i;
for(i = 0 ; i < 10; i++){
if (dirty_valid[i] == 1 && dirty_b_num[i] == block_index){
dirty_update_age(i);
return i;
}
}
return -1;
}
void clean_insert(int block_index , char * data){
printf("------ in clean_insert function, block_index is %d\n", block_index);
int i;
for (i = 0 ; i < 30; i++){
if(clean_valid[i] == 0){
clean_valid[i] = 1;
clean_b_num[i] = block_index;
if (clean_b_data[i] == NULL){
free( clean_b_data[i]);
}
clean_b_data[i] = malloc(FS_BLOCK_SIZE);
memcpy(clean_b_data[i] , data, FS_BLOCK_SIZE);
clean_update_age(i);
printf("------ insert in free index %d\n", i);
return;
}
}
printf("------ no free space on clean cache");
int free_index = 0;
int max_age = 0;
for (i = 0 ; i < 30; i++){
if (clean_age[i] > max_age){
free_index = i;
max_age = clean_age[i];
}
}
clean_b_num[ free_index] = block_index;
clean_valid[ free_index] = 1;
if (clean_b_data[free_index] != NULL)
free(clean_b_data[free_index]);
clean_b_data[free_index ] = malloc(FS_BLOCK_SIZE);
memcpy(clean_b_data[free_index], data, FS_BLOCK_SIZE);
clean_update_age( free_index);
return ;
}
int dirty_insert ( int block_index, char * data, char ** old_data){
printf ("------ in dirty_insert function, block_index is %d\n", block_index );
int i;
for (i = 0; i < 10; i++){
if (dirty_valid[i] == 0){
printf("------ index %d is empty\n", i);
dirty_valid[i] = 1;
dirty_b_num[i] = block_index;
if (dirty_b_data[i] != NULL)
free(dirty_b_data[i]);
dirty_b_data[i] = malloc(FS_BLOCK_SIZE);
memcpy(dirty_b_data[i], data, FS_BLOCK_SIZE);
dirty_update_age(i);
return -1;
}
}
printf("------ cache is full\n");
int free_index = 0;
int max_age = 0;
for (i = 0 ; i < 10; i++){
if (dirty_age[i] > max_age){
max_age = dirty_age[i];
free_index = i;
}
}
printf ("------ free index is %d\n", free_index);
int old_block_index = dirty_b_num[free_index];
* old_data = dirty_b_data[free_index];
// memset(old_data, 0, FS_BLOCK_SIZE);
//memcpy(old_data, dirty_b_data[free_index], FS_BLOCK_SIZE);
dirty_valid[free_index] = 1;
dirty_b_num[free_index] = block_index;
dirty_b_data[free_index] = malloc (FS_BLOCK_SIZE);
memcpy (dirty_b_data[free_index], data, FS_BLOCK_SIZE);
dirty_update_age(free_index);
return old_block_index;
}
void clean_delete_by_cache_index (int cache_index ){
printf("------ clean_delete_by_cache_index, cache_index %d\n", cache_index);
clean_b_num [cache_index] = -1;
clean_valid [cache_index] = 0 ;
clean_age [cache_index] = 0;
if ( clean_b_data [cache_index] != NULL)
free( clean_b_data [cache_index]);
return;
}
void cache_read_by_block(struct blkdev * dev, int block_index , char * buf){
printf("------ cache_read_by_block\n");
int clean_cache_index = clean_get_cache_index(block_index);
if (clean_cache_index >= 0){
memcpy(buf, clean_b_data[clean_cache_index] , FS_BLOCK_SIZE);
return;
}
printf("------ clean_cache_index should be -1, %d\n", clean_cache_index);
int dirty_cache_index = dirty_get_cache_index(block_index);
if(dirty_cache_index >=0 ){
memcpy(buf, dirty_b_data[dirty_cache_index], FS_BLOCK_SIZE);
return;
}
printf("------ dirty_cache_index should be -1, %d\n", dirty_cache_index);
struct blkdev * old_disk = dev->private;
char * data = malloc(FS_BLOCK_SIZE);
old_disk->ops->read(old_disk, block_index, 1, data );
clean_insert(block_index, data);
memcpy(buf, data, FS_BLOCK_SIZE);
free(data);
}
void cache_write_by_block(struct blkdev * dev, int block_index, char * buf){
printf("------ cache_write_by_block, block_index is %d \n", block_index);
int clean_cache_index = clean_get_cache_index(block_index);
printf("------ clean_cache_index is %d\n", clean_cache_index);
if ( clean_cache_index >= 0 ){
clean_delete_by_cache_index( clean_cache_index);
char * old_data;
int old_b_index = dirty_insert(block_index, buf, &old_data);
if (old_b_index != -1 && old_data != NULL){
struct blkdev * old_disk = dev->private;
old_disk->ops->write(old_disk, old_b_index, 1, old_data );
free(old_data);
}
return;
}
printf("------ clean_cache_index should be -1, %d\n", clean_cache_index);
int dirty_cache_index = dirty_get_cache_index(block_index);
printf("------ dirty_cache_index is %d\n", dirty_cache_index);
if ( dirty_cache_index >= 0){
memset(dirty_b_data[ dirty_cache_index ], 0, FS_BLOCK_SIZE);
memcpy (dirty_b_data[ dirty_cache_index ] , buf, FS_BLOCK_SIZE);
return;
}
// insert to dirty
// maybe write evicted block
char * old_data;
int old_b_index = dirty_insert(block_index, buf, & old_data);
if (old_b_index != -1 && old_data != NULL){
struct blkdev * old_disk = dev->private;
old_disk->ops->write(old_disk, old_b_index, 1, old_data );
free(old_data);
}
return;
}
int cache_nops(struct blkdev *dev)
{
struct blkdev *d = dev->private;
return d->ops->num_blocks(d);
}
int cache_read(struct blkdev *dev, int first, int n, void *buf)
{
printf("------ in cache_read function, first is %d, n is %d\n", first, n);
int i ;
int offset = 0;
for ( i = first ; i < (first + n) ; i++ ){
// struct blkdev * old_disk = dev->private;
// old_disk->ops->read(old_disk, i, 1, ((char *) buf) + offset );
cache_read_by_block(dev, i, ((char * ) buf) + offset);
offset += FS_BLOCK_SIZE;
}
//struct blkdev * old_disk = dev->private;
//old_disk->ops->read(old_disk, first, n, buf);
return SUCCESS;
}
int cache_write(struct blkdev *dev, int first, int n, void *buf)
{
printf("------ in cache_write function, first is %d, n is %d\n", first, n);
int i ;
int offset = 0;
/**/
for ( i = first ; i < (first + n) ; i++ ){
cache_write_by_block(dev, i,((char * )buf) + offset);
//struct blkdev * old_disk = dev->private;
//old_disk->ops->write(old_disk, i, 1, buf + offset );
offset += FS_BLOCK_SIZE;
}
/**/
// struct blkdev * old_disk = dev->private;
// old_disk->ops->write(old_disk, first, n, buf);
return SUCCESS;
}
struct blkdev_ops cache_ops = {
.num_blocks = cache_nops,
.read = cache_read,
.write = cache_write
};
struct blkdev *cache_create(struct blkdev *d)
{
b_cache_initialize();
struct blkdev *dev = malloc(sizeof(*d));
dev->ops = &cache_ops;
dev->private = d;
return dev;
}
/* by defining bitmaps as 'fd_set' pointers, you can use existing
* macros to handle them.
* FD_ISSET(##, inode_map);
* FD_CLR(##, block_map);
* FD_SET(##, block_map);
*/
fd_set *inode_map; /* = malloc(sb.inode_map_size * FS_BLOCK_SIZE); */
fd_set *block_map;
struct fs7600_inode * inodes;
struct fs7600_super sb;
int max_num_inodes;
//------------------------------------------------------------------
char * cache_path[50];
int cache_inode[50];
int cache_age [50];
uint32_t cache_valid[50];
void cache_initialize(){
printf("------ cache_initialize\n");
int i;
for (i = 0 ; i < 50 ; i++){
cache_inode[i] = -1;
cache_age[i] = 0;
cache_valid[i] = 0;
}
}
void cache_update_age(int cache_index ){
printf("------ cache_update_age, cache_index is %d\n", cache_index);
int i ;
for (i = 0 ; i < 50; i ++){
if ( cache_valid [i] == 1 && i == cache_index){
cache_age[i] = 1;
}else if (cache_valid [i] == 1 && cache_inode[i] > 0 ){
cache_age[i] ++ ;
}
}
}
int cache_get_inode(const char * path){
printf("------ cache_get_inode, path is %s\n", path);
int i ;
for (i = 0 ; i < 50; i++){
if (cache_valid[i] == 1 && cache_path[i] != NULL){
if ( strcmp(cache_path[i], path) == 0 ){
printf("------ cache index is %d, inode is %d\n", i, cache_inode[i]);
cache_update_age(i);
return cache_inode[i];
}
}
}
return -1;
}
void cache_insert(const char * path, int inode){
printf("------ cache_insert, path is %s, inode is %d\n", path, inode);
if (inode == 0)
printf("------ it is odd to have inode 0\n");
int i ;
for (i = 0 ; i < 50; i++){
if (cache_valid [i] == 0 ){
printf("------ cache has empty space at index %d\n", i);
cache_valid[i] = 1;
cache_inode[i] = inode;
cache_update_age(i);
if ( cache_path[i] != NULL){
free(cache_path[i]);
//cache_path[i] = NULL:
}
cache_path[i] = strdup(path);
return;
}
}
printf("------ cache is full \n");
int free_index = 0;
int max = 0;
for (i = 0 ; i < 50; i++){
if (cache_age[i] > max){
max = cache_age[i];
free_index = i;
}
}
printf("------ free index is %d\n", free_index);
cache_inode[free_index] = inode;
cache_update_age(free_index);
if ( cache_path[free_index] != NULL){
free(cache_path[free_index]);
}
cache_path[free_index] = strdup(path);
return;
}
void cache_delete(int inode){
printf("------ cache_delete, inode is %d\n", inode);
int i;
for (i = 0 ; i < 50; i++){
if (cache_valid [i] == 1 && cache_inode[i] == inode){
printf("------ cache_index %d is delete\n", i);
cache_valid[i] = 0;
cache_inode[i] = -1;
cache_age[i] = 0;
}
}
}
//------------------------------------------------------------------
void update_disk_block(int block_index , int num_block, void * buff){
printf("------ in update_disk_block function\n");
if (disk->ops->write(disk, block_index, num_block, buff) < 0)
exit(0);
}
int get_inode_block_index(int inode_index){
printf("------ in get_inode_block_index function, inode_index is %d\n", inode_index);
int inodes_per_block = FS_BLOCK_SIZE / (sizeof(struct fs7600_inode ));
//printf("------ inodes_per_block is %d\n", inodes_per_block);
int block_index = inode_index / inodes_per_block;
block_index += (1 + sb.inode_map_sz + sb.block_map_sz);
//printf("------ block_index is %d\n", block_index );
return block_index;
}
void * get_inode_block_data(int inode_index){
printf("------ in get_inode_block_data function, inode_index is %d\n", inode_index);
int inodes_per_block = FS_BLOCK_SIZE / (sizeof(struct fs7600_inode ));
//printf("------ inodes_per_block is %d\n", inodes_per_block);
int block_index = inode_index / inodes_per_block;
void * block_data = malloc(FS_BLOCK_SIZE);
int offset = block_index * FS_BLOCK_SIZE;
//printf("------ offset is %d", offset);
memcpy(block_data,( (char *) inodes) + offset , FS_BLOCK_SIZE);
}
void update_disk(int inode_index){
printf ("------ in update_disk function\n");
// write inode bitmap // no need to do this!
// write block bitmap
// write inodes
if (disk->ops->write(disk, 1, sb.inode_map_sz, inode_map) < 0)
exit(0);
if (disk->ops->write(disk, 1 + sb.inode_map_sz, sb.block_map_sz, block_map) < 0)
exit(0);
//if (disk->ops->write(disk, 1 + sb.inode_map_sz + sb.block_map_sz, sb.inode_region_sz, inodes) < 0)
// exit(0);
int inode_block_index = get_inode_block_index(inode_index);
void * inode_block_data = get_inode_block_data(inode_index);
update_disk_block(inode_block_index, 1, inode_block_data );
free(inode_block_data);
}
/**/
/**/
/* init - this is called once by the FUSE framework at startup. Ignore
* the 'conn' argument.
* recommended actions:
* - read superblock
* - allocate memory, read bitmaps and inodes
*/
void* fs_init(struct fuse_conn_info *conn)
{
printf("------ in fs_init function\n");
printf("------ homework part is %d\n", homework_part);
//struct fs7600_super sb;
// it is global now
if (disk->ops->read(disk, 0, 1, &sb) < 0)
exit(1);
printf("--------------------------------------------------\n");
printf("root inode is %d \n", sb.root_inode) ;
printf("inode map size is %d \n", sb.inode_map_sz);
printf("block map size is %d \n", sb.block_map_sz);
printf("inode region size is %d \n", sb.inode_region_sz);
printf("num of blocks are %d \n",sb.num_blocks );
inode_map = (fd_set *) malloc(sb.inode_map_sz * FS_BLOCK_SIZE);
block_map = (fd_set *) malloc(sb.block_map_sz * FS_BLOCK_SIZE);
inodes = ( struct fs7600_inode * ) malloc(sb.inode_region_sz * FS_BLOCK_SIZE);
max_num_inodes = (sb.inode_region_sz * FS_BLOCK_SIZE)/ ( sizeof(struct fs7600_inode));
printf("max_num_inodes %d \n", max_num_inodes);
if (disk->ops->read(disk, 1, sb.inode_map_sz, inode_map) < 0)
exit(1);
if (disk->ops->read(disk, 1 + sb.inode_map_sz, sb.block_map_sz, block_map) < 0)
exit(1);
if (disk->ops->read(disk, 1 + sb.inode_map_sz + sb.block_map_sz, sb.inode_region_sz ,inodes ) < 0)
exit(1);
printf("--------------------------------------------------\n");
if (homework_part > 2){
cache_initialize();
}
if (homework_part > 3)
disk = cache_create(disk);
return NULL;
}
//---------------------------------------------------------------------
void fill_stat(struct stat * sb, int inode_index){
printf("------ in fill_stat function, inode index is %d\n", inode_index);
memset(sb,0,sizeof(sb));
sb -> st_uid = inodes[inode_index].uid;
sb -> st_gid = inodes[inode_index].gid;
sb -> st_mode = inodes[inode_index].mode;
sb -> st_ctime = inodes[inode_index].mtime;
sb -> st_mtime = inodes[inode_index].mtime;
sb -> st_atime = inodes[inode_index].mtime;
sb -> st_size = inodes[inode_index].size;
sb -> st_nlink = 1;
sb -> st_ino = inode_index;
}
/* Note on path translation errors:
* In addition to the method-specific errors listed below, almost
* every method can return one of the following errors if it fails to
* locate a file or directory corresponding to a specified path.
*
* ENOENT - a component of the path is not present.
* ENOTDIR - an intermediate component of the path (e.g. 'b' in
* /a/b/c) is not a directory
*/
/* note on splitting the 'path' variable:
* the value passed in by the FUSE framework is declared as 'const',
* which means you can't modify it. The standard mechanisms for
* splitting strings in C (strtok, strsep) modify the string in place,
* so you have to copy the string and then free the copy when you're
* done. One way of doing this:
*
* char *_path = strdup(path);
* int inum = translate(_path);
* free(_path);
*/
/* getattr - get file or directory attributes. For a description of
* the fields in 'struct stat', see 'man lstat'.
*
* Note - fields not provided in CS7600fs are:
* st_nlink - always set to 1
* st_atime, st_ctime - set to same value as st_mtime
*
* errors - path translation, ENOENT
*/
static int fs_getattr(const char *path, struct stat *sb)
{
printf ("------ in fs_getattr function, path is %s\n", path);
char * _path = strdup(path);
int inode_index = translate(_path);
free(_path);
printf("------ inode_index is %d\n",inode_index);
if (inode_index <0){
return -ENOENT;
}else {
fill_stat(sb,inode_index);
return 0;
}
}
// in translate function, path is given and it returns inode number
int translate(char* _path){
printf("------ in translate function, path is %s\n", _path);
char * path_cache = strdup(_path);
int inode_index ;
//----------------------
if (homework_part > 2){
inode_index = cache_get_inode(_path);
if (inode_index > 0)
return inode_index;
printf("------ _path %s is not in cache\n", _path);
}
//----------------------
const char delimiters[2] = "/";
char * token = strtok(_path, delimiters);
// printf("------- token is %s\n", token);
inode_index = 1;// sb.root_inode
int index;
//printf("--- %s %d\n", token, inode_index);
while(token != NULL){
int block_num = inodes[inode_index].direct[0];
// printf("--- %d\n", block_num);
struct fs7600_dirent * entries = malloc(FS_BLOCK_SIZE);
if(disk->ops->read(disk,block_num, 1, entries ) < 0 )
exit(0);
index = find_index(token,entries );
// printf("index in the entries %d ", index);
if (index < 0 ){
return -ENOENT;
}else{
inode_index = entries[index].inode;
}
// printf("--- this is inode index %s %d\n", token, inode_index);
token = strtok(NULL, delimiters);
free(entries);
}
//-------------------------------------
if (homework_part > 2){
printf("------ in translate function, _path is %s before inserting in cache\n", _path);
cache_insert(path_cache, inode_index);
free(path_cache);
}
//-------------------------------------
return inode_index;
}
/*
find_index function get a name file or directory/ folder and search in the entries array,
if it finds a valid entry with the same name, then return the index of that entry in the array.
*/
int find_index (const char * name, struct fs7600_dirent * entries ){
printf("------ in find_index function, name is %s\n", name);
int i ;
// the assumption is that directories have 32 entries
for ( i = 0 ; i < 32 ; i++){
// printf("----%d %d %s %s\n",i, entries[i].inode, entries[i].name , name);
if ( entries[i].valid && (strcmp( entries[i].name, name) == 0) ){
return i;
}
}
return -1;
}
/**
uint32_t is_dir_f (char * _path ){
printf("------ in is_dir_f function, path is %s\n", _path);
uint32_t is_dir = 1;
const char delimiters[2] = "/";
char * token = strtok(_path, delimiters);
int inode_index = 1;
int index;
while(token != NULL){
int block_num = inodes[inode_index].direct[0];
struct fs7600_dirent * entries = malloc(FS_BLOCK_SIZE);
if(disk->ops->read(disk,block_num, 1, entries ) < 0 )
exit(0);
index = find_index(token,entries );
if (index < 0 ){
return 0;
}else{
inode_index = entries[index].inode;
is_dir = entries[index].isDir;
}
token = strtok(NULL, delimiters);
}
return is_dir;
}
/**/
/* readdir - get directory contents.
*
* for each entry in the directory, invoke the 'filler' function,
* which is passed as a function pointer, as follows:
* filler(buf, <name>, <statbuf>, 0)
* where <statbuf> is a struct stat, just like in getattr.
*
* Errors - path resolution, ENOTDIR, ENOENT
*/
static int fs_readdir(const char *path, void *ptr, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
printf("------ in readdir function, path is %s\n", path);
int inode_index;
if (homework_part >= 2 ){
inode_index = fi->fh;
}
else {
char * _path = strdup(path);
inode_index = translate(_path);
free(_path);
}
int block_num = inodes[inode_index].direct[0];
// printf("--- %d\n", block_num);
struct fs7600_dirent * entries = malloc(FS_BLOCK_SIZE);
if(disk->ops->read(disk,block_num, 1, entries ) < 0 )
exit(0);
int i;
for (i = 0 ; i < 32; i++){
if (entries[i].valid){
// was
//filler(ptr, entries[i].name, NULL, 0 );
//struct stat * sb = malloc(sizeof( struct stat));
//fill_stat(sb,entries[i].inode);
//filler(ptr, entries[i].name, sb, 0 );
struct stat sb;
fill_stat( &sb, entries[i].inode);
filler(ptr, entries[i].name, &sb, 0);
}
}
free(entries);
return 0;
}
/* see description of Part 2. In particular, you can save information
* in fi->fh. If you allocate memory, free it in fs_releasedir.
*/
static int fs_opendir(const char *path, struct fuse_file_info *fi)
{
printf("------ in fs_opendir function, path is %s\n", path);
/**/
if(homework_part >= 2){
printf("------ opendir, part2\n");
char * _path = strdup(path);
int inode_index = translate(_path);
free(_path);
fi->fh = inode_index;
}
/**/
/**
if (homework_part == 3){
printf("------ opendir, part3\n");
char * _path = strdup(path);
int inode_index = translate(_path);
free(_path);
fi->fh = inode_index;
}
/**/
return 0;
}
static int fs_releasedir(const char *path, struct fuse_file_info *fi)
{
printf("------ in fs_releasedir function, path is %s\n", path);
return 0;
}
//-----------------------------------------------------------------------------
//
int get_parent_inode(const char* path){
printf("------ in get_parent_inode function, path is %s\n", path);
char * _path = strdup(path);
const char delimiters[2] = "/";
char * token = strtok(_path, delimiters);
int parent_inode_index = 0;
int inode_index = 1;
//printf("---token is %s\n", token);
while(token != NULL){
parent_inode_index = inode_index;
int block_num = inodes[inode_index].direct[0];
// printf("--- %d\n", block_num);
struct fs7600_dirent * entries = malloc(FS_BLOCK_SIZE);
if(disk->ops->read(disk,block_num, 1, entries ) < 0 )
exit(0);
int index = find_index(token,entries );
token = strtok(NULL, delimiters);
// printf("---index in the entries %d\n, token is %s\n", index, token);
// if (index < 0 && token != NULL){
// return -ENOTDIR;
// }else if (index < 0){
// return parent_inode_index;
// }else{
inode_index = entries[index].inode;
// }
// printf("--- this is inode index %s %d\n", token, inode_index);
}
free(_path);
// if (inode_index > 0)
// return -EEXIST;
return parent_inode_index;
}
int get_empty_entry(int inode_index){
printf("------ in get_empty_entry function, inode_index is %d\n", inode_index);
int block_num = inodes[inode_index].direct[0];
struct fs7600_dirent * entries = malloc(FS_BLOCK_SIZE);
if(disk->ops->read(disk,block_num, 1, entries ) < 0 )
exit(0);
/*
// printf("------ name of entry 0: %s, is valid %d \n", entries[0].name, entries[0].valid );
// printf("------ name of entry 1: %s, is valid %d \n", entries[1].name, entries[1].valid );
// printf("------ name of entry 2: %s, is valid %d \n", entries[2].name, entries[2].valid );
printf("------ name of entry 3: %s, is valid %d \n", entries[3].name, entries[3].valid );
printf("------ name of entry 4: %s, is valid %d \n", entries[4].name, entries[4].valid );
printf("------ name of entry 5: %s, is valid %d \n", entries[5].name, entries[5].valid );
/**/
int i ;
for (i = 0 ; i < 32 ; i++ ){
// printf("------ name of entery : %s\n", entries[i].name);
if ( !entries[i].valid ){
return i;
}
}
return -1;
}
int get_empty_inode_num(){
printf("------ in get_empty_inode_num function\n");
int max_limit = max_num_inodes;
int i ;
for (i = 1; i < max_limit; i++){
if( ! FD_ISSET(i,inode_map)){
return i ;
}
}
return -1;
}
int get_empty_datablk_num(){
printf("------ in get_empty_datablk_num function \n");
int max_limit = sb.num_blocks ;
int i = 1 + sb.inode_map_sz + sb.block_map_sz + sb.inode_region_sz ;
for ( ; i < max_limit ; i ++){