summaryrefslogtreecommitdiffstats
path: root/filesys/filesys.c
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-03-27 11:51:08 +0200
committermanuel <manuel@mausz.at>2012-03-27 11:51:08 +0200
commit4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b (patch)
tree868c52e06f207b5ec8a3cc141f4b8b2bdfcc165c /filesys/filesys.c
parenteae0bd57f0a26314a94785061888d193d186944a (diff)
downloadprogos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.gz
progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.tar.bz2
progos-4f670845ff9ab6c48bcb5f7bf4d4ef6dc3c3064b.zip
reorganize file structure to match the upstream requirements
Diffstat (limited to 'filesys/filesys.c')
-rw-r--r--filesys/filesys.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/filesys/filesys.c b/filesys/filesys.c
new file mode 100644
index 0000000..7a53f5f
--- /dev/null
+++ b/filesys/filesys.c
@@ -0,0 +1,103 @@
1#include "filesys/filesys.h"
2#include <debug.h>
3#include <stdio.h>
4#include <string.h>
5#include "filesys/file.h"
6#include "filesys/free-map.h"
7#include "filesys/inode.h"
8#include "filesys/directory.h"
9
10/* Partition that contains the file system. */
11struct block *fs_device;
12
13static void do_format (void);
14
15/* Initializes the file system module.
16 If FORMAT is true, reformats the file system. */
17void
18filesys_init (bool format)
19{
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.");
23
24 inode_init ();
25 free_map_init ();
26
27 if (format)
28 do_format ();
29
30 free_map_open ();
31}
32
33/* Shuts down the file system module, writing any unwritten data
34 to disk. */
35void
36filesys_done (void)
37{
38 free_map_close ();
39}
40
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. */
45bool
46filesys_create (const char *name, off_t initial_size)
47{
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);
56 dir_close (dir);
57
58 return success;
59}
60
61/* Opens the file with the given NAME.
62 Returns the new file if successful or a null pointer
63 otherwise.
64 Fails if no file named NAME exists,
65 or if an internal memory allocation fails. */
66struct file *
67filesys_open (const char *name)
68{
69 struct dir *dir = dir_open_root ();
70 struct inode *inode = NULL;
71
72 if (dir != NULL)
73 dir_lookup (dir, name, &inode);
74 dir_close (dir);
75
76 return file_open (inode);
77}
78
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. */
83bool
84filesys_remove (const char *name)
85{
86 struct dir *dir = dir_open_root ();
87 bool success = dir != NULL && dir_remove (dir, name);
88 dir_close (dir);
89
90 return success;
91}
92
93/* Formats the file system. */
94static void
95do_format (void)
96{
97 printf ("Formatting file system...");
98 free_map_create ();
99 if (!dir_create (ROOT_DIR_SECTOR, 16))
100 PANIC ("root directory creation failed");
101 free_map_close ();
102 printf ("done.\n");
103}