Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul memory card selection and folder creation code #308

Merged
merged 3 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,22 @@ static char cfgDevice[8];

char *configGetDir(void)
{
if (!strncmp(cfgDevice, "mc", 2)) {
cfgDevice[2] = getmcID();
}

char *path = cfgDevice;
return path;
}

void configPrepareNotifications(char *prefix)
{
int mcID;
char *colpos;

snprintf(cfgDevice, sizeof(cfgDevice), prefix);
if (!strncmp(cfgDevice, "mc?", 3)) {
mcID = getmcID();
cfgDevice[2] = mcID;
}

if ((colpos = strchr(cfgDevice, ':')) != NULL)
*(colpos + 1) = '\0';

showCfgPopup = 1;
}

void configInit(char *prefix)
Expand Down
9 changes: 7 additions & 2 deletions src/opl.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ static void _loadConfig()

lscret = result;
lscstatus = 0;
showCfgPopup = 1;
}

static int trySaveConfigUSB(int types)
Expand Down Expand Up @@ -895,6 +896,12 @@ static void _saveConfig()
configSetStr(configNet, CONFIG_NET_SMB_PASSW, gPCPassword);
}

char *path = configGetDir();
if (!strncmp(path, "mc", 2)) {
checkMCFolder();
configPrepareNotifications(gBaseMCDir);
}

lscret = configWriteMulti(lscstatus);
if (lscret == 0)
lscret = trySaveAlternateDevice(lscstatus);
Expand Down Expand Up @@ -956,8 +963,6 @@ int saveConfig(int types, int showUI)
if (showUI) {
if (lscret) {
char *path = configGetDir();
if (!strncmp(path, "mc", 2))
checkMCFolder();

snprintf(notification, sizeof(notification), _l(_STR_SETTINGS_SAVED), path);

Expand Down
153 changes: 97 additions & 56 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,76 @@ 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, 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 == 0 && memcardtype == 2);
mc0_has_folder = 0;

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

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;
}
}
}

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;
}
}
}

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 +127,29 @@ 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);
if (checkMC() < 0) {
return;
}

mcSync(0, NULL, NULL);
mcMkDir(mcID & 1, 0, "OPL");
mcSync(0, NULL, NULL);

snprintf(path, sizeof(path), "mc%d:OPL/opl.icn", mcID);
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 +172,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