aboutsummaryrefslogtreecommitdiffstats
path: root/src/filesys/filesys.c
diff options
context:
space:
mode:
authorklaar36 <klas.arvidsson@liu.se>2015-03-20 17:30:24 +0100
committerklaar36 <klas.arvidsson@liu.se>2015-03-20 17:30:24 +0100
commite7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad (patch)
tree4de97af7207676b69cb6a9aba8cb443cc134855d /src/filesys/filesys.c
parentb0418a24e709f0632d2ede5b0f327c422931939b (diff)
downloadpintos-rs-e7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad.tar.gz
Initial Pintos
Diffstat (limited to 'src/filesys/filesys.c')
-rw-r--r--src/filesys/filesys.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/filesys/filesys.c b/src/filesys/filesys.c
new file mode 100644
index 0000000..97a3f04
--- /dev/null
+++ b/src/filesys/filesys.c
@@ -0,0 +1,114 @@
+#include "filesys/filesys.h"
+#include <debug.h>
+#include <stdio.h>
+#include <string.h>
+#include "filesys/file.h"
+#include "filesys/free-map.h"
+#include "filesys/inode.h"
+#include "filesys/directory.h"
+#include "devices/disk.h"
+#include "threads/synch.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 ();
+
+ if (format)
+ do_format ();
+
+ free_map_open ();
+}
+
+/* Shuts down the file system module, writing any unwritten data
+ to disk. */
+void
+filesys_done (void)
+{
+ 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, off_t initial_size)
+{
+ 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)
+{
+ struct dir *dir = dir_open_root ();
+ struct inode *inode = NULL;
+ struct file *file = NULL;
+
+ if (dir != NULL)
+ dir_lookup (dir, name, &inode);
+ dir_close (dir);
+
+ file = file_open (inode);
+
+ return file;
+}
+
+void
+filesys_close (struct file *file)
+{
+ file_close(file);
+}
+
+/* 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)
+{
+ struct dir *dir = dir_open_root ();
+ bool success = dir != NULL && dir_remove (dir, name);
+ dir_close (dir);
+
+ return success;
+}
+
+/* Formats the file system. */
+static void
+do_format (void)
+{
+ 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");
+}