Fixing up doxygen comments
[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, 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  * \fn tVFS_Node *Root_InitDevice(char *Device, char **Options)
40  * \brief Initialise the root filesystem
41  */
42 tVFS_Node *Root_InitDevice(char *Device, 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 = parent->Data.FirstChild;
77         tRamFS_File     *prev = (tRamFS_File *) &parent->Data.FirstChild;
78         
79         ENTER("pNode sName xFlags", Node, Name, Flags);
80         
81         // Find last child, while we're at it, check for duplication
82         for( ; child; prev = child, child = child->Next )
83         {
84                 if(strcmp(child->Name, Name) == 0) {
85                         LEAVE('i', 0);
86                         return 0;
87                 }
88         }
89         
90         child = Root_int_AllocFile();
91         memset(child, 0, sizeof(tRamFS_File));
92         
93         child->Name = malloc(strlen(Name)+1);
94         strcpy(child->Name, Name);
95         
96         child->Parent = parent;
97         child->Next = NULL;
98         child->Data.FirstChild = NULL;
99         
100         child->Node.ImplPtr = child;
101         child->Node.Flags = Flags;
102         child->Node.NumACLs = 3;
103         child->Node.Size = 0;
104         
105         if(Flags & VFS_FFLAG_DIRECTORY)
106         {
107                 child->Node.ACLs = RootFS_DirACLs;
108                 child->Node.ReadDir = Root_ReadDir;
109                 child->Node.FindDir = Root_FindDir;
110                 child->Node.MkNod = Root_MkNod;
111         } else {
112                 if(Flags & VFS_FFLAG_SYMLINK)
113                         child->Node.ACLs = RootFS_DirACLs;
114                 else
115                         child->Node.ACLs = RootFS_FileACLs;
116                 child->Node.Read = Root_Read;
117                 child->Node.Write = Root_Write;
118         }
119         
120         prev->Next = child;
121         
122         parent->Node.Size ++;
123         
124         LEAVE('i', 1);
125         return 1;
126 }
127
128 /**
129  * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
130  * \brief Find an entry in the filesystem
131  */
132 tVFS_Node *Root_FindDir(tVFS_Node *Node, const char *Name)
133 {
134         tRamFS_File     *parent = Node->ImplPtr;
135         tRamFS_File     *child = parent->Data.FirstChild;
136         
137         //Log("Root_FindDir: (Node=%p, Name='%s')", Node, Name);
138         
139         for(;child;child = child->Next)
140         {
141                 //Log(" Root_FindDir: strcmp('%s', '%s')", child->Node.Name, Name);
142                 if(strcmp(child->Name, Name) == 0)      return &child->Node;
143         }
144         
145         return NULL;
146 }
147
148 /**
149  * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
150  * \brief Get an entry from the filesystem
151  */
152 char *Root_ReadDir(tVFS_Node *Node, int Pos)
153 {
154         tRamFS_File     *parent = Node->ImplPtr;
155         tRamFS_File     *child = parent->Data.FirstChild;
156         
157         for( ; child && Pos--; child = child->Next ) ;
158         
159         if(Pos) return strdup(child->Name);
160         
161         return NULL;
162 }
163
164 /**
165  * \fn Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
166  * \brief Read from a file in the root directory
167  */
168 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
169 {
170         tRamFS_File     *file = Node->ImplPtr;
171         
172         if(Offset > Node->Size) return 0;
173         if(Length > Node->Size) return 0;
174         
175         if(Offset+Length > Node->Size)
176                 Length = Node->Size - Offset;
177         
178         memcpy(Buffer, file->Data.Bytes+Offset, Length);
179         
180         return Length;
181 }
182
183 /**
184  * \fn Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
185  * \brief Write to a file in the root directory
186  */
187 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
188 {
189         tRamFS_File     *file = Node->ImplPtr;
190         
191         // Check if buffer needs to be expanded
192         if(Offset + Length > Node->Size)
193         {
194                 void *tmp = realloc( file->Data.Bytes, Offset + Length );
195                 if(tmp == NULL) {
196                         Warning("Root_Write - Increasing buffer size failed");
197                         return -1;
198                 }
199                 file->Data.Bytes = tmp;
200                 Node->Size = Offset + Length;
201                 //LOG("Expanded buffer to %i bytes", Node->Size);
202         }
203         
204         memcpy(file->Data.Bytes+Offset, Buffer, Length);
205         
206         return Length;
207 }
208
209 /**
210  * \fn tRamFS_File *Root_int_AllocFile(void)
211  * \brief Allocates a file from the pool
212  */
213 tRamFS_File *Root_int_AllocFile(void)
214 {
215          int    i;
216         for( i = 0; i < MAX_FILES; i ++ )
217         {
218                 if( RootFS_Files[i].Name == NULL )
219                 {
220                         return &RootFS_Files[i];
221                 }
222         }
223         return NULL;
224 }

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