X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffilesys%2Ffsutil.c;h=9c81fb015b605432ee2d60198454d1ce48e21bdf;hb=f415a37905c57f61b444806bf84f5405184452aa;hp=57cd4e8d5d2024faf3c21eed04ca1ec2819dae2c;hpb=4e56c3db3a372c96da21981f270a791e29452039;p=pintos-anon diff --git a/src/filesys/fsutil.c b/src/filesys/fsutil.c index 57cd4e8..9c81fb0 100644 --- a/src/filesys/fsutil.c +++ b/src/filesys/fsutil.c @@ -1,107 +1,189 @@ -#include "fsutil.h" -#include -#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 +#include +#include +#include +#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) +{ + printf ("Files in the root directory:\n"); + filesys_list (); + 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 *filename = argv[1]; + + struct file *file; + char *buffer; + + printf ("Printing '%s' to the console...\n", filename); + file = filesys_open (filename); + if (file == NULL) + PANIC ("%s: open failed", filename); + 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 *filename = argv[1]; + + printf ("Deleting '%s'...\n", filename); + if (!filesys_remove (filename)) + PANIC ("%s: delete failed\n", filename); +} + +/* Copies from the "scratch" disk, hdc or hd1:0 to file ARGV[1] + in the filesystem. + + 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 *filename = 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", filename); + + /* 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", filename); + size = ((int32_t *) buffer)[1]; + if (size < 0) + PANIC ("%s: invalid file size %d", filename, 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. */ - 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", + filename, 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 FILENAME 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 *filename = 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", filename); - 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; - - if (!filesys_open (filename, &file)) + /* Open source file. */ + src = filesys_open (filename); + if (src == NULL) PANIC ("%s: open failed", filename); - buffer = palloc_get (PAL_ASSERT); - for (;;) - { - off_t n = file_read (&file, buffer, PGSIZE); - if (n == 0) - break; + size = file_length (src); - hex_dump (buffer, n, true); + /* 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) + { + int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size; + if (sector >= disk_size (dst)) + PANIC ("%s: out of space on scratch disk", filename); + if (file_read (src, buffer, chunk_size) != chunk_size) + PANIC ("%s: read failed with %"PROTd" bytes unread", filename, 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); }