5 #include "threads/malloc.h"
7 /* Initializes D as a directory that holds ENTRY_CNT entries. */
9 dir_init (struct dir *d, size_t entry_cnt)
12 d->entry_cnt = entry_cnt;
13 d->entries = calloc (1, sizeof *d->entries * entry_cnt);
14 return d->entries != NULL;
17 /* Destroys D and frees associated resources. */
19 dir_destroy (struct dir *d)
25 /* Returns the size of D in bytes. */
27 dir_size (struct dir *d)
30 return d->entry_cnt * sizeof *d->entries;
34 D must have already been initialized, to the correct number of
35 entries, with dir_init(). */
37 dir_read (struct dir *d, struct file *file)
40 ASSERT (file != NULL);
41 ASSERT (file_length (file) >= dir_size (d));
43 file_read_at (file, d->entries, dir_size (d), 0);
47 D must have already been initialized, to the correct number of
48 entries, with dir_init(). */
50 dir_write (struct dir *d, struct file *file)
52 file_write_at (file, d->entries, dir_size (d), 0);
55 /* Searches D for a file named NAME.
56 If successful, returns the file's entry;
57 otherwise, returns a null pointer. */
58 static struct dir_entry *
59 lookup (const struct dir *d, const char *name)
64 ASSERT (name != NULL);
66 if (strlen (name) > NAME_MAX)
69 for (i = 0; i < d->entry_cnt; i++)
71 struct dir_entry *e = &d->entries[i];
72 if (e->in_use && !strcmp (name, e->name))
78 /* Searches D for a file named NAME
79 and returns true if one exists, false otherwise.
80 If FILEHDR_SECTOR is nonnull, then on success *FILEHDR_SECTOR
81 is set to the sector that contains the file's header. */
83 dir_lookup (const struct dir *d, const char *name,
84 disk_sector_t *filehdr_sector)
86 const struct dir_entry *e;
89 ASSERT (name != NULL);
94 if (filehdr_sector != NULL)
95 *filehdr_sector = e->filehdr_sector;
102 /* Adds a file named NAME to D, which must not already contain a
103 file by that name. The file's header is in sector
105 Returns true if successful, false on failure.
106 Fails if NAME is invalid (i.e. too long) or if D has no free
107 directory entries. */
109 dir_add (struct dir *d, const char *name, disk_sector_t filehdr_sector)
114 ASSERT (name != NULL);
115 ASSERT (lookup (d, name) == NULL);
117 if (strlen (name) > NAME_MAX)
120 for (i = 0; i < d->entry_cnt; i++)
122 struct dir_entry *e = &d->entries[i];
126 strlcpy (e->name, name, sizeof e->name);
127 e->filehdr_sector = filehdr_sector;
134 /* Removes any entry for NAME in D.
135 Returns true if successful, false on failure,
136 which occurs only if there is no file with the given NAME. */
138 dir_remove (struct dir *d, const char *name)
143 ASSERT (name != NULL);
145 e = lookup (d, name);
155 /* Prints the names of the files in D to the system console. */
157 dir_list (const struct dir *d)
161 for (e = d->entries; e < d->entries + d->entry_cnt; e++)
163 printk ("%s\n", e->name);
166 /* Dumps the contents of D, including its files' names and their
167 contents, to the system console. */
169 dir_dump (const struct dir *d)
173 for (e = d->entries; e < d->entries + d->entry_cnt; e++)
176 printk ("Contents of %s:\n", e->name);
177 fsutil_print (e->name);