3 * - Kernel Status Driver
12 #define VERSION ((0 << 8) | (1)) // 0.01
15 typedef struct sSysFS_Ent
17 struct sSysFS_Ent *Next;
18 struct sSysFS_Ent *ListNext;
19 struct sSysFS_Ent *Parent;
25 int SysFS_Install(char **Arguments);
26 int SysFS_IOCtl(tVFS_Node *Node, int Id, void *Data);
29 int SysFS_RegisterFile(const char *Path, const char *Data, int Length);
30 int SysFS_UpdateFile(int ID, const char *Data, int Length);
31 int SysFS_RemoveFile(int ID);
34 char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Id);
35 tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, const char *Filename);
36 size_t SysFS_Comm_ReadFile(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer);
37 void SysFS_Comm_CloseFile(tVFS_Node *Node);
40 extern tSysFS_Ent gSysFS_Version; // Defined Later
41 extern tSysFS_Ent gSysFS_Root; // Defined Later
42 MODULE_DEFINE(0, VERSION, SysFS, SysFS_Install, NULL, NULL);
43 tVFS_NodeType gSysFS_FileNodeType = {
44 .TypeName = "SysFS File",
45 .Read = SysFS_Comm_ReadFile
47 tVFS_NodeType gSysFS_DirNodeType = {
48 .TypeName = "SysFS Dir",
49 .ReadDir = SysFS_Comm_ReadDir,
50 .FindDir = SysFS_Comm_FindDir
52 tSysFS_Ent gSysFS_Version_Kernel = {
54 &gSysFS_Version, // Parent
56 .Inode = 1, // File #1
58 .ImplInt = (Uint)&gSysFS_Version_Kernel, // Self-Link
61 .ACLs = &gVFS_ACL_EveryoneRO,
62 .Type = &gSysFS_FileNodeType
66 tSysFS_Ent gSysFS_Version = {
71 .ImplPtr = &gSysFS_Version_Kernel,
72 .ImplInt = (Uint)&gSysFS_Version, // Self-Link
74 .ACLs = &gVFS_ACL_EveryoneRX,
75 .Flags = VFS_FFLAG_DIRECTORY,
76 .Type = &gSysFS_DirNodeType
80 // Root of the SysFS tree (just used to keep the code clean)
81 tSysFS_Ent gSysFS_Root = {
86 .ImplPtr = &gSysFS_Version,
87 .ImplInt = (Uint)&gSysFS_Root // Self-Link
91 tDevFS_Driver gSysFS_DriverInfo = {
95 .ACLs = &gVFS_ACL_EveryoneRX,
96 .Flags = VFS_FFLAG_DIRECTORY,
97 .ImplPtr = &gSysFS_Version,
98 .Size = sizeof(gSysFS_Root)/sizeof(tSysFS_Ent),
99 .Type = &gSysFS_DirNodeType
102 int giSysFS_NextFileID = 2;
103 tSysFS_Ent *gSysFS_FileList;
107 * \fn int SysFS_Install(char **Options)
108 * \brief Installs the SysFS Driver
110 int SysFS_Install(char **Options)
112 gSysFS_Version_Kernel.Node.Size = strlen(gsBuildInfo);
113 gSysFS_Version_Kernel.Node.ImplPtr = (void*)gsBuildInfo;
115 DevFS_AddDevice( &gSysFS_DriverInfo );
116 return MODULE_ERR_OK;
120 * \fn int SysFS_RegisterFile(char *Path, char *Data, int Length)
121 * \brief Registers a file (buffer) for the user to be able to read from
122 * \param Path Path for the file to be accessable from (relative to SysFS root)
123 * \param Data Pointer to the data buffer (must be non-volatile)
124 * \param Length Length of the data buffer
125 * \return The file's identifier
127 int SysFS_RegisterFile(const char *Path, const char *Data, int Length)
131 tSysFS_Ent *ent = NULL;
132 tSysFS_Ent *child, *prev;
134 // Find parent directory
135 while( (tmp = strpos(&Path[start], '/')) != -1 )
140 child = ent->Node.ImplPtr;
142 child = gSysFS_DriverInfo.RootNode.ImplPtr;
143 for( ; child; prev = child, child = child->Next )
145 if( strncmp( &Path[start], child->Name, tmp+1 ) == '/' )
149 // Need a new directory?
152 child = calloc( 1, sizeof(tSysFS_Ent)+tmp+1 );
154 memcpy(child->Name, &Path[start], tmp);
155 child->Name[tmp] = '\0';
157 child->Node.Inode = 0;
158 child->Node.ImplPtr = NULL;
159 child->Node.ImplInt = (Uint)child; // Uplink
160 child->Node.NumACLs = 1;
161 child->Node.ACLs = &gVFS_ACL_EveryoneRX;
162 child->Node.Flags = VFS_FFLAG_DIRECTORY;
163 child->Node.Type = &gSysFS_DirNodeType;
166 ent->Node.ImplPtr = child;
168 gSysFS_DriverInfo.RootNode.ImplPtr = child;
169 // ^^^ Impossible (There is already /Version)
176 gSysFS_DriverInfo.RootNode.Size ++;
177 Log_Log("SysFS", "Added directory '%s'", child->Name);
185 // ent: Parent tSysFS_Ent or NULL
186 // start: beginning of last path element
188 // Check if the name is taken
191 child = ent->Node.ImplPtr;
193 child = gSysFS_DriverInfo.RootNode.ImplPtr;
194 for( ; child; child = child->Next )
196 if( strcmp( &Path[start], child->Name ) == 0 )
200 Log_Warning("SysFS", "'%s' is taken (in '%s')\n", &Path[start], Path);
205 child = calloc( 1, sizeof(tSysFS_Ent)+strlen(&Path[start])+1 );
207 strcpy(child->Name, &Path[start]);
210 child->Node.Inode = giSysFS_NextFileID++;
211 child->Node.ImplPtr = (void*)Data;
212 child->Node.ImplInt = (Uint)child; // Uplink
213 child->Node.Size = Length;
214 child->Node.NumACLs = 1;
215 child->Node.ACLs = &gVFS_ACL_EveryoneRO;
216 child->Node.Type = &gSysFS_FileNodeType;
218 // Add to parent's child list
221 child->Next = ent->Node.ImplPtr;
222 ent->Node.ImplPtr = child;
225 gSysFS_DriverInfo.RootNode.Size ++;
226 child->Next = gSysFS_DriverInfo.RootNode.ImplPtr;
227 gSysFS_DriverInfo.RootNode.ImplPtr = child;
229 // Add to global file list
230 child->ListNext = gSysFS_FileList;
231 gSysFS_FileList = child;
233 Log_Log("SysFS", "Added '%s' (%p)", Path, Data);
235 return child->Node.Inode;
239 * \fn int SysFS_UpdateFile(int ID, char *Data, int Length)
240 * \brief Updates a file
241 * \param ID Identifier returned by ::SysFS_RegisterFile
242 * \param Data Pointer to the data buffer
243 * \param Length Length of the data buffer
244 * \return Boolean Success
246 int SysFS_UpdateFile(int ID, const char *Data, int Length)
250 for( ent = gSysFS_FileList; ent; ent = ent->Next )
252 // It's a reverse sorted list
253 if(ent->Node.Inode < ID) return 0;
254 if(ent->Node.Inode == ID)
256 ent->Node.ImplPtr = (void*)Data;
257 ent->Node.Size = Length;
266 * \fn int SysFS_RemoveFile(int ID)
267 * \brief Removes a file from user access
268 * \param ID Identifier returned by ::SysFS_RegisterFile
269 * \return Boolean Success
270 * \note If a handle is still open to the file, it will be invalidated
272 int SysFS_RemoveFile(int ID)
275 tSysFS_Ent *ent, *parent, *prev;
278 for( ent = gSysFS_FileList; ent; prev = ent, ent = ent->Next )
280 // It's a reverse sorted list
281 if(ent->Node.Inode < ID) return 0;
282 if(ent->Node.Inode == ID) break;
287 // Set up for next part
289 parent = file->Parent;
291 // Remove from file list
293 prev->ListNext = file->ListNext;
295 gSysFS_FileList = file->ListNext;
297 file->Node.ImplPtr = NULL;
299 // Search parent directory
300 for( ent = parent->Node.ImplPtr; ent; prev = ent, ent = ent->Next )
302 if( ent == file ) break;
305 Log_Warning("SysFS", "Bookkeeping Error: File in list, but not in directory");
309 // Remove from parent directory
311 prev->Next = ent->Next;
313 parent->Node.ImplPtr = ent->Next;
315 // Free if not in use
316 if(file->Node.ReferenceCount == 0)
323 * \fn char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Pos)
324 * \brief Reads from a SysFS directory
326 char *SysFS_Comm_ReadDir(tVFS_Node *Node, int Pos)
328 tSysFS_Ent *child = (tSysFS_Ent*)Node->ImplPtr;
329 if(Pos < 0 || Pos >= Node->Size) return NULL;
331 for( ; child; child = child->Next, Pos-- )
333 if( Pos == 0 ) return strdup(child->Name);
339 * \fn tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, const char *Filename)
340 * \brief Find a file in a SysFS directory
342 tVFS_Node *SysFS_Comm_FindDir(tVFS_Node *Node, const char *Filename)
344 tSysFS_Ent *child = (tSysFS_Ent*)Node->ImplPtr;
346 for( ; child; child = child->Next )
348 if( strcmp(child->Name, Filename) == 0 )
356 * \brief Read from an exposed buffer
358 size_t SysFS_Comm_ReadFile(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer)
360 if( Offset > Node->Size ) return -1;
361 if( Length > Node->Size ) Length = Node->Size;
362 if( Offset + Length > Node->Size) Length = Node->Size - Offset;
365 memcpy(Buffer, (void*)((Uint)Node->ImplPtr+(Uint)Offset), Length);
373 * \fn void SysFS_Comm_CloseFile(tVFS_Node *Node)
374 * \brief Closes an open file
375 * \param Node Node to close
377 void SysFS_Comm_CloseFile(tVFS_Node *Node)
380 Node->ReferenceCount --;
381 if( Node->ReferenceCount > 0 ) return;
383 // Check if it is still valid
384 if( Node->ImplPtr ) return;
387 free( (void*)Node->ImplInt );