Consistently spell "file name" and "file system" as two words.
[pintos-anon] / src / filesys / fsutil.c
index 57cd4e8d5d2024faf3c21eed04ca1ec2819dae2c..14a45079c7a65d74166b8cd2b720fb6c9b10dfbe 100644 (file)
-#include "fsutil.h"
-#include <stdbool.h>
-#include "debug.h"
-#include "filesys.h"
-#include "file.h"
-#include "lib.h"
-#include "mmu.h"
-#include "palloc.h"
-
-char *fsutil_copy_arg;
-char *fsutil_print_file;
-char *fsutil_remove_file;
-bool fsutil_list_files;
-bool fsutil_dump_filesys;
-
-static void
-copy (const char *filename, off_t size) 
+#include "filesys/fsutil.h"
+#include <debug.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "filesys/directory.h"
+#include "filesys/file.h"
+#include "filesys/filesys.h"
+#include "devices/disk.h"
+#include "threads/malloc.h"
+#include "threads/palloc.h"
+#include "threads/vaddr.h"
+
+/* List files in the root directory. */
+void
+fsutil_ls (char **argv UNUSED) 
+{
+  struct dir *dir;
+  char name[NAME_MAX + 1];
+  
+  printf ("Files in the root directory:\n");
+  dir = dir_open_root ();
+  if (dir == NULL)
+    PANIC ("root dir open failed");
+  while (dir_readdir (dir, name))
+    printf ("%s\n", name);
+  printf ("End of listing.\n");
+}
+
+/* Prints the contents of file ARGV[1] to the system console as
+   hex and ASCII. */
+void
+fsutil_cat (char **argv)
+{
+  const char *file_name = argv[1];
+  
+  struct file *file;
+  char *buffer;
+
+  printf ("Printing '%s' to the console...\n", file_name);
+  file = filesys_open (file_name);
+  if (file == NULL)
+    PANIC ("%s: open failed", file_name);
+  buffer = palloc_get_page (PAL_ASSERT);
+  for (;;) 
+    {
+      off_t pos = file_tell (file);
+      off_t n = file_read (file, buffer, PGSIZE);
+      if (n == 0)
+        break;
+
+      hex_dump (pos, buffer, n, true); 
+    }
+  palloc_free_page (buffer);
+  file_close (file);
+}
+
+/* Deletes file ARGV[1]. */
+void
+fsutil_rm (char **argv) 
+{
+  const char *file_name = argv[1];
+  
+  printf ("Deleting '%s'...\n", file_name);
+  if (!filesys_remove (file_name))
+    PANIC ("%s: delete failed\n", file_name);
+}
+
+/* Copies from the "scratch" disk, hdc or hd1:0 to file ARGV[1]
+   in the file system.
+
+   The current sector on the scratch disk must begin with the
+   string "PUT\0" followed by a 32-bit little-endian integer
+   indicating the file size in bytes.  Subsequent sectors hold
+   the file content.
+
+   The first call to this function will read starting at the
+   beginning of the scratch disk.  Later calls advance across the
+   disk.  This disk position is independent of that used for
+   fsutil_get(), so all `put's should precede all `get's. */
+void
+fsutil_put (char **argv) 
 {
+  static disk_sector_t sector = 0;
+
+  const char *file_name = argv[1];
   struct disk *src;
-  struct file dst;
-  disk_sector_no sector;
+  struct file *dst;
+  off_t size;
   void *buffer;
 
-  /* Open source disk. */
-  src = disk_get (2);
+  printf ("Putting '%s' into the file system...\n", file_name);
+
+  /* Allocate buffer. */
+  buffer = malloc (DISK_SECTOR_SIZE);
+  if (buffer == NULL)
+    PANIC ("couldn't allocate buffer");
+
+  /* Open source disk and read file size. */
+  src = disk_get (1, 0);
   if (src == NULL)
-    PANIC ("couldn't open source disk (hdc or ide1:0)");
-  if (size > (off_t) disk_size (src) * DISK_SECTOR_SIZE)
-    PANIC ("source disk is too small for %Ld-byte file",
-           (unsigned long long) size);
+    PANIC ("couldn't open source disk (hdc or hd1:0)");
+
+  /* Read file size. */
+  disk_read (src, sector++, buffer);
+  if (memcmp (buffer, "PUT", 4))
+    PANIC ("%s: missing PUT signature on scratch disk", file_name);
+  size = ((int32_t *) buffer)[1];
+  if (size < 0)
+    PANIC ("%s: invalid file size %d", file_name, size);
   
   /* Create destination file. */
-  if (!filesys_create (filename, size))
-    PANIC ("%s: create failed", filename);
-  if (!filesys_open (filename, &dst))
-    PANIC ("%s: open failed", filename);
+  if (!filesys_create (file_name, size))
+    PANIC ("%s: create failed", file_name);
+  dst = filesys_open (file_name);
+  if (dst == NULL)
+    PANIC ("%s: open failed", file_name);
 
   /* Do copy. */
-  buffer = palloc_get (PAL_ASSERT);
-  sector = 0;
   while (size > 0)
     {
       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",
-               filename, (unsigned long long) size);
+      if (file_write (dst, buffer, chunk_size) != chunk_size)
+        PANIC ("%s: write failed with %"PROTd" bytes unwritten",
+               file_name, size);
       size -= chunk_size;
     }
