mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-08 14:56:12 -04:00
Merge pull request #1346 from Phil564/master
N64: Filesystem support for flashcarts
This commit is contained in:
commit
d7e1525ff4
@ -425,7 +425,6 @@ typedef cc_uint8 cc_bool;
|
|||||||
#define CC_BUILD_SPLITSCREEN
|
#define CC_BUILD_SPLITSCREEN
|
||||||
#undef CC_BUILD_RESOURCES
|
#undef CC_BUILD_RESOURCES
|
||||||
#undef CC_BUILD_NETWORKING
|
#undef CC_BUILD_NETWORKING
|
||||||
#undef CC_BUILD_FILESYSTEM
|
|
||||||
#elif defined PLAT_PS2
|
#elif defined PLAT_PS2
|
||||||
#define CC_BUILD_PS2
|
#define CC_BUILD_PS2
|
||||||
#define CC_BUILD_CONSOLE
|
#define CC_BUILD_CONSOLE
|
||||||
|
@ -19,8 +19,14 @@
|
|||||||
//#include <timer.h>
|
//#include <timer.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <sys/fcntl.h>
|
||||||
#include "_PlatformConsole.h"
|
#include "_PlatformConsole.h"
|
||||||
|
|
||||||
|
// These functions are private in libdragon. FatFs functions are instead recommanded.
|
||||||
|
int open( const char *file, int flags, ... );
|
||||||
|
int stat( const char *file, struct stat *st );
|
||||||
|
int mkdir( const char * path, mode_t mode );
|
||||||
|
|
||||||
const cc_result ReturnCode_FileShareViolation = 1000000000; // not used
|
const cc_result ReturnCode_FileShareViolation = 1000000000; // not used
|
||||||
const cc_result ReturnCode_FileNotFound = ENOENT;
|
const cc_result ReturnCode_FileNotFound = ENOENT;
|
||||||
const cc_result ReturnCode_DirectoryExists = EEXIST;
|
const cc_result ReturnCode_DirectoryExists = EEXIST;
|
||||||
@ -29,7 +35,7 @@ const cc_result ReturnCode_SocketWouldBlock = EWOULDBLOCK;
|
|||||||
const cc_result ReturnCode_SocketDropped = EPIPE;
|
const cc_result ReturnCode_SocketDropped = EPIPE;
|
||||||
|
|
||||||
const char* Platform_AppNameSuffix = " N64";
|
const char* Platform_AppNameSuffix = " N64";
|
||||||
cc_bool Platform_ReadonlyFilesystem = true;
|
cc_bool Platform_ReadonlyFilesystem = false;
|
||||||
|
|
||||||
|
|
||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
@ -81,14 +87,16 @@ void Process_Abort2(cc_result result, const char* raw_msg) {
|
|||||||
/*########################################################################################################################*
|
/*########################################################################################################################*
|
||||||
*-----------------------------------------------------Directory/File------------------------------------------------------*
|
*-----------------------------------------------------Directory/File------------------------------------------------------*
|
||||||
*#########################################################################################################################*/
|
*#########################################################################################################################*/
|
||||||
static const cc_string root_path = String_FromConst("/");
|
static cc_string root_path = String_FromConst("sd:/ClassiCube/");
|
||||||
|
static cc_string root_path_rom = String_FromConst("rom:/");
|
||||||
|
|
||||||
void Platform_EncodePath(cc_filepath* dst, const cc_string* path) {
|
void Platform_EncodePath(cc_filepath* dst, const cc_string* path) {
|
||||||
char* str = dst->buffer;
|
char* str = dst->buffer;
|
||||||
// TODO temp hack
|
|
||||||
cc_string path_ = *path;
|
cc_string path_ = *path;
|
||||||
int idx = String_IndexOf(path, '/');
|
if (Platform_ReadonlyFilesystem) { // read from rom root
|
||||||
if (idx >= 0) path_ = String_UNSAFE_SubstringAt(&path_, idx + 1);
|
int idx = String_IndexOf(path, '/');
|
||||||
|
if (idx >= 0) path_ = String_UNSAFE_SubstringAt(&path_, idx + 1);
|
||||||
|
}
|
||||||
|
|
||||||
Mem_Copy(str, root_path.buffer, root_path.length);
|
Mem_Copy(str, root_path.buffer, root_path.length);
|
||||||
str += root_path.length;
|
str += root_path.length;
|
||||||
@ -96,80 +104,90 @@ void Platform_EncodePath(cc_filepath* dst, const cc_string* path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cc_result Directory_Create(const cc_filepath* path) {
|
cc_result Directory_Create(const cc_filepath* path) {
|
||||||
return ERR_NOT_SUPPORTED;
|
return mkdir(path->buffer, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1 ? errno : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int File_Exists(const cc_filepath* path) {
|
int File_Exists(const cc_filepath* path) {
|
||||||
return false;
|
struct stat sb;
|
||||||
|
return stat(path->buffer, &sb) == 0 && S_ISREG(sb.st_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
|
cc_result Directory_Enum(const cc_string* dirPath, void* obj, Directory_EnumCallback callback) {
|
||||||
return ERR_NOT_SUPPORTED; // TODO add support
|
cc_string path; char pathBuffer[FILENAME_SIZE];
|
||||||
|
cc_filepath str;
|
||||||
|
dir_t entry;
|
||||||
|
|
||||||
|
Platform_EncodePath(&str, dirPath);
|
||||||
|
int res = dir_findfirst(str.buffer, &entry);
|
||||||
|
if (res != 0) return errno;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
String_InitArray(path, pathBuffer);
|
||||||
|
|
||||||
|
while (res == 0) {
|
||||||
|
path.length = 0;
|
||||||
|
String_Format1(&path, "%s/", dirPath);
|
||||||
|
|
||||||
|
/* ignore . and .. entry */
|
||||||
|
char* src = entry.d_name;
|
||||||
|
if (src[0] == '.' && src[1] == '\0') continue;
|
||||||
|
if (src[0] == '.' && src[1] == '.' && src[2] == '\0') continue;
|
||||||
|
|
||||||
|
int len = String_Length(src);
|
||||||
|
String_AppendUtf8(&path, src, len);
|
||||||
|
|
||||||
|
int is_dir = entry.d_type == DT_DIR;
|
||||||
|
callback(&path, obj, is_dir);
|
||||||
|
errno = 0;
|
||||||
|
res = dir_findnext(str.buffer, &entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return errno; // no clue if libdragon actually sets errno...
|
||||||
}
|
}
|
||||||
|
|
||||||
static cc_result File_Do(cc_file* file, const char* path) {
|
static cc_result File_Do(cc_file* file, const char* path, int mode) {
|
||||||
//*file = -1;
|
*file = open(path, mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||||
//return ReturnCode_FileNotFound;
|
return *file == -1 ? errno : 0;
|
||||||
// TODO: Why does trying this code break everything
|
|
||||||
|
|
||||||
int ret = dfs_open(path);
|
|
||||||
Platform_Log2("Opened %c = %i", path, &ret);
|
|
||||||
if (ret < 0) { *file = -1; return ret; }
|
|
||||||
|
|
||||||
*file = ret;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result File_Open(cc_file* file, const cc_filepath* path) {
|
cc_result File_Open(cc_file* file, const cc_filepath* path) {
|
||||||
return File_Do(file, path->buffer);
|
return File_Do(file, path->buffer, O_RDONLY);
|
||||||
}
|
}
|
||||||
cc_result File_Create(cc_file* file, const cc_filepath* path) {
|
cc_result File_Create(cc_file* file, const cc_filepath* path) {
|
||||||
*file = -1;
|
return File_Do(file, path->buffer, O_RDWR | O_CREAT | O_TRUNC);
|
||||||
return ERR_NOT_SUPPORTED;
|
|
||||||
//return File_Do(file, path->buffer);
|
|
||||||
}
|
}
|
||||||
cc_result File_OpenOrCreate(cc_file* file, const cc_filepath* path) {
|
cc_result File_OpenOrCreate(cc_file* file, const cc_filepath* path) {
|
||||||
*file = -1;
|
return File_Do(file, path->buffer, O_RDWR | O_CREAT);
|
||||||
return ERR_NOT_SUPPORTED;
|
|
||||||
//return File_Do(file, path->buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) {
|
cc_result File_Read(cc_file file, void* data, cc_uint32 count, cc_uint32* bytesRead) {
|
||||||
int ret = dfs_read(data, 1, count, file);
|
*bytesRead = read(file, data, count);
|
||||||
if (ret < 0) { *bytesRead = -1; return ret; }
|
return *bytesRead == -1 ? errno : 0;
|
||||||
|
|
||||||
*bytesRead = ret;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) {
|
cc_result File_Write(cc_file file, const void* data, cc_uint32 count, cc_uint32* bytesWrote) {
|
||||||
return ERR_NOT_SUPPORTED;
|
*bytesWrote = write(file, data, count);
|
||||||
|
return *bytesWrote == -1 ? errno : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result File_Close(cc_file file) {
|
cc_result File_Close(cc_file file) {
|
||||||
if (file < 0) return 0;
|
return close(file) == -1 ? errno : 0;
|
||||||
return dfs_close(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result File_Seek(cc_file file, int offset, int seekType) {
|
cc_result File_Seek(cc_file file, int offset, int seekType) {
|
||||||
static cc_uint8 modes[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
|
static cc_uint8 modes[3] = { SEEK_SET, SEEK_CUR, SEEK_END };
|
||||||
return dfs_seek(file, offset, modes[seekType]);
|
return lseek(file, offset, modes[seekType]) == -1 ? errno : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result File_Position(cc_file file, cc_uint32* pos) {
|
cc_result File_Position(cc_file file, cc_uint32* pos) {
|
||||||
int ret = dfs_tell(file);
|
*pos = lseek(file, 0, SEEK_CUR);
|
||||||
if (ret < 0) { *pos = -1; return ret; }
|
return *pos == -1 ? errno : 0;
|
||||||
|
|
||||||
*pos = ret;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cc_result File_Length(cc_file file, cc_uint32* len) {
|
cc_result File_Length(cc_file file, cc_uint32* len) {
|
||||||
int ret = dfs_size(file);
|
struct stat st;
|
||||||
if (ret < 0) { *len = -1; return ret; }
|
if (fstat(file, &st) == -1) { *len = -1; return errno; }
|
||||||
|
*len = st.st_size; return 0;
|
||||||
*len = ret;
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -277,6 +295,11 @@ void Platform_Init(void) {
|
|||||||
dfs_init(DFS_DEFAULT_LOCATION);
|
dfs_init(DFS_DEFAULT_LOCATION);
|
||||||
timer_init();
|
timer_init();
|
||||||
rtc_init();
|
rtc_init();
|
||||||
|
if (!debug_init_sdfs("sd:/", -1)) {
|
||||||
|
String_Copy(&root_path, &root_path_rom);
|
||||||
|
Platform_ReadonlyFilesystem = true;
|
||||||
|
Platform_LogConst("Couldn't access SD card.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void Platform_Free(void) { }
|
void Platform_Free(void) { }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user