From: Ben Pfaff Date: Fri, 19 May 2006 04:47:28 +0000 (+0000) Subject: Make directory interface more like file interface. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=872c41a62e291a3a277188c02e32674841f670c2;p=pintos-anon Make directory interface more like file interface. In reference solution, push locking up to inode layer, simplifying changes to directory code. --- diff --git a/src/filesys/directory.c b/src/filesys/directory.c index 5f7dd9e..45bbdae 100644 --- a/src/filesys/directory.c +++ b/src/filesys/directory.c @@ -28,37 +28,39 @@ dir_create (disk_sector_t sector, size_t entry_cnt) return inode_create (sector, entry_cnt * sizeof (struct dir_entry)); } -/* Opens the directory in the given INODE, of which it takes - ownership, and sets *DIRP to the new directory or a null - pointer on failure. Return true if successful, false on - failure. */ -bool -dir_open (struct inode *inode, struct dir **dirp) +/* Opens and returns the directory for the given INODE, of which + it takes ownership. Returns a null pointer on failure. */ +struct dir * +dir_open (struct inode *inode) { - struct dir *dir = NULL; - - ASSERT (dirp != NULL); - - if (inode != NULL) + struct dir *dir = calloc (1, sizeof *dir); + if (inode != NULL && dir != NULL) { - dir = malloc (sizeof *dir); - if (dir != NULL) - dir->inode = inode; + dir->inode = inode; + return dir; + } + else + { + inode_close (inode); + free (dir); + return NULL; } - - *dirp = dir; - if (dir == NULL) - inode_close (inode); - return dir != NULL; } -/* Opens the root directory and sets *DIRP to it or to a null - pointer on failure. Return true if successful, false on - failure. */ -bool -dir_open_root (struct dir **dirp) +/* Opens the root directory and returns a directory for it. + Return true if successful, false on failure. */ +struct dir * +dir_open_root (void) +{ + return dir_open (inode_open (ROOT_DIR_SECTOR)); +} + +/* Opens and returns a new directory for the same inode as DIR. + Returns a null pointer on failure. */ +struct dir * +dir_reopen (struct dir *dir) { - return dir_open (inode_open (ROOT_DIR_SECTOR), dirp); + return dir_open (inode_reopen (dir->inode)); } /* Destroys DIR and frees associated resources. */ diff --git a/src/filesys/directory.h b/src/filesys/directory.h index abc3029..cc8692d 100644 --- a/src/filesys/directory.h +++ b/src/filesys/directory.h @@ -12,10 +12,10 @@ #define NAME_MAX 14 struct inode; -struct dir; bool dir_create (disk_sector_t sector, size_t entry_cnt); -bool dir_open (struct inode *, struct dir **); -bool dir_open_root (struct dir **); +struct dir *dir_open (struct inode *); +struct dir *dir_open_root (void); +struct dir *dir_reopen (struct dir *); void dir_close (struct dir *); bool dir_lookup (const struct dir *, const char *name, struct inode **); bool dir_add (struct dir *, const char *name, disk_sector_t); diff --git a/src/filesys/filesys.c b/src/filesys/filesys.c index 0ca8e9e..39bdff9 100644 --- a/src/filesys/filesys.c +++ b/src/filesys/filesys.c @@ -46,9 +46,9 @@ filesys_done (void) bool filesys_create (const char *name, off_t initial_size) { - struct dir *dir; disk_sector_t inode_sector = 0; - bool success = (dir_open_root (&dir) + 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)); @@ -67,10 +67,10 @@ filesys_create (const char *name, off_t initial_size) struct file * filesys_open (const char *name) { - struct dir *dir; + struct dir *dir = dir_open_root (); struct inode *inode = NULL; - if (dir_open_root (&dir)) + if (dir != NULL) dir_lookup (dir, name, &inode); dir_close (dir); @@ -84,9 +84,8 @@ filesys_open (const char *name) bool filesys_remove (const char *name) { - struct dir *dir = NULL; - bool success = (dir_open_root (&dir) - && dir_remove (dir, name)); + struct dir *dir = dir_open_root (); + bool success = dir != NULL && dir_remove (dir, name); dir_close (dir); return success; @@ -99,13 +98,15 @@ filesys_remove (const char *name) bool filesys_list (void) { - struct dir *dir = NULL; - bool success = dir_open_root (&dir); - if (success) - dir_list (dir); - dir_close (dir); - - return success; + struct dir *dir = dir_open_root (); + if (dir != NULL) + { + dir_list (dir); + dir_close (dir); + return true; + } + else + return false; } static void must_succeed_function (int, bool) NO_INLINE;