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

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