int path_to_inumber(struct unixfilesystem *fs, const char *pathname,uint16_t dirinumber){ struct direntv6 buf; // The pathname is to a file if(plain_name(pathname)){ int success = directory_findname(fs,pathname,dirinumber, &buf); if (success < 0){ return -1; } // Return the inumber return buf.d_inumber; // The pathname is to a directory }else{ // The first directory in the pathname char *dir = first(pathname); int success = directory_findname(fs, dir, dirinumber, &buf); // Check for errors if (success < 0){ return -1; } // The rest of the pathname const char *restOfPath = rest(pathname); dirinumber = buf.d_inumber; free(dir); return path_to_inumber(fs,restOfPath,dirinumber); } }
int file_open(char *path) { // Check if file exists int file_num; int inumber = path_to_inumber(path); printf("path_to_inumber returned %d\n", inumber); if ( inumber <= 0 ) return ERR_FILE_EXISTS; // Check if file or dir inode *in = get_inode(inumber); printf("get_inode returned %p\n", in); if ( in->directory_flag ) return ERR_FILE_NOT_FOUND; // make file_info and add to open_files arraylist file_info *new_file = (file_info *) malloc(sizeof(file_info)); strcpy(new_file->filepath, path); new_file->inumber = inumber; new_file->cursor = 0; file_num = arraylist_add(new_file,open_files); return file_num; }
/* * Return the inumber associated with the specified pathname. This need only * handle absolute paths. Return a negative number if an error is encountered. */ int pathname_lookup(struct unixfilesystem *fs, const char *pathname) { // Looking for the if(strcmp(pathname,"/") == 0) return ROOT_INUMBER; // Get rid of the first slash pathname += strlen("/"); return path_to_inumber(fs,pathname,ROOT_INUMBER); }