Rewrite filesystem to support Unix "delete" semantics.
[pintos-anon] / src / filesys / directory.c
index fba32f0c0242fe5fb62e1fb6857856199c6fb79e..ec6ecd98715d30ba99acff1449e15b0d440c92ea 100644 (file)
@@ -5,14 +5,44 @@
 #include "filesys/fsutil.h"
 #include "threads/malloc.h"
 
-/* Initializes D as a directory that holds ENTRY_CNT entries. */
-bool
-dir_init (struct dir *d, size_t entry_cnt) 
+/* A directory. */
+struct dir 
+  {
+    size_t entry_cnt;           /* Number of entries. */
+    struct dir_entry *entries;  /* Array of entries. */
+  };
+
+/* A single directory entry. */
+struct dir_entry 
+  {
+    bool in_use;                        /* In use or free? */
+    char name[NAME_MAX + 1];            /* Null terminated file name. */
+    disk_sector_t inode_sector;         /* Sector number of header. */
+  };
+
+/* Returns a new directory that holds ENTRY_CNT entries, if
+   successful, or a null pointer if memory is unavailable. */
+struct dir *
+dir_create (size_t entry_cnt) 
 {
-  ASSERT (d != NULL);
-  d->entry_cnt = entry_cnt;
-  d->entries = calloc (1, sizeof *d->entries * entry_cnt);
-  return d->entries != NULL;
+  struct dir *d = malloc (sizeof *d);
+  if (d != NULL)
+    {
+      d->entry_cnt = entry_cnt;
+      d->entries = calloc (1, sizeof *d->entries * entry_cnt);
+      if (d->entries != NULL)
+        return d;
+      free (d); 
+    }
+  return NULL;
+}
+
+/* Returns the size, in bytes, of a directory with ENTRY_CNT
+   entries. */
+size_t
+dir_size (size_t entry_cnt) 
+{
+  return entry_cnt * sizeof (struct dir_entry);
 }
 
 /* Destroys D and frees associated resources. */
@@ -20,15 +50,10 @@ void
 dir_destroy (struct dir *d) 
 {
   if (d != NULL) 
-    free (d->entries);
-}
-
-/* Returns the size of D in bytes. */
-static off_t
-dir_size (struct dir *d) 
-{
-  ASSERT (d != NULL);
-  return d->entry_cnt * sizeof *d->entries;
+    {
+      free (d->entries);
+      free (d); 
+    }
 }
 
 /* Reads D from FILE.
@@ -39,9 +64,9 @@ dir_read (struct dir *d, struct file *file)
 {
   ASSERT (d != NULL);
   ASSERT (file != NULL);
-  ASSERT (file_length (file) >= dir_size (d));
+  ASSERT (file_length (file) >= (off_t) dir_size (d->entry_cnt));
 
-  file_read_at (file, d->entries, dir_size (d), 0);
+  file_read_at (file, d->entries, dir_size (d->entry_cnt), 0);
 }
 
 /* Writes D to FILE.
@@ -50,7 +75,7 @@ dir_read (struct dir *d, struct file *file)
 void
 dir_write (struct dir *d, struct file *file) 
 {
-  file_write_at (file, d->entries, dir_size (d), 0);
+  file_write_at (file, d->entries, dir_size (d->entry_cnt), 0);
 }
 
 /* Searches D for a file named NAME.
@@ -78,11 +103,11 @@ lookup (const struct dir *d, const char *name)
 
 /* Searches D for a file named NAME
    and returns true if one exists, false otherwise.
-   If FILEHDR_SECTOR is nonnull, then on success *FILEHDR_SECTOR
-   is set to the sector that contains the file's header. */
+   If INODE_SECTOR is nonnull, then on success *INODE_SECTOR
+   is set to the sector that contains the file's inode. */
 bool
 dir_lookup (const struct dir *d, const char *name,
-            disk_sector_t *filehdr_sector) 
+            disk_sector_t *inode_sector) 
 {
   const struct dir_entry *e;
 
@@ -92,8 +117,8 @@ dir_lookup (const struct dir *d, const char *name,
   e = lookup (d, name);
   if (e != NULL) 
     {
-      if (filehdr_sector != NULL)
-        *filehdr_sector = e->filehdr_sector;
+      if (inode_sector != NULL)
+        *inode_sector = e->inode_sector;
       return true;
     }
   else
@@ -101,13 +126,13 @@ dir_lookup (const struct dir *d, const char *name,
 }
 
 /* Adds a file named NAME to D, which must not already contain a
-   file by that name.  The file's header is in sector
-   FILEHDR_SECTOR.
+   file by that name.  The file's inode is in sector
+   INODE_SECTOR.
    Returns true if successful, false on failure.
    Fails if NAME is invalid (i.e. too long) or if D has no free
    directory entries. */
 bool
-dir_add (struct dir *d, const char *name, disk_sector_t filehdr_sector) 
+dir_add (struct dir *d, const char *name, disk_sector_t inode_sector) 
 {
   size_t i;
   
@@ -125,7 +150,7 @@ dir_add (struct dir *d, const char *name, disk_sector_t filehdr_sector)
         {
           e->in_use = true;
           strlcpy (e->name, name, sizeof e->name);
-          e->filehdr_sector = filehdr_sector;
+          e->inode_sector = inode_sector;
           return true;
         }
     }