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

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