1 #include "filesys/fsutil.h"
7 #include "filesys/file.h"
8 #include "filesys/filesys.h"
9 #include "threads/mmu.h"
10 #include "threads/palloc.h"
12 /* Filename and file size to use for copy operations,
13 as "filename:size". */
14 char *fsutil_copy_arg;
16 /* Name of a file print to print to console. */
17 char *fsutil_print_file;
19 /* Name of a file to delete. */
20 char *fsutil_remove_file;
22 /* List all files in the filesystem to the system console? */
23 bool fsutil_list_files;
25 /* Dump full contents of filesystem to the system console? */
26 bool fsutil_dump_filesys;
28 /* Copies from the "scratch" disk, hdc or hd1:0,
29 to a file named FILENAME in the filesystem.
30 The file will be SIZE bytes in length. */
32 copy (const char *filename, off_t size)
39 /* Open source disk. */
40 src = disk_get (1, 0);
42 PANIC ("couldn't open source disk (hdc or hd1:0)");
43 if (size > (off_t) disk_size (src) * DISK_SECTOR_SIZE)
44 PANIC ("source disk is too small for %lld-byte file",
45 (unsigned long long) size);
47 /* Create destination file. */
48 if (!filesys_create (filename, size))
49 PANIC ("%s: create failed", filename);
50 dst = filesys_open (filename);
52 PANIC ("%s: open failed", filename);
55 buffer = palloc_get (PAL_ASSERT);
59 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
60 disk_read (src, sector++, buffer);
61 if (file_write (dst, buffer, chunk_size) != chunk_size)
62 PANIC ("%s: write failed with %lld bytes unwritten",
63 filename, (unsigned long long) size);
71 /* Executes the filesystem operations described by the variables
72 declared in fsutil.h. */
76 if (fsutil_copy_arg != NULL)
79 char *filename = strtok_r (fsutil_copy_arg, ":", &save);
80 char *size = strtok_r (NULL, "", &save);
82 if (filename == NULL || size == NULL)
83 PANIC ("bad format for -cp option; use -u for usage");
85 copy (filename, atoi (size));
88 if (fsutil_print_file != NULL)
89 fsutil_print (fsutil_print_file);
91 if (fsutil_remove_file != NULL)
93 if (filesys_remove (fsutil_remove_file))
94 printf ("%s: removed\n", fsutil_remove_file);
96 PANIC ("%s: remove failed\n", fsutil_remove_file);
99 if (fsutil_list_files)
102 if (fsutil_dump_filesys)
106 /* Prints the contents of file FILENAME to the system console as
109 fsutil_print (const char *filename)
114 file = filesys_open (filename);
116 PANIC ("%s: open failed", filename);
117 buffer = palloc_get (PAL_ASSERT);
120 off_t n = file_read (file, buffer, PGSIZE);
124 hex_dump (buffer, n, true);
126 palloc_free (buffer);