10 /* Filename and file size to use for copy operations,
11 as "filename:size". */
12 char *fsutil_copy_arg;
14 /* Name of a file print to print to console. */
15 char *fsutil_print_file;
17 /* Name of a file to delete. */
18 char *fsutil_remove_file;
20 /* List all files in the filesystem to the system console? */
21 bool fsutil_list_files;
23 /* Dump full contents of filesystem to the system console? */
24 bool fsutil_dump_filesys;
26 /* Copies from the "scratch" disk, hdc or hd1:0,
27 to a file named FILENAME in the filesystem.
28 The file will be SIZE bytes in length. */
30 copy (const char *filename, off_t size)
37 /* Open source disk. */
38 src = disk_get (1, 0);
40 PANIC ("couldn't open source disk (hdc or hd1:0)");
41 if (size > (off_t) disk_size (src) * DISK_SECTOR_SIZE)
42 PANIC ("source disk is too small for %lld-byte file",
43 (unsigned long long) size);
45 /* Create destination file. */
46 if (!filesys_create (filename, size))
47 PANIC ("%s: create failed", filename);
48 if (!filesys_open (filename, &dst))
49 PANIC ("%s: open failed", filename);
52 buffer = palloc_get (PAL_ASSERT);
56 int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
57 disk_read (src, sector++, buffer);
58 if (file_write (&dst, buffer, chunk_size) != chunk_size)
59 PANIC ("%s: write failed with %lld bytes unwritten",
60 filename, (unsigned long long) size);
68 /* Executes the filesystem operations described by the variables
69 declared in fsutil.h. */
73 if (fsutil_copy_arg != NULL)
76 char *filename = strtok_r (fsutil_copy_arg, ":", &save);
77 char *size = strtok_r (NULL, "", &save);
79 if (filename == NULL || size == NULL)
80 PANIC ("bad format for -cp option; use -u for usage");
82 copy (filename, atoi (size));
85 if (fsutil_print_file != NULL)
86 fsutil_print (fsutil_print_file);
88 if (fsutil_remove_file != NULL)
90 if (filesys_remove (fsutil_remove_file))
91 printk ("%s: removed\n", fsutil_remove_file);
93 PANIC ("%s: remove failed\n", fsutil_remove_file);
96 if (fsutil_list_files)
99 if (fsutil_dump_filesys)
103 /* Prints the contents of file FILENAME to the system console as
106 fsutil_print (const char *filename)
111 if (!filesys_open (filename, &file))
112 PANIC ("%s: open failed", filename);
113 buffer = palloc_get (PAL_ASSERT);
116 off_t n = file_read (&file, buffer, PGSIZE);
120 hex_dump (buffer, n, true);
122 palloc_free (buffer);