@@ -335,7 +335,7 @@ static int vfs_mkdir(const char *path, mode_t mode) {
335
335
}
336
336
}
337
337
338
- // TODO: Out of memory message?
338
+ // Out of memory message?
339
339
return -1 ;
340
340
}
341
341
@@ -523,7 +523,6 @@ static int vfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
523
523
}
524
524
}
525
525
526
- // TODO: Out of memory message?
527
526
return -1 ;
528
527
}
529
528
@@ -825,7 +824,6 @@ file_loc get_file(char *abs_path, char *path, file_loc parent) {
825
824
}
826
825
827
826
// Read data into a Dnode struct
828
- // TODO when implementing multi-directory, this will change
829
827
dnode thisDnode = get_dnode (parent .node_block .block , buf );
830
828
831
829
// look at the right snippet of the path
@@ -977,8 +975,7 @@ int write_dnode(unsigned int b, char *buf, dnode d) {
977
975
}
978
976
979
977
// Reads the inode at the given block number
980
- inode get_inode (unsigned int b ) {
981
- char buf [BLOCKSIZE ];
978
+ inode get_inode (unsigned int b , char * buf ) {
982
979
// Allocate appropriate memory
983
980
memset (buf , 0 , BLOCKSIZE );
984
981
// Read Dnode
@@ -990,6 +987,7 @@ inode get_inode(unsigned int b) {
990
987
}
991
988
992
989
// Write the given inode (i) to disk at the given block (b)
990
+ // The passed in buf should be of size BLOCKSIZE
993
991
int write_inode (unsigned int b , char * buf , inode i ) {
994
992
// Allocate appropriate memory
995
993
memset (buf , 0 , BLOCKSIZE );
@@ -1004,6 +1002,7 @@ int write_inode(unsigned int b, char *buf, inode i) {
1004
1002
}
1005
1003
1006
1004
// Reads the db at the given block number into buf
1005
+ // The passed in buf should be of size BLOCKSIZE
1007
1006
db get_db (unsigned int b , char * buf ) {
1008
1007
// Allocate appropriate memory
1009
1008
memset (buf , 0 , BLOCKSIZE );
@@ -1016,6 +1015,7 @@ db get_db(unsigned int b, char *buf) {
1016
1015
}
1017
1016
1018
1017
// Write the given db (d) to disk at the given block (b)
1018
+ // The passed in buf should be of size BLOCKSIZE
1019
1019
int write_db (unsigned int b , char * buf , db d ) {
1020
1020
// Allocate appropriate memory
1021
1021
memset (buf , 0 , BLOCKSIZE );
@@ -1030,6 +1030,7 @@ int write_db(unsigned int b, char *buf, db d) {
1030
1030
}
1031
1031
1032
1032
// Reads the indirect at the given block number into buf
1033
+ // The passed in buf should be of size BLOCKSIZE
1033
1034
indirect get_indirect2 (unsigned int b , char * buf ) {
1034
1035
// Allocate appropriate memory
1035
1036
memset (buf , 0 , BLOCKSIZE );
@@ -1042,6 +1043,7 @@ indirect get_indirect2(unsigned int b, char *buf) {
1042
1043
}
1043
1044
1044
1045
// Write the given indirect (i) to disk at the given block (b)
1046
+ // The passed in buf should be of size BLOCKSIZE
1045
1047
int write_indirect (unsigned int b , char * buf , indirect i ) {
1046
1048
// Allocate appropriate memory
1047
1049
memset (buf , 0 , BLOCKSIZE );
@@ -1095,6 +1097,7 @@ blocknum get_free() {
1095
1097
// If b is valid, it is the caller's responsibility to ensure that b is a
1096
1098
// blocknum to a dirent. The behavior if b is a valid blocknum to another
1097
1099
// structure type is undefined.
1100
+ // The passed in buf should be of size BLOCKSIZE
1098
1101
file_loc get_inode_dirent (blocknum b , char * buf , const char * path ) {
1099
1102
1100
1103
// the file_loc to return
@@ -1132,6 +1135,7 @@ file_loc get_inode_dirent(blocknum b, char *buf, const char *path) {
1132
1135
// Returns a file_loc to the file specified by path if it exists in the
1133
1136
// any dirent within the thisDnode.direct array. If not found, an invalid
1134
1137
// file_loc is returned.
1138
+ // The passed in buf should be of size BLOCKSIZE
1135
1139
file_loc get_inode_direct_dirent (dnode * thisDnode , char * buf , const char * path ) {
1136
1140
1137
1141
// the file_loc to return
@@ -1165,6 +1169,7 @@ file_loc get_inode_direct_dirent(dnode *thisDnode, char *buf, const char *path)
1165
1169
// If b is valid, it is the caller's responsibility to ensure that b is a
1166
1170
// blocknum to an indirect of dirents. The behavior if b is a valid blocknum
1167
1171
// to another structure type is undefined.
1172
+ // The passed in buf should be of size BLOCKSIZE
1168
1173
file_loc get_inode_single_indirect_dirent (blocknum b , char * buf , const char * path ) {
1169
1174
1170
1175
// the file_loc to return
@@ -1199,6 +1204,7 @@ file_loc get_inode_single_indirect_dirent(blocknum b, char *buf, const char *pat
1199
1204
// If b is valid, it is the caller's responsibility to ensure that b is a
1200
1205
// blocknum to an indirect of indirects of dirents. The behavior if b is a
1201
1206
// valid blocknum to another structure type is undefined.
1207
+ // The passed in buf should be of size BLOCKSIZE
1202
1208
file_loc get_inode_double_indirect_dirent (blocknum b , char * buf , const char * path ) {
1203
1209
1204
1210
// the file_loc to return
@@ -1227,18 +1233,6 @@ file_loc get_inode_double_indirect_dirent(blocknum b, char *buf, const char *pat
1227
1233
return loc ;
1228
1234
}
1229
1235
1230
- // TODO: Multiple things have these structs.. can we abstract by passing a param???
1231
- // Access Single_indirect
1232
- blocknum get_single_block (int loc ) {
1233
- // mod by 128 for single index
1234
- // divide by 128 for index in direct
1235
- }
1236
- // Access Double_indirect
1237
- blocknum get_double_block (int loc ) {
1238
- // mod by 128 for index in double single index
1239
- // call get_single with loc/128
1240
- }
1241
-
1242
1236
/*
1243
1237
* The function vfs_read provides the ability to read data from
1244
1238
* an absolute path 'path,' which should specify an existing file.
@@ -1264,11 +1258,10 @@ static int vfs_read(const char *path, char *buf, size_t size, off_t offset,
1264
1258
return -1 ;
1265
1259
}
1266
1260
1267
- // TODO use a helper function to get inode block
1268
1261
char tmp_buf [BLOCKSIZE ];
1269
1262
1270
1263
// Get the inode
1271
- inode this_inode = get_inode (loc .node_block .block );
1264
+ inode this_inode = get_inode (loc .node_block .block , tmp_buf );
1272
1265
1273
1266
// if the offset is larger than the size of the inode, no read possible
1274
1267
if (offset >= this_inode .size ) {
@@ -1395,11 +1388,10 @@ static int vfs_write(const char *path, const char *buf, size_t size,
1395
1388
return -1 ;
1396
1389
}
1397
1390
1398
- // TODO use a helper function to get inode block
1399
1391
char tmp_buf [BLOCKSIZE ];
1400
1392
1401
1393
// Get the inode
1402
- inode this_inode = get_inode (loc .node_block .block );
1394
+ inode this_inode = get_inode (loc .node_block .block , tmp_buf );
1403
1395
1404
1396
// calculate the extra bytes we need to write to the file
1405
1397
int needed_bytes = offset + size ;
@@ -1453,7 +1445,7 @@ static int vfs_write(const char *path, const char *buf, size_t size,
1453
1445
int loc_in_s = block_loc % indirect_blocks ;
1454
1446
blocknum sing_blocknum = doub .blocks [s_loc ];
1455
1447
1456
- // REMOVE THE NESCESSITY TO READ EVERYTIME
1448
+ // REMOVE THE NECESSITY TO READ EVERYTIME
1457
1449
indirect new_sing = get_indirect2 (sing_blocknum .block , tmp_buf );
1458
1450
all_blocks [i ] = new_sing .blocks [loc_in_s ];
1459
1451
}
@@ -1712,7 +1704,8 @@ static int vfs_delete(const char *path)
1712
1704
// Redult is stored in blocks array with size size.
1713
1705
void get_file_blocks (blocknum in , blocknum * blocks [], int * size ) {
1714
1706
// Free the inode; free the inode's db blocks; free the inodes
1715
- inode this_inode = get_inode (in .block );
1707
+ char tmp_buf [BLOCKSIZE ];
1708
+ inode this_inode = get_inode (in .block , tmp_buf );
1716
1709
1717
1710
// all the blocks; direct + single + double (single^2) + this file block
1718
1711
int all_blocks_size = NUM_DIRECT + NUM_INDIRECT_BLOCKS +
@@ -1794,15 +1787,15 @@ static int vfs_rename(const char *from, const char *to)
1794
1787
vfs_delete (to );
1795
1788
1796
1789
// Get the dirent and adjust the name...
1797
- // TODO: Should we abstarct this as well???
1798
1790
dirent this_dirent ;
1799
1791
char buf [BLOCKSIZE ];
1800
1792
dread (loc .dirent_block .block , buf );
1801
1793
memcpy (& this_dirent , buf , sizeof (dirent ));
1802
1794
1795
+ parent = get_root_dir ();
1796
+ file_loc to_loc = get_dir (to , to , & parent );
1803
1797
// Change the name at the proper direntry
1804
- // TODO: Change the +1
1805
- strncpy (this_dirent .entries [loc .direntry_idx ].name , to + 1 , MAX_FILENAME_LEN );
1798
+ strncpy (this_dirent .entries [loc .direntry_idx ].name , to_loc .name , MAX_FILENAME_LEN );
1806
1799
1807
1800
// Write modified one to disk
1808
1801
memcpy (buf , & this_dirent , sizeof (dirent ));
@@ -1814,7 +1807,6 @@ static int vfs_rename(const char *from, const char *to)
1814
1807
}
1815
1808
1816
1809
// No such file exists
1817
- // TODO: get this to work for directories
1818
1810
else {
1819
1811
return -1 ;
1820
1812
}
@@ -1841,14 +1833,10 @@ static int vfs_chmod(const char *file, mode_t mode)
1841
1833
// If the file exists, change mode
1842
1834
if (loc .valid ) {
1843
1835
// Get the inode for the file
1844
- // TODO: Should we abstarct this as well???
1845
- inode this_inode ;
1846
1836
char buf [BLOCKSIZE ];
1847
- dread (loc .node_block .block , buf );
1848
- memcpy (& this_inode , buf , sizeof (inode ));
1849
-
1850
- // Change shit up
1851
- this_inode .mode = (mode_t ) mode ;
1837
+ inode this_inode = get_inode (loc .node_block .block , buf );
1838
+ //update the mode
1839
+ this_inode .mode = (mode_t ) mode ;
1852
1840
1853
1841
// Write modified one to disk
1854
1842
memcpy (buf , & this_inode , sizeof (inode ));
@@ -1858,7 +1846,6 @@ static int vfs_chmod(const char *file, mode_t mode)
1858
1846
}
1859
1847
1860
1848
// No such file exists
1861
- // TODO: get this to work for directories
1862
1849
else {
1863
1850
return -1 ;
1864
1851
}
@@ -1938,7 +1925,6 @@ static int vfs_utimens(const char *file, const struct timespec ts[2])
1938
1925
}
1939
1926
1940
1927
// No such file exists
1941
- // TODO: get this to work for directories
1942
1928
else {
1943
1929
return -1 ;
1944
1930
}
@@ -1966,7 +1952,7 @@ static int vfs_truncate(const char *file, off_t offset)
1966
1952
1967
1953
char tmp_buf [BLOCKSIZE ];
1968
1954
// Get the inode
1969
- inode this_inode = get_inode (loc .node_block .block );
1955
+ inode this_inode = get_inode (loc .node_block .block , tmp_buf );
1970
1956
1971
1957
// check if the offset exceeds the file size
1972
1958
if (this_inode .size - 1 < offset ) {
0 commit comments