X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffilesys%2Fdirectory.c;h=45bbdaed320b3ff801e72588c8518fa670db0bb2;hb=872c41a62e291a3a277188c02e32674841f670c2;hp=5f7dd9ed002ee0bf16582c5a65b80306a8c1832c;hpb=4163bd7a767d1556c831c96c6381b18f65946d79;p=pintos-anon 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. */