First implementation of custom path type.
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
|
||||
#include "../platform/folders.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace mijin
|
||||
{
|
||||
|
||||
@@ -26,100 +30,91 @@ namespace mijin
|
||||
// internal functions
|
||||
//
|
||||
|
||||
namespace
|
||||
{
|
||||
void doGetFileInfo(const fs::path& stlPath, FileInfo& outInfo)
|
||||
{
|
||||
std::error_code err;
|
||||
|
||||
outInfo.isFolder = fs::is_directory(stlPath, err);
|
||||
outInfo.isSymlink = fs::is_symlink(stlPath, err);
|
||||
outInfo.isSpecial = !outInfo.isFolder && !fs::is_regular_file(stlPath, err);
|
||||
outInfo.isHidden = stlPath.c_str()[0] == '.'; // at least for Linux
|
||||
if (outInfo.isFolder)
|
||||
{
|
||||
const fs::directory_iterator dirIt(stlPath, err);
|
||||
if (err != std::error_code{})
|
||||
{
|
||||
outInfo.size = std::distance(dirIt, fs::directory_iterator());
|
||||
}
|
||||
else
|
||||
{
|
||||
outInfo.size = 0;
|
||||
}
|
||||
}
|
||||
else if (!outInfo.isSpecial)
|
||||
{
|
||||
outInfo.size = fs::file_size(stlPath, err);
|
||||
if (err)
|
||||
{
|
||||
outInfo.size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// public functions
|
||||
//
|
||||
|
||||
fs::path OSFileSystemAdapter::getHomeFolder()
|
||||
std::vector<FolderEntry> OSFileSystemAdapter::listFiles(PathView folder)
|
||||
{
|
||||
return getKnownFolder(KnownFolder::USER_HOME);
|
||||
}
|
||||
|
||||
std::vector<FileInfo> OSFileSystemAdapter::listFiles(const fs::path& folder)
|
||||
{
|
||||
std::vector<FileInfo> entries;
|
||||
std::vector<FolderEntry> entries;
|
||||
std::error_code err;
|
||||
const fs::directory_iterator iterator(folder, fs::directory_options::skip_permission_denied, err);
|
||||
|
||||
const fs::path stlFolder(folder.stringView());
|
||||
const fs::directory_iterator iterator(stlFolder, fs::directory_options::skip_permission_denied, err);
|
||||
if (err) {
|
||||
return {}; // TODO: propagate?
|
||||
}
|
||||
for (const fs::directory_entry& entry : iterator)
|
||||
for (const fs::directory_entry& stlEntry : 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)
|
||||
{
|
||||
std::error_code errorCode;
|
||||
fs::directory_iterator dirIt(info.path, errorCode);
|
||||
if (errorCode != std::error_code{})
|
||||
{
|
||||
info.size = std::distance(dirIt, fs::directory_iterator());
|
||||
}
|
||||
else
|
||||
{
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
else if (!info.isSpecial)
|
||||
{
|
||||
info.size = entry.file_size(err);
|
||||
if (err)
|
||||
{
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
FolderEntry& entry = entries.emplace_back();
|
||||
entry.path = stlEntry.path().generic_string();
|
||||
entry.info.exists = true;
|
||||
doGetFileInfo(stlEntry.path(), entry.info);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
FileInfo OSFileSystemAdapter::getFileInfo(const fs::path& file)
|
||||
FileInfo OSFileSystemAdapter::getFileInfo(PathView file)
|
||||
{
|
||||
const fs::path stlFile(file.stringView());
|
||||
|
||||
FileInfo info = {};
|
||||
std::error_code err;
|
||||
info.path = file;
|
||||
info.exists = fs::exists(file, err);
|
||||
info.exists = fs::exists(stlFile, 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) {
|
||||
MIJIN_TRY
|
||||
{
|
||||
info.size = std::distance(fs::directory_iterator(info.path), fs::directory_iterator());
|
||||
}
|
||||
MIJIN_CATCH(std::runtime_error&)
|
||||
{
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
else if (!info.isSpecial)
|
||||
{
|
||||
info.size = fs::file_size(file, err);
|
||||
if (err) {
|
||||
info.size = 0;
|
||||
}
|
||||
}
|
||||
doGetFileInfo(fs::path(file.stringView()), info);
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
Optional<fs::path> OSFileSystemAdapter::getNativePath(const fs::path& file)
|
||||
Optional<NativePath> OSFileSystemAdapter::getNativePath(PathView file)
|
||||
{
|
||||
return file;
|
||||
return NativePath(file);
|
||||
}
|
||||
|
||||
StreamError OSFileSystemAdapter::open(const fs::path& path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||
StreamError OSFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
|
||||
{
|
||||
const std::string pathStr = path.string();
|
||||
const PathView::string_view_t pathSv = path.stringView();
|
||||
char* pathStr = static_cast<char*>(alloca(pathSv.size() + 1));
|
||||
std::memcpy(pathStr, pathSv.data(), pathSv.size());
|
||||
pathStr[pathSv.size()] = '\0';
|
||||
|
||||
auto stream = std::make_unique<FileStream>();
|
||||
const StreamError error = stream->open(pathStr.c_str(), mode);
|
||||
const StreamError error = stream->open(pathStr, mode);
|
||||
if (error != StreamError::SUCCESS) {
|
||||
return error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user