intial commit
This commit is contained in:
128
source/mijin/virtual_filesystem/filesystem.cpp
Normal file
128
source/mijin/virtual_filesystem/filesystem.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
|
||||
#include "./filesystem.hpp"
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
|
||||
//
|
||||
// internal defines
|
||||
//
|
||||
|
||||
//
|
||||
// internal constants
|
||||
//
|
||||
|
||||
//
|
||||
// internal types
|
||||
//
|
||||
|
||||
//
|
||||
// internal variables
|
||||
//
|
||||
|
||||
//
|
||||
// internal functions
|
||||
//
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
|
||||
std::vector<fs::path> OSFileSystemAdapter::getRoots()
|
||||
{
|
||||
return {
|
||||
"/" // TODO: other OSs
|
||||
};
|
||||
}
|
||||
|
||||
fs::path OSFileSystemAdapter::getHomeFolder()
|
||||
{
|
||||
return "/home/mewin"; // very TODO
|
||||
}
|
||||
|
||||
std::vector<FileInfo> OSFileSystemAdapter::listFiles(const fs::path& folder)
|
||||
{
|
||||
std::vector<FileInfo> entries;
|
||||
std::error_code err;
|
||||
fs::directory_iterator iterator(folder, fs::directory_options::skip_permission_denied, err);
|
||||
if (err) {
|
||||
return {}; // TODO: propagate?
|
||||
}
|
||||
for (const fs::directory_entry& entry : iterator)
|
||||
{
|
||||
FileInfo& info = entries.emplace_back();
|
||||
info.path = entry.path();
|
||||
info.exists = true;
|
||||
info.isFolder = entry.is_directory(err);
|
||||
info.isSymlink = entry.is_symlink(err);
|
||||
info.isSpecial = !info.isFolder && !entry.is_regular_file(err);
|
||||
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
|
||||
if (info.isFolder) {
|
||||
try {
|
||||
info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator());
|
||||
}
|
||||
catch(std::runtime_error&) {
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
else if (!info.isSpecial)
|
||||
{
|
||||
info.size = entry.file_size(err);
|
||||
if (err) {
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
FileInfo OSFileSystemAdapter::getFileInfo(const fs::path& file)
|
||||
{
|
||||
FileInfo info = {};
|
||||
std::error_code err;
|
||||
info.path = file;
|
||||
info.exists = fs::exists(file, err);
|
||||
if (info.exists)
|
||||
{
|
||||
info.isFolder = fs::is_directory(file, err);
|
||||
info.isSymlink = fs::is_symlink(file, err);
|
||||
info.isSpecial = !info.isFolder && !fs::is_regular_file(file, err);
|
||||
info.isHidden = info.path.filename().string().starts_with('.'); // at least for Linux
|
||||
if (info.isFolder) {
|
||||
try {
|
||||
info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator());
|
||||
}
|
||||
catch(std::runtime_error&) {
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
else if (!info.isSpecial)
|
||||
{
|
||||
info.size = fs::file_size(file, err);
|
||||
if (err) {
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
StreamError OSFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||
{
|
||||
const std::string pathStr = path.string();
|
||||
auto stream = std::make_unique<FileStream>();
|
||||
const StreamError error = stream->open(pathStr.c_str(), mode);
|
||||
if (error != StreamError::SUCCESS) {
|
||||
return error;
|
||||
}
|
||||
outStream = std::move(stream);
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
OSFileSystemAdapter& OSFileSystemAdapter::getInstance() // static
|
||||
{
|
||||
static OSFileSystemAdapter instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
} // namespace mijin
|
||||
Reference in New Issue
Block a user