Start work on partition support.
[pintos-anon] / src / filesys / file.c
index d059f379f5cda80173162f07828584d132519e6d..17001eab2855682907908a97ea64ce171246f717 100644 (file)
@@ -4,6 +4,7 @@
 #include "filesys/directory.h"
 #include "filesys/inode.h"
 #include "filesys/filesys.h"
+#include "devices/partition.h"
 #include "threads/malloc.h"
 
 /* An open file. */
@@ -75,7 +76,7 @@ file_read_at (struct file *file, void *buffer_, off_t size,
   while (size > 0) 
     {
       /* Disk sector to read, starting byte offset within sector. */
-      off_t sector_idx = inode_byte_to_sector (file->inode, file_ofs);
+      disk_sector_t sector_idx;
       int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
 
       /* Bytes left in file, bytes left in sector, lesser of the two. */
@@ -85,12 +86,13 @@ file_read_at (struct file *file, void *buffer_, off_t size,
 
       /* Number of bytes to actually copy out of this sector. */
       int chunk_size = size < min_left ? size : min_left;
-      if (chunk_size == 0)
+      if (chunk_size <= 0)
         break;
 
       /* Read sector into bounce buffer, then copy into caller's
          buffer. */
-      disk_read (filesys_disk, sector_idx, file->bounce);
+      sector_idx = inode_byte_to_sector (file->inode, file_ofs);
+      partition_read (filesys_partition, sector_idx, file->bounce);
       memcpy (buffer + bytes_read, file->bounce + sector_ofs, chunk_size);
 
       /* Advance. */
@@ -133,8 +135,8 @@ file_write_at (struct file *file, const void *buffer_, off_t size,
 
   while (size > 0) 
     {
-      /* Starting byte offset within sector to read. */
-      off_t sector_idx = inode_byte_to_sector (file->inode, file_ofs);
+      /* Sector to write, starting byte offset within sector. */
+      off_t sector_idx;
       int sector_ofs = file_ofs % DISK_SECTOR_SIZE;
 
       /* Bytes left in file, bytes left in sector, lesser of the two. */
@@ -142,20 +144,21 @@ file_write_at (struct file *file, const void *buffer_, off_t size,
       int sector_left = DISK_SECTOR_SIZE - sector_ofs;
       int min_left = file_left < sector_left ? file_left : sector_left;
 
-      /* Number of bytes to actually writen into this sector. */
+      /* Number of bytes to actually write into this sector. */
       int chunk_size = size < min_left ? size : min_left;
-      if (chunk_size == 0)
+      if (chunk_size <= 0)
         break;
 
       /* 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);
+      sector_idx = inode_byte_to_sector (file->inode, file_ofs);
+      if (sector_ofs > 0 || chunk_size < sector_left)
+        partition_read (filesys_partition, 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);
+      partition_write (filesys_partition, sector_idx, file->bounce);
 
       /* Advance. */
       size -= chunk_size;