3 * - Root Filesystem Driver
13 tVFS_Node *Root_InitDevice(const char *Device, const char **Options);
14 int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);
15 tVFS_Node *Root_FindDir(tVFS_Node *Node, const 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 = {
23 "rootfs", 0, Root_InitDevice, NULL, NULL
25 tRamFS_File RootFS_Files[MAX_FILES];
26 tVFS_ACL RootFS_DirACLs[3] = {
27 {{0,0}, {0,VFS_PERM_ALL}}, // Owner (Root)
28 {{1,0}, {0,VFS_PERM_ALL}}, // Group (Root)
29 {{0,-1}, {0,VFS_PERM_ALL^VFS_PERM_WRITE}} // World (Nobody)
31 tVFS_ACL RootFS_FileACLs[3] = {
32 {{0,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}}, // Owner (Root)
33 {{1,0}, {0,VFS_PERM_ALL^VFS_PERM_EXECUTE}}, // Group (Root)
34 {{0,-1}, {0,VFS_PERM_READ}} // World (Nobody)
39 * \brief Initialise the root filesystem
41 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
44 if(strcmp(Device, "root") != 0) {
49 root = &RootFS_Files[0];
51 root->Node.ImplPtr = root;
55 = root->Node.ATime = now();
56 root->Node.NumACLs = 3;
57 root->Node.ACLs = RootFS_DirACLs;
59 //root->Node.Close = Root_CloseFile; // Not Needed (It's a RAM Disk!)
60 //root->Node.Relink = Root_RelinkRoot; // Not Needed (Why relink the root of the tree)
61 root->Node.FindDir = Root_FindDir;
62 root->Node.ReadDir = Root_ReadDir;
63 root->Node.MkNod = Root_MkNod;
69 * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
70 * \brief Create an entry in the root directory
72 int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
74 tRamFS_File *parent = Node->ImplPtr;
75 tRamFS_File *child = parent->Data.FirstChild;
76 tRamFS_File *prev = (tRamFS_File *) &parent->Data.FirstChild;
78 ENTER("pNode sName xFlags", Node, Name, Flags);
80 // Find last child, while we're at it, check for duplication
81 for( ; child; prev = child, child = child->Next )
83 if(strcmp(child->Name, Name) == 0) {
89 child = Root_int_AllocFile();
90 memset(child, 0, sizeof(tRamFS_File));
92 child->Name = malloc(strlen(Name)+1);
93 strcpy(child->Name, Name);
95 child->Parent = parent;
97 child->Data.FirstChild = NULL;
99 child->Node.ImplPtr = child;
100 child->Node.Flags = Flags;
101 child->Node.NumACLs = 3;
102 child->Node.Size = 0;
104 if(Flags & VFS_FFLAG_DIRECTORY)
106 child->Node.ACLs = RootFS_DirACLs;
107 child->Node.ReadDir = Root_ReadDir;
108 child->Node.FindDir = Root_FindDir;
109 child->Node.MkNod = Root_MkNod;
111 if(Flags & VFS_FFLAG_SYMLINK)
112 child->Node.ACLs = RootFS_DirACLs;
114 child->Node.ACLs = RootFS_FileACLs;
115 child->Node.Read = Root_Read;
116 child->Node.Write = Root_Write;
121 parent->Node.Size ++;
128 * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
129 * \brief Find an entry in the filesystem
131 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
133 tRamFS_File *parent = Node->ImplPtr;
134 tRamFS_File *child = parent->Data.FirstChild;
136 //Log("Root_FindDir: (Node=%p, Name='%s')", Node, Name);
138 for(;child;child = child->Next)
140 //Log(" Root_FindDir: strcmp('%s', '%s')", child->Node.Name, Name);
141 if(strcmp(child->Name, Name) == 0) return &child->Node;
148 * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
149 * \brief Get an entry from the filesystem
151 char *Root_ReadDir(tVFS_Node *Node, int Pos)
153 tRamFS_File *parent = Node->ImplPtr;
154 tRamFS_File *child = parent->Data.FirstChild;
156 for( ; child && Pos--; child = child->Next ) ;
158 if(Pos) return strdup(child->Name);
164 * \fn Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
165 * \brief Read from a file in the root directory
167 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
169 tRamFS_File *file = Node->ImplPtr;
171 if(Offset > Node->Size) return 0;
172 if(Length > Node->Size) return 0;
174 if(Offset+Length > Node->Size)
175 Length = Node->Size - Offset;
177 memcpy(Buffer, file->Data.Bytes+Offset, Length);
183 * \fn Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
184 * \brief Write to a file in the root directory
186 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
188 tRamFS_File *file = Node->ImplPtr;
190 // Check if buffer needs to be expanded
191 if(Offset + Length > Node->Size)
193 void *tmp = realloc( file->Data.Bytes, Offset + Length );
195 Warning("Root_Write - Increasing buffer size failed");
198 file->Data.Bytes = tmp;
199 Node->Size = Offset + Length;
200 //LOG("Expanded buffer to %i bytes", Node->Size);
203 memcpy(file->Data.Bytes+Offset, Buffer, Length);
209 * \fn tRamFS_File *Root_int_AllocFile(void)
210 * \brief Allocates a file from the pool
212 tRamFS_File *Root_int_AllocFile(void)
215 for( i = 0; i < MAX_FILES; i ++ )
217 if( RootFS_Files[i].Name == NULL )
219 return &RootFS_Files[i];