Kernel/vfs - Debugging changes (and some limits to FS_Root)
[tpg/acess2.git] / Kernel / vfs / fs / root.c
1 /* 
2  * AcessMicro VFS
3  * - Root Filesystem Driver
4  */
5 #define DEBUG   0
6 #include <acess.h>
7 #include <vfs.h>
8 #include <vfs_ramfs.h>
9
10 // === CONSTANTS ===
11 #define MAX_FILES       64
12 #define MAX_FILE_SIZE   1024
13
14 // === PROTOTYPES ===
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);
22
23 // === GLOBALS ===
24 tVFS_Driver     gRootFS_Info = {
25         "rootfs", 0, Root_InitDevice, NULL, NULL
26         };
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)
32 };
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)
37 };
38
39 // === CODE ===
40 /**
41  * \brief Initialise the root filesystem
42  */
43 tVFS_Node *Root_InitDevice(const char *Device, const char **Options)
44 {
45         tRamFS_File     *root;
46         if(strcmp(Device, "root") != 0) {
47                 return NULL;
48         }
49         
50         // Create Root Node
51         root = &RootFS_Files[0];
52         
53         root->Node.ImplPtr = root;
54         
55         root->Node.CTime
56                 = root->Node.MTime
57                 = root->Node.ATime = now();
58         root->Node.NumACLs = 3;
59         root->Node.ACLs = RootFS_DirACLs;
60         
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;
66         
67         return &root->Node;
68 }
69
70 /**
71  * \fn int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
72  * \brief Create an entry in the root directory
73  */
74 int Root_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)
75 {
76         tRamFS_File     *parent = Node->ImplPtr;
77         tRamFS_File     *child;
78         tRamFS_File     *prev = (tRamFS_File *) &parent->Data.FirstChild;
79         
80         ENTER("pNode sName xFlags", Node, Name, Flags);
81         
82         LOG("%i > %i", strlen(Name)+1, sizeof(child->Name));
83         if(strlen(Name) + 1 > sizeof(child->Name))
84                 LEAVE_RET('i', 0);
85         
86         // Find last child, while we're at it, check for duplication
87         for( child = parent->Data.FirstChild; child; prev = child, child = child->Next )
88         {
89                 if(strcmp(child->Name, Name) == 0) {
90                         LEAVE('i', 0);
91                         return 0;
92                 }
93         }
94         
95         child = Root_int_AllocFile();
96         memset(child, 0, sizeof(tRamFS_File));
97         
98         strcpy(child->Name, Name);
99         
100         child->Parent = parent;
101         child->Next = NULL;
102         child->Data.FirstChild = NULL;
103         
104         child->Node.ImplPtr = child;
105         child->Node.Flags = Flags;
106         child->Node.NumACLs = 3;
107         child->Node.Size = 0;
108         
109         if(Flags & VFS_FFLAG_DIRECTORY)
110         {
111                 child->Node.ACLs = RootFS_DirACLs;
112                 child->Node.ReadDir = Root_ReadDir;
113                 child->Node.FindDir = Root_FindDir;
114                 child->Node.MkNod = Root_MkNod;
115         } else {
116                 if(Flags & VFS_FFLAG_SYMLINK)
117                         child->Node.ACLs = RootFS_DirACLs;
118                 else
119                         child->Node.ACLs = RootFS_FileACLs;
120                 child->Node.Read = Root_Read;
121                 child->Node.Write = Root_Write;
122         }
123         
124         prev->Next = child;
125         
126         parent->Node.Size ++;
127         
128         LEAVE('i', 1);
129         return 1;
130 }
131
132 /**
133  * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
134  * \brief Find an entry in the filesystem
135  */
136 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
137 {
138         tRamFS_File     *parent = Node->ImplPtr;
139         tRamFS_File     *child = parent->Data.FirstChild;
140         
141         //Log("Root_FindDir: (Node=%p, Name='%s')", Node, Name);
142         
143         for(;child;child = child->Next)
144         {
145                 //Log(" Root_FindDir: strcmp('%s', '%s')", child->Node.Name, Name);
146                 if(strcmp(child->Name, Name) == 0)      return &child->Node;
147         }
148         
149         return NULL;
150 }
151
152 /**
153  * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
154  * \brief Get an entry from the filesystem
155  */
156 char *Root_ReadDir(tVFS_Node *Node, int Pos)
157 {
158         tRamFS_File     *parent = Node->ImplPtr;
159         tRamFS_File     *child = parent->Data.FirstChild;
160         
161         for( ; child && Pos--; child = child->Next ) ;
162         
163         if(child)       return strdup(child->Name);
164         
165         return NULL;
166 }
167
168 /**
169  * \fn Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
170  * \brief Read from a file in the root directory
171  */
172 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
173 {
174         tRamFS_File     *file = Node->ImplPtr;
175         ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
176         
177         if(Offset > Node->Size) {
178                 LEAVE('i', 0);
179                 return 0;
180         }
181         if(Length > Node->Size) Length = Node->Size;
182         
183         if(Offset+Length > Node->Size)
184                 Length = Node->Size - Offset;
185         
186         memcpy(Buffer, file->Data.Bytes+Offset, Length);
187         LOG("Buffer = '%.*s'", (int)Length, Buffer);
188
189         LEAVE('i', Length);
190         return Length;
191 }
192
193 /**
194  * \fn Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
195  * \brief Write to a file in the root directory
196  */
197 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
198 {
199         tRamFS_File     *file = Node->ImplPtr;
200
201         ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
202
203         if(Offset > Node->Size) {
204                 LEAVE('i', -1);
205                 return -1;
206         }       
207
208         if(Offset + Length > MAX_FILE_SIZE)
209         {
210                 Length = MAX_FILE_SIZE - Offset;
211         }
212
213         LOG("Buffer = '%.*s'", (int)Length, Buffer);
214         
215         // Check if buffer needs to be expanded
216         if(Offset + Length > Node->Size)
217         {
218                 void *tmp = realloc( file->Data.Bytes, Offset + Length );
219                 if(tmp == NULL) {
220                         Warning("Root_Write - Increasing buffer size failed");
221                         LEAVE('i', -1);
222                         return -1;
223                 }
224                 file->Data.Bytes = tmp;
225                 Node->Size = Offset + Length;
226                 LOG("Expanded buffer to %i bytes", (int)Node->Size);
227         }
228         
229         memcpy(file->Data.Bytes+Offset, Buffer, Length);
230         LOG("File - '%.*s'", Node->Size, file->Data.Bytes);
231         
232         LEAVE('i', Length);
233         return Length;
234 }
235
236 /**
237  * \fn tRamFS_File *Root_int_AllocFile(void)
238  * \brief Allocates a file from the pool
239  */
240 tRamFS_File *Root_int_AllocFile(void)
241 {
242          int    i;
243         for( i = 0; i < MAX_FILES; i ++ )
244         {
245                 if( RootFS_Files[i].Name[0] == '\0' )
246                 {
247                         return &RootFS_Files[i];
248                 }
249         }
250         return NULL;
251 }

UCC git Repository :: git.ucc.asn.au