X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffilesys%2Ffsutil.c;h=14a45079c7a65d74166b8cd2b720fb6c9b10dfbe;hb=000aa464d48cc64e885137e1a8040aca75e9344a;hp=3876ee9775bb88fc7a8181a71d4daf7c10be9336;hpb=993c1d9f4452e2edd851f3175dfdf317f18bdb9f;p=pintos-anon diff --git a/src/filesys/fsutil.c b/src/filesys/fsutil.c index 3876ee9..14a4507 100644 --- a/src/filesys/fsutil.c +++ b/src/filesys/fsutil.c @@ -1,128 +1,197 @@ #include "filesys/fsutil.h" #include -#include #include #include #include +#include "filesys/directory.h" #include "filesys/file.h" #include "filesys/filesys.h" -#include "threads/mmu.h" +#include "devices/disk.h" +#include "threads/malloc.h" #include "threads/palloc.h" +#include "threads/vaddr.h" -/* Filename and file size to use for copy operations, - as "filename:size". */ -char *fsutil_copy_arg; +/* 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"); +} -/* Name of a file print to print to console. */ -char *fsutil_print_file; +/* 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; -/* Name of a file to delete. */ -char *fsutil_remove_file; + 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; -/* List all files in the filesystem to the system console? */ -bool fsutil_list_files; + 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); +} -/* Dump full contents of filesystem to the system console? */ -bool fsutil_dump_filesys; +/* Copies from the "scratch" disk, hdc or hd1:0 to file ARGV[1] + in the file system. -/* 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) + 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_t sector; + off_t size; void *buffer; - /* Open source disk. */ + 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 hd1:0)"); - if (size > (off_t) disk_size (src) * DISK_SECTOR_SIZE) - PANIC ("source disk is too small for %lld-byte file", - (unsigned long long) size); + + /* 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); - dst = filesys_open (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", filename); + 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 %lld bytes unwritten", - filename, (unsigned long long) size); + PANIC ("%s: write failed with %"PROTd" bytes unwritten", + file_name, size); size -= chunk_size; } - palloc_free (buffer); + /* Finish up. */ file_close (dst); + free (buffer); } -/* Executes the filesystem operations described by the variables - declared in fsutil.h. */ -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); +/* Copies file FILE_NAME from the file system to the scratch disk. - if (filename == NULL || size == NULL) - PANIC ("bad format for -cp option; use -u for usage"); - - 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)) - printf ("%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"); -/* Prints the contents of file FILENAME to the system console as - hex and ASCII. */ -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); - file = filesys_open (filename); - if (file == NULL) - 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); }