2020-11-11 09:18:35 +01:00
|
|
|
#include "providers/file_provider.hpp"
|
|
|
|
|
2020-11-17 14:09:48 +01:00
|
|
|
#undef __STRICT_ANSI__
|
2020-11-11 09:18:35 +01:00
|
|
|
#include <cstdio>
|
|
|
|
|
2020-11-15 16:06:10 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "utils.hpp"
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
namespace hex::prv {
|
|
|
|
|
2020-11-15 16:06:10 +01:00
|
|
|
FileProvider::FileProvider(std::string_view path) : Provider(), m_path(path) {
|
|
|
|
this->m_fileStatsValid = stat(path.data(), &this->m_fileStats) == 0;
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
this->m_file = fopen(path.data(), "r+b");
|
|
|
|
|
|
|
|
this->m_readable = true;
|
|
|
|
this->m_writable = true;
|
|
|
|
|
|
|
|
if (this->m_file == nullptr) {
|
|
|
|
this->m_file = fopen(path.data(), "rb");
|
|
|
|
this->m_writable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileProvider::~FileProvider() {
|
|
|
|
if (this->m_file != nullptr)
|
|
|
|
fclose(this->m_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileProvider::isAvailable() {
|
|
|
|
return this->m_file != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileProvider::isReadable() {
|
2020-11-11 10:47:02 +01:00
|
|
|
return isAvailable() && this->m_readable;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileProvider::isWritable() {
|
2020-11-11 10:47:02 +01:00
|
|
|
return isAvailable() && this->m_writable;
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileProvider::read(u64 offset, void *buffer, size_t size) {
|
|
|
|
if ((offset + size) > this->getSize() || buffer == nullptr || size == 0)
|
|
|
|
return;
|
|
|
|
|
2020-11-22 19:43:35 +01:00
|
|
|
fseeko64(this->m_file, this->getCurrentPage() * PageSize + offset, SEEK_SET);
|
2020-11-11 09:18:35 +01:00
|
|
|
fread(buffer, 1, size, this->m_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileProvider::write(u64 offset, void *buffer, size_t size) {
|
|
|
|
if (buffer == nullptr || size == 0)
|
|
|
|
return;
|
|
|
|
|
2020-11-22 19:43:35 +01:00
|
|
|
fseeko64(this->m_file, this->getCurrentPage() * PageSize + offset, SEEK_SET);
|
2020-11-11 09:18:35 +01:00
|
|
|
fwrite(buffer, 1, size, this->m_file);
|
|
|
|
}
|
|
|
|
|
2020-11-22 19:43:35 +01:00
|
|
|
size_t FileProvider::getActualSize() {
|
2020-11-17 14:09:48 +01:00
|
|
|
fseeko64(this->m_file, 0, SEEK_END);
|
|
|
|
return ftello64(this->m_file);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 16:06:10 +01:00
|
|
|
std::vector<std::pair<std::string, std::string>> FileProvider::getDataInformation() {
|
|
|
|
std::vector<std::pair<std::string, std::string>> result;
|
|
|
|
|
|
|
|
result.emplace_back("File path", this->m_path);
|
2020-11-22 19:43:35 +01:00
|
|
|
result.emplace_back("Size", hex::toByteString(this->getActualSize()));
|
2020-11-15 16:06:10 +01:00
|
|
|
|
|
|
|
if (this->m_fileStatsValid) {
|
|
|
|
result.emplace_back("Creation time", ctime(&this->m_fileStats.st_ctime));
|
|
|
|
result.emplace_back("Last access time", ctime(&this->m_fileStats.st_atime));
|
|
|
|
result.emplace_back("Last modification time", ctime(&this->m_fileStats.st_mtime));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|