5 static struct disk *disk;
7 static struct file free_map_file, root_dir_file;
9 #define FREE_MAP_SECTOR 0
10 #define ROOT_DIR_SECTOR 1
12 #define NUM_DIR_ENTRIES 10
13 #define ROOT_DIR_FILE_SIZE (sizeof (struct dir_entry) * NUM_DIR_ENTRIES)
18 struct bitmap free_map;
19 struct filehdr map_hdr, dir_hdr;
22 /* Create the initial bitmap and reserve sectors for the
23 free map and root directory file headers. */
24 if (!bitmap_init (&free_map, disk_size (disk)))
25 panic ("bitmap creation failed");
26 bitmap_mark (&free_map, FREE_MAP_SECTOR);
27 bitmap_mark (&free_map, ROOT_DIR_SECTOR);
29 /* Allocate data sector(s) for the free map file
30 and write its file header to disk. */
31 if (!filehdr_allocate (&map_hdr, bitmap_storage_size (&free_map)))
32 panic ("free map creation failed");
33 filehdr_write (&map_hdr, FREE_MAP_SECTOR);
34 filehdr_destroy (&map_hdr);
36 /* Allocate data sector(s) for the root directory file
37 and write its file header to disk. */
38 if (!filehdr_allocate (&dir_hdr, ROOT_DIR_FILE_SIZE))
39 panic ("root directory creation failed");
40 filehdr_write (&dir_hdr, FREE_MAP_SECTOR);
41 filehdr_destroy (&dir_hdr);
43 /* Write out the free map now that we have space reserved
45 file_open (&free_map_file, FREE_MAP_SECTOR);
46 bitmapio_write (&free_map, free_map_file);
47 bitmap_destroy (&free_map);
48 file_close (&free_map_file);
50 /* Write out the root directory in the same way. */
51 file_open (&root_dir_file, ROOT_DIR_SECTOR);
52 if (!dir_init (&dir, NUM_DIR_ENTRIES))
53 panic ("can't initialize root directory");
54 dir_write (root_dir_file);
56 file_close (&free_map_file);
60 filesys_init (bool format)
64 panic ("ide1:1 not present, filesystem initialization failed");
69 file_open (&free_map_file, FREE_MAP_SECTOR);
70 file_open (&root_dir_file, ROOT_DIR_SECTOR);
74 filesys_create (const char *name, off_t initial_size)
77 struct bitmap free_map;
78 disk_sector_no hdr_sector;
79 struct filehdr filehdr;
82 /* Read the root directory. */
83 dir_init (&dir, NUM_DIR_ENTRIES);
84 dir_read (&dir, &root_dir_file);
85 if (dir_lookup (&dir, name, NULL))
88 /* Allocate a block for the file header. */
89 if (!bitmap_init (&free_map, disk_size (disk)))
91 bitmapio_read (&free_map, &free_map_file);
92 hdr_sector = bitmap_find_and_set (&free_map);
93 if (hdr_sector == BITMAP_ERROR)
96 /* Add the file to the directory. */
97 if (!dir_add (&dir, name, hdr_sector))
100 /* Allocate space for the file. */
101 if (!filehdr_allocate (&filehdr, initial_size))
104 /* Write everything back. */
105 filehdr_write (&filehdr, hdr_sector);
106 dir_write (&dir, &root_dir_file);
107 bitmapio_write (&free_map, &free_map_file);
112 filehdr_destroy (&filehdr);
114 bitmap_destroy (&free_map);
122 filesys_remove (const char *name)
125 disk_sector_no hdr_sector;
126 struct filehdr filehdr;
127 struct bitmap free_map;
128 bool success = false;
130 /* Read the root directory. */
131 dir_init (&dir, NUM_DIR_ENTRIES);
132 dir_read (&dir, &root_dir_file);
133 if (!dir_lookup (&dir, name, &hdr_sector))
136 /* Read the file header. */
137 if (!filehdr_read (&filehdr, hdr_sector))
140 /* Allocate a block for the file header. */
141 if (!bitmap_init (&free_map, disk_size (disk)))
143 bitmapio_read (&free_map, &free_map_file);
146 filehdr_deallocate (&filehdr, &free_map);
147 bitmap_reset (&free_map, hdr_sector);
148 dir_remove (&dir, name);
150 /* Write everything back. */
151 bitmapio_write (&free_map, &free_map_file);
152 dir_write (&dir, &root_dir_file);
157 bitmap_destroy (&free_map);
159 filehdr_destroy (&filehdr);
171 filesys_self_test (void)
173 static const char s[] = "This is a test string.";
177 ASSERT (filesys_create ("foo"));
178 ASSERT ((file = filesys_open ("foo")) != NULL);
179 ASSERT (file_write (file, s, sizeof s) == sizeof s);
180 ASSERT (file_tell (file) == sizeof s);
181 ASSERT (file_length (file) == sizeof s);
184 ASSERT ((file = filesys_open ("foo")) != NULL);
185 ASSERT (file_read (file, s2, sizeof s2) == sizeof s2);
186 ASSERT (memcmp (s, s2, sizeof s) == 0);
187 ASSERT (file_tell (file) == sizeof s2);
188 ASSERT (file_length (file) == sizeof s2);
191 ASSERT (filesys_remove ("foo"));