Working filesystem.
[pintos-anon] / src / filesys / file.c
index e6f282f8062dc2c383f3a540c08b73d0a9628ce7..e16ff5fc695630cc55b4571f36d27a9d1f3e3fbb 100644 (file)
@@ -1,82 +1,74 @@
 #include "file.h"
-
-#ifdef FILESYS_STUB
 #include "debug.h"
-#include "filesys-stub.h"
 #include "lib.h"
 #include "malloc.h"
+#include "directory.h"
+#include "filehdr.h"
+#include "filesys.h"
 
 bool
-file_open (struct file *file, const char *name) 
-{
-  struct dir dir;
-  disk_sector_no hdr_sector;
-  bool success = false;
-
-  dir_init (&dir, NUM_DIR_ENTRIES);
-  dir_read (&dir, &root_dir_file);
-  if (dir_lookup (&dir, name, &hdr_sector))
-    success = file_open_sector (file, hdr_sector);
-
-  dir_destroy (&dir);
-  return success;
-}
-
-bool
-file_open_sector (struct file *file, disk_sector_no hdr_sector) 
+file_open (struct file *file, disk_sector_no hdr_sector) 
 {
+  file->hdr = filehdr_read (hdr_sector);
+  file->bounce = malloc (DISK_SECTOR_SIZE);
   file->pos = 0;
-  return filehdr_read (&file->hdr, hdr_sector);
+  if (file->hdr != NULL && file->bounce != NULL)
+    return true;
+  else
+    {
+      filehdr_destroy (file->hdr);
+      free (file->bounce);
+      return false;
+    }
 }
 
 void
 file_close (struct file *file) 
 {
-  filehdr_destroy (file);
+  filehdr_destroy (file->hdr);
 }
 
 off_t
 file_read (struct file *file, void *buffer, off_t size) 
 {
-  off_t retval = file_read_at (file, buffer, size, file->pos);
-  file->pos += retval;
-  return retval;
+  off_t bytes_read = file_read_at (file, buffer, size, file->pos);
+  file->pos += bytes_read;
+  return bytes_read;
 }
 
 off_t
-file_read_at (struct file *file, void *buffer_, off_t size, off_t start) 
+file_read_at (struct file *file, void *buffer_, off_t size,
+              off_t file_ofs) 
 {
   uint8_t *buffer = buffer_;
   off_t bytes_read = 0;
-  uint8_t *bounce;
 
-  bounce = malloc (DISK_SECTOR_SIZE);
   while (size > 0) 
     {
       /* Disk sector to read, starting byte offset within sector. */
-      off_t sector_idx = filehdr_byte_to_sector (file->hdr);
-      int sector_ofs = start % DISK_SECTOR_SIZE;
+      off_t sector_idx = filehdr_byte_to_sector (file->hdr, file_ofs);
+      int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
 
-      /* Bytes left in file, bytes left in sector. */
-      off_t file_left = filehdr_size (file->hdr) - start;
-      off_t sector_left = DISK_SECTOR_SIZE - sector_ofs;
+      /* Bytes left in file, bytes left in sector, lesser of the two. */
+      off_t file_left = filehdr_length (file->hdr) - file_ofs;
+      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
+      int min_left = file_left < sector_left ? file_left : sector_left;
 
       /* Number of bytes to actually copy out of this sector. */
-      int chunk_size = file_left < sector_left ? file_left : sector_left;
+      int chunk_size = size < min_left ? size : min_left;
       if (chunk_size == 0)
         break;
 
       /* Read sector into bounce buffer, then copy into caller's
          buffer. */
-      disk_read (disk, sector_idx, bounce);
-      memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
+      disk_read (filesys_disk, sector_idx, file->bounce);
+      memcpy (buffer + bytes_read, file->bounce + sector_ofs, chunk_size);
 
       /* Advance. */
       size -= chunk_size;
-      start += chunk_size;
+      file_ofs += chunk_size;
       bytes_read += chunk_size;
     }
-  free (bounce);
 
   return bytes_read;
 }
