Merge branch 'master' of git://cadel.mutabah.net/acess2
[tpg/acess2.git] / KernelLand / Kernel / vfs / fs / root.c
1 /* 
2  * AcessMicro VFS
3  * - Root Filesystem Driver
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <vfs.h>
8 #include <vfs_ramfs.h>
9
10 // === CONSTANTS ===
11 #define MAX_FILES       64
12 #define MAX_FILE_SIZE   1024
13
14 // === PROTOTYPES ===
15 tVFS_Node       *Root_InitDevice(const char *Device, const char **Options);
16 tVFS_Node       *Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
17 tVFS_Node       *Root_FindDir(tVFS_Node *Node, const char *Name);
18  int    Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
19 size_t  Root_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
20 size_t  Root_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer);
21 tRamFS_File     *Root_int_AllocFile(void);
22
23 // === GLOBALS ===
24 tVFS_Driver     gRootFS_Info = {
25         .Name = "rootfs", 
26         .InitDevice = Root_InitDevice
27         };
28 tRamFS_File     RootFS_Files[MAX_FILES];
29 tVFS_ACL        RootFS_DirACLs[3] = {
30         {{0,0}, {0,VFS_PERM_ALL}},      // Owner (Root)
31         {{1,0}, {0,VFS_PERM_ALL}},      // Group (Root)
32         {{0,-1}, {0,VFS_PERM_ALL^VFS_PERM_WRITE}}       // World (Nobody)
33 };
34 tVFS_ACL        RootFS_FileACLs[3] = {
35         {{0,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}},     // Owner (Root)
36         {{1,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}},     // Group (Root)
37         {{0,-1}, {0,VFS_PERM_READ}}     // World (Nobody)
38 };
39 tVFS_NodeType   gRootFS_DirType = {
40         .TypeName = "RootFS-Dir",
41         .ReadDir = Root_ReadDir,
42         .FindDir = Root_FindDir,
43         .MkNod = Root_MkNod
44 };
45 tVFS_NodeType   gRootFS_FileType = {
46         .TypeName = "RootFS-File",
47         .Read = Root_Read,
48         .Write = Root_Write,
49 };
50
51 // === CODE ===
52 /**
53  * \brief Initialise the root filesystem
54  */
55 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
56 {
57         tRamFS_File     *root;
58         if(strcmp(Device, "root") != 0) {
59                 return NULL;
60         }
61         
62         // Create Root Node
63         root = &RootFS_Files[0];
64
65         root->Name[0] = '/';
66         root->Name[1] = '\0';
67         root->Node.ImplPtr = root;
68         
69         root->Node.CTime
70                 = root->Node.MTime
71                 = root->Node.ATime = now();
72         root->Node.NumACLs = 3;
73         root->Node.ACLs = RootFS_DirACLs;
74
75         root->Node.Flags = VFS_FFLAG_DIRECTORY;
76         root->Node.Type = &gRootFS_DirType;
77         
78         return &root->Node;
79 }
80
81 /**
82  * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
83  * \brief Create an entry in the root directory
84  */
85 tVFS_Node *Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
86 {
87         tRamFS_File     *parent = Node->ImplPtr;
88         tRamFS_File     *child;
89         tRamFS_File     *prev = NULL;
90         
91         ENTER("pNode sName xFlags", Node, Name, Flags);
92         
93         LOG("Sanity check name length - %i > %i", strlen(Name)+1, sizeof(child->Name));
94         if(strlen(Name) + 1 > sizeof(child->Name)) {
95                 errno = EINVAL;
96                 LEAVE_RET('n', NULL);
97         }
98         
99         // Find last child, while we're at it, check for duplication
100         for( child = parent->Data.FirstChild; child; prev = child, child = child->Next )
101         {
102                 if(strcmp(child->Name, Name) == 0) {
103                         LOG("Duplicate");
104                         errno = EEXIST;
105                         LEAVE_RET('n', NULL);
106                 }
107         }
108         
109         child = Root_int_AllocFile();
110         memset(child, 0, sizeof(tRamFS_File));
111         
112         strcpy(child->Name, Name);
113         LOG("Name = '%s'", child->Name);
114         
115         child->Parent = parent;
116         child->Next = NULL;
117         child->Data.FirstChild = NULL;
118         
119         child->Node.ImplPtr = child;
120         child->Node.Flags = Flags;
121         child->Node.NumACLs = 3;
122         child->Node.Size = 0;
123         
124         if(Flags & VFS_FFLAG_DIRECTORY)
125         {
126                 child->Node.ACLs = RootFS_DirACLs;
127                 child->Node.Type = &gRootFS_DirType;
128         } else {
129                 if(Flags & VFS_FFLAG_SYMLINK)
130                         child->Node.ACLs = RootFS_DirACLs;
131                 else
132                         child->Node.ACLs = RootFS_FileACLs;
133                 child->Node.Type = &gRootFS_FileType;
134         }
135         
136         // Append!
137         if( prev )
138                 prev->Next = child;
139         else
140                 parent->Data.FirstChild = child;
141         
142         parent->Node.Size ++;
143         
144         LEAVE('n', &child->Node);
145         return &child->Node;
146 }
147
148 /**
149  * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
150  * \brief Find an entry in the filesystem
151  */
152 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
153 {
154         tRamFS_File     *parent = Node->ImplPtr;
155         tRamFS_File     *child = parent->Data.FirstChild;
156         
157         ENTER("pNode sName", Node, Name);
158         
159         for( child = parent->Data.FirstChild; child; child = child->Next )
160         {
161                 LOG("child->Name = '%s'", child->Name);
162                 if(strcmp(child->Name, Name) == 0)
163                 {
164                         LEAVE('p', &child->Node);
165                         return &child->Node;
166                 }
167         }
168         
169         LEAVE('n');
170         return NULL;
171 }
172
173 /**
174  * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
175  * \brief Get an entry from the filesystem
176  */
177 int Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
178 {
179         tRamFS_File     *parent = Node->ImplPtr;
180         tRamFS_File     *child = parent->Data.FirstChild;
181         
182         for( ; child && Pos--; child = child->Next ) ;
183         
184         if(child) {
185                 strncpy(Dest, child->Name, FILENAME_MAX);
186                 return 0;
187         }
188         
189         return -ENOENT;
190 }
191
192 /**
193  * \brief Read from a file in the root directory
194  */
195 size_t Root_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
196 {
197         tRamFS_File     *file = Node->ImplPtr;
198         
199         if(Offset > Node->Size) return 0;
200
201         if(Length > Node->Size)
202                 Length = Node->Size;
203         if(Offset+Length > Node->Size)
204                 Length = Node->Size - Offset;
205         
206         memcpy(Buffer, file->Data.Bytes+Offset, Length);
207         
208         return Length;
209 }
210
211 /**
212  * \brief Write to a file in the root directory
213  */
214 size_t Root_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
215 {
216         tRamFS_File     *file = Node->ImplPtr;
217
218         ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
219
220         if(Offset > Node->Size) {
221                 LEAVE('i', -1);
222                 return -1;
223         }       
224
225         if(Offset + Length > MAX_FILE_SIZE)
226         {
227                 Length = MAX_FILE_SIZE - Offset;
228         }
229
230         LOG("Buffer = '%.*s'", (int)Length, Buffer);
231         
232         // Check if buffer needs to be expanded
233         if(Offset + Length > Node->Size)
234         {
235                 void *tmp = realloc( file->Data.Bytes, Offset + Length );
236                 if(tmp == NULL) {
237                         Warning("Root_Write - Increasing buffer size failed");
238                         LEAVE('i', -1);
239                         return -1;
240                 }
241                 file->Data.Bytes = tmp;
242                 Node->Size = Offset + Length;
243                 LOG("Expanded buffer to %i bytes", (int)Node->Size);
244         }
245         
246         memcpy(file->Data.Bytes+Offset, Buffer, Length);
247         LOG("File - '%.*s'", Node->Size, file->Data.Bytes);
248         
249         LEAVE('i', Length);
250         return Length;
251 }
252
253 /**
254  * \fn tRamFS_File *Root_int_AllocFile(void)
255  * \brief Allocates a file from the pool
256  */
257 tRamFS_File *Root_int_AllocFile(void)
258 {
259          int    i;
260         for( i = 0; i < MAX_FILES; i ++ )
261         {
262                 if( RootFS_Files[i].Name[0] == '\0' )
263                 {
264                         return &RootFS_Files[i];
265                 }
266         }
267         return NULL;
268 }

UCC git Repository :: git.ucc.asn.au