//returns the inode of the last dir in name before the last / //the user has to check for existing file after the / struct dir * dir_lookup_rec (const char *name) { //printf("lookup path is %s\n",name); //if the string starts with / it is an absolut path //root dir if ( strcmp( name, "/") == 0 ) return dir_open_root(); //for the return value int success = 0; char *token ,*save_ptr; char * temp = (char *)malloc(strlen(name) + 1 ); strlcpy (temp, name, strlen(name) + 1); //open root and start struct dir * current; //if it is relative make it absolute if ( name[0] != '/' ) { current = dir_reopen(thread_current()->pwd); } else { current = dir_open_root(); } struct inode * nextdir = dir_get_inode(current); //go through and check that the previous direcrtories exist for (token = strtok_r (temp, "/", &save_ptr); token != NULL; token = strtok_r (NULL, "/", &save_ptr)) { //somethings wrong if this hapens if (current == NULL ) break; //last round has to be not existing if (strlen(save_ptr) == 0) { success = 1; break; } //goto next if token is empty in case of //a/ if(strlen(token) != 0) { //check if this directory exists true if exists if ( dir_lookup (current, token,&nextdir) ) { //check if it is a directory and then open it enum file_type type = inode_type (nextdir); //is it a dir if(type == FILE_DIR) { dir_close(current); current = dir_open(nextdir); } else break; } else break; } } if( success == 1) return current; else return NULL; }
struct dir *get_directory(const char *path, bool flag){ if(strlen(path)==0) return NULL; struct dir *curr; char *word, *brkt, *buffer = malloc(strlen(path)+1), *save, *last; struct inode *inode; memcpy(buffer, path, strlen(path)+1); save = buffer; if(buffer[0]=='/'){ curr = dir_open_root(); last = strtok_r(buffer+1, "/", &brkt); } else{ if(thread_current()->dir) curr = dir_reopen(thread_current()->dir); else curr = dir_open_root(); last = strtok_r(buffer, "/", &brkt); } while(1){ word = last; if(word == NULL) break; last = strtok_r(NULL, "/", &brkt); if(last == NULL && flag) break; if(strcmp(word,"")==0); else if(strcmp(word,".")==0); else if(strcmp(word,"..")==0){ inode = inode_open(inode_parent_number(dir_get_inode(curr))); dir_close(curr); curr = dir_open(inode); } else{ inode = NULL; if(dir_lookup(curr, word, &inode)){ dir_close(curr); if(inode_is_dir(inode)) curr = dir_open(inode); else{ inode_close(inode); free(save); return NULL; } } else{ dir_close(curr); free(save); return NULL; } } } free(save); if(inode_removed(curr->inode)) return NULL; return curr; }
/* Formats the file system. */ static void do_format (void) { printf ("Formatting file system..."); free_map_create (); if (!dir_create (ROOT_DIR_SECTOR, 2)) PANIC ("root directory creation failed"); if (dir_add(dir_open_root (), ".", ROOT_DIR_SECTOR, true) == NULL || dir_add(dir_open_root (), "..", ROOT_DIR_SECTOR, true) == NULL ){ PANIC ("root directory added . or .. failed"); } free_map_close (); printf ("done.\n"); }
static struct dir* filesys_get_dir (const char* path) { struct dir* dir; int len = strlen(path); char *p = (char *)malloc(sizeof(char) * (len + 1)); memcpy(p, path, len); p[len]='\0'; bool openRoot = p[0]=='/' || thread_current ()->cur_dir == NULL; dir = openRoot ? dir_open_root() : dir_reopen(thread_current()->cur_dir); char *save_ptr; char *token = strtok_r(p, "/", &save_ptr); char *next_token = token!=NULL ? strtok_r(NULL, "/", &save_ptr): NULL; struct inode *inode; bool isdir; while (next_token!=NULL){ if (dir_lookup(dir, token, &inode, &isdir) == NULL) return NULL; dir_close(dir); dir = dir_open(inode); if (isdir == false){ dir_close(dir); return NULL; } token = next_token; next_token = strtok_r(NULL, "/", &save_ptr); } return dir; }
/*! 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(); bool success = dir != NULL && dir_remove(dir, name); dir_close(dir); return success; }
static struct dir *get_cwd (const char *name) { struct dir *search_dir = NULL; if (name[0] == '/') { search_dir = dir_open_root(); } else { struct dir *cwd = thread_current()->cwd; if (cwd != NULL) { search_dir = dir_reopen(cwd); } else if (thread_current()->parent_data != NULL && thread_current()->parent_data->cwd != NULL) { search_dir = dir_reopen(thread_current()->parent_data->cwd); } else { search_dir = dir_open_root(); } } return search_dir; }
/*! 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) { struct dir *dir = dir_open_root(); struct inode *inode = NULL; if (dir != NULL) dir_lookup(dir, name, &inode); dir_close(dir); return file_open(inode); }
struct dir* get_containing_dir (const char* path) { char s[strlen(path)+1]; memcpy(s, path, strlen(path)+1); char *save_ptr, *next_token = NULL, *token = strtok_r(s, "/", &save_ptr); struct dir* dir; if (s[0] == 47 || !thread_current ()->cwd) { dir = dir_open_root (); } else { dir = dir_reopen (thread_current()->cwd); } if (token) { next_token = strtok_r (NULL, "/", &save_ptr); } while (next_token != NULL) { if (strcmp(token, ".") != 0) { struct inode *inode; if (strcmp(token, "..") == 0) { if (!dir_get_parent (dir, &inode)) { return NULL; } } else { if (!dir_lookup (dir, token, &inode)) { return NULL; } } if (inode_is_dir (inode)) { dir_close (dir); dir = dir_open (inode); } else { inode_close (inode); } } token = next_token; next_token = strtok_r (NULL, "/", &save_ptr); } return dir; }
/*! 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) { block_sector_t inode_sector = 0; struct dir *dir = dir_open_root(); bool success = (dir != NULL && free_map_allocate(1, &inode_sector) && inode_create(inode_sector, initial_size) && dir_add(dir, name, inode_sector)); if (!success && inode_sector != 0) free_map_release(inode_sector, 1); dir_close(dir); 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 thread *cur = thread_current(); struct dir *dir; if(cur->current_directory == NULL) dir = dir_open_root (); else dir = dir_open_current(); bool success = dir != NULL && dir_remove (dir, name); dir_close (dir); 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) { lock_acquire(&create_lock); struct dir *dir = dir_open_root (); struct inode *inode = NULL; if (dir != NULL) dir_lookup (dir, name, &inode); dir_close (dir); lock_release(&create_lock); return file_open (inode); }
/* 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) { // XXX lock_acquire(); lock_acquire(lock); // XXX struct dir *dir = dir_open_root (); bool success = dir != NULL && dir_remove (dir, name); dir_close (dir); // XXX lock_release(); lock_release(lock); // XXX return success; }
/* Formats the file system. */ static void do_format (void) { struct dir *root_dir = dir_open_root(); struct inode *root_inode = dir_get_inode(root_dir); printf ("Formatting file system..."); free_map_create (); if (!dir_create (ROOT_DIR_SECTOR, 16)) PANIC ("root directory creation failed"); /* add . and .. entry to root directory */ dir_add(root_dir, ".", inode_get_inumber(root_inode)); dir_add(root_dir,"..", inode_get_inumber(root_inode)); free_map_close (); printf ("done.\n"); }
struct dir * dir_from_path (char *path) { char *save_ptr, *token, *next = NULL; int length = strlen(path) + 1; char dir_path [length]; struct dir *directory; struct thread *cur = thread_current (); struct inode *dir_node; bool success = false; memcpy (dir_path, path, length); token = strtok_r (dir_path, "/", &save_ptr); if (dir_path[0] == SLASH_SYM || cur->dir == NULL) directory = dir_open_root (); else directory = dir_reopen (cur->dir); if (token != NULL) next = strtok_r (NULL, "/", &save_ptr); while (next != NULL) { if (strcmp (token, ".") != 0) { if (strcmp (token, "..") != 0) success = dir_lookup (directory, token, &dir_node); else success = dir_lookup (directory, token, &dir_node); if (success) { if (inode_is_dir(dir_node)) { dir_close (directory); directory = dir_open (dir_node); } } else return NULL; } token = next; next = strtok_r (NULL, "/", &save_ptr); } return directory; }
/* 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) { struct dir *dir = dir_open_root (); struct inode *inode = NULL; if (dir != NULL) dir_lookup (dir, name, &inode); dir_close (dir); /* SH proj4*/ // if (inode == NULL) // PANIC ("sh: inode is NULL\n"); /* == SH proj4*/ return file_open (inode); }
/* Formats the file system. */ static void do_format (void) { printf ("Formatting file system..."); free_map_create (); if (!dir_create (ROOT_DIR_SECTOR, 16)) PANIC ("root directory creation failed"); free_map_close (); struct dir *root = dir_open_root (); dir_add (root, ".", ROOT_DIR_SECTOR); dir_add (root, "..", ROOT_DIR_SECTOR); dir_close (root); printf ("done.\n"); }
/* 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) { bool exists = false; struct dir *dir = dir_open_root (); struct inode *inode = NULL; if (dir != NULL) exists = dir_lookup (dir, name, &inode); dir_close (dir); if (!exists) return NULL; return file_open (inode); }
/* 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) { struct thread *cur = thread_current(); struct dir *dir; if(cur->current_directory == NULL) dir = dir_open_root (); else dir = dir_open_current(); struct inode *inode = NULL; if (dir != NULL) dir_lookup (dir, name, &inode); dir_close (dir); return file_open (inode); }
/* Formats the file system. */ static void do_format (void) { struct dir *current_dir; printf ("Formatting file system..."); free_map_create (); /*Create directory with 2 entries - for . and .. */ if (!dir_create (ROOT_DIR_SECTOR, 2)) PANIC ("root directory creation failed"); free_map_close (); /* Create . and .. entries. */ current_dir = dir_open_root (); dir_add (current_dir, ".", ROOT_DIR_SECTOR, false); dir_add (current_dir, "..", ROOT_DIR_SECTOR, false); dir_close (current_dir); printf ("done.\n"); }
/* 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) { struct dir *dir = dir_open_root (); struct inode *inode = NULL; /*if(dir==NULL) { printf("DIR NULL\n"); }*/ if (dir != NULL) dir_lookup (dir, name, &inode); dir_close (dir); return file_open (inode); }
/* Initializes the file system module. If FORMAT is true, reformats the file system. */ void filesys_init (bool format) { fs_device = block_get_role (BLOCK_FILESYS); if (fs_device == NULL) PANIC ("No file system device found, can't initialize file system."); inode_init (); bc_init (); free_map_init (); if (format) do_format (); free_map_open (); thread_current()->thread_dir = dir_open_root(); }
/* 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) { block_sector_t inode_sector = 0; struct thread *cur = thread_current(); struct dir *dir; if(cur->current_directory == NULL) dir = dir_open_root (); else dir = dir_open_current(); bool success = (dir != NULL && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size, false) && dir_add (dir, name, inode_sector)); if (!success && inode_sector != 0) free_map_release (inode_sector, 1); dir_close (dir); return success; }
/* parsing the path, and return working directory */ struct dir* parse_path(char *path_name, char *file_name) { struct dir *dir; char *token, *next_token, *save_ptr; struct inode *inode; if(path_name == NULL || file_name == NULL) return NULL; if(strlen(path_name) == 0) return NULL; /* if path name is absulute path */ if(path_name[0] == '/') dir = dir_open_root(); else /* path name is relative path */ dir = dir_reopen(thread_current()->thread_dir); token = strtok_r(path_name, "/", &save_ptr); next_token = strtok_r(NULL, "/", &save_ptr); while(token != NULL && next_token != NULL) { /* find file named token in dir directory */ if(dir_lookup(dir, token, &inode) == false) { return NULL; } /* if token is not a directory */ if(inode_is_dir(inode) == false){ dir_close(dir); return NULL; } /* close dir */ dir_close(dir); /* set dir */ dir = dir_open(inode); /* advance */ token = next_token; next_token = strtok_r(NULL, "/", &save_ptr); } /* copy token to file_name */ strlcpy(file_name, token, strlen(token)+1); return dir; }
/* 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) { struct dir *dir = dir_open_root (); struct inode *inode = NULL; /* 02 =========== */ // bool exist; /*===============*/ if (dir != NULL) // printf("hi\n"); /* original ==== */ dir_lookup (dir, name, &inode); /* ============== */ // exist = dir_lookup(dir, name, &inode); dir_close (dir); // if(exist) return file_open(inode); // return NULL; /* original======*/ return file_open (inode); }
#include <string.h> #include <ustar.h> #include "filesys/directory.h" #include "filesys/file.h" #include "filesys/filesys.h" #include "threads/malloc.h" #include "threads/palloc.h" #include "threads/vaddr.h" /*! List files in the root directory. */ void fsutil_ls(char **argv UNUSED) { struct dir *dir; char name[NAME_MAX + 1]; printf("Files in the root directory:\n"); dir = dir_open_root(); if (dir == NULL) PANIC("root dir open failed"); while (dir_readdir(dir, name)) printf("%s\n", name); printf("End of listing.\n"); } /*! Prints the contents of file ARGV[1] to the system console as hex and ASCII. */ void fsutil_cat(char **argv) { const char *file_name = argv[1]; struct file *file; char *buffer;
/* Parse an absolute or relative file path and find the directory and the file name the put it to the buffers (dir and parsed_name) */ static bool parse_path (const char *name, struct dir **dir, char *parsed_name) { bool success = true; struct dir *cur_dir; /* If it is absolute path. */ if (name[0] == '/') { cur_dir = dir_open_root (); } /* If it is relative path. */ else { cur_dir = dir_open (inode_open (thread_current()->cur_dir_sector)); } /* Search through the directories to get the desired directory and the name of the file. */ struct inode *inode = NULL; char *token, *save_ptr; char *name_copy = (char*) malloc (strlen (name)); strlcpy (name_copy, name, strlen (name) + 1); if (name_copy == NULL) { success = false; goto return_result; } /* Initialize an array to stores the different file names of the path. */ char **dirs = (char**)malloc (MAX_DIR_DEPTH * sizeof(char*)); int i; for (i = 0; i < MAX_DIR_DEPTH; ++i) { dirs[i] = (char*) malloc ((NAME_MAX + 1) * sizeof(char*)); } /* Read all the file names of the path. */ for (token = strtok_r (name_copy, "/", &save_ptr), i = 0; token != NULL; token = strtok_r (NULL, "/", &save_ptr), ++i) { strlcpy (dirs[i], token, strlen (token) + 1); } /* Special case: the root directory. */ if (i == 0 && !strcmp ("/", name)) { strlcpy (parsed_name, name, strlen (name) + 1); } else { /* Get the last file name as the parsed name. */ strlcpy (parsed_name, dirs[i - 1], strlen (dirs[i - 1]) + 1); } /* Get the directory where the file (parsed name) is located. */ int dir_num = i - 1; for (i = 0; i < dir_num; ++i) { success = dir_lookup (cur_dir, dirs[i], &inode); if (!success) { goto return_result; } if (!(inode->data).is_dir) { success = false; goto return_result; } dir_close (cur_dir); cur_dir = dir_open (inode); if (cur_dir == NULL) { success = false; goto return_result; } } return_result: if (!success) { dir_close (cur_dir); } for (i = 0; i < MAX_DIR_DEPTH; ++i) { free (dirs[i]); } free (name_copy); free (dirs); *dir = cur_dir; return success; }
/* Decomposes the given path (dir) to a final file or directory name and * its parent directory. Write the name to ret_name, and the parent directory * struct to par_dir. Returns true if the decomposition is successfull, i.e. * successfully decomposed the final name, and open all the directories in * path successfully. */ bool decompose_dir(const char* dir, char* ret_name, struct dir** par_dir){ /* parent directory*/ struct dir* cur_dir; /* inode for iteration*/ struct inode* next_inode; /* iterator for the path string*/ unsigned i; /* Current process*/ struct thread* t = thread_current(); /* Final name of the file or the directory*/ char name[15]; /* Check that give path is not empty*/ if (*dir == NULL || dir[0] == '\0') return false; if (dir[0] == '/') { /* If give an absolute path, then first set cur_dir as root. */ cur_dir = dir_open_root(); } else if (t->cur_dir == NULL) { /* Given path is relative. However, as the process's cur_dir is * NULL, which is equivelant to the root directory in our * implemtation. Also set update proccess's cur_dir to root. */ cur_dir = dir_open_root(); t->cur_dir = dir_open_root(); } else { /* Give path is also relative. And set cur_dir same as the process's * working director. */ if (dir_get_inode(t->cur_dir)->removed) /* If the working directory has been removed, then return false * right away. */ return false; cur_dir = dir_reopen(t->cur_dir); } /* Iterate the path with dir as iterator until the null terminator. */ while (*dir != '\0'){ /* Reset the index iterator. for extract next subdirectory name. */ i = 0; /*Skip the directory symbol*/ while (*dir == '/') ++dir; /* Increment i until the directory symbol or null terminator*/ while ((*(dir + i) != '/' && *(dir + i) != '\0') && (i <= 15)){ ++i; } /* Enforces NAME_MAX length to all directories*/ if (i > 15) { dir_close(cur_dir); return false; } /* Copy this directory name to name*/ memcpy(name, dir, i); name[i] = '\0'; /* If this is the last name, then we got our final dir / file name, * i.e. finished decomposition of path. */ if (*(dir + i) == '\0') break; /* Look up the file / dir from the parent directory cur_dir. */ if (!dir_lookup(cur_dir, name, &next_inode)) { dir_close(cur_dir); return false; } /* Close the current parent directory, as we have found the next * one.*/ dir_close(cur_dir); /* Makes sure that we are always opening a directory. */ if (next_inode->data.type != DIR_INODE_DISK) { inode_close(next_inode); return false; } /* Open up the next directory in path. */ cur_dir = dir_open(next_inode); if (cur_dir == NULL) { inode_close(next_inode); return false; } /* Advance. */ dir += i; } /* Set the parent directory as cur_dir*/ *par_dir = cur_dir; /*Copy the final name ret_name*/ strlcpy(ret_name, name, strlen(name) + 1); return true; }
static struct dir *get_directory(char *path) { char s[1024]; memcpy(s,path,strlen(path)+1); char *token, *save_ptr,*prev_token="", *next_token="";//,*save,*real; token = strtok_r(s,"/",&save_ptr); // real = strtok_r(token,"/",&save); struct dir *start; struct inode *inode; if( s[0]=='/' || !thread_current()->curr_dir ) { // printf("when create a/b\n"); start = dir_open_root(); } else { // printf("come on\n"); if( inode_is_removed(dir_get_inode(thread_current()->curr_dir))) return NULL; else start = dir_reopen(thread_current()->curr_dir); } /* if( strcmp(token,"." )==0) { } else if( strcmp(token,"..") == 0) { if( thread_current()->curr_dir) start = dir_get_parent(thread_current()->curr_dir); else start = dir_open_root(); } else { printf("here\n"); if(thread_current()->curr_dir) { // printf("also here\n"); start = dir_reopen(thread_current()->curr_dir); } else { // printf("when create a/b\n"); start = dir_open_root(); } }*/ // printf("first setting\n"); // real = strtok_r(token,"/",&save); // printf("token %s s %s\n",token,s); if(token) { next_token = strtok_r(NULL,"/",&save_ptr); /* if(next_token == NULL) printf("AAAA\n"); else printf("%s\n",next_token);*/ // printf("first %s second %s\n",token,next_token); } while( next_token!=NULL) { // printf("but not here\n"); if( strcmp(token,"." )==0) { continue; } else if( strcmp(token,"..") == 0) { if(inode_get_inumber(dir_get_inode(start))!=1) { start=dir_get_parent(start); } } else { if(dir_lookup(start ,token, &inode)) { // printf("when create a/b token:%s\n",token); // printf("aaaa\n"); dir_close(start); start = dir_open(inode); } else return NULL; // dir_close(start); /* if( inode==NULL ) { return NULL; } else if(inode_is_removed(inode)) return NULL;*/ // start = dir_open(inode); } prev_token = token; token = next_token; next_token = strtok_r(NULL,"/",&save_ptr); // printf("first %s second %s\n",token,next_token); } // printf("directory name : %s \n",token); return start; }