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 bitmap_init (&free_map, disk_size (disk));
25 bitmap_mark (&free_map, FREE_MAP_SECTOR);
26 bitmap_mark (&free_map, ROOT_DIR_SECTOR);
28 /* Allocate data sector(s) for the free map file
29 and write its file header to disk. */
30 if (!filehdr_allocate (&map_hdr, bitmap_storage_size (&free_map)))
31 panic ("free map creation failed");
32 filehdr_write (&map_hdr, FREE_MAP_SECTOR);
33 filehdr_destroy (&map_hdr);
35 /* Allocate data sector(s) for the root directory file
36 and write its file header to disk. */
37 if (!filehdr_allocate (&dir_hdr, ROOT_DIR_FILE_SIZE))
38 panic ("root directory creation failed");
39 filehdr_write (&dir_hdr, FREE_MAP_SECTOR);
40 filehdr_destroy (&dir_hdr);
42 /* Write out the free map now that we have space reserved
44 file_open (&free_map_file, FREE_MAP_SECTOR);
45 bitmapio_write (&free_map, free_map_file);
46 bitmap_destroy (&free_map);
47 file_close (&free_map_file);
49 /* Write out the root directory in the same way. */
50 file_open (&root_dir_file, ROOT_DIR_SECTOR);
51 if (!dir_init (&dir, NUM_DIR_ENTRIES))
52 panic ("can't initialize root directory");
53 dir_write (root_dir_file);
55 file_close (&free_map_file);
59 filesys_init (bool format)
63 panic ("ide1:1 not present, filesystem initialization failed");
68 file_open (&free_map_file, FREE_MAP_SECTOR);
69 file_open (&root_dir_file, ROOT_DIR_SECTOR);
73 filesys_create (const char *name, off_t initial_size)
76 struct bitmap free_map;
77 disk_sector_no hdr_sector;
78 struct filehdr filehdr;
81 /* Read the root directory. */
82 dir_init (&dir, NUM_DIR_ENTRIES);
83 dir_read (&dir, &root_dir_file);
84 if (dir_lookup (&dir, name, NULL))
87 /* Allocate a block for the file header. */
88 bitmap_init (&free_map, disk_size (disk));
89 bitmapio_read (&free_map, &free_map_file);
90 hdr_sector = bitmap_find_and_set (&free_map);
91 if (hdr_sector == BITMAP_ERROR)
94 /* Add the file to the directory. */
95 if (!dir_add (&dir, name, hdr_sector))
98 /* Allocate space for the file. */
99 if (!filehdr_allocate (&filehdr, initial_size))
102 /* Write everything back. */
103 filehdr_write (&filehdr, hdr_sector);
104 dir_write (&dir, &root_dir_file);
105 bitmapio_write (&free_map, &free_map_file);
110 filehdr_destroy (&filehdr);
112 bitmap_destroy (&free_map);
120 filesys_remove (const char *name)
123 disk_sector_no hdr_sector;
124 struct filehdr filehdr;
125 struct bitmap free_map;
126 bool success = false;
128 /* Read the root directory. */
129 dir_init (&dir, NUM_DIR_ENTRIES);
130 dir_read (&dir, &root_dir_file);
131 if (!dir_lookup (&dir, name, &hdr_sector))
134 /* Read the file header. */
135 filehdr_read (&filehdr, hdr_sector);
137 /* Allocate a block for the file header. */
138 bitmap_init (&free_map, disk_size (disk));
139 bitmapio_read (&free_map, &free_map_file);
142 filehdr_deallocate (&filehdr, &free_map);
143 bitmap_reset (&free_map, hdr_sector);
144 dir_remove (&dir, name);
146 /* Write everything back. */
147 bitmapio_write (&free_map, &free_map_file);
148 dir_write (&dir, &root_dir_file);
153 filehdr_destroy (&filehdr);
154 bitmap_destroy (&free_map);
166 filesys_self_test (void)
168 static const char s[] = "This is a test string.";
172 ASSERT (filesys_create ("foo"));
173 ASSERT ((file = filesys_open ("foo")) != NULL);
174 ASSERT (file_write (file, s, sizeof s) == sizeof s);
175 ASSERT (file_tell (file) == sizeof s);
176 ASSERT (file_length (file) == sizeof s);
179 ASSERT ((file = filesys_open ("foo")) != NULL);
180 ASSERT (file_read (file, s2, sizeof s2) == sizeof s2);
181 ASSERT (memcmp (s, s2, sizeof s) == 0);
182 ASSERT (file_tell (file) == sizeof s2);
183 ASSERT (file_length (file) == sizeof s2);
186 ASSERT (filesys_remove ("foo"));