Working backdoor filesystem implementation.
[pintos-anon] / src / filesys / filesys.c
1 #include "filesys.h"
2
3
4 #ifdef FILESYS_STUB
5 #include <stdint.h>
6 #include "debug.h"
7 #include "filesys-stub.h"
8 #include "lib.h"
9
10 void
11 filesys_init (bool reformat) 
12 {
13   if (reformat)
14     printk ("filesystem stubs don't support formatting\n");
15   filesys_stub_init ();
16 }
17
18 bool
19 filesys_create (const char *name) 
20 {
21   bool success;
22
23   filesys_stub_lock ();
24   filesys_stub_put_string ("create");
25   filesys_stub_put_string (name);
26   filesys_stub_match_string ("create");
27   success = filesys_stub_get_bool ();
28   filesys_stub_unlock ();
29
30   return success;
31 }
32
33 struct file *
34 filesys_open (const char *name) 
35 {
36   struct file *file;
37
38   filesys_stub_lock ();
39   filesys_stub_put_string ("open");
40   filesys_stub_put_string (name);
41   filesys_stub_match_string ("open");
42   file = filesys_stub_get_file ();
43   filesys_stub_unlock ();
44   
45   return file;
46 }
47
48 bool
49 filesys_remove (const char *name) 
50 {
51   bool success;
52
53   filesys_stub_lock ();
54   filesys_stub_put_string ("remove");
55   filesys_stub_put_string (name);
56   filesys_stub_match_string ("remove");
57   success = filesys_stub_get_bool ();
58   filesys_stub_unlock ();
59
60   return success;
61 }
62 #endif /* FILESYS_STUB */
63
64 #undef NDEBUG
65 #include "debug.h"
66 #include "file.h"
67
68 void
69 filesys_self_test (void)
70 {
71   static const char s[] = "This is a test string.";
72   struct file *file;
73   char s2[sizeof s];
74
75   ASSERT (filesys_create ("foo"));
76   ASSERT ((file = filesys_open ("foo")) != NULL);
77   ASSERT (file_write (file, s, sizeof s) == sizeof s);
78   ASSERT (file_tell (file) == sizeof s);
79   ASSERT (file_length (file) == sizeof s);
80   file_close (file);
81
82   ASSERT ((file = filesys_open ("foo")) != NULL);
83   ASSERT (file_read (file, s2, sizeof s2) == sizeof s2);
84   ASSERT (memcmp (s, s2, sizeof s) == 0);
85   ASSERT (file_tell (file) == sizeof s2);
86   ASSERT (file_length (file) == sizeof s2);
87   file_close (file);
88
89   ASSERT (filesys_remove ("foo"));
90 }