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 /* Destination filename and size for copy-in operations. */
13 char *fsutil_copyin_file;
14 int fsutil_copyin_size;
16 /* Source filename for copy-out operations. */
17 char *fsutil_copyout_file;
19 /* Name of a file print to print to console. */
20 char *fsutil_print_file;
22 /* Name of a file to delete. */
23 char *fsutil_remove_file;
25 /* List all files in the filesystem to the system console? */
26 bool fsutil_list_files;
28 /* Dump full contents of filesystem to the system console? */
29 bool fsutil_dump_filesys;
31 /* Copies from the "scratch" disk, hdc or hd1:0,
32 to a file named FILENAME in the filesystem.
33 The file will be SIZE bytes in length. */
35 copy_in (const char *filename, off_t size)
42 /* Open source disk. */
43 src = disk_get (1, 0);
45 PANIC ("couldn't open source disk (hdc or hd1:0)");
46 if (size > (off_t) disk_size (src) * DISK_SECTOR_SIZE)
47 PANIC ("source disk is too small for %lld-byte file",
48 (unsigned long long) size);
50 /* Create destination file. */
51 if (!filesys_create (filename, size))
52 PANIC ("%s: create failed", filename);
53 dst = filesys_open (filename);
55 PANIC ("%s: open failed", filename);
58 buffer = palloc_get_page (PAL_ASSERT);
62 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
63 disk_read (src, sector++, buffer);
64 if (file_write (dst, buffer, chunk_size) != chunk_size)
65 PANIC ("%s: write failed with %lld bytes unwritten",
66 filename, (unsigned long long) size);
69 palloc_free_page (buffer);
74 /* Copies FILENAME from the file system to the scratch disk.
75 The first four bytes of the first sector in the disk
76 receive the file's size in bytes as a little-endian integer.
77 The second and subsequent sectors receive the file's data. */
79 copy_out (const char *filename)
87 buffer = palloc_get_page (PAL_ASSERT | PAL_ZERO);
89 /* Open source file. */
90 src = filesys_open (filename);
92 PANIC ("%s: open failed", filename);
93 size = file_length (src);
95 /* Open target disk. */
96 dst = disk_get (1, 0);
98 PANIC ("couldn't open target disk (hdc or hd1:0)");
99 if (size + DISK_SECTOR_SIZE > (off_t) disk_size (dst) * DISK_SECTOR_SIZE)
100 PANIC ("target disk is too small for %lld-byte file",
101 (unsigned long long) size);
103 /* Write size to sector 0. */
104 *(uint32_t *) buffer = size;
105 disk_write (dst, 0, buffer);
111 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
112 if (file_read (src, buffer, chunk_size) != chunk_size)
113 PANIC ("%s: read failed with %lld bytes unread",
114 filename, (unsigned long long) size);
115 disk_write (dst, sector++, buffer);
118 palloc_free_page (buffer);
123 /* Executes the filesystem operations described by the variables
124 declared in fsutil.h. */
128 if (fsutil_copyin_file != NULL)
129 copy_in (fsutil_copyin_file, fsutil_copyin_size);
131 if (fsutil_copyout_file != NULL)
132 copy_out (fsutil_copyout_file);
134 if (fsutil_print_file != NULL)
135 fsutil_print (fsutil_print_file);
137 if (fsutil_remove_file != NULL)
139 if (filesys_remove (fsutil_remove_file))
140 printf ("%s: removed\n", fsutil_remove_file);
142 PANIC ("%s: remove failed\n", fsutil_remove_file);
145 if (fsutil_list_files)
148 if (fsutil_dump_filesys)
152 /* Prints the contents of file FILENAME to the system console as
155 fsutil_print (const char *filename)
160 file = filesys_open (filename);
162 PANIC ("%s: open failed", filename);
163 buffer = palloc_get_page (PAL_ASSERT);
166 off_t n = file_read (file, buffer, PGSIZE);
170 hex_dump (0, buffer, n, true);
172 palloc_free_page (buffer);