@@ -84,88 +76,72 @@ file_read_at (struct file *file, void *buffer_, off_t size, off_t start)
 off_t
 file_write (struct file *file, const void *buffer, off_t size) 
 {
-  off_t retval = file_write_at (file, buffer, size, file->pos);
-  file->pos += retval;
-  return retval;
+  off_t bytes_written = file_write_at (file, buffer, size, file->pos);
+  file->pos += bytes_written;
+  return bytes_written;
 }
 
 off_t
 file_write_at (struct file *file, const void *buffer_, off_t size,
-               off_t start
+               off_t file_ofs
 {
-  uint8_t *buffer = buffer_;
-  off_t bytes_read = 0;
-  uint8_t *bounce;
+  const uint8_t *buffer = buffer_;
+  off_t bytes_written = 0;
 
-  bounce = malloc (DISK_SECTOR_SIZE);
   while (size > 0) 
     {
-      /* Disk sector to read, starting byte offset within sector. */
-      off_t sector_idx = filehdr_byte_to_sector (file->hdr);
-      int sector_ofs = start % DISK_SECTOR_SIZE;
+      /* Starting byte offset within sector to read. */
+      off_t sector_idx = filehdr_byte_to_sector (file->hdr, file_ofs);
+      int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
 
-      /* Bytes left in file, bytes left in sector. */
-      off_t file_left = filehdr_size (file->hdr) - start;
-      off_t sector_left = DISK_SECTOR_SIZE - sector_ofs;
+      /* Bytes left in file, bytes left in sector, lesser of the two. */
+      off_t file_left = filehdr_length (file->hdr) - file_ofs;
+      int sector_left = DISK_SECTOR_SIZE - sector_ofs;
+      int min_left = file_left < sector_left ? file_left : sector_left;
 
-      /* Number of bytes to actually copy out of this sector. */
-      int chunk_size = file_left < sector_left ? file_left : sector_left;
+      /* Number of bytes to actually writen into this sector. */
+      int chunk_size = size < min_left ? size : min_left;
       if (chunk_size == 0)
         break;
 
-      /* Read sector into bounce buffer, then copy into caller's
-         buffer. */
-      disk_read (disk, sector_idx, bounce);
-      memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
+      /* If the sector contains data before or after the chunk
+         we're writing, then we need to read in the sector
+         first.  Otherwise we start with a sector of all zeros. */
+      if (sector_ofs > 0 || chunk_size < sector_ofs)
+        disk_read (filesys_disk, sector_idx, file->bounce);
+      else
+        memset (file->bounce, 0, DISK_SECTOR_SIZE);
+      memcpy (file->bounce + sector_ofs, buffer + bytes_written, chunk_size);
+      disk_write (filesys_disk, sector_idx, file->bounce);
 
       /* Advance. */
       size -= chunk_size;
-      start += chunk_size;
-      bytes_read += chunk_size;
+      file_ofs += chunk_size;
+      bytes_written += chunk_size;
     }
-  free (bounce);
+  free (file->bounce);
 
-  return bytes_read;
+  return bytes_written;
 }
 
 off_t
 file_length (struct file *file) 
 {
-  int32_t length;
-
-  filesys_stub_lock ();
-  filesys_stub_put_string ("length");
-  filesys_stub_put_file (file);
-  filesys_stub_match_string ("length");
-  length = filesys_stub_get_int32 ();
-  filesys_stub_unlock ();
-
-  return length;
+  ASSERT (file != NULL);
+  return filehdr_length (file->hdr);
 }
 
 void
-file_seek (struct file *file, off_t pos) 
+file_seek (struct file *file, off_t file_ofs) 
 {
-  filesys_stub_lock ();
-  filesys_stub_put_string ("seek");
-  filesys_stub_put_file (file);
-  filesys_stub_put_uint32 (pos);
-  filesys_stub_match_string ("seek");
-  filesys_stub_unlock ();
+  ASSERT (file != NULL);
+  ASSERT (file_ofs >= 0);
+  file->pos = file_ofs;
 }
 
 off_t
 file_tell (struct file *file) 
 {
-  int32_t pos;
-
-  filesys_stub_lock ();
-  filesys_stub_put_string ("tell");
-  filesys_stub_put_file (file);
-  filesys_stub_match_string ("tell");
-  pos = filesys_stub_get_int32 ();
-  filesys_stub_unlock ();
-
-  return pos;
+  ASSERT (file != NULL);
+  return file->pos;
 }
-#endif /* FILESYS_STUB */