3 * - Root Filesystem Driver
12 #define MAX_FILE_SIZE 1024
15 tVFS_Node *Root_InitDevice(const char *Device, const char **Options);
16 int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
17 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name);
18 char *Root_ReadDir(tVFS_Node *Node, int Pos);
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);
24 tVFS_Driver gRootFS_Info = {
26 .InitDevice = Root_InitDevice
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)
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)
39 tVFS_NodeType gRootFS_DirType = {
40 .TypeName = "RootFS-Dir",
41 .ReadDir = Root_ReadDir,
42 .FindDir = Root_FindDir,
45 tVFS_NodeType gRootFS_FileType = {
46 .TypeName = "RootFS-File",
53 * \brief Initialise the root filesystem
55 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
58 if(strcmp(Device, "root") != 0) {
63 root = &RootFS_Files[0];
67 root->Node.ImplPtr = root;
71 = root->Node.ATime = now();
72 root->Node.NumACLs = 3;
73 root->Node.ACLs = RootFS_DirACLs;
75 root->Node.Flags = VFS_FFLAG_DIRECTORY;
76 root->Node.Type = &gRootFS_DirType;
82 * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
83 * \brief Create an entry in the root directory
85 int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
87 tRamFS_File *parent = Node->ImplPtr;
89 tRamFS_File *prev = NULL;
91 ENTER("pNode sName xFlags", Node, Name, Flags);
93 LOG("Sanity check name length - %i > %i", strlen(Name)+1, sizeof(child->Name));
94 if(strlen(Name) + 1 > sizeof(child->Name))
95 LEAVE_RET('i', EINVAL);
97 // Find last child, while we're at it, check for duplication
98 for( child = parent->Data.FirstChild; child; prev = child, child = child->Next )
100 if(strcmp(child->Name, Name) == 0) {
107 child = Root_int_AllocFile();
108 memset(child, 0, sizeof(tRamFS_File));
110 strcpy(child->Name, Name);
111 LOG("Name = '%s'", child->Name);
113 child->Parent = parent;
115 child->Data.FirstChild = NULL;
117 child->Node.ImplPtr = child;
118 child->Node.Flags = Flags;
119 child->Node.NumACLs = 3;
120 child->Node.Size = 0;
122 if(Flags & VFS_FFLAG_DIRECTORY)
124 child->Node.ACLs = RootFS_DirACLs;
125 child->Node.Type = &gRootFS_DirType;
127 if(Flags & VFS_FFLAG_SYMLINK)
128 child->Node.ACLs = RootFS_DirACLs;
130 child->Node.ACLs = RootFS_FileACLs;
131 child->Node.Type = &gRootFS_FileType;
138 parent->Data.FirstChild = child;
140 parent->Node.Size ++;
147 * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
148 * \brief Find an entry in the filesystem
150 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
152 tRamFS_File *parent = Node->ImplPtr;
153 tRamFS_File *child = parent->Data.FirstChild;
155 ENTER("pNode sName", Node, Name);
157 for( child = parent->Data.FirstChild; child; child = child->Next )
159 LOG("child->Name = '%s'", child->Name);
160 if(strcmp(child->Name, Name) == 0)
162 LEAVE('p', &child->Node);
172 * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
173 * \brief Get an entry from the filesystem
175 char *Root_ReadDir(tVFS_Node *Node, int Pos)
177 tRamFS_File *parent = Node->ImplPtr;
178 tRamFS_File *child = parent->Data.FirstChild;
180 for( ; child && Pos--; child = child->Next ) ;
182 if(child) return strdup(child->Name);
188 * \brief Read from a file in the root directory
190 size_t Root_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
192 tRamFS_File *file = Node->ImplPtr;
194 if(Offset > Node->Size) return 0;
196 if(Length > Node->Size)
198 if(Offset+Length > Node->Size)
199 Length = Node->Size - Offset;
201 memcpy(Buffer, file->Data.Bytes+Offset, Length);
207 * \brief Write to a file in the root directory
209 size_t Root_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
211 tRamFS_File *file = Node->ImplPtr;
213 ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
215 if(Offset > Node->Size) {
220 if(Offset + Length > MAX_FILE_SIZE)
222 Length = MAX_FILE_SIZE - Offset;
225 LOG("Buffer = '%.*s'", (int)Length, Buffer);
227 // Check if buffer needs to be expanded
228 if(Offset + Length > Node->Size)
230 void *tmp = realloc( file->Data.Bytes, Offset + Length );
232 Warning("Root_Write - Increasing buffer size failed");
236 file->Data.Bytes = tmp;
237 Node->Size = Offset + Length;
238 LOG("Expanded buffer to %i bytes", (int)Node->Size);
241 memcpy(file->Data.Bytes+Offset, Buffer, Length);
242 LOG("File - '%.*s'", Node->Size, file->Data.Bytes);
249 * \fn tRamFS_File *Root_int_AllocFile(void)
250 * \brief Allocates a file from the pool
252 tRamFS_File *Root_int_AllocFile(void)
255 for( i = 0; i < MAX_FILES; i ++ )
257 if( RootFS_Files[i].Name[0] == '\0' )
259 return &RootFS_Files[i];