int main(int argc, char **argv) { char * target_file_path; char target_file_name[256]; unsigned int tpath_index, tfile_index; struct ext2_inode parent, target; if(argc != 3) { fprintf(stderr, "Usage: ext2_rm <image file name> <target path>\n"); exit(1); } if (ext2_init(argv[1]) == -1){ exit(1); } // check target path if(!check_file_path(argv[2]) ) { printf("invaid abs path\n"); return ENOENT; } // get target file path and name target_file_path = malloc(strlen(argv[2])); get_last_entry(argv[2],target_file_name, target_file_path); // check if it is vaild target file name if (! strlen(target_file_name)){ printf("invaid target file name\n"); return ENOENT; } if (is_dir(argv[2])){ printf("invaid target file name\n"); return ENOENT; } // get inode index for source file and target file tfile_index = get_inode_index(argv[2]); tpath_index = get_inode_index(target_file_path); free(target_file_path); // check if source path and target path vaild if(!tpath_index){ printf("No such file or directory\n"); return ENOENT; } // check if the target is the file if(tfile_index){ if(! (get_inode(tfile_index)->i_mode & EXT2_S_IFREG)){ printf("%s not a file\n", target_file_name); return ENOENT; } } remove_inode(get_inode(tpath_index), get_inode(tfile_index), tfile_index); return 0; }
/* * Return whether the given node is a PID directory. */ static int dir_is_pid(struct inode *node) { return (get_parent_inode(node) == get_root_inode() && get_inode_index(node) != NO_INDEX); }
/* * Construct one requested file entry, or all file entries, in a PID directory. */ static void construct_pid_entries(struct inode * parent, char * name) { int slot; slot = get_inode_index(parent); assert(slot >= 0 && slot < NR_TASKS + NR_PROCS); /* If this process is already gone, delete the directory now. */ if (!slot_in_use(slot)) { delete_inode(parent); return; } /* * If a specific file name is being looked up, see if we have to add * an inode for that file. If the directory contents are being * retrieved, add all files that have not yet been added. */ if (name != NULL) make_one_pid_entry(parent, name, slot); else make_all_pid_entries(parent, slot); }
/*===========================================================================* * dir_is_pid * *===========================================================================*/ static int dir_is_pid(struct inode *node) { /* Return whether the given node is a PID directory. */ return (get_parent_inode(node) == get_root_inode() && get_inode_index(node) != NO_INDEX); }
/* * Data is requested from one of the files in a PID directory. Call the * function that is responsible for generating the data for that file. */ static void pid_read(struct inode * node) { struct inode *parent; int slot, index; /* * Get the slot number of the process. Note that this currently will * not work for files not in the top-level pid subdirectory. */ parent = get_parent_inode(node); slot = get_inode_index(parent); /* Get this file's index number. */ index = get_inode_index(node); /* Call the handler procedure for the file. */ ((void (*)(int))pid_files[index].data)(slot); }
/*===========================================================================* * pid_read * *===========================================================================*/ static void pid_read(struct inode *node) { /* Data is requested from one of the files in a PID directory. Call the * function that is responsible for generating the data for that file. */ struct inode *parent; int slot, index; /* Get the slot number of the process. Note that this currently will * not work for files not in the top-level pid subdirectory. */ parent = get_parent_inode(node); slot = get_inode_index(parent); /* Get this file's index number. */ index = get_inode_index(node); /* Call the handler procedure for the file. */ ((void (*) (int)) pid_files[index].data)(slot); }
/*===========================================================================* * read_hook * *===========================================================================*/ int read_hook(struct inode *node, off_t off, char **ptr, size_t *len, cbdata_t cbdata) { /* Regular file read hook. Call the appropriate callback function to * generate and return the data. */ buf_init(off, *len); /* Populate the buffer with the proper content. */ if (get_inode_index(node) != NO_INDEX) { pid_read(node); } else { ((void (*) (void)) cbdata)(); } *len = buf_get(ptr); return OK; }