2 * Acess2 Native Kernel
\r
3 * - Acess kernel emulation on another OS using SDL and UDP
\r
6 * - Host filesystem access
\r
9 #define off_t _acess_off_t
\r
10 #include <acess.h> // Acess
\r
11 #include <vfs.h> // Acess
\r
13 #include <dirent.h> // Posix
\r
14 #include <sys/stat.h> // Posix
\r
15 #include <stdio.h> // Posix
\r
18 // tVFS_Node->ImplPtr is a pointer to the filesystem flags (tNativeFS)
\r
19 // tVFS_Node->Data is the path string (heap string)
\r
20 // tVFS_Node->ImplInt is the path length
\r
22 // === STRUCTURES ===
\r
29 // === PROTOTYPES ===
\r
30 int NativeFS_Install(char **Arguments);
\r
31 tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments);
\r
32 void NativeFS_Unmount(tVFS_Node *Node);
\r
33 tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name);
\r
34 char *NativeFS_ReadDir(tVFS_Node *Node, int Position);
\r
35 Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
38 tVFS_NodeType gNativeFS_FileNodeType = {
\r
39 .Read = NativeFS_Read
\r
41 tVFS_NodeType gNativeFS_DirNodeType = {
\r
42 .FindDir = NativeFS_FindDir,
\r
43 .ReadDir = NativeFS_ReadDir,
\r
45 tVFS_Driver gNativeFS_Driver = {
\r
47 NativeFS_Mount, NativeFS_Unmount,
\r
52 int NativeFS_Install(char **Arguments)
\r
54 VFS_AddDriver(&gNativeFS_Driver);
\r
58 tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments)
\r
64 dp = opendir(Device);
\r
66 Log_Warning("NativeFS", "ERRO: Unable to open device root '%s'", Device);
\r
70 // Check if directory exists
\r
71 // Parse flags from arguments
\r
72 info = malloc(sizeof(tNativeFS));
\r
73 info->InodeHandle = Inode_GetHandle();
\r
74 info->bReadOnly = 0;
\r
76 ret = malloc(sizeof(tVFS_Node));
\r
77 memset(ret, 0, sizeof(tVFS_Node));
\r
78 ret->Data = strdup(Device);
\r
79 ret->ImplInt = strlen(ret->Data);
\r
80 ret->ImplPtr = info;
\r
81 ret->Inode = (Uint64)dp;
\r
83 ret->Type = &gNativeFS_DirNodeType;
\r
88 void NativeFS_Unmount(tVFS_Node *Node)
\r
90 tNativeFS *info = Node->ImplPtr;
\r
91 Inode_ClearCache( info->InodeHandle );
\r
92 closedir( (void *)Node->Inode );
\r
98 void NativeFS_Close(tVFS_Node *Node)
\r
100 tNativeFS *info = Node->ImplPtr;
\r
101 Inode_UncacheNode( info->InodeHandle, Node->Inode );
\r
104 tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)
\r
106 char *path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);
\r
107 tNativeFS *info = Node->ImplPtr;
\r
109 struct stat statbuf;
\r
111 ENTER("pNode sName", Node, Name);
\r
114 strcpy(path, Node->Data);
\r
115 path[Node->ImplInt] = '/';
\r
116 strcpy(path + Node->ImplInt + 1, Name);
\r
118 LOG("path = '%s'", path);
\r
120 // Check if file exists
\r
121 if( stat(path, &statbuf) ) {
\r
123 LOG("Doesn't exist");
\r
128 memset(&baseRet, 0, sizeof(tVFS_Node));
\r
131 if( S_ISDIR(statbuf.st_mode) )
\r
134 baseRet.Inode = (Uint64) opendir(path);
\r
135 baseRet.Type = &gNativeFS_DirNodeType;
\r
136 baseRet.Flags |= VFS_FFLAG_DIRECTORY;
\r
142 baseRet.Inode = (Uint64) fopen(path, "r+");
\r
143 baseRet.Type = &gNativeFS_FileNodeType;
\r
145 fseek( (FILE*)(tVAddr)baseRet.Inode, 0, SEEK_END );
\r
146 baseRet.Size = ftell( (FILE*)(tVAddr)baseRet.Inode );
\r
150 baseRet.ImplPtr = info;
\r
151 baseRet.ImplInt = strlen(path);
\r
152 baseRet.Data = path;
\r
155 return Inode_CacheNode(info->InodeHandle, &baseRet);
\r
158 char *NativeFS_ReadDir(tVFS_Node *Node, int Position)
\r
160 struct dirent *ent;
\r
161 DIR *dp = (void*)(tVAddr)Node->Inode;
\r
164 ENTER("pNode iPosition", Node, Position);
\r
166 // TODO: Keep track of current position in the directory
\r
167 // TODO: Lock node during this
\r
171 } while(Position-- && ent);
\r
178 ret = strdup(ent->d_name);
\r
180 // TODO: Unlock node
\r
186 Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
188 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
189 if( fseek( (void *)Node->Inode, Offset, SEEK_SET ) != 0 )
\r
195 return fread( Buffer, 1, Length, (void *)Node->Inode );
\r