Rewrite filesystem to support Unix "delete" semantics.
[pintos-anon] / src / filesys / fsutil.c
index 57cd4e8d5d2024faf3c21eed04ca1ec2819dae2c..3876ee9775bb88fc7a8181a71d4daf7c10be9336 100644 (file)
@@ -1,38 +1,54 @@
-#include "fsutil.h"
+#include "filesys/fsutil.h"
+#include <debug.h>
 #include <stdbool.h>
-#include "debug.h"
-#include "filesys.h"
-#include "file.h"
-#include "lib.h"
-#include "mmu.h"
-#include "palloc.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "filesys/file.h"
+#include "filesys/filesys.h"
+#include "threads/mmu.h"
+#include "threads/palloc.h"
 
+/* Filename and file size to use for copy operations,
+   as "filename:size". */
 char *fsutil_copy_arg;
+
+/* Name of a file print to print to console. */
 char *fsutil_print_file;
+
+/* Name of a file to delete. */
 char *fsutil_remove_file;
+
+/* List all files in the filesystem to the system console? */
 bool fsutil_list_files;
+
+/* Dump full contents of filesystem to the system console? */
 bool fsutil_dump_filesys;
 
+/* Copies from the "scratch" disk, hdc or hd1:0,
+   to a file named FILENAME in the filesystem.
+   The file will be SIZE bytes in length. */
 static void
 copy (const char *filename, off_t size) 
 {
   struct disk *src;
-  struct file dst;
-  disk_sector_no sector;
+  struct file *dst;
+  disk_sector_t sector;
   void *buffer;
 
   /* Open source disk. */
-  src = disk_get (2);
+  src = disk_get (1, 0);
   if (src == NULL)
-    PANIC ("couldn't open source disk (hdc or ide1:0)");
+    PANIC ("couldn't open source disk (hdc or hd1:0)");
   if (size > (off_t) disk_size (src) * DISK_SECTOR_SIZE)
-    PANIC ("source disk is too small for %Ld-byte file",
+    PANIC ("source disk is too small for %lld-byte file",
            (unsigned long long) size);
   
   /* Create destination file. */
   if (!filesys_create (filename, size))
     PANIC ("%s: create failed", filename);
-  if (!filesys_open (filename, &dst))
+  dst = filesys_open (filename);
+  if (dst == NULL)
     PANIC ("%s: open failed", filename);
 
   /* Do copy. */
@@ -42,16 +58,18 @@ copy (const char *filename, off_t size)
     {
       int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
       disk_read (src, sector++, buffer);
-      if (file_write (&dst, buffer, chunk_size) != chunk_size)
-        PANIC ("%s: write failed with %Ld bytes unwritten",
+      if (file_write (dst, buffer, chunk_size) != chunk_size)
+        PANIC ("%s: write failed with %lld bytes unwritten",
                filename, (unsigned long long) size);
       size -= chunk_size;
     }
   palloc_free (buffer);
 
-  file_close (&dst);
+  file_close (dst);
 }
 
+/* Executes the filesystem operations described by the variables
+   declared in fsutil.h. */
 void
 fsutil_run (void) 
 {
@@ -73,7 +91,7 @@ fsutil_run (void)
   if (fsutil_remove_file != NULL) 
     {
       if (filesys_remove (fsutil_remove_file))
-        printk ("%s: removed\n", fsutil_remove_file);
+        printf ("%s: removed\n", fsutil_remove_file);
       else
         PANIC ("%s: remove failed\n", fsutil_remove_file);
     }
@@ -85,23 +103,26 @@ fsutil_run (void)
     filesys_dump ();
 }
 
+/* Prints the contents of file FILENAME to the system console as
+   hex and ASCII. */
 void
 fsutil_print (const char *filename) 
 {
-  struct file file;
+  struct file *file;
   char *buffer;
 
-  if (!filesys_open (filename, &file))
+  file = filesys_open (filename);
+  if (file == NULL)
     PANIC ("%s: open failed", filename);
   buffer = palloc_get (PAL_ASSERT);
   for (;;) 
     {
-      off_t n = file_read (&file, buffer, PGSIZE);
+      off_t n = file_read (file, buffer, PGSIZE);
       if (n == 0)
         break;
 
       hex_dump (buffer, n, true);
     }
   palloc_free (buffer);
-  file_close (&file);
+  file_close (file);
 }