Rename printk() to printf().
[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   if (!filesys_open (filename, &dst))
51     PANIC ("%s: open failed", filename);
52
53   /* Do copy. */
54   buffer = palloc_get (PAL_ASSERT);
55   sector = 0;
56   while (size > 0)
57     {
58       int chunk_size = size > DISK_SECTOR_SIZE ? DISK_SECTOR_SIZE : size;
59       disk_read (src, sector++, buffer);
60       if (file_write (&dst, buffer, chunk_size) != chunk_size)
61         PANIC ("%s: write failed with %lld bytes unwritten",
62                filename, (unsigned long long) size);
63       size -= chunk_size;
64     }
65   palloc_free (buffer);
66
67   file_close (&dst);
68 }
69
70 /* Executes the filesystem operations described by the variables
71    declared in fsutil.h. */
72 void
73 fsutil_run (void) 
74 {
75   if (fsutil_copy_arg != NULL) 
76     {
77       char *save;
78       char *filename = strtok_r (fsutil_copy_arg, ":", &save);
79       char *size = strtok_r (NULL, "", &save);
80
81       if (filename == NULL || size == NULL)
82         PANIC ("bad format for -cp option; use -u for usage");
83
84       copy (filename, atoi (size));
85     }
86
87   if (fsutil_print_file != NULL)
88     fsutil_print (fsutil_print_file);
89
90   if (fsutil_remove_file != NULL) 
91     {
92       if (filesys_remove (fsutil_remove_file))
93         printf ("%s: removed\n", fsutil_remove_file);
94       else
95         PANIC ("%s: remove failed\n", fsutil_remove_file);
96     }
97
98   if (fsutil_list_files)
99     filesys_list ();
100
101   if (fsutil_dump_filesys)
102     filesys_dump ();
103 }
104
105 /* Prints the contents of file FILENAME to the system console as
106    hex and ASCII. */
107 void
108 fsutil_print (const char *filename) 
109 {
110   struct file file;
111   char *buffer;
112
113   if (!filesys_open (filename, &file))
114     PANIC ("%s: open failed", filename);
115   buffer = palloc_get (PAL_ASSERT);
116   for (;;) 
117     {
118       off_t n = file_read (&file, buffer, PGSIZE);
119       if (n == 0)
120         break;
121
122       hex_dump (buffer, n, true);
123     }
124   palloc_free (buffer);
125   file_close (&file);
126 }