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 file system. */
12 struct disk *filesys_disk;
14 static void do_format (void);
16 /* Initializes the file system module.
17 If FORMAT is true, reformats the file system. */
19 filesys_init (bool format)
21 filesys_disk = disk_get (0, 1);
22 if (filesys_disk == NULL)
23 PANIC ("hd0:1 (hdb) not present, file system initialization failed");
34 /* Shuts down the file system 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)
49 disk_sector_t inode_sector = 0;
50 struct dir *dir = dir_open_root ();
51 bool success = (dir != NULL
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)
70 struct dir *dir = dir_open_root ();
71 struct inode *inode = NULL;
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 = dir_open_root ();
88 bool success = dir != NULL && dir_remove (dir, name);
94 /* Formats the file system. */
98 printf ("Formatting file system...");
100 if (!dir_create (ROOT_DIR_SECTOR, 16))
101 PANIC ("root directory creation failed");