struct dir
{
struct inode *inode; /* Backing store. */
+ off_t pos; /* Current position. */
};
/* A single directory entry. */
struct dir_entry
{
- disk_sector_t inode_sector; /* Sector number of header. */
+ block_sector_t inode_sector; /* Sector number of header. */
char name[NAME_MAX + 1]; /* Null terminated file name. */
bool in_use; /* In use or free? */
};
/* Creates a directory with space for ENTRY_CNT entries in the
given SECTOR. Returns true if successful, false on failure. */
bool
-dir_create (disk_sector_t sector, size_t entry_cnt)
+dir_create (block_sector_t sector, size_t entry_cnt)
{
return inode_create (sector, entry_cnt * sizeof (struct dir_entry));
}
if (inode != NULL && dir != NULL)
{
dir->inode = inode;
+ dir->pos = 0;
return dir;
}
else
}
}
+/* Returns the inode encapsulated by DIR. */
+struct inode *
+dir_get_inode (struct dir *dir)
+{
+ return dir->inode;
+}
+
/* Searches DIR for a file with the given NAME.
If successful, returns true, sets *EP to the directory entry
if EP is non-null, and sets *OFSP to the byte offset of the
Fails if NAME is invalid (i.e. too long) or a disk or memory
error occurs. */
bool
-dir_add (struct dir *dir, const char *name, disk_sector_t inode_sector)
+dir_add (struct dir *dir, const char *name, block_sector_t inode_sector)
{
struct dir_entry e;
off_t ofs;
bool success = false;
-
+
ASSERT (dir != NULL);
ASSERT (name != NULL);
return success;
}
-/* Prints the names of the files in DIR to the system console. */
-void
-dir_list (const struct dir *dir)
+/* Reads the next directory entry in DIR and stores the name in
+ NAME. Returns true if successful, false if the directory
+ contains no more entries. */
+bool
+dir_readdir (struct dir *dir, char name[NAME_MAX + 1])
{
struct dir_entry e;
- size_t ofs;
-
- for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
- ofs += sizeof e)
- if (e.in_use)
- printf ("%s\n", e.name);
+
+ while (inode_read_at (dir->inode, &e, sizeof e, dir->pos) == sizeof e)
+ {
+ dir->pos += sizeof e;
+ if (e.in_use)
+ {
+ strlcpy (name, e.name, NAME_MAX + 1);
+ return true;
+ }
+ }
+ return false;
}