static BD_DIR_H *_combine_dirs(BD_DIR_H *ovl, BD_DIR_H *rom) { BD_DIR_H *dp = calloc(1, sizeof(BD_DIR_H)); BD_DIRENT entry; if (dp) { dp->read = _comb_dir_read; dp->close = _comb_dir_close; dp->internal = calloc(1, sizeof(COMB_DIR)); if (!dp->internal) { X_FREE(dp); goto out; } while (!dir_read(ovl, &entry)) { _comb_dir_append(dp, &entry); } while (!dir_read(rom, &entry)) { _comb_dir_append(dp, &entry); } } out: dir_close(ovl); dir_close(rom); return dp; }
/* 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, enum file_type type) { block_sector_t inode_sector = 0; char * parse = parse_filename (name); //get the correct dir struct dir *dir = dir_lookup_rec (parse); bool success = (dir != NULL && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size, type) && dir_add (dir, parse, inode_sector)); if (!success && inode_sector != 0) free_map_release (inode_sector, 1); if( success == true && type == FILE_DIR ) { //we want to add . and .. as well if it is a dir //open the created directory struct file * created = filesys_open(parse); struct dir * mydir = dir_open(file_get_inode (created)); //add . to it dir_add (mydir, ".", inode_sector); struct inode * parent = dir_get_inode (dir); block_sector_t inode_sector_parent = inode_id(parent); //add .. to it dir_add (mydir, "..", inode_sector_parent); dir_close(mydir); file_close(created); } dir_close (dir); return success; }
bool filesys_cd (const char* dir) { bool result = false; char* name_; if (strlen(dir) == 0) { return false; } else { struct dir* dir_ = filesys_get_dir(dir); name_ = filesys_get_name(dir); struct inode* inode = NULL; bool isdir = false; if (strcmp(name_, "") == 0) { if (thread_current()->cur_dir) dir_close(thread_current()->cur_dir); thread_current()->cur_dir = dir_; result = true; } else if (dir_lookup(dir_, name_, &inode, &isdir) ==NULL || isdir == NULL) { dir_close(dir_); result = false; } else if (isdir != NULL){ if (thread_current()->cur_dir) dir_close(thread_current()->cur_dir); thread_current()->cur_dir = dir_open(inode); dir_close(dir_); result = true; } } free(name_); return result; }
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; }
/* 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; struct inode *inode = NULL; char parsed_name[NAME_MAX + 1]; if (*name == NULL) { return NULL; } bool success = parse_path (name, &dir, parsed_name); if ((strlen (parsed_name) > NAME_MAX)) { dir_close (dir); return NULL; } if (success) { if (dir != NULL) dir_lookup (dir, parsed_name, &inode); dir_close (dir); } struct file *result = file_open (inode); return result; }
bool filesys_chdir (const char* name) { struct dir* dir = containing_dir(name); char* file_name = get_name(name); struct inode *inode = NULL; if (dir != NULL){ if (strcmp(file_name, "..") == 0){ //Check parent if (!dir_parent(dir, &inode)){ free(file_name); return false; } } else if ((strlen(file_name) == 0 && (dir_is_root(dir))) || (strcmp(file_name, ".") == 0)){ thread_current()->cur_dir = 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) { dir_close(thread_current()->cur_dir); thread_current()->cur_dir = dir; return true; } return false; }
/* 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) { char leaf_name[NAME_MAX + 1]; if (!dir_get_leaf_name (name, leaf_name)) return false; struct dir *parent_dir = dir_get_parent_dir (name); if (parent_dir == NULL) return false; struct inode *inode; if (!dir_lookup (parent_dir, leaf_name, &inode)) { dir_close (parent_dir); return false; } bool success; if (!inode_is_dir(inode)) // if is file success = dir_remove (parent_dir, leaf_name); else { if (dir_is_empty (inode)) { success = dir_remove (parent_dir, leaf_name); } else success = false; } dir_close (parent_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) { if (strnlen (name, FULLPATH_MAX_LEN) == 0) return NULL; if (strcmp (".", name) == 0) { block_sector_t cwd = thread_current ()->cwd_sector; struct inode *curr = NULL; curr = inode_open (cwd); struct dir *p; p = dir_open (inode_open (curr->data.parent_dir_sector)); struct dir_entry e; size_t ofs; ASSERT (p != NULL); for (ofs = 0; inode_read_at (p->inode, &e, sizeof e, ofs) == sizeof e; ofs += sizeof e) { if (e.inode_sector == cwd && e.in_use) { return filesys_open (e.name); } } return NULL; } struct inode *crr = NULL; crr = inode_open (thread_current ()->cwd_sector); struct dir *parent_dir = dir_reopen(dir_get_parent_dir (name)); //if (crr->data.is_dir) //parent_dir = dir_open (inode_open (crr->data.parent_dir_sector)); //else if (parent_dir == NULL) return NULL; struct inode *inode = NULL; char leaf_name[NAME_MAX + 1]; if (!dir_get_leaf_name (name, leaf_name) && strnlen(leaf_name, NAME_MAX) == 0) { inode = inode_reopen (dir_get_inode (parent_dir)); dir_close (parent_dir); return file_open (inode); } if (parent_dir != NULL) dir_lookup (parent_dir, leaf_name, &inode); dir_close (parent_dir); return file_open (inode); }
/* Removes any entry for NAME in DIR. Returns true if successful, false on failure, which occurs only if there is no file with the given NAME. */ bool dir_remove (struct dir *dir, const char *name) { struct dir_entry e; struct inode *inode = NULL; bool success = false; off_t ofs; ASSERT (dir != NULL); ASSERT (name != NULL); //lock the directory for the rest of the lookup operation inode_lock(dir->inode); name = getfilename(name); /* Find directory entry. */ if (!lookup (dir, name, &e, &ofs)) goto done; /* Open inode. */ inode = inode_open (e.inode_sector); if (inode == NULL) goto done; //if it is still open don't allow deletion if(inode_type(inode) == FILE_DIR) { //is file in use? if(inode_opencnt(inode) > 1) goto done; char * temp = (char *)malloc(sizeof(char) * (NAME_MAX + 1) ); struct dir * dirtemp = dir_open(inode); //is dir empty? if (dir_readdir(dirtemp,temp)) { free(temp); dir_close(dirtemp); goto done; } free(temp); dir_close(dirtemp); } /* Erase directory entry. */ e.in_use = false; if (inode_write_at (dir->inode, &e, sizeof e, ofs) != sizeof e) goto done; /* Remove inode. */ inode_remove (inode); success = true; done: //unlock the directory inode_unlock(dir->inode); inode_close (inode); return success; }
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; }
void exit_handler (int status) { printf("%s: exit(%d)\n", &thread_current ()->name, status); lock_acquire(&ref_count_lock); struct p_data* parent = thread_current()->parent_data; if (parent != NULL) { parent->exit_status = status; parent->child_thread = NULL; sema_up(&parent->sema); parent->ref_count--; if (parent->cwd != NULL) { dir_close(parent->cwd); } if (parent->ref_count == 0) { thread_current()->parent_data = NULL; free(parent); } } if (thread_current()->cwd != NULL) { dir_close(thread_current()->cwd); } /* iterate through children and remove this as their parent*/ struct list_elem* e; struct list *childs = &thread_current()->child_processes; for (e = list_begin(childs); e != list_end(childs); e = list_next(e)) { struct p_data* child = list_entry(e, struct p_data, elem); child->ref_count --; list_remove(e); if (child->ref_count == 0) { struct thread *t = child->child_thread; if (t != NULL) { t->parent_data = NULL; } free(child); } } struct list *files = &thread_current()->files; for (e = list_begin(files); e != list_end(files); e = list_begin(files)) { close_handler(list_entry(e, struct file_struct, elem)->fd); } struct file_struct *executable = thread_current()->executable; if (executable != NULL) { file_allow_write(executable->sys_file); file_close(executable->sys_file); thread_current()->executable = NULL; free(executable); } lock_release(&ref_count_lock); thread_exit(); }
/* Creates directory. True if successful. */ bool filesys_mkdir (const char *dir) { bool success = false; /*The string *dir cannot be empty.*/ if (*dir == NULL) { return success; } /* Locates the directory where the new directory should be created. */ struct dir *create_dir; char parsed_name[NAME_MAX + 1]; parse_path (dir, &create_dir, parsed_name); block_sector_t sector; /*Find a free sector for the directory.*/ if (!free_map_allocate (1, §or)) { return success; } success = dir_create (sector, 16); if (!success) { free_map_release (sector, 1); return success; } success = dir_add (create_dir, parsed_name, sector); if (!success) { free_map_release (sector, 1); return success; } /* Get the dir struct of the directory that is just created and add "." and "..". */ struct inode *inode = inode_open (sector); struct dir *new_dir = dir_open (inode); ASSERT (inode != NULL); ASSERT (new_dir != NULL); dir_add (new_dir, ".", sector); dir_add (new_dir, "..", create_dir->inode->sector); dir_close (new_dir); dir_close (create_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 inode * filesys_open (const char *name) { struct dir *search_dir = get_cwd(name); if (search_dir == NULL) { return NULL; } struct inode *inode = NULL; if (name[0] == '/') { inode = dir_get_inode(search_dir); } char *part = malloc(NAME_MAX + 1); if (part == NULL) { return false; } memset(part, 0, NAME_MAX + 1); int retrieved_next_part; for (retrieved_next_part = get_next_part(part, &name); retrieved_next_part > 0; retrieved_next_part = get_next_part(part, &name)) { if (dir_lookup (search_dir, part, &inode)) { if (!inode_is_dir(inode)) { break; } else { dir_close(search_dir); search_dir = dir_open(inode); if (search_dir == NULL) { free(part); return false; } } } else { inode = NULL; break; } } if (inode != NULL && inode_is_dir(inode) && get_next_part(part, &name) == 0) { inode = inode_reopen(inode); } dir_close(search_dir); if (inode != NULL && get_next_part(part, &name) != 0) { inode_close(inode); inode = NULL; } free(part); return 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; }
dir_t* dir_open(char *path) { dir_t *d=0; int i, err = -1; char *p=0; do { i = strlen(path)+4; p = malloc(i); assertb(p); strncpy(p, path, i); strncat(p, "/*", i); d = calloc(1, sizeof(*d)); assertb(d); d->h = INVALID_HANDLE_VALUE; d->h = FindFirstFile(p, &d->fi); if(d->h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_NO_MORE_FILES) { break; } assertb_syserr(d->h); d->first = 1; err = 0; } while(0); if( p ) { free(p); } if( err ) { dir_close(d); d = 0; } return d; }
struct inode * filesys_open_inode (const char *path, bool *is_directory) { struct dir *dir = NULL; struct inode *inode = NULL; bool success = false; char *name = malloc (NAME_MAX + 1); if (name == NULL) goto done; if (!dir_follow_path (thread_current ()->cwd, path, &dir, name)) goto done; if (dir == NULL) goto done; /* open directory in this case */ if (*path != '\0' && strlen (name) == 0) { inode = inode_reopen (dir_get_inode (dir)); if (inode == NULL) goto done; *is_directory = true; } else if (!dir_lookup (dir, name, &inode, is_directory)) goto done; success = true; done: dir_close (dir); free (name); return success ? inode : NULL; }
static int moddir_glob( INSTANCE * my, int * params ) { const char * path = string_get( params[ 0 ] ); static __DIR_ST * dh = NULL; int result; if ( dh && strcmp( dh->path, path ) ) { dir_close( dh ); dh = NULL; } if ( !dh ) dh = dir_open( path ); string_discard( params[ 0 ] ); if ( !dh ) { result = string_new( "" ); string_use( result ); return ( result ); } return ( __moddir_read( dh ) ) ; }
static void _findMetaXMLfiles(META_ROOT *meta, BD_DISC *disc) { BD_DIR_H *dir; BD_DIRENT ent; dir = disc_open_dir(disc, "BDMV" DIR_SEP "META" DIR_SEP "DL"); if (dir == NULL) { BD_DEBUG(DBG_DIR, "Failed to open meta dir BDMV/META/DL/\n"); return; } int res; for (res = dir_read(dir, &ent); !res; res = dir_read(dir, &ent)) { if (ent.d_name[0] == '.') continue; else if (strncasecmp(ent.d_name, "bdmt_", 5) == 0) { META_DL *new_dl_entries = realloc(meta->dl_entries, ((meta->dl_count + 1)*sizeof(META_DL))); if (new_dl_entries) { uint8_t i = meta->dl_count; meta->dl_count++; meta->dl_entries = new_dl_entries; memset(&meta->dl_entries[i], 0, sizeof(meta->dl_entries[i])); meta->dl_entries[i].filename = str_dup(ent.d_name); strncpy(meta->dl_entries[i].language_code, ent.d_name+5,3); meta->dl_entries[i].language_code[3] = '\0'; str_tolower(meta->dl_entries[i].language_code); } } } dir_close(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) { char s[100]; char *save_ptr; // printf("%s \n",name); memcpy(s,name,strlen(name)+1); struct inode *inode = NULL; char *file_name = get_file_name(name); // printf("file name : %s\n",file_name); if( strtok_r(s,"/",&save_ptr) ==NULL&&strlen(name)>0 ) { // printf("aaa\n"); free(file_name); return file_open(inode_open(1)); } if( strcmp(file_name,"")==0 ) return NULL; struct dir *dir = get_directory(name); // printf("sector of dir : %d\n", inode_get_inumber(dir_get_inode(dir))); // printf("lookup start\n"); if (dir != NULL) { // printf("here?\n"); if(strcmp(file_name,".")==0) { // printf("here\n"); inode = dir_get_inode(dir); // printf("ohhhhhh\n"); // dir_close (dir); free(file_name); if(inode_is_removed(inode)) return NULL; else return file_open (inode); } else if( strcmp(file_name,"..")==0) { inode = dir_get_inode(inode); // dir_close (dir); free(file_name); return file_open (inode); } else dir_lookup (dir, file_name, &inode); dir_close (dir); if(inode==NULL) return NULL; free(file_name); return file_open (inode); } else { // printf("here\n"); return NULL; } // if(inode_is_removed(inode)) // return NULL; // return file_open (inode); }
/* 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; char parsed_name[NAME_MAX + 1]; if (*name == NULL || (strlen (name) > NAME_MAX)) { return false; } bool success = parse_path (name, &dir, parsed_name); if (!success) { return success; } struct inode *inode; success = (dir != NULL && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size, false) && dir_add (dir, parsed_name, inode_sector) && dir_lookup (dir, ".", &inode)); if (!success && inode_sector != 0) free_map_release (inode_sector, 1); dir_close (dir); return success; }
void chdir (struct intr_frame *f) { const char * dirname = *(char **)value_stack(f->esp,4); //if empty or root return if(strlen(dirname) == 0 || strcmp( dirname, "/")) f->eax = 0; /* CLOSE FILE ? */ struct file * file = filesys_open(dirname); if ( file == NULL ) { f->eax = 0; return; } struct inode * myinode = file_get_inode (file); enum file_type type = inode_type (myinode); //if the file is a dir open it and set pwd to it if(type == FILE_DIR) { f->eax = 1; dir_close(thread_current()->pwd); thread_current()->pwd = dir_open(inode_reopen(myinode)); } else f->eax = 0; file_close(file); }
/*! Remove a file */ bool remove(const char *f_name) { struct dir* cur_dir; char name[15]; /* Checks the validity of the given pointer */ //printf("going to remove file %s\n\n", f_name); if (!checkva(f_name)) exit(-1); if (!decompose_dir(f_name, name, &cur_dir)) return false; if (strcmp(".", name) == 0 || strcmp("..", name) == 0) return false; lock_acquire(&filesys_lock); /* Remove the file, while locking the file system. */ bool flag = filesys_dir_remove(name, cur_dir); //printf("name decomposed! %x\n\n", cur_dir); dir_close(cur_dir); lock_release(&filesys_lock); return flag; }
/* 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 *path, off_t initial_size) { block_sector_t inode_sector = 0; struct dir *dir = NULL; bool success = false; char *name = malloc (NAME_MAX + 1); if (name == NULL) goto done; if (!dir_follow_path (thread_current ()->cwd, path, &dir, name)) goto done; 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) { cache_remove (fs_cache, inode_sector, 1); free_map_release (inode_sector, 1); } done: dir_close (dir); free (name); 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) { char *cp_name; char *file_name; struct dir *dir; struct inode *inode = NULL; cp_name = malloc( sizeof(char) * (strlen(name)+1) ); if(cp_name == NULL) return NULL; file_name = malloc( sizeof(char) * (strlen(name)+1) ); if(file_name == NULL){ free(cp_name); return NULL; } /* copy name to cp_name */ strlcpy(cp_name, name, strlen(name)+1 ); dir = parse_path (cp_name, file_name); if (dir != NULL) dir_lookup (dir, file_name, &inode); dir_close (dir); free(file_name); free(cp_name); return file_open (inode); }
/* 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; }
static void _findMetaXMLfiles(META_ROOT *meta, const char *device_path) { BD_DIR_H *dir; BD_DIRENT ent; char *path = NULL; path = str_printf("%s" DIR_SEP "BDMV" DIR_SEP "META" DIR_SEP "DL", device_path); dir = dir_open(path); if (dir == NULL) { BD_DEBUG(DBG_DIR, "Failed to open meta dir %s\n", path); X_FREE(path); return; } int res; for (res = dir_read(dir, &ent); !res; res = dir_read(dir, &ent)) { if (ent.d_name[0] == '.') continue; else if (ent.d_name != NULL && strncasecmp(ent.d_name, "bdmt_", 5) == 0) { uint8_t i = meta->dl_count; meta->dl_count++; meta->dl_entries = realloc(meta->dl_entries, (meta->dl_count*sizeof(META_DL))); meta->dl_entries[i].filename = str_dup(ent.d_name); strncpy(meta->dl_entries[i].language_code, ent.d_name+5,3); meta->dl_entries[i].language_code[3] = '\0'; str_tolower(meta->dl_entries[i].language_code); } } dir_close(dir); X_FREE(path); }
/*! 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; }
/* Creates a file named NAME with the given INITIAL_SIZE.*/ bool filesys_create (const char *name, off_t initial_size, bool isdir) { if (strlen(name) == 0) return false; block_sector_t inode_sector = 0; struct dir *dir_ = filesys_get_dir(name); char *name_ = filesys_get_name(name); bool success = false; if (strcmp (name_, "") == 0) goto done; success = (dir_ && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size) && dir_add (dir_, name_, inode_sector, isdir)); struct inode *ninode = NULL; struct dir *ndir = NULL; bool success1 = true; if (success && isdir){ success1 = ((ninode = inode_open (inode_sector)) && (ndir = dir_open (ninode)) && dir_add (ndir, ".", inode_sector, true) && dir_add (ndir, "..",inode_get_inumber (dir_get_inode (dir_)), true)); } if (inode_sector != 0 && !success) free_map_release (inode_sector, 1); if (success && !success1) { success = false; printf("Failure: create dir: %s\n", name); dir_remove (dir_, name_); } done: dir_close (dir_); free(name_); if (!ndir && ninode){ inode_close(ninode); } else if (ndir) { dir_close(ndir); } return success; }
//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; }
bool filesys_chdir(const char *name) { bool success=false; struct inode *inode = NULL; struct thread *t = thread_current(); char *file_name = get_file_name(name); struct dir *dir = get_directory(name); // printf("lookup start\n"); if (dir != NULL) { if( strcmp(file_name,".")==0) { t->curr_dir = dir_reopen(dir); free(file_name); return true; } else if(strcmp(file_name,"..")==0) { if( inode_get_inumber(dir_get_inode(dir))==1 ) { t->curr_dir = dir_open(inode_open(1)); free(file_name); dir_close(dir); return true; } else { t->curr_dir = dir_open( inode_open(inode_get_parent(dir_get_inode(dir)))); free(file_name); dir_close(dir); return true; } } success=dir_lookup (dir, file_name, &inode); } dir_close (dir); if(success) { t->curr_dir = dir_open(inode); } free(file_name); /* printf("changed dir sector: %d\n",inode_get_inumber(dir_get_inode(t->curr_dir))); printf("value %d\n",inode_get_data(dir_get_inode(t->curr_dir),0));*/ return success; }