3 * - By John Hodge (thePowersGang)
6 * - Root Filesystem Driver
8 * TODO: Restrict to directories only
13 #include <vfs_ramfs.h>
17 #define MAX_FILE_SIZE 1024
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);
29 tVFS_Driver gRootFS_Info = {
31 .InitDevice = Root_InitDevice
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)
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)
44 tVFS_NodeType gRootFS_DirType = {
45 .TypeName = "RootFS-Dir",
46 .ReadDir = Root_ReadDir,
47 .FindDir = Root_FindDir,
50 tVFS_NodeType gRootFS_FileType = {
51 .TypeName = "RootFS-File",
58 * \brief Initialise the root filesystem
60 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
63 if(strcmp(Device, "root") != 0) {
68 root = &RootFS_Files[0];
72 root->Node.ImplPtr = root;
76 = root->Node.ATime = now();
77 root->Node.NumACLs = 3;
78 root->Node.ACLs = RootFS_DirACLs;
80 root->Node.Flags = VFS_FFLAG_DIRECTORY;
81 root->Node.Type = &gRootFS_DirType;
87 * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
88 * \brief Create an entry in the root directory
90 tVFS_Node *Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
92 tRamFS_File *parent = Node->ImplPtr;
94 tRamFS_File *prev = NULL;
96 ENTER("pNode sName xFlags", Node, Name, Flags);
98 LOG("Sanity check name length - %i > %i", strlen(Name)+1, sizeof(child->Name));
99 if(strlen(Name) + 1 > sizeof(child->Name)) {
101 LEAVE_RET('n', NULL);
104 // Find last child, while we're at it, check for duplication
105 for( child = parent->Data.FirstChild; child; prev = child, child = child->Next )
107 if(strcmp(child->Name, Name) == 0) {
110 LEAVE_RET('n', NULL);
114 child = Root_int_AllocFile();
115 memset(child, 0, sizeof(tRamFS_File));
117 strcpy(child->Name, Name);
118 LOG("Name = '%s'", child->Name);
120 child->Parent = parent;
122 child->Data.FirstChild = NULL;
124 child->Node.ImplPtr = child;
125 child->Node.Flags = Flags;
126 child->Node.NumACLs = 3;
127 child->Node.Size = 0;
129 if(Flags & VFS_FFLAG_DIRECTORY)
131 child->Node.ACLs = RootFS_DirACLs;
132 child->Node.Type = &gRootFS_DirType;
134 if(Flags & VFS_FFLAG_SYMLINK)
135 child->Node.ACLs = RootFS_DirACLs;
137 child->Node.ACLs = RootFS_FileACLs;
138 child->Node.Type = &gRootFS_FileType;
145 parent->Data.FirstChild = child;
147 parent->Node.Size ++;
149 LEAVE('n', &child->Node);
154 * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
155 * \brief Find an entry in the filesystem
157 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
159 tRamFS_File *parent = Node->ImplPtr;
160 tRamFS_File *child = parent->Data.FirstChild;
162 ENTER("pNode sName", Node, Name);
164 for( child = parent->Data.FirstChild; child; child = child->Next )
166 LOG("child->Name = '%s'", child->Name);
167 if(strcmp(child->Name, Name) == 0)
169 LEAVE('p', &child->Node);
179 * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
180 * \brief Get an entry from the filesystem
182 int Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
184 tRamFS_File *parent = Node->ImplPtr;
185 tRamFS_File *child = parent->Data.FirstChild;
187 for( ; child && Pos--; child = child->Next ) ;
190 strncpy(Dest, child->Name, FILENAME_MAX);
198 * \brief Read from a file in the root directory
200 size_t Root_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
202 tRamFS_File *file = Node->ImplPtr;
204 if(Offset > Node->Size) return 0;
206 if(Length > Node->Size)
208 if(Offset+Length > Node->Size)
209 Length = Node->Size - Offset;
211 memcpy(Buffer, file->Data.Bytes+Offset, Length);
217 * \brief Write to a file in the root directory
219 size_t Root_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
221 tRamFS_File *file = Node->ImplPtr;
223 ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
225 if(Offset > Node->Size) {
230 if(Offset + Length > MAX_FILE_SIZE)
232 Length = MAX_FILE_SIZE - Offset;
235 LOG("Buffer = '%.*s'", (int)Length, Buffer);
237 // Check if buffer needs to be expanded
238 if(Offset + Length > Node->Size)
240 void *tmp = realloc( file->Data.Bytes, Offset + Length );
242 Warning("Root_Write - Increasing buffer size failed");
246 file->Data.Bytes = tmp;
247 Node->Size = Offset + Length;
248 LOG("Expanded buffer to %i bytes", (int)Node->Size);
251 memcpy(file->Data.Bytes+Offset, Buffer, Length);
252 LOG("File - '%.*s'", Node->Size, file->Data.Bytes);
259 * \fn tRamFS_File *Root_int_AllocFile(void)
260 * \brief Allocates a file from the pool
262 tRamFS_File *Root_int_AllocFile(void)
265 for( i = 0; i < MAX_FILES; i ++ )
267 if( RootFS_Files[i].Name[0] == '\0' )
269 return &RootFS_Files[i];