/* Deletes the file named NAME. Returns true if successful, false on failure. Fails if no file named NAME exists, or if an internal memory allocation fails. */ bool filesys_remove (const char *name) { struct dir *dir = get_containing_dir(name); char *file_name = get_filename(name); // Get dir struct dir* dir = get_containing_dir(name); // Get file name char* file_name = get_filename(name); >>>>>>> Stashed changes
/* Creates a file named NAME with the given INITIAL_SIZE. Returns true if successful, false otherwise. Fails if a file named NAME already exists, or if internal memory allocation fails. */ bool filesys_create (const char *name, off_t initial_size, bool isdir) { block_sector_t inode_sector = 0; // Get directory struct dir *dir = get_containing_dir(name); char *file_name = get_filename(name); // Get file name char* file_name = get_filename(name); bool success = false; if (strcmp(file_name, ".") != 0 && strcmp(file_name, "..") != 0) { success = (dir != NULL && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size, isdir) && dir_add (dir, file_name, inode_sector)); } if (!success && inode_sector != 0) free_map_release (inode_sector, 1); dir_close (dir); free(file_name); return success; }
/* Deletes the file named NAME. Returns true if successful, false on failure. Fails if no file named NAME exists, or if an internal memory allocation fails. */ bool filesys_remove (const char *name) { // struct dir *dir = dir_open_root (); struct dir* dir = get_containing_dir (name); char* filename = get_filename(name); bool success = dir != NULL && dir_remove (dir, filename); dir_close (dir); free (filename); return success; }
/* Opens the file with the given NAME. Returns the new file if successful or a null pointer otherwise. Fails if no file named NAME exists, or if an internal memory allocation fails. */ struct file * filesys_open (const char *name) { if (strlen(name) == 0) { return NULL; } struct dir *dir = get_containing_dir (name); char* file_name = get_filename (name); struct inode *inode = NULL; if (dir != NULL) { if (strcmp(file_name, "..") == 0) { if (!dir_get_parent(dir, &inode)) { free (file_name); return NULL; } } else if ((dir_is_root(dir) && strlen(file_name) == 0) || strcmp(file_name, ".") == 0) { free (file_name); return (struct file*)dir; } else { dir_lookup(dir, file_name, &inode); } } dir_close (dir); free(file_name); if (!inode) { return NULL; } if (inode_is_dir(inode)) { return (struct file *)dir_open(inode); } return file_open (inode); }
/* changes the current working directory to path */ bool filesys_chdir(const char *path) { struct dir *dir = get_containing_dir(path); char *file_name = get_file_name(path); struct inode *inode = NULL; struct thread *cur = thread_current(); if (dir != NULL) { if(strcmp(file_name, "..") == 0) { if(!dir_get_parent(dir, &inode)) { free(file_name); return false; } } else if(strcmp(file_name, ".") == 0) { cur->cwd = dir; free(file_name); return true; } else if(dir_is_root(dir) && strlen(file_name) == 0) { cur->cwd = dir; free(file_name); return true; } else { dir_lookup (dir, file_name, &inode); } } dir_close(dir); free(file_name); dir = dir_open(inode); if(dir != NULL) { dir_close(cur->cwd); cur->cwd = dir; return true; } return false; }