Skip to content

Commit

Permalink
Use standard POSIX functions for memory card handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uyjulian committed Jul 10, 2021
1 parent 70ed88a commit 0a08a70
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 65 deletions.
18 changes: 8 additions & 10 deletions src/system.c
Expand Up @@ -907,19 +907,17 @@ int sysExecElf(const char *path)

int sysCheckMC(void)
{
int dummy, ret;

mcGetInfo(0, 0, &dummy, &dummy, &dummy);
mcSync(0, NULL, &ret);

if (-1 == ret || 0 == ret)
DIR *mc0_root_dir = opendir("mc0:");
if (mc0_root_dir != NULL) {
closedir(mc0_root_dir);
return 0;
}

mcGetInfo(1, 0, &dummy, &dummy, &dummy);
mcSync(0, NULL, &ret);

if (-1 == ret || 0 == ret)
DIR *mc1_root_dir = opendir("mc1:");
if (mc1_root_dir != NULL) {
closedir(mc1_root_dir);
return 1;
}

return -11;
}
Expand Down
87 changes: 32 additions & 55 deletions src/util.c
Expand Up @@ -41,64 +41,53 @@ static int checkMC()
{
int mc0_is_ps2card, mc1_is_ps2card;
int mc0_has_folder, mc1_has_folder;
int memcardtype, dummy;
int i, ret;
static sceMcTblGetDir mc_direntry[MAX_ENTRY] __attribute__((aligned(64)));

if (mcID == -1) {
mcSync(0, NULL, NULL);

mcGetInfo(0, 0, &memcardtype, &dummy, &dummy);
mcSync(0, NULL, &ret);
mc0_is_ps2card = (ret == -1 && memcardtype == 2);
mc0_has_folder = 0;
mc0_is_ps2card = 0;
DIR *mc0_root_dir = opendir("mc0:");
if (mc0_root_dir != NULL) {
closedir(mc0_root_dir);
mc0_is_ps2card = 1;
}

mcGetInfo(1, 0, &memcardtype, &dummy, &dummy);
mcSync(0, NULL, &ret);
mc1_is_ps2card = (ret == -1 && memcardtype == 2);
mc1_has_folder = 0;
mc1_is_ps2card = 0;
DIR *mc1_root_dir = opendir("mc1:");
if (mc1_root_dir != NULL) {
closedir(mc1_root_dir);
mc1_is_ps2card = 1;
}

if (mc0_is_ps2card) {
memset(mc_direntry, 0, sizeof(mc_direntry));
mcGetDir(0, 0, "*", 0, MAX_ENTRY - 2, mc_direntry);
mcSync(0, NULL, &ret);
for (i = 0; i < ret; i++) {
if (mc_direntry[i].AttrFile & sceMcFileAttrSubdir && !strcmp((char *)mc_direntry[i].EntryName, "OPL")) {
mc0_has_folder = 1;
break;
}
}
mc0_has_folder = 0;
DIR *mc0_opl_dir = opendir("mc0:OPL/");
if (mc0_opl_dir != NULL) {
closedir(mc0_opl_dir);
mc0_has_folder = 1;
}

if (mc1_is_ps2card) {
memset(mc_direntry, 0, sizeof(mc_direntry));
mcGetDir(1, 0, "*", 0, MAX_ENTRY - 2, mc_direntry);
mcSync(0, NULL, &ret);
for (i = 0; i < ret; i++) {
if (mc_direntry[i].AttrFile & sceMcFileAttrSubdir && !strcmp((char *)mc_direntry[i].EntryName, "OPL")) {
mc1_has_folder = 1;
break;
}
}
mc1_has_folder = 0;
DIR *mc1_opl_dir = opendir("mc1:OPL/");
if (mc1_opl_dir != NULL) {
closedir(mc1_opl_dir);
mc1_has_folder = 1;
}

if (mc0_has_folder) {
mcID = 0x30;
mcID = '0';
return mcID;
}

if (mc1_has_folder) {
mcID = 0x31;
mcID = '1';
return mcID;
}

if (mc0_is_ps2card) {
mcID = 0x30;
mcID = '0';
return mcID;
}

if (mc1_is_ps2card) {
mcID = 0x31;
mcID = '1';
return mcID;
}
}
Expand Down Expand Up @@ -131,9 +120,8 @@ void checkMCFolder(void)
return;
}

mcSync(0, NULL, NULL);
mcMkDir(mcID & 1, 0, "OPL");
mcSync(0, NULL, NULL);
snprintf(path, sizeof(path), "mc%d:OPL/", mcID & 1);
mkdir(path, 0777);

snprintf(path, sizeof(path), "mc%d:OPL/opl.icn", mcID & 1);
fd = open(path, O_RDONLY, 0666);
Expand Down Expand Up @@ -169,21 +157,10 @@ static int checkFile(char *path, int mode)

// in create mode, we check that the directory exist, or create it
if (mode & O_CREAT) {
char dirPath[256];
char *pos = strrchr(path, '/');
if (pos) {
memcpy(dirPath, path + 4, (pos - path) - 4);
dirPath[(pos - path) - 4] = '\0';
int ret = 0;
mcSync(0, NULL, NULL);
mcMkDir(path[2] - '0', 0, dirPath);
mcSync(0, NULL, &ret);
if (ret < 0) {
// If the error is that the folder already exists, just pass through
if (ret != -4) {
return 0;
}
}
int res = mkdir(path, 0777);
// Non-standard POSIX check: the error value is supposed to be assigned to errno, not the return value
if (res >= 0 || res == -EEXIST) {
return 0;
}
}
}
Expand Down

0 comments on commit 0a08a70

Please sign in to comment.