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

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