Skip to content

Commit

Permalink
Overhaul memory card selection and folder creation code
Browse files Browse the repository at this point in the history
  • Loading branch information
uyjulian committed Aug 1, 2020
1 parent 2e9e27c commit 473ee4b
Showing 1 changed file with 91 additions and 56 deletions.
147 changes: 91 additions & 56 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,72 @@ int getFileSize(int fd)
return size;
}

#define MAX_ENTRY 128

static int checkMC()
{
int mc0_is_ps2card, mc1_is_ps2card;
int mc0_has_folder, mc1_has_folder;
int memcardtype, dummy;
int i, j, ret;
static sceMcTblGetDir mc_direntry[MAX_ENTRY] __attribute__((aligned(64)));

if (mcID == -1) {
mcGetInfo(0, 0, &memcardtype, &dummy, &dummy);
mcSync(0, NULL, &ret);
mc0_is_ps2card = (memcardtype == 2);
mc0_has_folder = 0;

mcGetInfo(1, 0, &memcardtype, &dummy, &dummy);
mcSync(0, NULL, &ret);
mc1_is_ps2card = (memcardtype == 2);
mc1_has_folder = 0;

if (mc0_is_ps2card) {
mcGetDir(0, 0, ".", 0, MAX_ENTRY - 2, mc_direntry);
mcSync(0, NULL, &ret);
for (i = j = 0; i < ret; i++, j++) {
if (mc_direntry[i].AttrFile & sceMcFileAttrSubdir && !strcmp((char *)mc_direntry[i].EntryName, "OPL")) {
mc0_has_folder = 1;
break;
}
}
}

if (mc1_is_ps2card) {
mcGetDir(1, 0, ".", 0, MAX_ENTRY - 2, mc_direntry);
mcSync(0, NULL, &ret);
for (i = j = 0; i < ret; i++, j++) {
if (mc_direntry[i].AttrFile & sceMcFileAttrSubdir && !strcmp((char *)mc_direntry[i].EntryName, "OPL")) {
mc1_has_folder = 1;
break;
}
}
}

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

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

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

if (mc1_is_ps2card) {
mcID = 0x31;
return mcID;
}
}
return mcID;
}

static void writeMCIcon(void)
{
int fd;
Expand All @@ -57,62 +123,27 @@ void checkMCFolder(void)
char path[32];
int fd;

snprintf(path, sizeof(path), "mc%d:OPL", mcID);
DIR *dir = opendir(path);
if (dir == NULL)
mkdir(path, 0777);
else
closedir(dir);
checkMC();

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

snprintf(path, sizeof(path), "mc%d:OPL/opl.icn", mcID & 1);
fd = open(path, O_RDONLY, 0666);
if (fd < 0)
if (fd < 0) {
writeMCIcon();
} else {
close(fd);
}

close(fd);

snprintf(path, sizeof(path), "mc%d:OPL/icon.sys", mcID);
snprintf(path, sizeof(path), "mc%d:OPL/icon.sys", mcID & 1);
fd = open(path, O_RDONLY, 0666);
if (fd < 0)
if (fd < 0) {
writeMCIcon();

close(fd);
}

static int checkMC()
{
int fd;

if (mcID == -1) {
DIR *dir = opendir("mc0:OPL");
if (dir == NULL) {
dir = opendir("mc1:OPL");
if (dir == NULL) {
// No base dir found on any MC, check MC is inserted
fd = sysCheckMC();
if (fd >= 0) {
dir = opendir("mc0:");
if (dir != NULL) {
closedir(dir);
mcID = 0x30;
} else {
dir = opendir("mc1:");
if (dir != NULL) {
closedir(dir);
mcID = 0x31;
}
}
}
} else {
closedir(dir);
mcID = 0x31;
}
} else {
closedir(dir);
mcID = 0x30;
}
} else {
close(fd);
}
return mcID;
}

static int checkFile(char *path, int mode)
Expand All @@ -135,14 +166,18 @@ static int checkFile(char *path, int mode)
char dirPath[256];
char *pos = strrchr(path, '/');
if (pos) {
memcpy(dirPath, path, pos - path);
dirPath[pos - path] = '\0';
DIR *dir = opendir(dirPath);
if (dir == NULL) {
if (mkdir(dirPath, 0777) < 0)
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;
} else
closedir(dir);
}
}
}
}
}
Expand Down

0 comments on commit 473ee4b

Please sign in to comment.