2021-10-09 00:58:55 +02:00
|
|
|
#include "FSUtils.h"
|
|
|
|
#include "CFile.hpp"
|
2022-07-28 13:44:06 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2022-07-26 08:16:27 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2021-10-09 00:58:55 +02:00
|
|
|
|
|
|
|
int32_t FSUtils::CheckFile(const char *filepath) {
|
|
|
|
if (!filepath)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct stat filestat;
|
|
|
|
|
|
|
|
char dirnoslash[strlen(filepath) + 2];
|
|
|
|
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);
|
|
|
|
|
|
|
|
while (dirnoslash[strlen(dirnoslash) - 1] == '/')
|
|
|
|
dirnoslash[strlen(dirnoslash) - 1] = '\0';
|
|
|
|
|
|
|
|
char *notRoot = strrchr(dirnoslash, '/');
|
|
|
|
if (!notRoot) {
|
|
|
|
strcat(dirnoslash, "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(dirnoslash, &filestat) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t FSUtils::CreateSubfolder(const char *fullpath) {
|
|
|
|
if (!fullpath)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int32_t result = 0;
|
|
|
|
|
|
|
|
char dirnoslash[strlen(fullpath) + 1];
|
|
|
|
strcpy(dirnoslash, fullpath);
|
|
|
|
|
|
|
|
int32_t pos = strlen(dirnoslash) - 1;
|
|
|
|
while (dirnoslash[pos] == '/') {
|
|
|
|
dirnoslash[pos] = '\0';
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CheckFile(dirnoslash)) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
char parentpath[strlen(dirnoslash) + 2];
|
|
|
|
strcpy(parentpath, dirnoslash);
|
|
|
|
char *ptr = strrchr(parentpath, '/');
|
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
//!Device root directory (must be with '/')
|
|
|
|
strcat(parentpath, "/");
|
|
|
|
struct stat filestat;
|
|
|
|
if (stat(parentpath, &filestat) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
ptr[0] = '\0';
|
|
|
|
|
|
|
|
result = CreateSubfolder(parentpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (mkdir(dirnoslash, 0777) == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t FSUtils::saveBufferToFile(const char *path, void *buffer, uint32_t size) {
|
|
|
|
CFile file(path, CFile::WriteOnly);
|
|
|
|
if (!file.isOpen()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int32_t written = file.write((const uint8_t *) buffer, size);
|
|
|
|
file.close();
|
|
|
|
return written;
|
|
|
|
}
|