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

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