AcessNative - Huge changes, cleaning up and getting it to work
[tpg/acess2.git] / AcessNative / acesskernel_src / nativefs.c
index 71774e2..6d6e2fe 100644 (file)
  * nativefs.c\r
  * - Host filesystem access\r
  */\r
-#include <acess.h>\r
-#include <vfs.h>\r
+#define DEBUG  1\r
+#include <acess.h>     // Acess\r
+#include <vfs.h>       // Acess\r
+#include <dirent.h>    // Posix\r
+#include <sys/stat.h>  // Posix\r
+#include <stdio.h>     // Posix\r
+\r
+//NOTES:\r
+// tVFS_Node->ImplPtr is a pointer to the filesystem flags (tNativeFS)\r
+// tVFS_Node->Data is the path string (heap string)\r
+// tVFS_Node->ImplInt is the path length\r
+\r
+// === STRUCTURES ===\r
+typedef struct\r
+{\r
+        int    InodeHandle;\r
+        int    bReadOnly;\r
+}      tNativeFS;\r
+\r
+// === PROTOTYPES ===\r
+ int   NativeFS_Install(char **Arguments);\r
+tVFS_Node      *NativeFS_Mount(const char *Device, const char **Arguments);\r
+void   NativeFS_Unmount(tVFS_Node *Node);\r
+tVFS_Node      *NativeFS_FindDir(tVFS_Node *Node, const char *Name);\r
+char   *NativeFS_ReadDir(tVFS_Node *Node, int Position);\r
+Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);\r
 \r
 // === GLOBALS ===\r
+tVFS_Driver    gNativeFS_Driver = {\r
+       "nativefs", 0,\r
+       NativeFS_Mount, NativeFS_Unmount,\r
+       NULL,\r
+};\r
 \r
 // === CODE ===\r
-tVFS_Node *Native_Mount(const char *Device, const char **Arguments)\r
+int NativeFS_Install(char **Arguments)\r
+{\r
+       VFS_AddDriver(&gNativeFS_Driver);\r
+       return 0;\r
+}\r
+\r
+tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments)\r
+{\r
+       tVFS_Node       *ret;\r
+       tNativeFS       *info;\r
+       DIR     *dp;\r
+       \r
+       dp = opendir(Device);\r
+       if(!dp) return NULL;\r
+       \r
+       // Check if directory exists\r
+       // Parse flags from arguments\r
+       info = malloc(sizeof(tNativeFS));\r
+       info->InodeHandle = Inode_GetHandle();\r
+       info->bReadOnly = 0;\r
+       // Create node\r
+       ret = malloc(sizeof(tVFS_Node));\r
+       memset(ret, 0, sizeof(tVFS_Node));\r
+       ret->Data = strdup(Device);\r
+       ret->ImplInt = strlen(ret->Data);\r
+       ret->ImplPtr = info;\r
+       ret->Inode = (Uint64)dp;\r
+       \r
+       ret->FindDir = NativeFS_FindDir;\r
+       ret->ReadDir = NativeFS_ReadDir;\r
+       \r
+       return ret;\r
+}\r
+\r
+void NativeFS_Unmount(tVFS_Node *Node)\r
 {\r
+       tNativeFS       *info = Node->ImplPtr;\r
+       Inode_ClearCache( info->InodeHandle );\r
+       closedir( (void *)Node->Inode );\r
+       free(Node->Data);\r
+       free(Node);\r
+       free(info);\r
+}\r
+\r
+void NativeFS_Close(tVFS_Node *Node)\r
+{\r
+       tNativeFS       *info = Node->ImplPtr;\r
+       Inode_UncacheNode( info->InodeHandle, Node->Inode );\r
+}\r
+\r
+tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)\r
+{\r
+       char    *path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);\r
+       tNativeFS       *info = Node->ImplPtr;\r
+       tVFS_Node       baseRet;\r
+       struct stat statbuf;\r
+\r
+       ENTER("pNode sName", Node, Name);\r
+       \r
+       // Create path\r
+       strcpy(path, Node->Data);\r
+       path[Node->ImplInt] = '/';\r
+       strcpy(path + Node->ImplInt + 1, Name);\r
+       \r
+       LOG("path = '%s'", path);\r
+       \r
+       // Check if file exists\r
+       if( stat(path, &statbuf) ) {\r
+               free(path);\r
+               LOG("Doesn't exist");\r
+               LEAVE('n');\r
+               return NULL;\r
+       }\r
+       \r
+       memset(&baseRet, 0, sizeof(tVFS_Node));\r
+       \r
+       // Check file type\r
+       if( S_ISDIR(statbuf.st_mode) )\r
+       {\r
+               LOG("Directory");\r
+               baseRet.Inode = (Uint64) opendir(path);\r
+               baseRet.FindDir = NativeFS_FindDir;\r
+               baseRet.ReadDir = NativeFS_ReadDir;\r
+               baseRet.Flags |= VFS_FFLAG_DIRECTORY;\r
+       }\r
+       else\r
+       {\r
+               LOG("File");\r
+               baseRet.Inode = (Uint64) fopen(path, "r+");\r
+               baseRet.Read = NativeFS_Read;\r
+       }\r
+       \r
+       // Create new node\r
+       baseRet.ImplPtr = info;\r
+       baseRet.ImplInt = strlen(path);\r
+       baseRet.Data = path;\r
+       \r
+       LEAVE('-');\r
+       return Inode_CacheNode(info->InodeHandle, &baseRet);\r
+}\r
+\r
+char *NativeFS_ReadDir(tVFS_Node *Node, int Position)\r
+{\r
+       // Keep track of the current directory position\r
        return NULL;\r
 }\r
+\r
+Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)\r
+{\r
+       ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);\r
+       if( fseek( (void *)Node->Inode, Offset, SEEK_SET ) != 0 )\r
+       {\r
+               LEAVE('i', 0);\r
+               return 0;\r
+       }\r
+       LEAVE('-');\r
+       return fread( Buffer, 1, Length, (void *)Node->Inode );\r
+}\r

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