Using PathView to pass paths to APIs

This commit is contained in:
catalinvasile
2024-07-10 13:31:12 +02:00
parent d1ebc32f9d
commit 28b74b4056
499 changed files with 99851 additions and 300 deletions
+40 -33
View File
@@ -25,14 +25,14 @@ tl::atomic<int> s_lastId(0);
//////////////////////////////////////////////////////////////////////////
tl::result<CustomFilesystem::PackId> CustomFilesystem::mountFront(AbsPath mountPoint, tl::unique_ref<IPack> pack)
tl::result<CustomFilesystem::PackId> CustomFilesystem::mountFront(AbsPathView mountPoint, tl::unique_ref<IPack> pack)
{
return mount(std::move(mountPoint), std::move(pack), MountPolicy::Front);
}
//////////////////////////////////////////////////////////////////////////
tl::result<CustomFilesystem::PackId> CustomFilesystem::mountBack(AbsPath mountPoint, tl::unique_ref<IPack> pack)
tl::result<CustomFilesystem::PackId> CustomFilesystem::mountBack(AbsPathView mountPoint, tl::unique_ref<IPack> pack)
{
return mount(std::move(mountPoint), std::move(pack), MountPolicy::Back);
}
@@ -67,7 +67,7 @@ tl::unique_ptr<IPack> CustomFilesystem::unmount(PackId packId)
//////////////////////////////////////////////////////////////////////////
OpenSourceResult CustomFilesystem::openSource(const AbsPath& path, SourceFlags flags) const
OpenSourceResult CustomFilesystem::openSource(AbsPathView path, SourceFlags flags) const
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -93,7 +93,7 @@ OpenSourceResult CustomFilesystem::openSource(const AbsPath& path, SourceFlags f
//////////////////////////////////////////////////////////////////////////
OpenStreamSourceResult CustomFilesystem::openStreamSource(const AbsPath& path, SourceFlags flags) const
OpenStreamSourceResult CustomFilesystem::openStreamSource(AbsPathView path, SourceFlags flags) const
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -119,7 +119,7 @@ OpenStreamSourceResult CustomFilesystem::openStreamSource(const AbsPath& path, S
//////////////////////////////////////////////////////////////////////////
OpenMapSourceResult CustomFilesystem::openMapSource(const AbsPath& path, MapView mapView, SourceFlags flags) const
OpenMapSourceResult CustomFilesystem::openMapSource(AbsPathView path, MapView mapView, SourceFlags flags) const
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -145,7 +145,7 @@ OpenMapSourceResult CustomFilesystem::openMapSource(const AbsPath& path, MapView
//////////////////////////////////////////////////////////////////////////
OpenMapSinkResult CustomFilesystem::openMapSink(const AbsPath& path, Mode mode, size_t size, SinkFlags flags)
OpenMapSinkResult CustomFilesystem::openMapSink(AbsPathView path, Mode mode, size_t size, SinkFlags flags)
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -172,7 +172,7 @@ OpenMapSinkResult CustomFilesystem::openMapSink(const AbsPath& path, Mode mode,
//////////////////////////////////////////////////////////////////////////
OpenSinkResult CustomFilesystem::openSink(const AbsPath& path, Mode mode, SinkFlags flags)
OpenSinkResult CustomFilesystem::openSink(AbsPathView path, Mode mode, SinkFlags flags)
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -197,7 +197,7 @@ OpenSinkResult CustomFilesystem::openSink(const AbsPath& path, Mode mode, SinkFl
//////////////////////////////////////////////////////////////////////////
OpenStreamSinkResult CustomFilesystem::openStreamSink(const AbsPath& path, Mode mode, SinkFlags flags)
OpenStreamSinkResult CustomFilesystem::openStreamSink(AbsPathView path, Mode mode, SinkFlags flags)
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -222,7 +222,7 @@ OpenStreamSinkResult CustomFilesystem::openStreamSink(const AbsPath& path, Mode
//////////////////////////////////////////////////////////////////////////
ExistsResult CustomFilesystem::exists(const AbsPath& path) const
ExistsResult CustomFilesystem::exists(AbsPathView path) const
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -252,7 +252,7 @@ ExistsResult CustomFilesystem::exists(const AbsPath& path) const
//////////////////////////////////////////////////////////////////////////
IsFileResult CustomFilesystem::isFile(const AbsPath& path) const
IsFileResult CustomFilesystem::isFile(AbsPathView path) const
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -277,7 +277,7 @@ IsFileResult CustomFilesystem::isFile(const AbsPath& path) const
//////////////////////////////////////////////////////////////////////////
RenameResult CustomFilesystem::rename(const AbsPath& path, const AbsPath& newPath)
RenameResult CustomFilesystem::rename(AbsPathView path, AbsPathView newPath)
{
AbsPath fromPackPath;
AbsPath toPackPath;
@@ -304,7 +304,7 @@ RenameResult CustomFilesystem::rename(const AbsPath& path, const AbsPath& newPat
//////////////////////////////////////////////////////////////////////////
CopyResult CustomFilesystem::copy(const AbsPath& path, const AbsPath& newPath)
CopyResult CustomFilesystem::copy(AbsPathView path, AbsPathView newPath)
{
OUTCOME_TRY(const auto source, openStreamSource(path, SourceFlags(SourceFlag::SequentialHint, SourceFlag::Uncached)));
OUTCOME_TRY(const auto destination, openStreamSink(newPath, Mode::CreateOrOpenAndClear));
@@ -318,7 +318,7 @@ CopyResult CustomFilesystem::copy(const AbsPath& path, const AbsPath& newPath)
//////////////////////////////////////////////////////////////////////////
MakeHardLinkResult CustomFilesystem::makeHardLink(const AbsPath& sourcePath, const AbsPath& linkPath)
MakeHardLinkResult CustomFilesystem::makeHardLink(AbsPathView sourcePath, AbsPathView linkPath)
{
OUTCOME_TRY(const auto sp, convertToNativePath(sourcePath));
OUTCOME_TRY(const auto lp, convertToNativePath(linkPath));
@@ -327,7 +327,7 @@ MakeHardLinkResult CustomFilesystem::makeHardLink(const AbsPath& sourcePath, con
//////////////////////////////////////////////////////////////////////////
MakeSoftLinkResult CustomFilesystem::makeSymLink(const AbsPath& sourcePath, const AbsPath& linkPath)
MakeSoftLinkResult CustomFilesystem::makeSymLink(AbsPathView sourcePath, AbsPathView linkPath)
{
OUTCOME_TRY(const auto sp, convertToNativePath(sourcePath));
OUTCOME_TRY(const auto lp, convertToNativePath(linkPath));
@@ -336,7 +336,7 @@ MakeSoftLinkResult CustomFilesystem::makeSymLink(const AbsPath& sourcePath, cons
//////////////////////////////////////////////////////////////////////////
IsFolderResult CustomFilesystem::isFolder(const AbsPath& path) const
IsFolderResult CustomFilesystem::isFolder(AbsPathView path) const
{
AbsPath folderPackPath;
for (const auto& pd : m_packsData)
@@ -360,7 +360,7 @@ IsFolderResult CustomFilesystem::isFolder(const AbsPath& path) const
//////////////////////////////////////////////////////////////////////////
MakeFolderResult CustomFilesystem::makeFolder(const AbsPath& path)
MakeFolderResult CustomFilesystem::makeFolder(AbsPathView path)
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -387,7 +387,7 @@ MakeFolderResult CustomFilesystem::makeFolder(const AbsPath& path)
//////////////////////////////////////////////////////////////////////////
RemoveResult CustomFilesystem::remove(const AbsPath& path)
RemoveResult CustomFilesystem::remove(AbsPathView path)
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -412,7 +412,7 @@ RemoveResult CustomFilesystem::remove(const AbsPath& path)
//////////////////////////////////////////////////////////////////////////
RemoveRecursivelyResult CustomFilesystem::removeRecursively(const AbsPath& path)
RemoveRecursivelyResult CustomFilesystem::removeRecursively(AbsPathView path)
{
for (const EnumerateEntry& ee : enumerate(path))
{
@@ -432,7 +432,7 @@ RemoveRecursivelyResult CustomFilesystem::removeRecursively(const AbsPath& path)
//////////////////////////////////////////////////////////////////////////
ConvertToNativePathResult CustomFilesystem::convertToNativePath(const AbsPath& path) const
ConvertToNativePathResult CustomFilesystem::convertToNativePath(AbsPathView path) const
{
AbsPath folderPackPath;
for (const auto& pd : m_packsData)
@@ -453,7 +453,7 @@ ConvertToNativePathResult CustomFilesystem::convertToNativePath(const AbsPath& p
//////////////////////////////////////////////////////////////////////////
SetWriteTimeResult CustomFilesystem::setWriteTime(const AbsPath& path, time_t time)
SetWriteTimeResult CustomFilesystem::setWriteTime(AbsPathView path, time_t time)
{
AbsPath filePackPath;
for (const auto& pd : m_packsData)
@@ -478,7 +478,7 @@ SetWriteTimeResult CustomFilesystem::setWriteTime(const AbsPath& path, time_t ti
//////////////////////////////////////////////////////////////////////////
GetStatResult CustomFilesystem::getStat(const AbsPath& path) const
GetStatResult CustomFilesystem::getStat(AbsPathView path) const
{
AbsPath folderPackPath;
for (const auto& pd : m_packsData)
@@ -499,7 +499,7 @@ GetStatResult CustomFilesystem::getStat(const AbsPath& path) const
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerate(const AbsPath& path) const
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerate(AbsPathView path) const
{
AbsPath basePackPath;
for (const auto& pd : m_packsData)
@@ -521,16 +521,19 @@ cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerate(const AbsPath& pa
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerateRecursively(const AbsPath& path) const
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerateRecursively(AbsPathView path) const
{
tl::unordered_set<RelPath> checkedFiles;
const AbsPath rootPath(path);
//Note, the path might not survive after the first co_yield, if it came from a rvalue, in some conditions, so use the rootPath
AbsPath basePackPath;
for (const auto& pd : m_packsData)
{
if (pd.mountPoint.is_prefix_of(path))
if (pd.mountPoint.is_prefix_of(rootPath))
{
convertToPackPath(basePackPath, path, pd.mountPoint);
convertToPackPath(basePackPath, rootPath, pd.mountPoint);
if (!basePackPath.empty())
{
@@ -545,13 +548,17 @@ cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerateRecursively(const
co_yield std::move(ee);
}
}
else if (path.is_prefix_of(pd.mountPoint))
else if (rootPath.is_prefix_of(pd.mountPoint))
{
const RelPath parentPath = path.path_to(pd.mountPoint);
const RelPathView parentPath = rootPath.path_to(pd.mountPoint);
RelPath p = parentPath;
for (EnumerateEntry ee : pd.pack->enumerateRecursively(AbsPath(PosixRootTag::value())))
{
ee.path = parentPath + ee.path;
const size_t eePathSize = ee.path.size();
p += ee.path;
ee.path = p;
p.shrink(eePathSize);
if (checkedFiles.insert(ee.path).second)
co_yield std::move(ee);
}
@@ -561,19 +568,19 @@ cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerateRecursively(const
//////////////////////////////////////////////////////////////////////////
void CustomFilesystem::convertToPackPath(AbsPath& packPath, const AbsPath& path, const AbsPath& mountPoint) const
void CustomFilesystem::convertToPackPath(AbsPath& packPath, AbsPathView path, AbsPathView mountPoint) const
{
RelPath basePath = mountPoint.path_to(path);
RelPathView basePath = mountPoint.prefix_path_to(path);
packPath = PosixRootTag::value();
packPath.reserve(basePath.size() + 1);
for (tl::string& element : basePath)
packPath.push_back(std::move(element));
for (const tl::string& element : basePath)
packPath.push_back(element);
}
//////////////////////////////////////////////////////////////////////////
tl::result<CustomFilesystem::PackId> CustomFilesystem::mount(AbsPath mountPoint, tl::unique_ref<IPack> pack, MountPolicy policy)
tl::result<CustomFilesystem::PackId> CustomFilesystem::mount(AbsPathView mountPoint, tl::unique_ref<IPack> pack, MountPolicy policy)
{
if (!mountPoint.is_valid())