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"
10 /* Partition that contains the file system. */
11 struct block *fs_device;
13 static void do_format (void);
15 /* Initializes the file system module.
16 If FORMAT is true, reformats the file system. */
18 filesys_init (bool format)
20 fs_device = block_get_role (BLOCK_FILESYS);
21 if (fs_device == NULL)
22 PANIC ("No file system device found, can't initialize file system.");
33 /* Shuts down the file system module, writing any unwritten data
41 /* Creates a file named NAME with the given INITIAL_SIZE.
42 Returns true if successful, false otherwise.
43 Fails if a file named NAME already exists,
44 or if internal memory allocation fails. */
46 filesys_create (const char *name, off_t initial_size)
48 block_sector_t inode_sector = 0;
49 struct dir *dir = dir_open_root ();
50 bool success = (dir != NULL
51 && free_map_allocate (1, &inode_sector)
52 && inode_create (inode_sector, initial_size)
53 && dir_add (dir, name, inode_sector));
54 if (!success && inode_sector != 0)
55 free_map_release (inode_sector, 1);
61 /* Opens the file with the given NAME.
62 Returns the new file if successful or a null pointer
64 Fails if no file named NAME exists,
65 or if an internal memory allocation fails. */
67 filesys_open (const char *name)
69 struct dir *dir = dir_open_root ();
70 struct inode *inode = NULL;
73 dir_lookup (dir, name, &inode);
76 return file_open (inode);
79 /* Deletes the file named NAME.
80 Returns true if successful, false on failure.
81 Fails if no file named NAME exists,
82 or if an internal memory allocation fails. */
84 filesys_remove (const char *name)
86 struct dir *dir = dir_open_root ();
87 bool success = dir != NULL && dir_remove (dir, name);
93 /* Formats the file system. */
97 printf ("Formatting file system...");
99 if (!dir_create (ROOT_DIR_SECTOR, 16))
100 PANIC ("root directory creation failed");