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

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