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 if (!filesys_open (filename, &dst))
51 PANIC ("%s: open failed", filename);
54 buffer = palloc_get (PAL_ASSERT);
58 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
59 disk_read (src, sector++, buffer);
60 if (file_write (&dst, buffer, chunk_size) != chunk_size)
61 PANIC ("%s: write failed with %lld bytes unwritten",
62 filename, (unsigned long long) size);
70 /* Executes the filesystem operations described by the variables
71 declared in fsutil.h. */
75 if (fsutil_copy_arg != NULL)
78 char *filename = strtok_r (fsutil_copy_arg, ":", &save);
79 char *size = strtok_r (NULL, "", &save);
81 if (filename == NULL || size == NULL)
82 PANIC ("bad format for -cp option; use -u for usage");
84 copy (filename, atoi (size));
87 if (fsutil_print_file != NULL)
88 fsutil_print (fsutil_print_file);
90 if (fsutil_remove_file != NULL)
92 if (filesys_remove (fsutil_remove_file))
93 printf ("%s: removed\n", fsutil_remove_file);
95 PANIC ("%s: remove failed\n", fsutil_remove_file);
98 if (fsutil_list_files)
101 if (fsutil_dump_filesys)
105 /* Prints the contents of file FILENAME to the system console as
108 fsutil_print (const char *filename)
113 if (!filesys_open (filename, &file))
114 PANIC ("%s: open failed", filename);
115 buffer = palloc_get (PAL_ASSERT);
118 off_t n = file_read (&file, buffer, PGSIZE);
122 hex_dump (buffer, n, true);
124 palloc_free (buffer);