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 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
20 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, 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)
41 * \brief Initialise the root filesystem
43 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
46 if(strcmp(Device, "root") != 0) {
51 root = &RootFS_Files[0];
53 root->Node.ImplPtr = root;
57 = root->Node.ATime = now();
58 root->Node.NumACLs = 3;
59 root->Node.ACLs = RootFS_DirACLs;
61 //root->Node.Close = Root_CloseFile; // Not Needed (It's a RAM Disk!)
62 //root->Node.Relink = Root_RelinkRoot; // Not Needed (Why relink the root of the tree)
63 root->Node.FindDir = Root_FindDir;
64 root->Node.ReadDir = Root_ReadDir;
65 root->Node.MkNod = Root_MkNod;
71 * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
72 * \brief Create an entry in the root directory
74 int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
76 tRamFS_File *parent = Node->ImplPtr;
78 tRamFS_File *prev = (tRamFS_File *) &parent->Data.FirstChild;
80 ENTER("pNode sName xFlags", Node, Name, Flags);
82 LOG("%i > %i", strlen(Name)+1, sizeof(child->Name));
83 if(strlen(Name) + 1 > sizeof(child->Name))
86 // Find last child, while we're at it, check for duplication
87 for( child = parent->Data.FirstChild; child; prev = child, child = child->Next )
89 if(strcmp(child->Name, Name) == 0) {
95 child = Root_int_AllocFile();
96 memset(child, 0, sizeof(tRamFS_File));
98 strcpy(child->Name, Name);
100 child->Parent = parent;
102 child->Data.FirstChild = NULL;
104 child->Node.ImplPtr = child;
105 child->Node.Flags = Flags;
106 child->Node.NumACLs = 3;
107 child->Node.Size = 0;
109 if(Flags & VFS_FFLAG_DIRECTORY)
111 child->Node.ACLs = RootFS_DirACLs;
112 child->Node.ReadDir = Root_ReadDir;
113 child->Node.FindDir = Root_FindDir;
114 child->Node.MkNod = Root_MkNod;
116 if(Flags & VFS_FFLAG_SYMLINK)
117 child->Node.ACLs = RootFS_DirACLs;
119 child->Node.ACLs = RootFS_FileACLs;
120 child->Node.Read = Root_Read;
121 child->Node.Write = Root_Write;
126 parent->Node.Size ++;
133 * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
134 * \brief Find an entry in the filesystem
136 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
138 tRamFS_File *parent = Node->ImplPtr;
139 tRamFS_File *child = parent->Data.FirstChild;
141 //Log("Root_FindDir: (Node=%p, Name='%s')", Node, Name);
143 for(;child;child = child->Next)
145 //Log(" Root_FindDir: strcmp('%s', '%s')", child->Node.Name, Name);
146 if(strcmp(child->Name, Name) == 0) return &child->Node;
153 * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
154 * \brief Get an entry from the filesystem
156 char *Root_ReadDir(tVFS_Node *Node, int Pos)
158 tRamFS_File *parent = Node->ImplPtr;
159 tRamFS_File *child = parent->Data.FirstChild;
161 for( ; child && Pos--; child = child->Next ) ;
163 if(child) return strdup(child->Name);
169 * \fn Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
170 * \brief Read from a file in the root directory
172 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
174 tRamFS_File *file = Node->ImplPtr;
175 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
177 if(Offset > Node->Size) {
181 if(Length > Node->Size) Length = Node->Size;
183 if(Offset+Length > Node->Size)
184 Length = Node->Size - Offset;
186 memcpy(Buffer, file->Data.Bytes+Offset, Length);
187 LOG("Buffer = '%.*s'", (int)Length, Buffer);
194 * \fn Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
195 * \brief Write to a file in the root directory
197 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
199 tRamFS_File *file = Node->ImplPtr;
201 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
203 if(Offset > Node->Size) {
208 if(Offset + Length > MAX_FILE_SIZE)
210 Length = MAX_FILE_SIZE - Offset;
213 LOG("Buffer = '%.*s'", (int)Length, Buffer);
215 // Check if buffer needs to be expanded
216 if(Offset + Length > Node->Size)
218 void *tmp = realloc( file->Data.Bytes, Offset + Length );
220 Warning("Root_Write - Increasing buffer size failed");
224 file->Data.Bytes = tmp;
225 Node->Size = Offset + Length;
226 LOG("Expanded buffer to %i bytes", (int)Node->Size);
229 memcpy(file->Data.Bytes+Offset, Buffer, Length);
230 LOG("File - '%.*s'", Node->Size, file->Data.Bytes);
237 * \fn tRamFS_File *Root_int_AllocFile(void)
238 * \brief Allocates a file from the pool
240 tRamFS_File *Root_int_AllocFile(void)
243 for( i = 0; i < MAX_FILES; i ++ )
245 if( RootFS_Files[i].Name[0] == '\0' )
247 return &RootFS_Files[i];