-  palloc_free (buffer);
 
-  file_close (&dst);
+  /* Finish up. */
+  file_close (dst);
+  free (buffer);
 }
 
-void
-fsutil_run (void) 
-{
-  if (fsutil_copy_arg != NULL) 
-    {
-      char *save;
-      char *filename = strtok_r (fsutil_copy_arg, ":", &save);
-      char *size = strtok_r (NULL, "", &save);
-
-      if (filename == NULL || size == NULL)
-        PANIC ("bad format for -cp option; use -u for usage");
+/* Copies file FILE_NAME from the file system to the scratch disk.
 
-      copy (filename, atoi (size));
-    }
+   The current sector on the scratch disk will receive "GET\0"
+   followed by the file's size in bytes as a 32-bit,
+   little-endian integer.  Subsequent sectors receive the file's
+   data.
 
-  if (fsutil_print_file != NULL)
-    fsutil_print (fsutil_print_file);
+   The first call to this function will write starting at the
+   beginning of the scratch disk.  Later calls advance across the
+   disk.  This disk position is independent of that used for
+   fsutil_put(), so all `put's should precede all `get's. */
+void
+fsutil_get (char **argv)
+{
+  static disk_sector_t sector = 0;
 
-  if (fsutil_remove_file != NULL) 
-    {
-      if (filesys_remove (fsutil_remove_file))
-        printk ("%s: removed\n", fsutil_remove_file);
-      else
-        PANIC ("%s: remove failed\n", fsutil_remove_file);
-    }
+  const char *file_name = argv[1];
+  void *buffer;
+  struct file *src;
+  struct disk *dst;
+  off_t size;
 
-  if (fsutil_list_files)
-    filesys_list ();
+  printf ("Getting '%s' from the file system...\n", file_name);
 
-  if (fsutil_dump_filesys)
-    filesys_dump ();
-}
+  /* Allocate buffer. */
+  buffer = malloc (DISK_SECTOR_SIZE);
+  if (buffer == NULL)
+    PANIC ("couldn't allocate buffer");
 
-void
-fsutil_print (const char *filename) 
-{
-  struct file file;
-  char *buffer;
+  /* Open source file. */
+  src = filesys_open (file_name);
+  if (src == NULL)
+    PANIC ("%s: open failed", file_name);
+  size = file_length (src);
 
-  if (!filesys_open (filename, &file))
-    PANIC ("%s: open failed", filename);
-  buffer = palloc_get (PAL_ASSERT);
-  for (;;) 
+  /* Open target disk. */
+  dst = disk_get (1, 0);
+  if (dst == NULL)
+    PANIC ("couldn't open target disk (hdc or hd1:0)");
+  
+  /* Write size to sector 0. */
+  memset (buffer, 0, DISK_SECTOR_SIZE);
+  memcpy (buffer, "GET", 4);
+  ((int32_t *) buffer)[1] = size;
+  disk_write (dst, sector++, buffer);
+  
+  /* Do copy. */
+  while (size > 0) 
     {
-      off_t n = file_read (&file, buffer, PGSIZE);
-      if (n == 0)
-        break;
-
-      hex_dump (buffer, n, true);
+      int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
+      if (sector >= disk_size (dst))
+        PANIC ("%s: out of space on scratch disk", file_name);
+      if (file_read (src, buffer, chunk_size) != chunk_size)
+        PANIC ("%s: read failed with %"PROTd" bytes unread", file_name, size);
+      memset (buffer + chunk_size, 0, DISK_SECTOR_SIZE - chunk_size);
+      disk_write (dst, sector++, buffer);
+      size -= chunk_size;
     }
-  palloc_free (buffer);
-  file_close (&file);
+
+  /* Finish up. */
+  file_close (src);
+  free (buffer);
 }