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 /* 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_in (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_page (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);
66 palloc_free_page (buffer);
71 /* Copies FILENAME from the file system to the scratch disk.
72 The first four bytes of the first sector in the disk
73 receive the file's size in bytes as a little-endian integer.
74 The second and subsequent sectors receive the file's data. */
76 copy_out (const char *filename)
84 buffer = palloc_get_page (PAL_ASSERT | PAL_ZERO);
86 /* Open source file. */
87 src = filesys_open (filename);
89 PANIC ("%s: open failed", filename);
90 size = file_length (src);
92 /* Open target disk. */
93 dst = disk_get (1, 0);
95 PANIC ("couldn't open target disk (hdc or hd1:0)");
96 if (size + DISK_SECTOR_SIZE > (off_t) disk_size (dst) * DISK_SECTOR_SIZE)
97 PANIC ("target disk is too small for %lld-byte file",
98 (unsigned long long) size);
100 /* Write size to sector 0. */
101 *(uint32_t *) buffer = size;
102 disk_write (dst, 0, buffer);
108 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
109 if (file_read (src, buffer, chunk_size) != chunk_size)
110 PANIC ("%s: read failed with %lld bytes unread",
111 filename, (unsigned long long) size);
112 disk_write (dst, sector++, buffer);
115 palloc_free_page (buffer);
120 /* Executes the filesystem operations described by the variables
121 declared in fsutil.h. */
125 if (fsutil_copyin_file != NULL)
126 copy_in (fsutil_copyin_file, fsutil_copyin_size);
128 if (fsutil_copyout_file != NULL)
129 copy_out (fsutil_copyout_file);
131 if (fsutil_print_file != NULL)
132 fsutil_print (fsutil_print_file);
134 if (fsutil_remove_file != NULL)
136 if (filesys_remove (fsutil_remove_file))
137 printf ("%s: removed\n", fsutil_remove_file);
139 PANIC ("%s: remove failed\n", fsutil_remove_file);
142 if (fsutil_list_files)
146 /* Prints the contents of file FILENAME to the system console as
149 fsutil_print (const char *filename)
154 file = filesys_open (filename);
156 PANIC ("%s: open failed", filename);
157 buffer = palloc_get_page (PAL_ASSERT);
160 off_t n = file_read (file, buffer, PGSIZE);
164 hex_dump (0, buffer, n, true);
166 palloc_free_page (buffer);