95e308ae43f70dba71117a82c1e1d94b6c41f7cd
[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         return 1;
110 }
111
112 /**
113  * \fn tVFS_Node *Root_FindDir(tVFS_Node *Node, char *Name)
114  * \brief Find an entry in the filesystem
115  */
116 tVFS_Node *Root_FindDir(tVFS_Node *Node, char *Name)
117 {
118         tRamFS_File     *parent = Node->ImplPtr;
119         tRamFS_File     *child = parent->Data.FirstChild;
120         
121         //Log("Root_FindDir: (Node=%p, Name='%s')", Node, Name);
122         
123         for(;child;child = child->Next)
124         {
125                 //Log(" Root_FindDir: strcmp('%s', '%s')", child->Node.Name, Name);
126                 if(strcmp(child->Name, Name) == 0)      return &child->Node;
127         }
128         
129         return NULL;
130 }
131
132 /**
133  * \fn char *Root_ReadDir(tVFS_Node *Node, int Pos)
134  * \brief Get an entry from the filesystem
135  */
136 char *Root_ReadDir(tVFS_Node *Node, int Pos)
137 {
138         tRamFS_File     *parent = Node->ImplPtr;
139         tRamFS_File     *child = parent->Data.FirstChild;
140         
141         for( ; child && Pos--; child = child->Next ) ;
142         
143         if(Pos) return child->Name;
144         
145         return child->Name;
146 }
147
148 /**
149  * \fn Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
150  * \brief Read from a file in the root directory
151  */
152 Uint64 Root_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
153 {
154         tRamFS_File     *file = Node->ImplPtr;
155         
156         if(Offset > Node->Size) return 0;
157         if(Length > Node->Size) return 0;
158         
159         if(Offset+Length > Node->Size)
160                 Length = Node->Size - Offset;
161         
162         memcpy(Buffer, file->Data.Bytes+Offset, Length);
163         
164         return Length;
165 }
166
167 /**
168  * \fn Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
169  * \brief Write to a file in the root directory
170  */
171 Uint64 Root_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
172 {
173         tRamFS_File     *file = Node->ImplPtr;
174         
175         // Check if buffer needs to be expanded
176         if(Offset + Length > Node->Size)
177         {
178                 void *tmp = realloc( file->Data.Bytes, Offset + Length );
179                 if(tmp == NULL) {
180                         Warning("Root_Write - Increasing buffer size failed\n");
181                         return -1;
182                 }
183                 file->Data.Bytes = tmp;
184                 Node->Size = Offset + Length;
185                 Log(" Root_Write: Expanded buffer to %i bytes\n", Node->Size);
186         }
187         
188         memcpy(file->Data.Bytes+Offset, Buffer, Length);
189         
190         return Length;
191 }
192
193 /**
194  * \fn tRamFS_File *Root_int_AllocFile()
195  * \brief Allocates a file from the pool
196  */
197 tRamFS_File *Root_int_AllocFile()
198 {
199          int    i;
200         for( i = 0; i < MAX_FILES; i ++ )
201         {
202                 if( RootFS_Files[i].Name == NULL )
203                 {
204                         return &RootFS_Files[i];
205                 }
206         }
207         return NULL;
208 }

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