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

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