1 #include "filesys/directory.h"
4 #include "filesys/file.h"
5 #include "filesys/fsutil.h"
6 #include "threads/malloc.h"
8 /* Initializes D as a directory that holds ENTRY_CNT entries. */
10 dir_init (struct dir *d, size_t entry_cnt)
13 d->entry_cnt = entry_cnt;
14 d->entries = calloc (1, sizeof *d->entries * entry_cnt);
15 return d->entries != NULL;
18 /* Destroys D and frees associated resources. */
20 dir_destroy (struct dir *d)
26 /* Returns the size of D in bytes. */
28 dir_size (struct dir *d)
31 return d->entry_cnt * sizeof *d->entries;
35 D must have already been initialized, to the correct number of
36 entries, with dir_init(). */
38 dir_read (struct dir *d, struct file *file)
41 ASSERT (file != NULL);
42 ASSERT (file_length (file) >= dir_size (d));
44 file_read_at (file, d->entries, dir_size (d), 0);
48 D must have already been initialized, to the correct number of
49 entries, with dir_init(). */
51 dir_write (struct dir *d, struct file *file)
53 file_write_at (file, d->entries, dir_size (d), 0);
56 /* Searches D for a file named NAME.
57 If successful, returns the file's entry;
58 otherwise, returns a null pointer. */
59 static struct dir_entry *
60 lookup (const struct dir *d, const char *name)
65 ASSERT (name != NULL);
67 if (strlen (name) > NAME_MAX)
70 for (i = 0; i < d->entry_cnt; i++)
72 struct dir_entry *e = &d->entries[i];
73 if (e->in_use && !strcmp (name, e->name))
79 /* Searches D for a file named NAME
80 and returns true if one exists, false otherwise.
81 If FILEHDR_SECTOR is nonnull, then on success *FILEHDR_SECTOR
82 is set to the sector that contains the file's header. */
84 dir_lookup (const struct dir *d, const char *name,
85 disk_sector_t *filehdr_sector)
87 const struct dir_entry *e;
90 ASSERT (name != NULL);
95 if (filehdr_sector != NULL)
96 *filehdr_sector = e->filehdr_sector;
103 /* Adds a file named NAME to D, which must not already contain a
104 file by that name. The file's header is in sector
106 Returns true if successful, false on failure.
107 Fails if NAME is invalid (i.e. too long) or if D has no free
108 directory entries. */
110 dir_add (struct dir *d, const char *name, disk_sector_t filehdr_sector)
115 ASSERT (name != NULL);
116 ASSERT (lookup (d, name) == NULL);
118 if (strlen (name) > NAME_MAX)
121 for (i = 0; i < d->entry_cnt; i++)
123 struct dir_entry *e = &d->entries[i];
127 strlcpy (e->name, name, sizeof e->name);
128 e->filehdr_sector = filehdr_sector;
135 /* Removes any entry for NAME in D.
136 Returns true if successful, false on failure,
137 which occurs only if there is no file with the given NAME. */
139 dir_remove (struct dir *d, const char *name)
144 ASSERT (name != NULL);
146 e = lookup (d, name);
156 /* Prints the names of the files in D to the system console. */
158 dir_list (const struct dir *d)
162 for (e = d->entries; e < d->entries + d->entry_cnt; e++)
164 printf ("%s\n", e->name);
167 /* Dumps the contents of D, including its files' names and their
168 contents, to the system console. */
170 dir_dump (const struct dir *d)
174 for (e = d->entries; e < d->entries + d->entry_cnt; e++)
177 printf ("Contents of %s:\n", e->name);
178 fsutil_print (e->name);