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

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