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 = {
25 "rootfs", 0, Root_InitDevice, NULL, NULL
27 tRamFS_File RootFS_Files[MAX_FILES];
28 tVFS_ACL RootFS_DirACLs[3] = {
29 {{0,0}, {0,VFS_PERM_ALL}}, // Owner (Root)
30 {{1,0}, {0,VFS_PERM_ALL}}, // Group (Root)
31 {{0,-1}, {0,VFS_PERM_ALL^VFS_PERM_WRITE}} // World (Nobody)
33 tVFS_ACL RootFS_FileACLs[3] = {
34 {{0,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}}, // Owner (Root)
35 {{1,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}}, // Group (Root)
36 {{0,-1}, {0,VFS_PERM_READ}} // World (Nobody)
38 tVFS_NodeType gRootFS_DirType = {
39 .TypeName = "RootFS-Dir",
40 .ReadDir = Root_ReadDir,
41 .FindDir = Root_FindDir,
44 tVFS_NodeType gRootFS_FileType = {
45 .TypeName = "RootFS-File",
52 * \brief Initialise the root filesystem
54 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
57 if(strcmp(Device, "root") != 0) {
62 root = &RootFS_Files[0];
66 root->Node.ImplPtr = root;
70 = root->Node.ATime = now();
71 root->Node.NumACLs = 3;
72 root->Node.ACLs = RootFS_DirACLs;
74 root->Node.Type = &gRootFS_DirType;
80 * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
81 * \brief Create an entry in the root directory
83 int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
85 tRamFS_File *parent = Node->ImplPtr;
87 tRamFS_File *prev = NULL;
89 ENTER("pNode sName xFlags", Node, Name, Flags);
91 LOG("Sanity check name length - %i > %i", strlen(Name)+1, sizeof(child->Name));
92 if(strlen(Name) + 1 > sizeof(child->Name))
95 // Find last child, while we're at it, check for duplication
96 for( child = parent->Data.FirstChild; child; prev = child, child = child->Next )
98 if(strcmp(child->Name, Name) == 0) {
105 child = Root_int_AllocFile();
106 memset(child, 0, sizeof(tRamFS_File));
108 strcpy(child->Name, Name);
109 LOG("Name = '%s'", child->Name);
111 child->Parent = parent;
113 child->Data.FirstChild = NULL;
115 child->Node.ImplPtr = child;
116 child->Node.Flags = Flags;
117 child->Node.NumACLs = 3;
118 child->Node.Size = 0;
120 if(Flags & VFS_FFLAG_DIRECTORY)
122 child->Node.ACLs = RootFS_DirACLs;
123 child->Node.Type = &gRootFS_DirType;
125 if(Flags & VFS_FFLAG_SYMLINK)
126 child->Node.ACLs = RootFS_DirACLs;
128 child->Node.ACLs = RootFS_FileACLs;
129 child->Node.Type = &gRootFS_FileType;
136 parent->Data.FirstChild = child;
138 parent->Node.Size ++;
145 * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
146 * \brief Find an entry in the filesystem
148 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
150 tRamFS_File *parent = Node->ImplPtr;
151 tRamFS_File *child = parent->Data.FirstChild;
153 ENTER("pNode sName", Node, Name);
155 for( child = parent->Data.FirstChild; child; child = child->Next )
157 LOG("child->Name = '%s'", child->Name);
158 if(strcmp(child->Name, Name) == 0)
160 LEAVE('p', &child->Node);
170 * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
171 * \brief Get an entry from the filesystem
173 char *Root_ReadDir(tVFS_Node *Node, int Pos)
175 tRamFS_File *parent = Node->ImplPtr;
176 tRamFS_File *child = parent->Data.FirstChild;
178 for( ; child && Pos--; child = child->Next ) ;
180 if(child) return strdup(child->Name);
186 * \brief Read from a file in the root directory
188 size_t Root_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
190 tRamFS_File *file = Node->ImplPtr;
192 if(Offset > Node->Size) return 0;
194 if(Length > Node->Size)
196 if(Offset+Length > Node->Size)
197 Length = Node->Size - Offset;
199 memcpy(Buffer, file->Data.Bytes+Offset, Length);
205 * \brief Write to a file in the root directory
207 size_t Root_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer)
209 tRamFS_File *file = Node->ImplPtr;
211 ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
213 if(Offset > Node->Size) {
218 if(Offset + Length > MAX_FILE_SIZE)
220 Length = MAX_FILE_SIZE - Offset;
223 LOG("Buffer = '%.*s'", (int)Length, Buffer);
225 // Check if buffer needs to be expanded
226 if(Offset + Length > Node->Size)
228 void *tmp = realloc( file->Data.Bytes, Offset + Length );
230 Warning("Root_Write - Increasing buffer size failed");
234 file->Data.Bytes = tmp;
235 Node->Size = Offset + Length;
236 LOG("Expanded buffer to %i bytes", (int)Node->Size);
239 memcpy(file->Data.Bytes+Offset, Buffer, Length);
240 LOG("File - '%.*s'", Node->Size, file->Data.Bytes);
247 * \fn tRamFS_File *Root_int_AllocFile(void)
248 * \brief Allocates a file from the pool
250 tRamFS_File *Root_int_AllocFile(void)
253 for( i = 0; i < MAX_FILES; i ++ )
255 if( RootFS_Files[i].Name[0] == '\0' )
257 return &RootFS_Files[i];