3 * - By John Hodge (thePowersGang)
6 * - Root Filesystem Driver
8 * TODO: Restrict to directories+symlinks only
13 #include <vfs_ramfs.h>
17 #define MAX_FILE_SIZE 10*1024*1024
20 tVFS_Node *Root_InitDevice(const char *Device, const char **Options);
21 tVFS_Node *Root_GetByINode(tVFS_Node *RootNode, Uint64 Inode);
22 tVFS_Node *Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
23 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
24 int Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX]);
25 size_t Root_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
26 size_t Root_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
27 tRamFS_File *Root_int_AllocFile(void);
30 tVFS_Driver gRootFS_Info = {
32 .InitDevice = Root_InitDevice,
33 .GetNodeFromINode = Root_GetByINode
35 tRamFS_File RootFS_Files[MAX_FILES];
36 tVFS_ACL RootFS_DirACLs[3] = {
37 {{0,0}, {0,VFS_PERM_ALL}}, // Owner (Root)
38 {{1,0}, {0,VFS_PERM_ALL}}, // Group (Root)
39 {{0,-1}, {0,VFS_PERM_ALL^VFS_PERM_WRITE}} // World (Nobody)
41 tVFS_ACL RootFS_FileACLs[3] = {
42 {{0,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}}, // Owner (Root)
43 {{1,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}}, // Group (Root)
44 {{0,-1}, {0,VFS_PERM_READ}} // World (Nobody)
46 tVFS_NodeType gRootFS_DirType = {
47 .TypeName = "RootFS-Dir",
48 .ReadDir = Root_ReadDir,
49 .FindDir = Root_FindDir,
52 tVFS_NodeType gRootFS_FileType = {
53 .TypeName = "RootFS-File",
60 * \brief Initialise the root filesystem
62 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
65 if(strcmp(Device, "root") != 0) {
70 root = &RootFS_Files[0];
74 root->Node.ImplPtr = root;
78 = root->Node.ATime = now();
79 root->Node.NumACLs = 3;
80 root->Node.ACLs = RootFS_DirACLs;
82 root->Node.Flags = VFS_FFLAG_DIRECTORY;
83 root->Node.Type = &gRootFS_DirType;
88 tVFS_Node *Root_GetByINode(tVFS_Node *RootNode, Uint64 Inode)
90 if( Inode >= MAX_FILES )
92 if( RootFS_Files[Inode].Name[0] == '\0' )
94 Debug("Root_GetByINode: (%llx) = '%s' %p",
96 RootFS_Files[Inode].Name, &RootFS_Files[Inode].Node
98 return &RootFS_Files[Inode].Node;
102 * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
103 * \brief Create an entry in the root directory
105 tVFS_Node *Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
107 tRamFS_File *parent = Node->ImplPtr;
109 tRamFS_File *prev = NULL;
111 ENTER("pNode sName xFlags", Node, Name, Flags);
113 LOG("Sanity check name length - %i > %i", strlen(Name)+1, sizeof(child->Name));
114 if(strlen(Name) + 1 > sizeof(child->Name)) {
116 LEAVE_RET('n', NULL);
119 // Find last child, while we're at it, check for duplication
120 for( child = parent->Data.FirstChild; child; prev = child, child = child->Next )
122 if(strcmp(child->Name, Name) == 0) {
125 LEAVE_RET('n', NULL);
129 child = Root_int_AllocFile();
131 strcpy(child->Name, Name);
132 LOG("Name = '%s'", child->Name);
134 child->Parent = parent;
136 child->Data.FirstChild = NULL;
138 child->Node.ImplPtr = child;
139 child->Node.Flags = Flags;
140 child->Node.NumACLs = 3;
141 child->Node.Size = 0;
143 if(Flags & VFS_FFLAG_DIRECTORY)
145 child->Node.ACLs = RootFS_DirACLs;
146 child->Node.Type = &gRootFS_DirType;
148 if(Flags & VFS_FFLAG_SYMLINK)
149 child->Node.ACLs = RootFS_DirACLs;
151 child->Node.ACLs = RootFS_FileACLs;
152 child->Node.Type = &gRootFS_FileType;
159 parent->Data.FirstChild = child;
161 parent->Node.Size ++;
163 LEAVE('n', &child->Node);
168 * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
169 * \brief Find an entry in the filesystem
171 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
173 tRamFS_File *parent = Node->ImplPtr;
174 tRamFS_File *child = parent->Data.FirstChild;
176 ENTER("pNode sName", Node, Name);
178 for( child = parent->Data.FirstChild; child; child = child->Next )
180 LOG("child->Name = '%s'", child->Name);
181 if(strcmp(child->Name, Name) == 0)
183 LEAVE('p', &child->Node);
193 * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
194 * \brief Get an entry from the filesystem
196 int Root_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
198 tRamFS_File *parent = Node->ImplPtr;
199 tRamFS_File *child = parent->Data.FirstChild;
201 for( ; child && Pos--; child = child->Next ) ;
204 strncpy(Dest, child->Name, FILENAME_MAX);
212 * \brief Read from a file in the root directory
214 size_t Root_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
216 tRamFS_File *file = Node->ImplPtr;
218 if(Offset > Node->Size) return 0;
220 if(Length > Node->Size)
222 if(Offset+Length > Node->Size)
223 Length = Node->Size - Offset;
225 memcpy(Buffer, file->Data.Bytes+Offset, Length);
231 * \brief Write to a file in the root directory
233 size_t Root_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
235 tRamFS_File *file = Node->ImplPtr;
237 ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
239 if(Offset > Node->Size) {
244 if(Offset + Length > MAX_FILE_SIZE)
246 Length = MAX_FILE_SIZE - Offset;
247 ASSERTC(Length, <=, MAX_FILE_SIZE);
250 LOG("Length = %x", Length);
252 // Check if buffer needs to be expanded
253 if(Offset + Length > Node->Size)
255 size_t newsize = Offset + Length;
256 void *tmp = realloc( file->Data.Bytes, newsize );
258 Warning("Root_Write - Increasing buffer size failed (0x%x)",
263 file->Data.Bytes = tmp;
264 Node->Size = Offset + Length;
265 LOG("Expanded buffer to %i bytes", (int)Node->Size);
268 memcpy(file->Data.Bytes+Offset, Buffer, Length);
269 LOG("File - '%.*s'", Node->Size, file->Data.Bytes);
276 * \fn tRamFS_File *Root_int_AllocFile(void)
277 * \brief Allocates a file from the pool
279 tRamFS_File *Root_int_AllocFile(void)
282 for( i = 0; i < MAX_FILES; i ++ )
284 if( RootFS_Files[i].Name[0] == '\0' )
286 RootFS_Files[i].Node.Inode = i;
287 return &RootFS_Files[i];