AcessNative/nativefs - Fixed memory leak due to Close() not being bound
[tpg/acess2.git] / AcessNative / acesskernel_src / nativefs.c
index 6d6e2fe..e51f286 100644 (file)
@@ -5,9 +5,11 @@
  * nativefs.c\r
  * - Host filesystem access\r
  */\r
-#define DEBUG  1\r
+#define DEBUG  0\r
+#define off_t  _acess_off_t\r
 #include <acess.h>     // Acess\r
 #include <vfs.h>       // Acess\r
+#undef off_t\r
 #include <dirent.h>    // Posix\r
 #include <sys/stat.h>  // Posix\r
 #include <stdio.h>     // Posix\r
@@ -30,9 +32,21 @@ tVFS_Node    *NativeFS_Mount(const char *Device, const char **Arguments);
 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
+size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer);\r
+size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer);\r
+void   NativeFS_Close(tVFS_Node *Node);\r
 \r
 // === GLOBALS ===\r
+tVFS_NodeType  gNativeFS_FileNodeType = {\r
+       .Read = NativeFS_Read,\r
+       .Write = NativeFS_Write,\r
+       .Close = NativeFS_Close\r
+};\r
+tVFS_NodeType  gNativeFS_DirNodeType = {\r
+       .FindDir = NativeFS_FindDir,\r
+       .ReadDir = NativeFS_ReadDir,\r
+       .Close = NativeFS_Close\r
+};\r
 tVFS_Driver    gNativeFS_Driver = {\r
        "nativefs", 0,\r
        NativeFS_Mount, NativeFS_Unmount,\r
@@ -53,7 +67,10 @@ tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments)
        DIR     *dp;\r
        \r
        dp = opendir(Device);\r
-       if(!dp) return NULL;\r
+       if(!dp) {\r
+               Log_Warning("NativeFS", "ERROR: Unable to open device root '%s'", Device);\r
+               return NULL;\r
+       }\r
        \r
        // Check if directory exists\r
        // Parse flags from arguments\r
@@ -66,11 +83,11 @@ tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments)
        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
+       ret->Inode = (Uint64)(tVAddr)dp;\r
+       ret->Flags = VFS_FFLAG_DIRECTORY;\r
+\r
+       ret->Type = &gNativeFS_DirNodeType;     \r
+\r
        return ret;\r
 }\r
 \r
@@ -78,7 +95,7 @@ void NativeFS_Unmount(tVFS_Node *Node)
 {\r
        tNativeFS       *info = Node->ImplPtr;\r
        Inode_ClearCache( info->InodeHandle );\r
-       closedir( (void *)Node->Inode );\r
+       closedir( (void *)(tVAddr)Node->Inode );\r
        free(Node->Data);\r
        free(Node);\r
        free(info);\r
@@ -87,12 +104,18 @@ void NativeFS_Unmount(tVFS_Node *Node)
 void NativeFS_Close(tVFS_Node *Node)\r
 {\r
        tNativeFS       *info = Node->ImplPtr;\r
-       Inode_UncacheNode( info->InodeHandle, Node->Inode );\r
+       DIR     *dp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? (DIR*)(tVAddr)Node->Inode : 0;\r
+       FILE    *fp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? 0 : (FILE*)(tVAddr)Node->Inode;\r
+       \r
+       if( Inode_UncacheNode( info->InodeHandle, Node->Inode ) == 1 ) {\r
+               if(dp)  closedir(dp);\r
+               if(fp)  fclose(fp);\r
+       }\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
+       char    *path;\r
        tNativeFS       *info = Node->ImplPtr;\r
        tVFS_Node       baseRet;\r
        struct stat statbuf;\r
@@ -100,6 +123,7 @@ tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)
        ENTER("pNode sName", Node, Name);\r
        \r
        // Create path\r
+       path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);\r
        strcpy(path, Node->Data);\r
        path[Node->ImplInt] = '/';\r
        strcpy(path + Node->ImplInt + 1, Name);\r
@@ -120,16 +144,19 @@ tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)
        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.Inode = (Uint64)(tVAddr) opendir(path);\r
+               baseRet.Type = &gNativeFS_DirNodeType;\r
                baseRet.Flags |= VFS_FFLAG_DIRECTORY;\r
+               baseRet.Size = -1;\r
        }\r
        else\r
        {\r
                LOG("File");\r
-               baseRet.Inode = (Uint64) fopen(path, "r+");\r
-               baseRet.Read = NativeFS_Read;\r
+               baseRet.Inode = (Uint64)(tVAddr) fopen(path, "r+");\r
+               baseRet.Type = &gNativeFS_FileNodeType;\r
+               \r
+               fseek( (FILE*)(tVAddr)baseRet.Inode, 0, SEEK_END );\r
+               baseRet.Size = ftell( (FILE*)(tVAddr)baseRet.Inode );\r
        }\r
        \r
        // Create new node\r
@@ -143,18 +170,56 @@ tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)
 \r
 char *NativeFS_ReadDir(tVFS_Node *Node, int Position)\r
 {\r
-       // Keep track of the current directory position\r
-       return NULL;\r
+       struct dirent   *ent;\r
+       DIR     *dp = (void*)(tVAddr)Node->Inode;\r
+       char    *ret;\r
+\r
+       ENTER("pNode iPosition", Node, Position);\r
+\r
+       // TODO: Keep track of current position in the directory\r
+       // TODO: Lock node during this\r
+       rewinddir(dp);\r
+       do {\r
+               ent = readdir(dp);\r
+       } while(Position-- && ent);\r
+\r
+       if( !ent ) {\r
+               LEAVE('n');\r
+               return NULL;\r
+       }\r
+       \r
+       ret = strdup(ent->d_name);\r
+\r
+       // TODO: Unlock node    \r
+\r
+       LEAVE('s', ret);\r
+       return ret;\r
 }\r
 \r
-Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)\r
+size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t 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
+       ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);\r
+       if( fseek( (FILE *)(tVAddr)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
+       return fread( Buffer, 1, Length, (FILE *)(tVAddr)Node->Inode );\r
+}\r
+\r
+size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer)\r
+{\r
+       FILE    *fp = (FILE *)(tVAddr)Node->Inode;\r
+       ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);\r
+       if( fseek( fp, Offset, SEEK_SET ) != 0 )\r
+       {\r
+               LEAVE('i', 0);\r
+               return 0;\r
+       }\r
+       size_t ret = fwrite( Buffer, 1, Length, fp );\r
+       fflush( fp );\r
+       LEAVE('i', ret);\r
+       return ret;\r
+\r
 }\r

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