int sfs_initialize(int erase) { if (erase == 1) { new_filesystem(); put_super_blk(); put_inode_table(); // open root folder / int fd = sfs_open("/"); if (fd >= 0) { printf("%s %d\n", "Root folder / is opened. File descriptor:", fd); } return 0; } else { get_inode_table_from_disk(); return 1; } return -1; }
// *pathname - the path of the new file or directory(file) // type - if the file at the pathname should be a file or directory. // When type = 1, file is a directory, otherwise the file is a file. int sfs_create(char *pathname, int type) { // parse pathname and store reference to cnum int result; int cnum = 0; result = parse_pathname(pathname, &cnum); if(result < 0) { printf("\nFailed to parse pathname.\n"); return -1; } // check if the file already exists and retrieve its inumber result = fileexists(cnum); if(result >= 0) { // file already exists printf("\nThe file already exists.\n"); return -1; } // if the result is anything but the last component if(result != -2) { return result; // return the value of the result } // retrieve the inumber of the parent directory of the new file result = fileexists(cnum-1); if(result < 0) { printf("\nCould not find i_number from parent directory.\n"); return -1; } int pinum; pinum = result; // retrieve the file pointer table get_fileptr_table(); // check if there is enough room for a new file if(file_pointer[pinum] == 120) { printf("\nThe directory is full.\n"); return -1; } char ninum[3]; sprintf(ninum, "%d", get_firstfree_inode()); // get the first free block available to store the file int freeblock; freeblock = get_firstfree_blk(); // retrieve the inumber table get_inode_table(); // create either a file or directory // directory are -1*freeblock // files are freeblock if(type) { file_blockno[0][atoi(ninum)] = -1*freeblock; } else { file_blockno[0][atoi(ninum)] = freeblock; } // get the block from the parent directory get_block(-1*(file_blockno[0][pinum]), directory_pointer); // Get the names of the newly creating file names char file_name[FILE_NAME_LENGTH + 1]; strcpy(file_name, pathname_parse[cnum-1]); int i; // check for file_name lengths to be file name length limit if(strlen(file_name) < 6) { // pad the filename with spaces for(i = strlen(file_name); i < FILE_NAME_LENGTH; i++) { file_name[i] = ' '; } } // Creating a new entry in parent directory for(i = 0; i < 6; i++) { directory_pointer[file_pointer[pinum] + i] = file_name[i]; } directory_pointer[file_pointer[pinum] + 6] = ninum[0]; directory_pointer[file_pointer[pinum] + 7] = ninum[1]; directory_pointer[file_pointer[pinum] + 8] = ' '; directory_pointer[file_pointer[pinum] + 9] = ' '; // increase the size of the file_pointer[pinum] by 10 file_pointer[pinum] = file_pointer[pinum] + 10; // saves the new info in the parent directory put_block(-1*(file_blockno[0][pinum]), directory_pointer); // save inode and file pointer to the disk put_inode_table(); put_fileptr_table(); return 1; // success }