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 size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t 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", "ERROR: 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)(tVAddr)dp;
\r
82 ret->Flags = VFS_FFLAG_DIRECTORY;
\r
84 ret->Type = &gNativeFS_DirNodeType;
\r
89 void NativeFS_Unmount(tVFS_Node *Node)
\r
91 tNativeFS *info = Node->ImplPtr;
\r
92 Inode_ClearCache( info->InodeHandle );
\r
93 closedir( (void *)(tVAddr)Node->Inode );
\r
99 void NativeFS_Close(tVFS_Node *Node)
\r
101 tNativeFS *info = Node->ImplPtr;
\r
102 Inode_UncacheNode( info->InodeHandle, Node->Inode );
\r
105 tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)
\r
107 char *path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);
\r
108 tNativeFS *info = Node->ImplPtr;
\r
110 struct stat statbuf;
\r
112 ENTER("pNode sName", Node, Name);
\r
115 strcpy(path, Node->Data);
\r
116 path[Node->ImplInt] = '/';
\r
117 strcpy(path + Node->ImplInt + 1, Name);
\r
119 LOG("path = '%s'", path);
\r
121 // Check if file exists
\r
122 if( stat(path, &statbuf) ) {
\r
124 LOG("Doesn't exist");
\r
129 memset(&baseRet, 0, sizeof(tVFS_Node));
\r
132 if( S_ISDIR(statbuf.st_mode) )
\r
135 baseRet.Inode = (Uint64)(tVAddr) opendir(path);
\r
136 baseRet.Type = &gNativeFS_DirNodeType;
\r
137 baseRet.Flags |= VFS_FFLAG_DIRECTORY;
\r
143 baseRet.Inode = (Uint64)(tVAddr) fopen(path, "r+");
\r
144 baseRet.Type = &gNativeFS_FileNodeType;
\r
146 fseek( (FILE*)(tVAddr)baseRet.Inode, 0, SEEK_END );
\r
147 baseRet.Size = ftell( (FILE*)(tVAddr)baseRet.Inode );
\r
151 baseRet.ImplPtr = info;
\r
152 baseRet.ImplInt = strlen(path);
\r
153 baseRet.Data = path;
\r
156 return Inode_CacheNode(info->InodeHandle, &baseRet);
\r
159 char *NativeFS_ReadDir(tVFS_Node *Node, int Position)
\r
161 struct dirent *ent;
\r
162 DIR *dp = (void*)(tVAddr)Node->Inode;
\r
165 ENTER("pNode iPosition", Node, Position);
\r
167 // TODO: Keep track of current position in the directory
\r
168 // TODO: Lock node during this
\r
172 } while(Position-- && ent);
\r
179 ret = strdup(ent->d_name);
\r
181 // TODO: Unlock node
\r
187 size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer)
\r
189 ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
\r
190 if( fseek( (void *)(tVAddr)Node->Inode, Offset, SEEK_SET ) != 0 )
\r
196 return fread( Buffer, 1, Length, (void *)(tVAddr)Node->Inode );
\r