1 #include "filesys/directory.h"
5 #include "filesys/filesys.h"
6 #include "filesys/inode.h"
7 #include "threads/malloc.h"
12 struct inode *inode; /* Backing store. */
15 /* A single directory entry. */
18 disk_sector_t inode_sector; /* Sector number of header. */
19 char name[NAME_MAX + 1]; /* Null terminated file name. */
20 bool in_use; /* In use or free? */
23 /* Creates a directory with space for ENTRY_CNT entries in the
24 given SECTOR. Returns true if successful, false on failure. */
26 dir_create (disk_sector_t sector, size_t entry_cnt)
28 return inode_create (sector, entry_cnt * sizeof (struct dir_entry));
31 /* Opens and returns the directory for the given INODE, of which
32 it takes ownership. Returns a null pointer on failure. */
34 dir_open (struct inode *inode)
36 struct dir *dir = calloc (1, sizeof *dir);
37 if (inode != NULL && dir != NULL)
50 /* Opens the root directory and returns a directory for it.
51 Return true if successful, false on failure. */
55 return dir_open (inode_open (ROOT_DIR_SECTOR));
58 /* Opens and returns a new directory for the same inode as DIR.
59 Returns a null pointer on failure. */
61 dir_reopen (struct dir *dir)
63 return dir_open (inode_reopen (dir->inode));
66 /* Destroys DIR and frees associated resources. */
68 dir_close (struct dir *dir)
72 inode_close (dir->inode);
77 /* Searches DIR for a file with the given NAME.
78 If successful, returns true, sets *EP to the directory entry
79 if EP is non-null, and sets *OFSP to the byte offset of the
80 directory entry if OFSP is non-null.
81 otherwise, returns false and ignores EP and OFSP. */
83 lookup (const struct dir *dir, const char *name,
84 struct dir_entry *ep, off_t *ofsp)
90 ASSERT (name != NULL);
92 for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
94 if (e.in_use && !strcmp (name, e.name))
105 /* Searches DIR for a file with the given NAME
106 and returns true if one exists, false otherwise.
107 On success, sets *INODE to an inode for the file, otherwise to
108 a null pointer. The caller must close *INODE. */
110 dir_lookup (const struct dir *dir, const char *name,
111 struct inode **inode)
115 ASSERT (dir != NULL);
116 ASSERT (name != NULL);
118 if (lookup (dir, name, &e, NULL))
119 *inode = inode_open (e.inode_sector);
123 return *inode != NULL;
126 /* Adds a file named NAME to DIR, which must not already contain a
127 file by that name. The file's inode is in sector
129 Returns true if successful, false on failure.
130 Fails if NAME is invalid (i.e. too long) or a disk or memory
133 dir_add (struct dir *dir, const char *name, disk_sector_t inode_sector)
137 bool success = false;
139 ASSERT (dir != NULL);
140 ASSERT (name != NULL);
142 /* Check NAME for validity. */
143 if (*name == '\0' || strlen (name) > NAME_MAX)
146 /* Check that NAME is not in use. */
147 if (lookup (dir, name, NULL, NULL))
150 /* Set OFS to offset of free slot.
151 If there are no free slots, then it will be set to the
154 inode_read_at() will only return a short read at end of file.
155 Otherwise, we'd need to verify that we didn't get a short
156 read due to something intermittent such as low memory. */
157 for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
164 strlcpy (e.name, name, sizeof e.name);
165 e.inode_sector = inode_sector;
166 success = inode_write_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
172 /* Removes any entry for NAME in DIR.
173 Returns true if successful, false on failure,
174 which occurs only if there is no file with the given NAME. */
176 dir_remove (struct dir *dir, const char *name)
179 struct inode *inode = NULL;
180 bool success = false;
183 ASSERT (dir != NULL);
184 ASSERT (name != NULL);
186 /* Find directory entry. */
187 if (!lookup (dir, name, &e, &ofs))
191 inode = inode_open (e.inode_sector);
195 /* Erase directory entry. */
197 if (inode_write_at (dir->inode, &e, sizeof e, ofs) != sizeof e)
201 inode_remove (inode);
209 /* Prints the names of the files in DIR to the system console. */
211 dir_list (const struct dir *dir)
216 for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
219 printf ("%s\n", e.name);