2021-01-01 01:39:28 +01:00
|
|
|
#include "FileUtils.h"
|
2022-02-04 16:35:35 +01:00
|
|
|
#include "utils/logger.h"
|
|
|
|
#include <coreinit/cache.h>
|
2022-03-12 21:21:21 +01:00
|
|
|
#include <sys/stat.h>
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2021-09-24 17:14:38 +02:00
|
|
|
int32_t CheckFile(const char *filepath) {
|
2021-01-24 15:45:30 +01:00
|
|
|
if (!filepath) {
|
2021-01-01 01:39:28 +01:00
|
|
|
return 0;
|
2021-01-24 15:45:30 +01:00
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2022-02-04 16:35:35 +01:00
|
|
|
struct stat filestat {};
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2021-01-24 15:45:30 +01:00
|
|
|
char dirnoslash[strlen(filepath) + 2];
|
2021-01-01 01:39:28 +01:00
|
|
|
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);
|
|
|
|
|
2021-01-24 15:45:30 +01:00
|
|
|
while (dirnoslash[strlen(dirnoslash) - 1] == '/') {
|
|
|
|
dirnoslash[strlen(dirnoslash) - 1] = '\0';
|
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2021-01-24 15:45:30 +01:00
|
|
|
char *notRoot = strrchr(dirnoslash, '/');
|
|
|
|
if (!notRoot) {
|
2021-01-01 01:39:28 +01:00
|
|
|
strcat(dirnoslash, "/");
|
|
|
|
}
|
|
|
|
|
2021-01-24 15:45:30 +01:00
|
|
|
if (stat(dirnoslash, &filestat) == 0) {
|
2021-01-01 01:39:28 +01:00
|
|
|
return 1;
|
2021-01-24 15:45:30 +01:00
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-24 17:14:38 +02:00
|
|
|
int32_t CreateSubfolder(const char *fullpath) {
|
|
|
|
if (!fullpath)
|
2021-01-01 01:39:28 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
int32_t result = 0;
|
|
|
|
|
2021-09-24 17:14:38 +02:00
|
|
|
char dirnoslash[strlen(fullpath) + 1];
|
2021-01-01 01:39:28 +01:00
|
|
|
strcpy(dirnoslash, fullpath);
|
|
|
|
|
2021-09-24 17:14:38 +02:00
|
|
|
int32_t pos = strlen(dirnoslash) - 1;
|
|
|
|
while (dirnoslash[pos] == '/') {
|
2021-01-01 01:39:28 +01:00
|
|
|
dirnoslash[pos] = '\0';
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
|
2021-09-24 17:14:38 +02:00
|
|
|
if (CheckFile(dirnoslash)) {
|
2021-01-01 01:39:28 +01:00
|
|
|
return 1;
|
|
|
|
} else {
|
2021-09-24 17:14:38 +02:00
|
|
|
char parentpath[strlen(dirnoslash) + 2];
|
2021-01-01 01:39:28 +01:00
|
|
|
strcpy(parentpath, dirnoslash);
|
2021-09-24 17:14:38 +02:00
|
|
|
char *ptr = strrchr(parentpath, '/');
|
2021-01-01 01:39:28 +01:00
|
|
|
|
2021-09-24 17:14:38 +02:00
|
|
|
if (!ptr) {
|
2021-01-01 01:39:28 +01:00
|
|
|
//!Device root directory (must be with '/')
|
|
|
|
strcat(parentpath, "/");
|
2022-02-04 16:35:35 +01:00
|
|
|
struct stat filestat {};
|
2021-09-24 17:14:38 +02:00
|
|
|
if (stat(parentpath, &filestat) == 0) {
|
2021-01-01 01:39:28 +01:00
|
|
|
return 1;
|
2021-01-24 15:45:30 +01:00
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
ptr[0] = '\0';
|
|
|
|
|
|
|
|
result = CreateSubfolder(parentpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir(dirnoslash, 0777) == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|