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); } }
/* * Return the inumber associated with the specified pathname. This need * handle absolute paths or normal path. Return a negative number if an error is encountered. */ int pathname_lookup(struct unixfilesystem *fs, const char *pathname) { //fprintf(stderr, "pathname_lookup(path=%s), unimplemented. Returing -1.\n", pathname); //return -1; struct direntv6 dirEnt; //printf("pathname = %s\n",pathname); /* handle normal name */ if (pathname[0] != '/') { //printf("pathname_lookup find normal Name\n"); int err = directory_findname(fs, pathname, fs->workDirInumber, &dirEnt); if (err < 0) { fprintf(stderr, "normal_name_lookup find dirName %s err"); return -1; } return dirEnt.d_inumber; } int i, inumber = ROOT_INUMBER; char dirName[20]; for (i = 0; pathname[i] != '\0';) { if (pathname[i] == '/') { i++; int cnt = 0; while (pathname[i] != '/' && pathname[i] != '\0') { dirName[cnt++] = pathname[i++]; if (cnt > 14) { fprintf(stderr, "pathname %s is illegal", pathname); return -1; } } dirName[cnt] = '\0'; /* after above loop, i must ended with pathname[i] == '/' or '\0' */ if (dirName[0] == '\0') { //printf("pathname_lookup start disk from ROOT_DIR\n"); return ROOT_INUMBER; } int err = directory_findname(fs, dirName, inumber, &dirEnt); if (err < 0) { fprintf(stderr, "pathname_lookup find dirName %s not exist %d\n", dirName, inumber); return -1; } inumber = dirEnt.d_inumber; // inumber update to dirEnt.d_inumber leads to next directory } } return inumber; }
/* * Wrapper function to call directory_findname to get the inumber for the * given directory name. */ int name_to_inode_number(struct unixfilesystem *fs, const char *name, int dir) { struct direntv6 file; int check = directory_findname(fs, name, dir, &file); if(check == 0) return file.d_inumber; else return -1; }
/* * 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) { char* path = strdup(pathname); char* tok = NULL; tok = strtok(path,"/"); int direntry = ROOT_INUMBER; while (tok != NULL) { struct direntv6 dirEnt; //printf("dirent: %s\n", dirEnt.d_name); if (directory_findname(fs, tok, direntry, &dirEnt) != 0) return direntry; direntry = dirEnt.d_inumber; tok = strtok(NULL,"/"); } return direntry; }
/* * 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) { if(strcmp(pathname, "/") == 0) return ROOT_INUMBER; //check if it's root struct direntv6 entry; int dirinumber = ROOT_INUMBER; char *path = malloc(strlen(pathname) + 1); memcpy(path, pathname, strlen(pathname) + 1); //copy pathname into path because strtok cannot be used on constant strings char *tokens = strtok(path, "/"); //tokenizes the string path while(tokens != NULL) { if(directory_findname(fs, tokens, dirinumber, &entry) < 0) return -1; //return negative value on failure dirinumber = entry.d_inumber; tokens = strtok(NULL, "/"); //read next token } free(path); return dirinumber; }