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. */
#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);
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));
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);
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;
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;
}
\f
static void must_succeed_function (int, bool) NO_INLINE;