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

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