fix(mcd): ensure parent directory creation is platform-independent

This commit is contained in:
moonpower
2025-11-23 13:51:04 +03:00
parent 30d4e57a3c
commit 69e68a5827
2 changed files with 81 additions and 77 deletions
Vendored
BIN
View File
Binary file not shown.
+81 -77
View File
@@ -1,76 +1,80 @@
#include "mcd.h"
#include "../log.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#ifdef _WIN32
#include <direct.h>
#endif
static void psx_mcd_ensure_parent(const char* path) {
if (!path)
return;
char tmp[PATH_MAX];
strncpy(tmp, path, sizeof(tmp) - 1);
tmp[sizeof(tmp) - 1] = '\0';
char* slash = strrchr(tmp, '/');
#ifdef _WIN32
char* bslash = strrchr(tmp, '\\');
if (!slash || (bslash && bslash > slash))
slash = bslash;
#endif
if (!slash || slash == tmp)
return;
*slash = '\0';
struct stat st;
if (stat(tmp, &st) == 0)
return;
mkdir(tmp, 0755);
}
#include "mcd.h"
#include "../log.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#ifdef _WIN32
#include <direct.h>
#endif
static void psx_mcd_ensure_parent(const char* path) {
if (!path)
return;
char tmp[PATH_MAX];
strncpy(tmp, path, sizeof(tmp) - 1);
tmp[sizeof(tmp) - 1] = '\0';
char* slash = strrchr(tmp, '/');
#ifdef _WIN32
char* bslash = strrchr(tmp, '\\');
if (!slash || (bslash && bslash > slash))
slash = bslash;
#endif
if (!slash || slash == tmp)
return;
*slash = '\0';
struct stat st;
if (stat(tmp, &st) == 0)
return;
#ifdef _WIN32
_mkdir(tmp);
#else
mkdir(tmp, 0755);
#endif
}
psx_mcd_t* psx_mcd_create(void) {
return (psx_mcd_t*)malloc(sizeof(psx_mcd_t));
}
int psx_mcd_init(psx_mcd_t* mcd, const char* path) {
memset(mcd, 0, sizeof(psx_mcd_t));
mcd->state = MCD_STATE_TX_HIZ;
mcd->flag = 0x08;
int psx_mcd_init(psx_mcd_t* mcd, const char* path) {
memset(mcd, 0, sizeof(psx_mcd_t));
mcd->state = MCD_STATE_TX_HIZ;
mcd->flag = 0x08;
mcd->path = path;
mcd->buf = malloc(MCD_MEMORY_SIZE);
mcd->tx_data_ready = 0;
memset(mcd->buf, 0, MCD_MEMORY_SIZE);
if (!path)
return 0;
psx_mcd_ensure_parent(path);
FILE* file = fopen(path, "rb");
if (!file) {
// Create a blank card if missing
file = fopen(path, "wb");
if (!file)
return 1;
fwrite(mcd->buf, 1, MCD_MEMORY_SIZE, file);
fclose(file);
// Re-open for read so subsequent logic flows
file = fopen(path, "rb");
if (!file)
return 1;
}
memset(mcd->buf, 0, MCD_MEMORY_SIZE);
if (!path)
return 0;
psx_mcd_ensure_parent(path);
FILE* file = fopen(path, "rb");
if (!file) {
// Create a blank card if missing
file = fopen(path, "wb");
if (!file)
return 1;
fwrite(mcd->buf, 1, MCD_MEMORY_SIZE, file);
fclose(file);
// Re-open for read so subsequent logic flows
file = fopen(path, "rb");
if (!file)
return 1;
}
if (!fread(mcd->buf, 1, MCD_MEMORY_SIZE, file))
return 2;
@@ -200,7 +204,7 @@ uint8_t psx_mcd_read(psx_mcd_t* mcd) {
}
void psx_mcd_write(psx_mcd_t* mcd, uint8_t data) {
switch (mcd->state) {
switch (mcd->state) {
case MCD_STATE_TX_FLG: mcd->mode = data; break;
case MCD_R_STATE_RX_MSB: mcd->msb = data; break;
case MCD_R_STATE_RX_LSB: {
@@ -225,14 +229,14 @@ void psx_mcd_reset(psx_mcd_t* mcd) {
mcd->state = MCD_STATE_TX_HIZ;
}
void psx_mcd_destroy(psx_mcd_t* mcd) {
FILE* file = mcd->path ? fopen(mcd->path, "wb") : NULL;
if (file) {
fwrite(mcd->buf, 1, MCD_MEMORY_SIZE, file);
fclose(file);
}
free(mcd->buf);
free(mcd);
}
void psx_mcd_destroy(psx_mcd_t* mcd) {
FILE* file = mcd->path ? fopen(mcd->path, "wb") : NULL;
if (file) {
fwrite(mcd->buf, 1, MCD_MEMORY_SIZE, file);
fclose(file);
}
free(mcd->buf);
free(mcd);
}