X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Ffilesys%2Ffilesys.c;h=fedda08e1d6730421a129e286117298821fd75fb;hb=b9fefe5e8d7d37e23970070a4614a5d1a6692eb2;hp=f8348ed2a2eaf4f17182507a9aebce1c47564966;hpb=3fc16f6e9abc98a3bd5427eb210669860609a224;p=pintos-anon diff --git a/src/filesys/filesys.c b/src/filesys/filesys.c index f8348ed..fedda08 100644 --- a/src/filesys/filesys.c +++ b/src/filesys/filesys.c @@ -1,90 +1,104 @@ -#include "filesys.h" +#include "filesys/filesys.h" +#include +#include +#include +#include "filesys/file.h" +#include "filesys/free-map.h" +#include "filesys/inode.h" +#include "filesys/directory.h" +#include "devices/disk.h" + +/* The disk that contains the file system. */ +struct disk *filesys_disk; + +static void do_format (void); + +/* Initializes the file system module. + If FORMAT is true, reformats the file system. */ +void +filesys_init (bool format) +{ + filesys_disk = disk_get (0, 1); + if (filesys_disk == NULL) + PANIC ("hd0:1 (hdb) not present, file system initialization failed"); + inode_init (); + free_map_init (); -#ifdef FILESYS_STUB -#include -#include "debug.h" -#include "filesys-stub.h" -#include "lib.h" + if (format) + do_format (); + + free_map_open (); +} +/* Shuts down the file system module, writing any unwritten data + to disk. */ void -filesys_init (bool reformat) +filesys_done (void) { - if (reformat) - printk ("filesystem stubs don't support formatting\n"); - filesys_stub_init (); + free_map_close (); } - + +/* Creates a file named NAME with the given INITIAL_SIZE. + Returns true if successful, false otherwise. + Fails if a file named NAME already exists, + or if internal memory allocation fails. */ bool -filesys_create (const char *name) +filesys_create (const char *name, off_t initial_size) { - bool success; - - filesys_stub_lock (); - filesys_stub_put_string ("create"); - filesys_stub_put_string (name); - filesys_stub_match_string ("create"); - success = filesys_stub_get_bool (); - filesys_stub_unlock (); + disk_sector_t inode_sector = 0; + struct dir *dir = dir_open_root (); + bool success = (dir != NULL + && free_map_allocate (1, &inode_sector) + && inode_create (inode_sector, initial_size) + && dir_add (dir, name, inode_sector)); + if (!success && inode_sector != 0) + free_map_release (inode_sector, 1); + dir_close (dir); return success; } +/* Opens the file with the given NAME. + Returns the new file if successful or a null pointer + otherwise. + Fails if no file named NAME exists, + or if an internal memory allocation fails. */ struct file * -filesys_open (const char *name) +filesys_open (const char *name) { - struct file *file; + struct dir *dir = dir_open_root (); + struct inode *inode = NULL; + + if (dir != NULL) + dir_lookup (dir, name, &inode); + dir_close (dir); - filesys_stub_lock (); - filesys_stub_put_string ("open"); - filesys_stub_put_string (name); - filesys_stub_match_string ("open"); - file = filesys_stub_get_file (); - filesys_stub_unlock (); - - return file; + return file_open (inode); } +/* Deletes the file named NAME. + Returns true if successful, false on failure. + Fails if no file named NAME exists, + or if an internal memory allocation fails. */ bool filesys_remove (const char *name) { - bool success; - - filesys_stub_lock (); - filesys_stub_put_string ("remove"); - filesys_stub_put_string (name); - filesys_stub_match_string ("remove"); - success = filesys_stub_get_bool (); - filesys_stub_unlock (); + struct dir *dir = dir_open_root (); + bool success = dir != NULL && dir_remove (dir, name); + dir_close (dir); return success; } -#endif /* FILESYS_STUB */ - -#undef NDEBUG -#include "debug.h" -#include "file.h" - -void -filesys_self_test (void) + +/* Formats the file system. */ +static void +do_format (void) { - static const char s[] = "This is a test string."; - struct file *file; - char s2[sizeof s]; - - ASSERT (filesys_create ("foo")); - ASSERT ((file = filesys_open ("foo")) != NULL); - ASSERT (file_write (file, s, sizeof s) == sizeof s); - ASSERT (file_tell (file) == sizeof s); - ASSERT (file_length (file) == sizeof s); - file_close (file); - - ASSERT ((file = filesys_open ("foo")) != NULL); - ASSERT (file_read (file, s2, sizeof s2) == sizeof s2); - ASSERT (memcmp (s, s2, sizeof s) == 0); - ASSERT (file_tell (file) == sizeof s2); - ASSERT (file_length (file) == sizeof s2); - file_close (file); - - ASSERT (filesys_remove ("foo")); + printf ("Formatting file system..."); + free_map_create (); + if (!dir_create (ROOT_DIR_SECTOR, 16)) + PANIC ("root directory creation failed"); + free_map_close (); + printf ("done.\n"); }