Rewrite filesystem to support Unix "delete" semantics.
[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 /* Filename and file size to use for copy operations,
13    as "filename:size". */
14 char *fsutil_copy_arg;
15
16 /* Name of a file print to print to console. */
17 char *fsutil_print_file;
18
19 /* Name of a file to delete. */
20 char *fsutil_remove_file;
21
22 /* List all files in the filesystem to the system console? */
23 bool fsutil_list_files;
24
25 /* Dump full contents of filesystem to the system console? */
26 bool fsutil_dump_filesys;
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 (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 (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 (buffer);
67
68   file_close (dst);
69 }
70
71 /* Executes the filesystem operations described by the variables
72    declared in fsutil.h. */
73 void
74 fsutil_run (void) 
75 {
76   if (fsutil_copy_arg != NULL) 
77     {
78       char *save;
79       char *filename = strtok_r (fsutil_copy_arg, ":", &save);
80       char *size = strtok_r (NULL, "", &save);
81
82       if (filename == NULL || size == NULL)
83         PANIC ("bad format for -cp option; use -u for usage");
84
85       copy (filename, atoi (size));
86     }
87
88   if (fsutil_print_file != NULL)
89     fsutil_print (fsutil_print_file);
90
91   if (fsutil_remove_file != NULL) 
92     {
93       if (filesys_remove (fsutil_remove_file))
94         printf ("%s: removed\n", fsutil_remove_file);
95       else
96         PANIC ("%s: remove failed\n", fsutil_remove_file);
97     }
98
99   if (fsutil_list_files)
100     filesys_list ();
101
102   if (fsutil_dump_filesys)
103     filesys_dump ();
104 }
105
106 /* Prints the contents of file FILENAME to the system console as
107    hex and ASCII. */
108 void
109 fsutil_print (const char *filename) 
110 {
111   struct file *file;
112   char *buffer;
113
114   file = filesys_open (filename);
115   if (file == NULL)
116     PANIC ("%s: open failed", filename);
117   buffer = palloc_get (PAL_ASSERT);
118   for (;;) 
119     {
120       off_t n = file_read (file, buffer, PGSIZE);
121       if (n == 0)
122         break;
123
124       hex_dump (buffer, n, true);
125     }
126   palloc_free (buffer);
127   file_close (file);
128 }