2 * Acess2 Native Kernel
\r
3 * - Acess kernel emulation on another OS using SDL and UDP
\r
6 * - Host filesystem access
\r
9 #include <acess.h> // Acess
\r
10 #include <vfs.h> // Acess
\r
11 #include <dirent.h> // Posix
\r
12 #include <sys/stat.h> // Posix
\r
13 #include <stdio.h> // Posix
\r
16 // tVFS_Node->ImplPtr is a pointer to the filesystem flags (tNativeFS)
\r
17 // tVFS_Node->Data is the path string (heap string)
\r
18 // tVFS_Node->ImplInt is the path length
\r
20 // === STRUCTURES ===
\r
27 // === PROTOTYPES ===
\r
28 int NativeFS_Install(char **Arguments);
\r
29 tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments);
\r
30 void NativeFS_Unmount(tVFS_Node *Node);
\r
31 tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name);
\r
32 char *NativeFS_ReadDir(tVFS_Node *Node, int Position);
\r
33 Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
\r
36 tVFS_NodeType gNativeFS_FileNodeType = {
\r
37 .Read = NativeFS_Read
\r
39 tVFS_NodeType gNativeFS_DirNodeType = {
\r
40 .FindDir = NativeFS_FindDir,
\r
41 .ReadDir = NativeFS_ReadDir,
\r
43 tVFS_Driver gNativeFS_Driver = {
\r
45 NativeFS_Mount, NativeFS_Unmount,
\r
50 int NativeFS_Install(char **Arguments)
\r
52 VFS_AddDriver(&gNativeFS_Driver);
\r
56 tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments)
\r
62 dp = opendir(Device);
\r
64 Log_Warning("NativeFS", "ERRO: Unable to open device root '%s'", Device);
\r
68 // Check if directory exists
\r
69 // Parse flags from arguments
\r
70 info = malloc(sizeof(tNativeFS));
\r
71 info->InodeHandle = Inode_GetHandle();
\r
72 info->bReadOnly = 0;
\r
74 ret = malloc(sizeof(tVFS_Node));
\r
75 memset(ret, 0, sizeof(tVFS_Node));
\r
76 ret->Data = strdup(Device);
\r
77 ret->ImplInt = strlen(ret->Data);
\r
78 ret->ImplPtr = info;
\r
79 ret->Inode = (Uint64)dp;
\r
81 ret->Type = &gNativeFS_DirNodeType;
\r
86 void NativeFS_Unmount(tVFS_Node *Node)
\r
88 tNativeFS *info = Node->ImplPtr;
\r
89 Inode_ClearCache( info->InodeHandle );
\r
90 closedir( (void *)Node->Inode );
\r
96 void NativeFS_Close(tVFS_Node *Node)
\r
98 tNativeFS *info = Node->ImplPtr;
\r
99 Inode_UncacheNode( info->InodeHandle, Node->Inode );
\r
102 tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)
\r
104 char *path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);
\r
105 tNativeFS *info = Node->ImplPtr;
\r
107 struct stat statbuf;
\r
109 ENTER("pNode sName", Node, Name);
\r
112 strcpy(path, Node->Data);
\r
113 path[Node->ImplInt] = '/';
\r
114 strcpy(path + Node->ImplInt + 1, Name);
\r
116 LOG("path = '%s'", path);
\r
118 // Check if file exists
\r
119 if( stat(path, &statbuf) ) {
\r
121 LOG("Doesn't exist");
\r
126 memset(&baseRet, 0, sizeof(tVFS_Node));
\r
129 if( S_ISDIR(statbuf.st_mode) )
\r
132 baseRet.Inode = (Uint64) opendir(path);
\r
133 baseRet.Type = &gNativeFS_DirNodeType;
\r
134 baseRet.Flags |= VFS_FFLAG_DIRECTORY;
\r
140 baseRet.Inode = (Uint64) fopen(path, "r+");
\r
141 baseRet.Type = &gNativeFS_FileNodeType;
\r
143 fseek( (FILE*)(tVAddr)baseRet.Inode, 0, SEEK_END );
\r
144 baseRet.Size = ftell( (FILE*)(tVAddr)baseRet.Inode );
\r
148 baseRet.ImplPtr = info;
\r
149 baseRet.ImplInt = strlen(path);
\r
150 baseRet.Data = path;
\r
153 return Inode_CacheNode(info->InodeHandle, &baseRet);
\r
156 char *NativeFS_ReadDir(tVFS_Node *Node, int Position)
\r
158 struct dirent *ent;
\r
159 DIR *dp = (void*)(tVAddr)Node->Inode;
\r
162 ENTER("pNode iPosition", Node, Position);
\r
164 // TODO: Keep track of current position in the directory
\r
165 // TODO: Lock node during this
\r
169 } while(Position-- && ent);
\r
176 ret = strdup(ent->d_name);
\r
178 // TODO: Unlock node
\r
184 Uint64 NativeFS_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
\r
186 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
\r
187 if( fseek( (void *)Node->Inode, Offset, SEEK_SET ) != 0 )
\r
193 return fread( Buffer, 1, Length, (void *)Node->Inode );
\r