3 * - Root Filesystem Driver
13 tVFS_Node *Root_InitDevice(char *Device, char **Options);
14 int Root_MkNod(tVFS_Node *Node, char *Name, Uint Flags);
15 tVFS_Node *Root_FindDir(tVFS_Node *Node, char *Name);
16 char *Root_ReadDir(tVFS_Node *Node, int Pos);
17 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
18 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
19 tRamFS_File *Root_int_AllocFile(void);
22 tVFS_Driver gRootFS_Info = {
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)
42 * \fn tVFS_Node *Root_InitDevice(char *Device, char **Options)
43 * \brief Initialise the root filesystem
45 tVFS_Node *Root_InitDevice(char *Device, char **Options)
48 if(strcmp(Device, "root") != 0) {
53 root = &RootFS_Files[0];
55 root->Node.ImplPtr = root;
59 = root->Node.ATime = now();
60 root->Node.NumACLs = 3;
61 root->Node.ACLs = RootFS_DirACLs;
63 //root->Node.Close = Root_CloseFile; // Not Needed (It's a RAM Disk!)
64 //root->Node.Relink = Root_RelinkRoot; // Not Needed (Why relink the root of the tree)
65 root->Node.FindDir = Root_FindDir;
66 root->Node.ReadDir = Root_ReadDir;
67 root->Node.MkNod = Root_MkNod;
73 * \fn int Root_MkNod(tVFS_Node *Node, char *Name, Uint Flags)
74 * \brief Create an entry in the root directory
76 int Root_MkNod(tVFS_Node *Node, char *Name, Uint Flags)
78 tRamFS_File *parent = Node->ImplPtr;
79 tRamFS_File *child = parent->Data.FirstChild;
80 tRamFS_File *prev = (tRamFS_File *) &parent->Data.FirstChild;
82 ENTER("pNode sName xFlags", Node, Name, Flags);
84 // Find last child, while we're at it, check for duplication
85 for( ; child; prev = child, child = child->Next )
87 if(strcmp(child->Name, Name) == 0) {
93 child = Root_int_AllocFile();
94 memset(child, 0, sizeof(tRamFS_File));
96 child->Name = malloc(strlen(Name)+1);
97 strcpy(child->Name, Name);
99 child->Parent = parent;
101 child->Data.FirstChild = NULL;
103 child->Node.ImplPtr = child;
104 child->Node.Flags = Flags;
105 child->Node.NumACLs = 3;
106 child->Node.Size = 0;
108 if(Flags & VFS_FFLAG_DIRECTORY)
110 child->Node.ACLs = RootFS_DirACLs;
111 child->Node.ReadDir = Root_ReadDir;
112 child->Node.FindDir = Root_FindDir;
113 child->Node.MkNod = Root_MkNod;
115 if(Flags & VFS_FFLAG_SYMLINK)
116 child->Node.ACLs = RootFS_DirACLs;
118 child->Node.ACLs = RootFS_FileACLs;
119 child->Node.Read = Root_Read;
120 child->Node.Write = Root_Write;
125 parent->Node.Size ++;
132 * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, char *Name)
133 * \brief Find an entry in the filesystem
135 tVFS_Node *Root_FindDir(tVFS_Node *Node, char *Name)
137 tRamFS_File *parent = Node->ImplPtr;
138 tRamFS_File *child = parent->Data.FirstChild;
140 //Log("Root_FindDir: (Node=%p, Name='%s')", Node, Name);
142 for(;child;child = child->Next)
144 //Log(" Root_FindDir: strcmp('%s', '%s')", child->Node.Name, Name);
145 if(strcmp(child->Name, Name) == 0) return &child->Node;
152 * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
153 * \brief Get an entry from the filesystem
155 char *Root_ReadDir(tVFS_Node *Node, int Pos)
157 tRamFS_File *parent = Node->ImplPtr;
158 tRamFS_File *child = parent->Data.FirstChild;
160 for( ; child && Pos--; child = child->Next ) ;
162 if(Pos) return strdup(child->Name);
168 * \fn Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
169 * \brief Read from a file in the root directory
171 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
173 tRamFS_File *file = Node->ImplPtr;
175 if(Offset > Node->Size) return 0;
176 if(Length > Node->Size) return 0;
178 if(Offset+Length > Node->Size)
179 Length = Node->Size - Offset;
181 memcpy(Buffer, file->Data.Bytes+Offset, Length);
187 * \fn Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
188 * \brief Write to a file in the root directory
190 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
192 tRamFS_File *file = Node->ImplPtr;
194 // Check if buffer needs to be expanded
195 if(Offset + Length > Node->Size)
197 void *tmp = realloc( file->Data.Bytes, Offset + Length );
199 Warning("Root_Write - Increasing buffer size failed");
202 file->Data.Bytes = tmp;
203 Node->Size = Offset + Length;
204 //LOG("Expanded buffer to %i bytes", Node->Size);
207 memcpy(file->Data.Bytes+Offset, Buffer, Length);
213 * \fn tRamFS_File *Root_int_AllocFile(void)
214 * \brief Allocates a file from the pool
216 tRamFS_File *Root_int_AllocFile(void)
219 for( i = 0; i < MAX_FILES; i ++ )
221 if( RootFS_Files[i].Name == NULL )
223 return &RootFS_Files[i];