1 #include "filesys/fsutil.h"
6 #include "filesys/file.h"
7 #include "filesys/filesys.h"
8 #include "devices/disk.h"
9 #include "threads/mmu.h"
10 #include "threads/malloc.h"
11 #include "threads/palloc.h"
13 /* List files in the root directory. */
15 fsutil_ls (char **argv UNUSED)
17 printf ("Files in the root directory:\n");
19 printf ("End of listing.\n");
22 /* Prints the contents of file ARGV[1] to the system console as
25 fsutil_cat (char **argv)
27 const char *filename = argv[1];
32 printf ("Printing '%s' to the console...\n", filename);
33 file = filesys_open (filename);
35 PANIC ("%s: open failed", filename);
36 buffer = palloc_get_page (PAL_ASSERT);
39 off_t pos = file_tell (file);
40 off_t n = file_read (file, buffer, PGSIZE);
44 hex_dump (pos, buffer, n, true);
46 palloc_free_page (buffer);
50 /* Deletes file ARGV[1]. */
52 fsutil_rm (char **argv)
54 const char *filename = argv[1];
56 printf ("Deleting '%s'...\n", filename);
57 if (!filesys_remove (filename))
58 PANIC ("%s: delete failed\n", filename);
61 /* Copies from the "scratch" disk, hdc or hd1:0 to file ARGV[1]
64 The current sector on the scratch disk must begin with the
65 string "PUT\0" followed by a 32-bit little-endian integer
66 indicating the file size in bytes. Subsequent sectors hold
69 The first call to this function will read starting at the
70 beginning of the scratch disk. Later calls advance across the
71 disk. This disk position is independent of that used for
72 fsutil_get(), so all `put's should precede all `get's. */
74 fsutil_put (char **argv)
76 static disk_sector_t sector = 0;
78 const char *filename = argv[1];
84 printf ("Putting '%s' into the file system...\n", filename);
86 /* Allocate buffer. */
87 buffer = malloc (DISK_SECTOR_SIZE);
89 PANIC ("couldn't allocate buffer");
91 /* Open source disk and read file size. */
92 src = disk_get (1, 0);
94 PANIC ("couldn't open source disk (hdc or hd1:0)");
97 disk_read (src, sector++, buffer);
98 if (memcmp (buffer, "PUT", 4))
99 PANIC ("%s: missing PUT signature on scratch disk", filename);
100 size = ((int32_t *) buffer)[1];
102 PANIC ("%s: invalid file size %d", filename, size);
104 /* Create destination file. */
105 if (!filesys_create (filename, size))
106 PANIC ("%s: create failed", filename);
107 dst = filesys_open (filename);
109 PANIC ("%s: open failed", filename);
114 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
115 disk_read (src, sector++, buffer);
116 if (file_write (dst, buffer, chunk_size) != chunk_size)
117 PANIC ("%s: write failed with %"PROTd" bytes unwritten",
127 /* Copies file FILENAME from the file system to the scratch disk.
129 The current sector on the scratch disk will receive "GET\0"
130 followed by the file's size in bytes as a 32-bit,
131 little-endian integer. Subsequent sectors receive the file's
134 The first call to this function will write starting at the
135 beginning of the scratch disk. Later calls advance across the
136 disk. This disk position is independent of that used for
137 fsutil_put(), so all `put's should precede all `get's. */
139 fsutil_get (char **argv)
141 static disk_sector_t sector = 0;
143 const char *filename = argv[1];
149 printf ("Getting '%s' from the file system...\n", filename);
151 /* Allocate buffer. */
152 buffer = malloc (DISK_SECTOR_SIZE);
154 PANIC ("couldn't allocate buffer");
156 /* Open source file. */
157 src = filesys_open (filename);
159 PANIC ("%s: open failed", filename);
160 size = file_length (src);
162 /* Open target disk. */
163 dst = disk_get (1, 0);
165 PANIC ("couldn't open target disk (hdc or hd1:0)");
167 /* Write size to sector 0. */
168 memset (buffer, 0, DISK_SECTOR_SIZE);
169 memcpy (buffer, "GET", 4);
170 ((int32_t *) buffer)[1] = size;
171 disk_write (dst, sector++, buffer);
176 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
177 if (sector >= disk_size (dst))
178 PANIC ("%s: out of space on scratch disk", filename);
179 if (file_read (src, buffer, chunk_size) != chunk_size)
180 PANIC ("%s: read failed with %"PROTd" bytes unread", filename, size);
181 memset (buffer + chunk_size, 0, DISK_SECTOR_SIZE - chunk_size);
182 disk_write (dst, sector++, buffer);