*/\r
#define DEBUG 0\r
#define off_t _acess_off_t\r
+#define sprintf _acess_sprintf\r
#include <acess.h> // Acess\r
#include <vfs.h> // Acess\r
#undef off_t\r
+#undef sprintf\r
#include <dirent.h> // Posix\r
#include <sys/stat.h> // Posix\r
-#include <stdio.h> // Posix\r
+#include <stdio.h> // C\r
\r
//NOTES:\r
// tVFS_Node->ImplPtr is a pointer to the filesystem flags (tNativeFS)\r
// === STRUCTURES ===\r
typedef struct\r
{\r
- int InodeHandle;\r
+ void *InodeHandle;\r
int bReadOnly;\r
} tNativeFS;\r
\r
int NativeFS_Install(char **Arguments);\r
tVFS_Node *NativeFS_Mount(const char *Device, const char **Arguments);\r
void NativeFS_Unmount(tVFS_Node *Node);\r
-tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name);\r
-char *NativeFS_ReadDir(tVFS_Node *Node, int Position);\r
-size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer);\r
-size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer);\r
+tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);\r
+ int NativeFS_ReadDir(tVFS_Node *Node, int Position, char Dest[FILENAME_MAX]);\r
+tVFS_Node *NativeFS_MkNod(tVFS_Node *Node, const char *Name, Uint Flags);\r
+size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer, Uint Flags);\r
+size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer, Uint Flags);\r
+void NativeFS_Close(tVFS_Node *Node);\r
\r
// === GLOBALS ===\r
tVFS_NodeType gNativeFS_FileNodeType = {\r
.Read = NativeFS_Read,\r
.Write = NativeFS_Write,\r
+ .Close = NativeFS_Close\r
};\r
tVFS_NodeType gNativeFS_DirNodeType = {\r
.FindDir = NativeFS_FindDir,\r
.ReadDir = NativeFS_ReadDir,\r
+ .MkNod = NativeFS_MkNod,\r
+ .Close = NativeFS_Close\r
};\r
tVFS_Driver gNativeFS_Driver = {\r
- "nativefs", 0,\r
- NativeFS_Mount, NativeFS_Unmount,\r
- NULL,\r
+ .Name = "nativefs",\r
+ .InitDevice = NativeFS_Mount,\r
+ .Unmount = NativeFS_Unmount\r
};\r
\r
// === CODE ===\r
// Check if directory exists\r
// Parse flags from arguments\r
info = malloc(sizeof(tNativeFS));\r
- info->InodeHandle = Inode_GetHandle();\r
+ info->InodeHandle = Inode_GetHandle(NULL);\r
info->bReadOnly = 0;\r
// Create node\r
ret = malloc(sizeof(tVFS_Node));\r
void NativeFS_Close(tVFS_Node *Node)\r
{\r
tNativeFS *info = Node->ImplPtr;\r
- Inode_UncacheNode( info->InodeHandle, Node->Inode );\r
+ DIR *dp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? (DIR*)(tVAddr)Node->Inode : 0;\r
+ FILE *fp = (Node->Flags & VFS_FFLAG_DIRECTORY) ? 0 : (FILE*)(tVAddr)Node->Inode;\r
+ \r
+ if( Inode_UncacheNode( info->InodeHandle, Node->Inode ) == 1 ) {\r
+ if(dp) closedir(dp);\r
+ if(fp) fclose(fp);\r
+ }\r
}\r
\r
-tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name)\r
+tVFS_Node *NativeFS_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)\r
{\r
- char *path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);\r
+ char *path;\r
tNativeFS *info = Node->ImplPtr;\r
tVFS_Node baseRet;\r
struct stat statbuf;\r
ENTER("pNode sName", Node, Name);\r
\r
// Create path\r
+ path = malloc(Node->ImplInt + 1 + strlen(Name) + 1);\r
strcpy(path, Node->Data);\r
path[Node->ImplInt] = '/';\r
strcpy(path + Node->ImplInt + 1, Name);\r
else\r
{\r
LOG("File");\r
- baseRet.Inode = (Uint64)(tVAddr) fopen(path, "r+");\r
+ FILE *fp = fopen(path, "r+");\r
+ if( !fp ) {\r
+ Log_Error("NativeFS", "fopen of '%s' failed: %s", path, strerror(errno));\r
+ free(path);\r
+ LEAVE('n');\r
+ return NULL;\r
+ }\r
+ baseRet.Inode = (Uint64)(tVAddr) fp;\r
baseRet.Type = &gNativeFS_FileNodeType;\r
\r
- fseek( (FILE*)(tVAddr)baseRet.Inode, 0, SEEK_END );\r
- baseRet.Size = ftell( (FILE*)(tVAddr)baseRet.Inode );\r
+ fseek( fp, 0, SEEK_END );\r
+ baseRet.Size = ftell( fp );\r
}\r
\r
// Create new node\r
return Inode_CacheNode(info->InodeHandle, &baseRet);\r
}\r
\r
-char *NativeFS_ReadDir(tVFS_Node *Node, int Position)\r
+int NativeFS_ReadDir(tVFS_Node *Node, int Position, char Dest[FILENAME_MAX])\r
{\r
struct dirent *ent;\r
DIR *dp = (void*)(tVAddr)Node->Inode;\r
- char *ret;\r
\r
ENTER("pNode iPosition", Node, Position);\r
\r
} while(Position-- && ent);\r
\r
if( !ent ) {\r
- LEAVE('n');\r
- return NULL;\r
+ LEAVE('i', -ENOENT);\r
+ return -ENOENT;\r
}\r
\r
- ret = strdup(ent->d_name);\r
+ strncpy(Dest, ent->d_name, FILENAME_MAX);\r
\r
// TODO: Unlock node \r
\r
- LEAVE('s', ret);\r
- return ret;\r
+ LEAVE('i', 0);\r
+ return 0;\r
+}\r
+\r
+tVFS_Node *NativeFS_MkNod(tVFS_Node *Node, const char *Name, Uint Flags)\r
+{\r
+ char path[Node->ImplInt+1+strlen(Name)+1];\r
+ sprintf(path, "%s/%s", Node->Data, Name);\r
+ if( Flags & VFS_FFLAG_DIRECTORY )\r
+ {\r
+ mkdir(path, 0755);\r
+ }\r
+ else\r
+ {\r
+ FILE *tmp = fopen(path, "w");\r
+ if(!tmp) return NULL;\r
+ fclose(tmp);\r
+ }\r
+ return NativeFS_FindDir(Node, Name, 0);\r
}\r
\r
-size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer)\r
+size_t NativeFS_Read(tVFS_Node *Node, _acess_off_t Offset, size_t Length, void *Buffer, Uint Flags)\r
{\r
ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);\r
if( fseek( (FILE *)(tVAddr)Node->Inode, Offset, SEEK_SET ) != 0 )\r
LEAVE('i', 0);\r
return 0;\r
}\r
- LEAVE('-');\r
- return fread( Buffer, 1, Length, (FILE *)(tVAddr)Node->Inode );\r
+ size_t ret = fread( Buffer, 1, Length, (FILE *)(tVAddr)Node->Inode );\r
+ LEAVE('x', ret);\r
+ return ret;\r
}\r
\r
-size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer)\r
+size_t NativeFS_Write(tVFS_Node *Node, _acess_off_t Offset, size_t Length, const void *Buffer, Uint Flags)\r
{\r
FILE *fp = (FILE *)(tVAddr)Node->Inode;\r
ENTER("pNode XOffset xLength pBuffer", Node, Offset, Length, Buffer);\r