Get rid of file system "dump" operations because they weren't useful
[pintos-anon] / src / filesys / fsutil.c
1 #include "filesys/fsutil.h"
2 #include <debug.h>
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "filesys/file.h"
8 #include "filesys/filesys.h"
9 #include "threads/mmu.h"
10 #include "threads/palloc.h"
11
12 /* Destination filename and size for copy-in operations. */
13 char *fsutil_copyin_file;
14 int fsutil_copyin_size;
15
16 /* Source filename for copy-out operations. */
17 char *fsutil_copyout_file;
18
19 /* Name of a file print to print to console. */
20 char *fsutil_print_file;
21
22 /* Name of a file to delete. */
23 char *fsutil_remove_file;
24
25 /* List all files in the filesystem to the system console? */
26 bool fsutil_list_files;
27
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. */
31 static void
32 copy_in (const char *filename, off_t size) 
33 {
34   struct disk *src;
35   struct file *dst;
36   disk_sector_t sector;
37   void *buffer;
38
39   /* Open source disk. */
40   src = disk_get (1, 0);
41   if (src == NULL)
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);
46   
47   /* Create destination file. */
48   if (!filesys_create (filename, size))
49     PANIC ("%s: create failed", filename);
50   dst = filesys_open (filename);
51   if (dst == NULL)
52     PANIC ("%s: open failed", filename);
53
54   /* Do copy. */
55   buffer = palloc_get_page (PAL_ASSERT);
56   sector = 0;
57   while (size > 0)
58     {
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);
64       size -= chunk_size;
65     }
66   palloc_free_page (buffer);
67
68   file_close (dst);
69 }
70
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. */
75 static void
76 copy_out (const char *filename) 
77 {
78   void *buffer;
79   struct file *src;
80   struct disk *dst;
81   off_t size;
82   disk_sector_t sector;
83
84   buffer = palloc_get_page (PAL_ASSERT | PAL_ZERO);
85
86   /* Open source file. */
87   src = filesys_open (filename);
88   if (src == NULL)
89     PANIC ("%s: open failed", filename);
90   size = file_length (src);
91
92   /* Open target disk. */
93   dst = disk_get (1, 0);
94   if (dst == NULL)
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);
99   
100   /* Write size to sector 0. */
101   *(uint32_t *) buffer = size;
102   disk_write (dst, 0, buffer);
103   
104   /* Do copy. */
105   sector = 1;
106   while (size > 0) 
107     {
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);
113       size -= chunk_size;
114     }
115   palloc_free_page (buffer);
116
117   file_close (src);
118 }
119
120 /* Executes the filesystem operations described by the variables
121    declared in fsutil.h. */
122 void
123 fsutil_run (void) 
124 {
125   if (fsutil_copyin_file != NULL) 
126     copy_in (fsutil_copyin_file, fsutil_copyin_size);
127
128   if (fsutil_copyout_file != NULL)
129     copy_out (fsutil_copyout_file);
130
131   if (fsutil_print_file != NULL)
132     fsutil_print (fsutil_print_file);
133
134   if (fsutil_remove_file != NULL) 
135     {
136       if (filesys_remove (fsutil_remove_file))
137         printf ("%s: removed\n", fsutil_remove_file);
138       else
139         PANIC ("%s: remove failed\n", fsutil_remove_file);
140     }
141
142   if (fsutil_list_files)
143     filesys_list ();
144 }
145
146 /* Prints the contents of file FILENAME to the system console as
147    hex and ASCII. */
148 void
149 fsutil_print (const char *filename) 
150 {
151   struct file *file;
152   char *buffer;
153
154   file = filesys_open (filename);
155   if (file == NULL)
156     PANIC ("%s: open failed", filename);
157   buffer = palloc_get_page (PAL_ASSERT);
158   for (;;) 
159     {
160       off_t n = file_read (file, buffer, PGSIZE);
161       if (n == 0)
162         break;
163
164       hex_dump (0, buffer, n, true);
165     }
166   palloc_free_page (buffer);
167   file_close (file);
168 }