X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=AcessNative%2Facesskernel_src%2Fnativefs.c;h=7c20b69e0d55e10cf8b4f37aa8b4e610e0b07f5e;hb=151e0400c6cf69d71a51e49bc75fac3d7bdfc2f7;hp=6d6e2fedd6a60ceaea9d3d06b90b0090341f2e20;hpb=3764c294f21229bdf700f436fa4884f5e76e0d3a;p=tpg%2Facess2.git diff --git a/AcessNative/acesskernel_src/nativefs.c b/AcessNative/acesskernel_src/nativefs.c index 6d6e2fed..7c20b69e 100644 --- a/AcessNative/acesskernel_src/nativefs.c +++ b/AcessNative/acesskernel_src/nativefs.c @@ -5,9 +5,11 @@ * nativefs.c * - Host filesystem access */ -#define DEBUG 1 +#define DEBUG 0 +#define off_t _acess_off_t #include // Acess #include // Acess +#undef off_t #include // Posix #include // Posix #include // Posix @@ -30,13 +32,25 @@ tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments); void NativeFS_Unmount(tVFS_Node *Node); tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name); char *NativeFS_ReadDir(tVFS_Node *Node, int Position); -Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer); +size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer); +size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer); +void NativeFS_Close(tVFS_Node *Node); // === GLOBALS === +tVFS_NodeType gNativeFS_FileNodeType = { + .Read = NativeFS_Read, + .Write = NativeFS_Write, + .Close = NativeFS_Close +}; +tVFS_NodeType gNativeFS_DirNodeType = { + .FindDir = NativeFS_FindDir, + .ReadDir = NativeFS_ReadDir, + .Close = NativeFS_Close +}; tVFS_Driver gNativeFS_Driver = { - "nativefs", 0, - NativeFS_Mount, NativeFS_Unmount, - NULL, + .Name = "nativefs", + .InitDevice = NativeFS_Mount, + .Unmount = NativeFS_Unmount }; // === CODE === @@ -53,7 +67,10 @@ tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments) DIR *dp; dp = opendir(Device); - if(!dp) return NULL; + if(!dp) { + Log_Warning("NativeFS", "ERROR: Unable to open device root '%s'", Device); + return NULL; + } // Check if directory exists // Parse flags from arguments @@ -66,11 +83,11 @@ tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments) ret->Data = strdup(Device); ret->ImplInt = strlen(ret->Data); ret->ImplPtr = info; - ret->Inode = (Uint64)dp; - - ret->FindDir = NativeFS_FindDir; - ret->ReadDir = NativeFS_ReadDir; - + ret->Inode = (Uint64)(tVAddr)dp; + ret->Flags = VFS_FFLAG_DIRECTORY; + + ret->Type = &gNativeFS_DirNodeType; + return ret; } @@ -78,7 +95,7 @@ void NativeFS_Unmount(tVFS_Node *Node) { tNativeFS *info = Node->ImplPtr; Inode_ClearCache( info->InodeHandle ); - closedir( (void *)Node->Inode ); + closedir( (void *)(tVAddr)Node->Inode ); free(Node->Data); free(Node); free(info); @@ -87,12 +104,18 @@ void NativeFS_Unmount(tVFS_Node *Node) void NativeFS_Close(tVFS_Node *Node) { tNativeFS *info = Node->ImplPtr; - Inode_UncacheNode( info->InodeHandle, Node->Inode ); + DIR *dp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? (DIR*)(tVAddr)Node->Inode : 0; + FILE *fp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? 0 : (FILE*)(tVAddr)Node->Inode; + + if( Inode_UncacheNode( info->InodeHandle, Node->Inode ) == 1 ) { + if(dp) closedir(dp); + if(fp) fclose(fp); + } } tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name) { - char *path = malloc(Node->ImplInt + 1 + strlen(Name) + 1); + char *path; tNativeFS *info = Node->ImplPtr; tVFS_Node baseRet; struct stat statbuf; @@ -100,6 +123,7 @@ tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name) ENTER("pNode sName", Node, Name); // Create path + path = malloc(Node->ImplInt + 1 + strlen(Name) + 1); strcpy(path, Node->Data); path[Node->ImplInt] = '/'; strcpy(path + Node->ImplInt + 1, Name); @@ -120,16 +144,19 @@ tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name) if( S_ISDIR(statbuf.st_mode) ) { LOG("Directory"); - baseRet.Inode = (Uint64) opendir(path); - baseRet.FindDir = NativeFS_FindDir; - baseRet.ReadDir = NativeFS_ReadDir; + baseRet.Inode = (Uint64)(tVAddr) opendir(path); + baseRet.Type = &gNativeFS_DirNodeType; baseRet.Flags |= VFS_FFLAG_DIRECTORY; + baseRet.Size = -1; } else { LOG("File"); - baseRet.Inode = (Uint64) fopen(path, "r+"); - baseRet.Read = NativeFS_Read; + baseRet.Inode = (Uint64)(tVAddr) fopen(path, "r+"); + baseRet.Type = &gNativeFS_FileNodeType; + + fseek( (FILE*)(tVAddr)baseRet.Inode, 0, SEEK_END ); + baseRet.Size = ftell( (FILE*)(tVAddr)baseRet.Inode ); } // Create new node @@ -143,18 +170,56 @@ tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name) char *NativeFS_ReadDir(tVFS_Node *Node, int Position) { - // Keep track of the current directory position - return NULL; + struct dirent *ent; + DIR *dp = (void*)(tVAddr)Node->Inode; + char *ret; + + ENTER("pNode iPosition", Node, Position); + + // TODO: Keep track of current position in the directory + // TODO: Lock node during this + rewinddir(dp); + do { + ent = readdir(dp); + } while(Position-- && ent); + + if( !ent ) { + LEAVE('n'); + return NULL; + } + + ret = strdup(ent->d_name); + + // TODO: Unlock node + + LEAVE('s', ret); + return ret; } -Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) +size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer) { - ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer); - if( fseek( (void *)Node->Inode, Offset, SEEK_SET ) != 0 ) + ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer); + if( fseek( (FILE *)(tVAddr)Node->Inode, Offset, SEEK_SET ) != 0 ) { LEAVE('i', 0); return 0; } LEAVE('-'); - return fread( Buffer, 1, Length, (void *)Node->Inode ); + return fread( Buffer, 1, Length, (FILE *)(tVAddr)Node->Inode ); +} + +size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer) +{ + FILE *fp = (FILE *)(tVAddr)Node->Inode; + ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer); + if( fseek( fp, Offset, SEEK_SET ) != 0 ) + { + LEAVE('i', 0); + return 0; + } + size_t ret = fwrite( Buffer, 1, Length, fp ); + fflush( fp ); + LEAVE('i', ret); + return ret; + }