WiiUPluginLoaderBackend/source/fs/FSUtils.cpp

62 lines
1.3 KiB
C++
Raw Permalink Normal View History

#include "fs/FSUtils.h"
#include "utils/logger.h"
#include "utils/utils.h"
#include <cstdio>
#include <cstring>
2022-02-04 16:25:44 +01:00
#include <fcntl.h>
#include <filesystem>
2022-02-04 16:25:44 +01:00
#include <unistd.h>
#include <vector>
int32_t FSUtils::LoadFileToMem(std::string_view filepath, std::vector<uint8_t> &buffer) {
//! always initialize input
buffer.clear();
int32_t iFd = open(filepath.data(), O_RDONLY);
if (iFd < 0) {
return -1;
}
struct stat file_stat {};
2022-08-08 11:55:13 +02:00
int rc = fstat(iFd, &file_stat);
if (rc < 0) {
close(iFd);
return -4;
}
uint32_t filesize = file_stat.st_size;
buffer.resize(filesize);
2022-08-08 11:55:13 +02:00
uint32_t blocksize = 0x80000;
2022-02-04 16:25:44 +01:00
uint32_t done = 0;
2022-08-08 11:55:13 +02:00
int32_t readBytes;
while (done < filesize) {
if (done + blocksize > filesize) {
blocksize = filesize - done;
}
readBytes = read(iFd, buffer.data() + done, blocksize);
2022-08-08 11:55:13 +02:00
if (readBytes <= 0) {
break;
2022-08-08 11:55:13 +02:00
}
done += readBytes;
}
::close(iFd);
if (done != filesize) {
buffer.clear();
return -3;
}
return 0;
}
bool FSUtils::CreateSubfolder(std::string_view fullpath) {
std::error_code err;
if (!std::filesystem::create_directories(fullpath, err)) {
return std::filesystem::exists(fullpath, err);
}
return true;
}