13 #include "sfs/sha256.h"
23 #define streq(a, b) (strcmp((a), (b)) == 0)
33 if(!mounted){
return false;}
34 if(MetaData.Protected)
return change_password();
38 char pass[1000], line[1000];
42 printf(
"Enter new password: ");
43 if (fgets(line, BUFSIZ, stdin) == NULL)
return false;
44 sscanf(line,
"%s", pass);
47 MetaData.Protected = 1;
48 strcpy(MetaData.PasswordHash,hasher(pass).c_str());
51 block.Super = MetaData;
52 fs_disk->write(0,block.
Data);
53 printf(
"New password set.\n");
63 if(!mounted){
return false;}
65 if(MetaData.Protected){
67 char pass[1000], line[1000];
71 printf(
"Enter current password: ");
72 if (fgets(line, BUFSIZ, stdin) == NULL)
return false;
73 sscanf(line,
"%s", pass);
76 if(hasher(pass) !=
string(MetaData.PasswordHash)){
77 printf(
"Old password incorrect.\n");
82 MetaData.Protected = 0;
92 if(!mounted){
return false;}
94 if(MetaData.Protected){
96 char pass[1000], line[1000];
101 printf(
"Enter old password: ");
102 if (fgets(line, BUFSIZ, stdin) == NULL)
return false;
103 sscanf(line,
"%s", pass);
106 if(hasher(pass) !=
string(MetaData.PasswordHash)){
107 printf(
"Old password incorrect.\n");
112 MetaData.Protected = 0;
115 block.Super = MetaData;
116 fs_disk->write(0,block.
Data);
117 printf(
"Password removed successfully.\n");
134 for(; idx < FileSystem::ENTRIES_PER_DIR; idx++){
139 if(idx == FileSystem::ENTRIES_PER_DIR){
140 printf(
"Directory entry limit reached..exiting\n");
147 tempdir.
Table[idx].type = type;
162 (offset >= ENTRIES_PER_DIR) ||
163 (curr_dir.Table[offset].valid == 0) ||
164 (curr_dir.Table[offset].type != 0)
165 ){
Directory temp; temp.Valid=0;
return temp;}
168 uint32_t inum = curr_dir.
Table[offset].
inum;
169 uint32_t block_idx = (inum / FileSystem::DIR_PER_BLOCK);
170 uint32_t block_offset = (inum % FileSystem::DIR_PER_BLOCK);
174 fs_disk->read(MetaData.Blocks - 1 - block_idx, blk.
Data);
184 uint32_t block_idx = (dir.
inum / FileSystem::DIR_PER_BLOCK) ;
185 uint32_t block_offset = (dir.
inum % FileSystem::DIR_PER_BLOCK);
189 fs_disk->read(MetaData.Blocks - 1 - block_idx, block.
Data);
193 fs_disk->write(MetaData.Blocks - 1 - block_idx, block.
Data);
203 for(;offset < FileSystem::ENTRIES_PER_DIR; offset++){
211 if(offset == FileSystem::ENTRIES_PER_DIR){
return -1;}
221 if(!mounted){
return false;}
224 int offset = dir_lookup(curr_dir,name);
225 if(offset == -1){printf(
"No such Directory\n");
return false;}
231 if(dir.Valid == 0){printf(
"Directory Invalid\n");
return false;}
234 printf(
" inum | name | type\n");
235 for(uint32_t idx=0; idx<FileSystem::ENTRIES_PER_DIR; idx++){
238 if(temp.type == 1) printf(
"%-10u | %-16s | %-5s\n",temp.
inum,temp.
Name,
"file");
239 else printf(
"%-10u | %-16s | %-5s\n",temp.
inum,temp.
Name,
"dir");
250 if(!mounted){
return false;}
253 uint32_t block_idx = 0;
254 for(;block_idx < MetaData.DirBlocks; block_idx++)
255 if(dir_counter[block_idx] < FileSystem::DIR_PER_BLOCK)
258 if(block_idx == MetaData.DirBlocks){printf(
"Directory limit reached\n");
return false;}
262 fs_disk->read(MetaData.Blocks - 1 - block_idx, block.
Data);
267 for(;offset < FileSystem::DIR_PER_BLOCK; offset++)
271 if(offset == DIR_PER_BLOCK){printf(
"Error in creating directory.\n");
return false;}
276 new_dir.
inum = block_idx*DIR_PER_BLOCK + offset;
278 strcpy(new_dir.
Name,name);
281 char tstr1[] =
".", tstr2[] =
"..";
283 temp = add_dir_entry(temp,temp.
inum,0,tstr1);
284 temp = add_dir_entry(temp,curr_dir.
inum,0,tstr2);
285 if(temp.Valid == 0){printf(
"Error creating new directory\n");
return false;}
289 temp = add_dir_entry(curr_dir,new_dir.
inum,0,new_dir.
Name);
290 if(temp.Valid == 0){printf(
"Error adding new directory\n");
return false;}
294 write_dir_back(new_dir);
297 write_dir_back(curr_dir);
300 dir_counter[block_idx]++;
313 uint32_t inum, blk_idx, blk_off;
317 if(!mounted){dir.Valid = 0;
return dir;}
320 int offset = dir_lookup(parent, name);
321 if(offset == -1){dir.Valid = 0;
return dir;}
325 blk_idx = inum / DIR_PER_BLOCK;
326 blk_off = inum % DIR_PER_BLOCK;
327 fs_disk->read(MetaData.Blocks - 1 -blk_idx, blk.
Data);
331 if(dir.Valid == 0){
return dir;}
334 if(streq(dir.
Name,curr_dir.Name)){printf(
"Current Directory cannot be removed.\n"); dir.Valid=0;
return dir;}
337 for(uint32_t ii=0; ii<ENTRIES_PER_DIR; ii++){
339 temp = rm_helper(dir, dir.
Table[ii].
Name);
340 if(temp.Valid == 0)
return temp;
347 fs_disk->read(MetaData.Blocks - 1 -blk_idx, blk.
Data);
352 fs_disk->write(MetaData.Blocks - 1-blk_idx, blk.
Data);
356 write_dir_back(parent);
359 dir_counter[blk_idx]--;
369 if(!mounted){dir.Valid = 0;
return dir;}
372 int offset = dir_lookup(dir,name);
373 if(offset == -1){printf(
"No such file/directory\n"); dir.Valid=0;
return dir;}
376 if(dir.
Table[offset].type == 0){
377 return rmdir_helper(dir,name);
386 if(!remove(inum)){printf(
"Failed to remove Inode\n"); dir.Valid = 0;
return dir;}
402 Directory temp = rmdir_helper(curr_dir,name);
415 if(!mounted){
return false;}
418 for(uint32_t offset=0; offset<FileSystem::ENTRIES_PER_DIR; offset++){
419 if(curr_dir.Table[offset].valid){
420 if(streq(curr_dir.Table[offset].Name,name)){
421 printf(
"File already exists\n");
428 if(new_node_idx == -1){printf(
"Error creating new inode\n");
return false;}
431 Directory temp = add_dir_entry(curr_dir,new_node_idx,1,name);
432 if(temp.Valid == 0){printf(
"Error adding new file\n");
return false;}
436 write_dir_back(curr_dir);
446 if(!mounted){
return false;}
448 int offset = dir_lookup(curr_dir,name);
449 if((offset == -1) || (curr_dir.Table[offset].type == 1)){
450 printf(
"No such directory\n");
455 Directory temp = read_dir_from_offset(offset);
456 if(temp.Valid == 0){
return false;}
462 if(!mounted){
return false;}
469 Directory temp = rm_helper(curr_dir,name);
478 if(!mounted){
return;}
491 if(!mounted){
return false;}
494 int offset = dir_lookup(curr_dir,name);
495 if(offset == -1){
return false;}
497 if(curr_dir.Table[offset].type == 0){
return false;}
499 uint32_t inum = curr_dir.Table[offset].inum;
502 FILE *stream = fopen(path,
"w");
503 if (stream ==
nullptr) {
504 fprintf(stderr,
"Unable to open %s: %s\n", path, strerror(errno));
509 char buffer[4*BUFSIZ] = {0};
512 ssize_t result = read(inum, buffer,
sizeof(buffer), offset);
516 fwrite(buffer, 1, result, stream);
521 printf(
"%d bytes copied\n", offset);
532 if(!mounted){
return false;}
536 int offset = dir_lookup(curr_dir,name);
537 if(offset == -1){
return false;}
539 if(curr_dir.Table[offset].type == 0){
return false;}
542 uint32_t inum = curr_dir.Table[offset].inum;
545 FILE *stream = fopen(path,
"r");
546 if (stream ==
nullptr) {
547 fprintf(stderr,
"Unable to open %s: %s\n", path, strerror(errno));
552 char buffer[4*BUFSIZ] = {0};
555 ssize_t result = fread(buffer, 1,
sizeof(buffer), stream);
561 ssize_t actual = write(inum, buffer, result, offset);
563 fprintf(stderr,
"fs.write returned invalid result %ld\n", actual);
569 if (actual != result) {
570 fprintf(stderr,
"fs.write only wrote %ld bytes, not %ld bytes\n", actual, result);
576 printf(
"%d bytes copied\n", offset);
589 if(!mounted){
return;}
593 fs_disk->read(0,blk.
Data);
594 printf(
"Total Blocks : %u\n",blk.Super.
Blocks);
595 printf(
"Total Directory Blocks : %u\n",blk.Super.
DirBlocks);
596 printf(
"Total Inode Blocks : %u\n",blk.Super.
InodeBlocks);
597 printf(
"Total Inode : %u\n",blk.Super.
Inodes);
598 printf(
"Password protected : %u\n\n",blk.Super.
Protected);
600 printf(
"Max Directories per block : %u\n",DIR_PER_BLOCK);
601 printf(
"Max Namsize : %u\n",NAMESIZE);
602 printf(
"Max Inodes per block : %u\n",INODES_PER_BLOCK);
603 printf(
"Max Entries per directory : %u\n\n",ENTRIES_PER_DIR);
606 for(uint32_t blk_idx=0; blk_idx<MetaData.DirBlocks; blk_idx++){
607 fs_disk->read(MetaData.Blocks - 1- blk_idx,blk.
Data);
608 printf(
"Block %u\n",blk_idx);
611 for(uint32_t offset=0; offset<DIR_PER_BLOCK; offset++){
614 printf(
" Offset %u: Directory Name - \"%s\"\n",offset,dir.
Name);
617 for(uint32_t tbl_idx=0; tbl_idx < ENTRIES_PER_DIR; tbl_idx++){
620 printf(
" tbl_idx %u: Entry Name - \"%s\", type - %u, inum - %u\n",tbl_idx,ent.
Name,ent.type,ent.
inum);