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 int NativeFS_ReadDir(tVFS_Node *Node, int Position, char Dest[FILENAME_MAX]);
\r
35 size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer);
\r
36 size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer);
\r
37 void NativeFS_Close(tVFS_Node *Node);
\r
40 tVFS_NodeType gNativeFS_FileNodeType = {
\r
41 .Read = NativeFS_Read,
\r
42 .Write = NativeFS_Write,
\r
43 .Close = NativeFS_Close
\r
45 tVFS_NodeType gNativeFS_DirNodeType = {
\r
46 .FindDir = NativeFS_FindDir,
\r
47 .ReadDir = NativeFS_ReadDir,
\r
48 .Close = NativeFS_Close
\r
50 tVFS_Driver gNativeFS_Driver = {
\r
52 .InitDevice = NativeFS_Mount,
\r
53 .Unmount = NativeFS_Unmount
\r
57 int NativeFS_Install(char **Arguments)
\r
59 VFS_AddDriver(&gNativeFS_Driver);
\r
63 tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments)
\r
69 dp = opendir(Device);
\r
71 Log_Warning("NativeFS", "ERROR: Unable to open device root '%s'", Device);
\r
75 // Check if directory exists
\r
76 // Parse flags from arguments
\r
77 info = malloc(sizeof(tNativeFS));
\r
78 info->InodeHandle = Inode_GetHandle();
\r
79 info->bReadOnly = 0;
\r
81 ret = malloc(sizeof(tVFS_Node));
\r
82 memset(ret, 0, sizeof(tVFS_Node));
\r
83 ret->Data = strdup(Device);
\r
84 ret->ImplInt = strlen(ret->Data);
\r
85 ret->ImplPtr = info;
\r
86 ret->Inode = (Uint64)(tVAddr)dp;
\r
87 ret->Flags = VFS_FFLAG_DIRECTORY;
\r
89 ret->Type = &gNativeFS_DirNodeType;
\r
94 void NativeFS_Unmount(tVFS_Node *Node)
\r
96 tNativeFS *info = Node->ImplPtr;
\r
97 Inode_ClearCache( info->InodeHandle );
\r
98 closedir( (void *)(tVAddr)Node->Inode );
\r
104 void NativeFS_Close(tVFS_Node *Node)
\r
106 tNativeFS *info = Node->ImplPtr;
\r
107 DIR *dp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? (DIR*)(tVAddr)Node->Inode : 0;
\r
108 FILE *fp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? 0 : (FILE*)(tVAddr)Node->Inode;
\r
110 if( Inode_UncacheNode( info->InodeHandle, Node->Inode ) == 1 ) {
\r
111 if(dp) closedir(dp);
\r
116 tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)
\r
119 tNativeFS *info = Node->ImplPtr;
\r
121 struct stat statbuf;
\r
123 ENTER("pNode sName", Node, Name);
\r
126 path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);
\r
127 strcpy(path, Node->Data);
\r
128 path[Node->ImplInt] = '/';
\r
129 strcpy(path + Node->ImplInt + 1, Name);
\r
131 LOG("path = '%s'", path);
\r
133 // Check if file exists
\r
134 if( stat(path, &statbuf) ) {
\r
136 LOG("Doesn't exist");
\r
141 memset(&baseRet, 0, sizeof(tVFS_Node));
\r
144 if( S_ISDIR(statbuf.st_mode) )
\r
147 baseRet.Inode = (Uint64)(tVAddr) opendir(path);
\r
148 baseRet.Type = &gNativeFS_DirNodeType;
\r
149 baseRet.Flags |= VFS_FFLAG_DIRECTORY;
\r
155 baseRet.Inode = (Uint64)(tVAddr) fopen(path, "r+");
\r
156 baseRet.Type = &gNativeFS_FileNodeType;
\r
158 fseek( (FILE*)(tVAddr)baseRet.Inode, 0, SEEK_END );
\r
159 baseRet.Size = ftell( (FILE*)(tVAddr)baseRet.Inode );
\r
163 baseRet.ImplPtr = info;
\r
164 baseRet.ImplInt = strlen(path);
\r
165 baseRet.Data = path;
\r
168 return Inode_CacheNode(info->InodeHandle, &baseRet);
\r
171 int NativeFS_ReadDir(tVFS_Node *Node, int Position, char Dest[FILENAME_MAX])
\r
173 struct dirent *ent;
\r
174 DIR *dp = (void*)(tVAddr)Node->Inode;
\r
176 ENTER("pNode iPosition", Node, Position);
\r
178 // TODO: Keep track of current position in the directory
\r
179 // TODO: Lock node during this
\r
183 } while(Position-- && ent);
\r
186 LEAVE('i', -ENOENT);
\r
190 strncpy(Dest, ent->d_name, FILENAME_MAX);
\r
192 // TODO: Unlock node
\r
198 size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer)
\r
200 ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
\r
201 if( fseek( (FILE *)(tVAddr)Node->Inode, Offset, SEEK_SET ) != 0 )
\r
207 return fread( Buffer, 1, Length, (FILE *)(tVAddr)Node->Inode );
\r
210 size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer)
\r
212 FILE *fp = (FILE *)(tVAddr)Node->Inode;
\r
213 ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);
\r
214 if( fseek( fp, Offset, SEEK_SET ) != 0 )
\r
219 size_t ret = fwrite( Buffer, 1, Length, fp );
\r