From 2cc17f04c1839cca2a79066d7323405673c1ed98 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 26 Sep 2024 17:05:59 +1000 Subject: [PATCH] 360: Try to fix crash from I/O --- misc/xbox360/Makefile | 2 +- src/Platform_Xbox360.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/misc/xbox360/Makefile b/misc/xbox360/Makefile index f8d3ae418..f4f26c4eb 100644 --- a/misc/xbox360/Makefile +++ b/misc/xbox360/Makefile @@ -43,7 +43,7 @@ LDFLAGS = -g $(MACHDEP) -Wl,--gc-sections -Wl,-Map,$(notdir $@).map #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lxenon -lm +LIBS := -lxenon -lm -lfat #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing diff --git a/src/Platform_Xbox360.c b/src/Platform_Xbox360.c index 11605ea54..f669840a8 100644 --- a/src/Platform_Xbox360.c +++ b/src/Platform_Xbox360.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "_PlatformConsole.h" const cc_result ReturnCode_FileShareViolation = 1000000000; /* TODO: not used apparently */ @@ -80,6 +81,7 @@ cc_uint64 Stopwatch_Measure(void) { *#########################################################################################################################*/ static char root_buffer[NATIVE_STR_LEN]; static cc_string root_path = String_FromArray(root_buffer); +static bool fat_available; void Platform_EncodePath(cc_filepath* dst, const cc_string* path) { char* str = dst->buffer; @@ -89,15 +91,21 @@ void Platform_EncodePath(cc_filepath* dst, const cc_string* path) { } cc_result Directory_Create(const cc_filepath* path) { + if (!fat_available) return ENOSYS; + return mkdir(path->buffer, 0) == -1 ? errno : 0; } int File_Exists(const cc_filepath* path) { + if (!fat_available) return 0; + 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) { + if (!fat_available) return ENOSYS; + cc_string path; char pathBuffer[FILENAME_SIZE]; cc_filepath str; struct dirent* entry; @@ -141,12 +149,17 @@ static cc_result File_Do(cc_file* file, const char* path, int mode) { } cc_result File_Open(cc_file* file, const cc_filepath* path) { + if (!fat_available) return ReturnCode_FileNotFound; return File_Do(file, path->buffer, O_RDONLY); } + cc_result File_Create(cc_file* file, const cc_filepath* path) { + if (!fat_available) return ENOTSUP; return File_Do(file, path->buffer, O_RDWR | O_CREAT | O_TRUNC); } + cc_result File_OpenOrCreate(cc_file* file, const cc_filepath* path) { + if (!fat_available) return ENOTSUP; return File_Do(file, path->buffer, O_RDWR | O_CREAT); } @@ -264,10 +277,39 @@ cc_result Socket_CheckWritable(cc_socket s, cc_bool* writable) { /*########################################################################################################################* *--------------------------------------------------------Platform---------------------------------------------------------* *#########################################################################################################################*/ +extern int bdev_enum(int handle, const char** name); + +static void FindRootDirectory(void) { + const char* dev = NULL; + int handle = bdev_enum(-1, &dev); + if (handle < 0) { fat_available = false; return; } + + root_path.length = 0; + String_Format1(&root_path, "%c:/ClassiCube", dev); +} + +static void CreateRootDirectory(void) { + if (!fat_available) return; + root_buffer[root_path.length] = '\0'; + + int res = mkdir(root_buffer, 0); + int err = res == -1 ? errno : 0; + Platform_Log1("Created root directory: %i", &err); +} + void Platform_Init(void) { xenos_init(VIDEO_MODE_AUTO); console_init(); //network_init(); + + xenon_ata_init(); + xenon_atapi_init(); + + fat_available = fatInitDefault(); + Platform_ReadonlyFilesystem = !fat_available; + + FindRootDirectory(); + CreateRootDirectory(); } void Platform_Free(void) {