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

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