1 #include "filesys/filesys.h"
5 #include "filesys/file.h"
6 #include "filesys/free-map.h"
7 #include "filesys/inode.h"
8 #include "filesys/directory.h"
9 #include "devices/disk.h"
11 /* The disk that contains the filesystem. */
12 struct disk *filesys_disk;
14 static void do_format (void);
16 /* Initializes the filesystem module.
17 If FORMAT is true, reformats the filesystem. */
19 filesys_init (bool format)
21 filesys_disk = disk_get (0, 1);
22 if (filesys_disk == NULL)
23 PANIC ("hd0:1 (hdb) not present, filesystem initialization failed");
34 /* Shuts down the filesystem module, writing any unwritten data
42 /* Creates a file named NAME with the given INITIAL_SIZE.
43 Returns true if successful, false otherwise.
44 Fails if a file named NAME already exists,
45 or if internal memory allocation fails. */
47 filesys_create (const char *name, off_t initial_size)
50 disk_sector_t inode_sector = 0;
51 bool success = (dir_open_root (&dir)
52 && free_map_allocate (1, &inode_sector)
53 && inode_create (inode_sector, initial_size)
54 && dir_add (dir, name, inode_sector));
55 if (!success && inode_sector != 0)
56 free_map_release (inode_sector, 1);
62 /* Opens the file with the given NAME.
63 Returns the new file if successful or a null pointer
65 Fails if no file named NAME exists,
66 or if an internal memory allocation fails. */
68 filesys_open (const char *name)
71 struct inode *inode = NULL;
73 if (dir_open_root (&dir))
74 dir_lookup (dir, name, &inode);
77 return file_open (inode);
80 /* Deletes the file named NAME.
81 Returns true if successful, false on failure.
82 Fails if no file named NAME exists,
83 or if an internal memory allocation fails. */
85 filesys_remove (const char *name)
87 struct dir *dir = NULL;
88 bool success = (dir_open_root (&dir)
89 && dir_remove (dir, name));
95 /* Prints a list of files in the filesystem to the system
97 Returns true if successful, false on failure,
98 which occurs only if an internal memory allocation fails. */
102 struct dir *dir = NULL;
103 bool success = dir_open_root (&dir);
111 static void must_succeed_function (int, bool) NO_INLINE;
112 #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
114 /* Performs basic sanity checks on the filesystem.
115 The filesystem should not contain a file named `foo' when
118 filesys_self_test (void)
120 static const char s[] = "This is a test string.";
121 static const char zeros[sizeof s] = {0};
126 filesys_remove ("foo");
127 for (i = 0; i < 2; i++)
129 /* Create file and check that it contains zeros
130 throughout the created length. */
131 MUST_SUCCEED (filesys_create ("foo", sizeof s));
132 MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
133 MUST_SUCCEED (file_read (file, s2, sizeof s2) == sizeof s2);
134 MUST_SUCCEED (memcmp (s2, zeros, sizeof s) == 0);
135 MUST_SUCCEED (file_tell (file) == sizeof s);
136 MUST_SUCCEED (file_length (file) == sizeof s);
139 /* Reopen file and write to it. */
140 MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
141 MUST_SUCCEED (file_write (file, s, sizeof s) == sizeof s);
142 MUST_SUCCEED (file_tell (file) == sizeof s);
143 MUST_SUCCEED (file_length (file) == sizeof s);
146 /* Reopen file and verify that it reads back correctly.
147 Delete file while open to check proper semantics. */
148 MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
149 MUST_SUCCEED (filesys_remove ("foo"));
150 MUST_SUCCEED (filesys_open ("foo") == NULL);
151 MUST_SUCCEED (file_read (file, s2, sizeof s) == sizeof s);
152 MUST_SUCCEED (memcmp (s, s2, sizeof s) == 0);
153 MUST_SUCCEED (file_tell (file) == sizeof s);
154 MUST_SUCCEED (file_length (file) == sizeof s);
158 printf ("filesys: self test ok\n");
161 /* If SUCCESS is false, panics with an error complaining about
164 must_succeed_function (int line_no, bool success)
167 PANIC ("filesys_self_test: operation failed on line %d", line_no);
170 /* Formats the filesystem. */
174 printf ("Formatting filesystem...");
176 if (!dir_create (ROOT_DIR_SECTOR, 16))
177 PANIC ("root directory creation failed");