Add comments.
[pintos-anon] / src / filesys / fsutil.c
1 #include "fsutil.h"
2 #include <stdbool.h>
3 #include "debug.h"
4 #include "filesys.h"
5 #include "file.h"
6 #include "lib.h"
7 #include "mmu.h"
8 #include "palloc.h"
9
10 /* Filename and file size to use for copy operations,
11    as "filename:size". */
12 char *fsutil_copy_arg;
13
14 /* Name of a file print to print to console. */
15 char *fsutil_print_file;
16
17 /* Name of a file to delete. */
18 char *fsutil_remove_file;
19
20 /* List all files in the filesystem to the system console? */
21 bool fsutil_list_files;
22
23 /* Dump full contents of filesystem to the system console? */
24 bool fsutil_dump_filesys;
25
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. */
29 static void
30 copy (const char *filename, off_t size) 
31 {
32   struct disk *src;
33   struct file dst;
34   disk_sector_t sector;
35   void *buffer;
36
37   /* Open source disk. */
38   src = disk_get (1, 0);
39   if (src == NULL)
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);
44   
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);
50
51   /* Do copy. */
52   buffer = palloc_get (PAL_ASSERT);
53   sector = 0;
54   while (size > 0)
55     {
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);
61       size -= chunk_size;
62     }
63   palloc_free (buffer);
64
65   file_close (&dst);
66 }
67
68 /* Executes the filesystem operations described by the variables
69    declared in fsutil.h. */
70 void
71 fsutil_run (void) 
72 {
73   if (fsutil_copy_arg != NULL) 
74     {
75       char *save;
76       char *filename = strtok_r (fsutil_copy_arg, ":", &save);
77       char *size = strtok_r (NULL, "", &save);
78
79       if (filename == NULL || size == NULL)
80         PANIC ("bad format for -cp option; use -u for usage");
81
82       copy (filename, atoi (size));
83     }
84
85   if (fsutil_print_file != NULL)
86     fsutil_print (fsutil_print_file);
87
88   if (fsutil_remove_file != NULL) 
89     {
90       if (filesys_remove (fsutil_remove_file))
91         printk ("%s: removed\n", fsutil_remove_file);
92       else
93         PANIC ("%s: remove failed\n", fsutil_remove_file);
94     }
95
96   if (fsutil_list_files)
97     filesys_list ();
98
99   if (fsutil_dump_filesys)
100     filesys_dump ();
101 }
102
103 /* Prints the contents of file FILENAME to the system console as
104    hex and ASCII. */
105 void
106 fsutil_print (const char *filename) 
107 {
108   struct file file;
109   char *buffer;
110
111   if (!filesys_open (filename, &file))
112     PANIC ("%s: open failed", filename);
113   buffer = palloc_get (PAL_ASSERT);
114   for (;;) 
115     {
116       off_t n = file_read (&file, buffer, PGSIZE);
117       if (n == 0)
118         break;
119
120       hex_dump (buffer, n, true);
121     }
122   palloc_free (buffer);
123   file_close (&file);
124 }