2017-10-29 09:24:06 +01:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "FSUtils.h"
|
2018-03-04 16:10:02 +01:00
|
|
|
#include "CFile.hpp"
|
2018-03-11 13:49:06 +01:00
|
|
|
#include "utils/logger.h"
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
s32 FSUtils::LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size) {
|
2017-10-29 09:24:06 +01:00
|
|
|
//! always initialze input
|
2018-03-11 16:44:04 +01:00
|
|
|
*inbuffer = NULL;
|
2017-10-29 09:24:06 +01:00
|
|
|
if(size)
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
s32 iFd = open(filepath, O_RDONLY);
|
2018-03-11 16:44:04 +01:00
|
|
|
if (iFd < 0)
|
|
|
|
return -1;
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
u32 filesize = lseek(iFd, 0, SEEK_END);
|
2017-10-29 09:24:06 +01:00
|
|
|
lseek(iFd, 0, SEEK_SET);
|
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
u8 *buffer = (u8 *) malloc(filesize);
|
|
|
|
if (buffer == NULL) {
|
2017-10-29 09:24:06 +01:00
|
|
|
close(iFd);
|
2018-03-11 16:44:04 +01:00
|
|
|
return -2;
|
|
|
|
}
|
2017-10-29 09:24:06 +01:00
|
|
|
|
|
|
|
u32 blocksize = 0x4000;
|
|
|
|
u32 done = 0;
|
|
|
|
s32 readBytes = 0;
|
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
while(done < filesize) {
|
2017-10-29 09:24:06 +01:00
|
|
|
if(done + blocksize > filesize) {
|
|
|
|
blocksize = filesize - done;
|
|
|
|
}
|
|
|
|
readBytes = read(iFd, buffer + done, blocksize);
|
|
|
|
if(readBytes <= 0)
|
|
|
|
break;
|
|
|
|
done += readBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(iFd);
|
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
if (done != filesize) {
|
|
|
|
free(buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
return -3;
|
|
|
|
}
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
*inbuffer = buffer;
|
2017-10-29 09:24:06 +01:00
|
|
|
|
|
|
|
//! sign is optional input
|
2018-03-11 16:44:04 +01:00
|
|
|
if(size) {
|
2017-10-29 09:24:06 +01:00
|
|
|
*size = filesize;
|
|
|
|
}
|
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
return filesize;
|
2017-10-29 09:24:06 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
s32 FSUtils::CheckFile(const char * filepath) {
|
|
|
|
if(!filepath)
|
|
|
|
return 0;
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
struct stat filestat;
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
char dirnoslash[strlen(filepath)+2];
|
|
|
|
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
while(dirnoslash[strlen(dirnoslash)-1] == '/')
|
|
|
|
dirnoslash[strlen(dirnoslash)-1] = '\0';
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
char * notRoot = strrchr(dirnoslash, '/');
|
|
|
|
if(!notRoot) {
|
|
|
|
strcat(dirnoslash, "/");
|
|
|
|
}
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
if (stat(dirnoslash, &filestat) == 0)
|
|
|
|
return 1;
|
2017-10-29 09:24:06 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
return 0;
|
2017-10-29 09:24:06 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
s32 FSUtils::CreateSubfolder(const char * fullpath) {
|
|
|
|
if(!fullpath)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
s32 result = 0;
|
|
|
|
|
|
|
|
char dirnoslash[strlen(fullpath)+1];
|
|
|
|
strcpy(dirnoslash, fullpath);
|
|
|
|
|
|
|
|
s32 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;
|
2017-10-29 09:24:06 +01:00
|
|
|
}
|
2018-03-04 16:10:02 +01:00
|
|
|
|
2018-03-11 16:44:04 +01:00
|
|
|
bool FSUtils::saveBufferToFile(const char * path, void * buffer, u32 size) {
|
2018-03-04 16:10:02 +01:00
|
|
|
s32 res = open(path, O_CREAT | O_TRUNC | O_WRONLY);
|
|
|
|
close(res);
|
|
|
|
CFile file(path, CFile::WriteOnly);
|
2018-03-11 16:44:04 +01:00
|
|
|
if (!file.isOpen()) {
|
2018-03-11 13:49:06 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Failed to open %s\n",path);
|
2018-03-04 16:10:02 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
file.write((const u8*) buffer,size);
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|