Working on backing files.
Also started a sketches project to illustrate quick ideas.
This commit is contained in:
85
File.cc
85
File.cc
@@ -21,12 +21,13 @@
|
||||
/// SOFTWARE.
|
||||
///
|
||||
|
||||
#include <filesystem>
|
||||
#include "File.h"
|
||||
|
||||
|
||||
const std::string &File::Path() const
|
||||
const std::string File::Path()
|
||||
{
|
||||
return path;
|
||||
return this->path.string();
|
||||
}
|
||||
|
||||
|
||||
@@ -41,3 +42,83 @@ File::File(std::string fPath)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
OptOutFileStream File::Flush()
|
||||
{
|
||||
// Exit early if we shouldn't be writing to the file.
|
||||
if (this->IsReadOnly()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (!this->Exists()) {
|
||||
if (!this->IsWriteable()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return {new std::ofstream(this->path, this->mode|std::ios::trunc)};
|
||||
}
|
||||
|
||||
return {new std::ofstream(this->path, this->mode)};
|
||||
}
|
||||
|
||||
|
||||
OptInFileStream
|
||||
File::Refresh()
|
||||
{
|
||||
if (!this->Exists()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return {new std::ifstream(this->path, this->mode)};
|
||||
}
|
||||
|
||||
|
||||
size_t
|
||||
File::Size()
|
||||
{
|
||||
return std::filesystem::file_size(this->path);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
File::Exists()
|
||||
{
|
||||
return std::filesystem::exists(this->path);
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
checkAccess(std::filesystem::path path, std::filesystem::perms mode, bool checkParent)
|
||||
{
|
||||
if (!std::filesystem::exists(path) && checkParent) {
|
||||
auto fullPath = std::filesystem::absolute(path);
|
||||
auto parent = fullPath.parent_path();
|
||||
auto dirEnt = std::filesystem::directory_entry(parent);
|
||||
auto perms = dirEnt.status().permissions();
|
||||
return (perms & mode) != std::filesystem::perms::none;
|
||||
}
|
||||
|
||||
return (status(path).permissions() & mode) != std::filesystem::perms::none;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
File::IsWriteable()
|
||||
{
|
||||
return checkAccess(this->path, std::filesystem::perms::owner_write, true);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
File::IsReadable()
|
||||
{
|
||||
return checkAccess(this->path, std::filesystem::perms::owner_read, false);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
File::IsReadWriteable()
|
||||
{
|
||||
auto checkMode = std::filesystem::perms::owner_read | std::filesystem::perms::owner_write;
|
||||
return checkAccess(this->path, checkMode, true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user