Using PathView to pass paths to APIs #2

Merged
jeanlemotan merged 3 commits from cv_path_view into main 2024-09-05 16:52:53 +02:00
10 changed files with 30 additions and 34 deletions
Showing only changes of commit 5ec1fddf57 - Show all commits
+2 -2
View File
@@ -64,8 +64,8 @@ public:
GetStatResult getStat(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPath path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPath path) const override;
private:
struct PackData
+2 -2
View File
@@ -25,8 +25,8 @@ public:
GetStatResult getStat(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPath path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPath path) const override;
ConvertToNativePathResult convertToNativePath(AbsPathView path) const override;
+6 -2
View File
@@ -117,8 +117,12 @@ public:
virtual SetWriteTimeResult setWriteTime(AbsPathView path, time_t time) = 0;
virtual cppcoro::generator<EnumerateEntry> enumerate(AbsPathView path) const = 0;
virtual cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPathView path) const = 0;
//The AbsPath is a copy, because there are lifetime subtleties when using coroutines.
//In particular, without pass-by-value, this would crash because nobody is storing the rvalue:
// for (auto e: fs.enumerate(path + "other folder"))
// {}
virtual cppcoro::generator<EnumerateEntry> enumerate(AbsPath path) const = 0;
virtual cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPath path) const = 0;
};
+2 -2
View File
@@ -33,8 +33,8 @@ public:
virtual GetStatResult getStat(AbsPathView i_path) const = 0;
virtual cppcoro::generator<EnumerateEntry> enumerate(AbsPathView i_path) const = 0;
virtual cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPathView i_path) const = 0;
virtual cppcoro::generator<EnumerateEntry> enumerate(AbsPath i_path) const = 0;
virtual cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPath i_path) const = 0;
virtual ConvertToNativePathResult convertToNativePath(AbsPathView i_path) const = 0;
+2 -2
View File
@@ -50,8 +50,8 @@ public:
GetStatResult getStat(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPath path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPath path) const override;
AbsPath getCurrentFolder() const;
tl::result<Error> setCurrentFolder(AbsPathView path);
+2 -2
View File
@@ -37,8 +37,8 @@ public:
GetStatResult getStat(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPathView path) const override;
cppcoro::generator<EnumerateEntry> enumerate(AbsPath path) const override;
cppcoro::generator<EnumerateEntry> enumerateRecursively(AbsPath path) const override;
tl::result<AbsPath, Error> convertToNativePath(AbsPathView path) const override;
+6 -9
View File
@@ -499,7 +499,7 @@ GetStatResult CustomFilesystem::getStat(AbsPathView path) const
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerate(AbsPathView path) const
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerate(AbsPath path) const
{
AbsPath basePackPath;
for (const auto& pd : m_packsData)
@@ -521,19 +521,16 @@ cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerate(AbsPathView path)
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerateRecursively(AbsPathView path) const
cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerateRecursively(AbsPath 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(rootPath))
if (pd.mountPoint.is_prefix_of(path))
{
convertToPackPath(basePackPath, rootPath, pd.mountPoint);
convertToPackPath(basePackPath, path, pd.mountPoint);
if (!basePackPath.empty())
{
@@ -548,9 +545,9 @@ cppcoro::generator<EnumerateEntry> CustomFilesystem::enumerateRecursively(AbsPat
co_yield std::move(ee);
}
}
else if (rootPath.is_prefix_of(pd.mountPoint))
else if (path.is_prefix_of(pd.mountPoint))
{
const RelPathView parentPath = rootPath.path_to(pd.mountPoint);
const RelPathView parentPath = path.path_to(pd.mountPoint);
RelPath p = parentPath;
for (EnumerateEntry ee : pd.pack->enumerateRecursively(AbsPath(PosixRootTag::value())))
+2 -2
View File
@@ -78,7 +78,7 @@ GetStatResult FolderPack::getStat(AbsPathView path) const
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> FolderPack::enumerate(AbsPathView path) const
cppcoro::generator<EnumerateEntry> FolderPack::enumerate(AbsPath path) const
{
const AbsPath localPath = convertToUnderlyingPath(path);
for (EnumerateEntry ee : m_filesystem->enumerate(localPath))
@@ -87,7 +87,7 @@ cppcoro::generator<EnumerateEntry> FolderPack::enumerate(AbsPathView path) const
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> FolderPack::enumerateRecursively(AbsPathView path) const
cppcoro::generator<EnumerateEntry> FolderPack::enumerateRecursively(AbsPath path) const
{
const AbsPath localPath = convertToUnderlyingPath(path);
for (EnumerateEntry ee : m_filesystem->enumerateRecursively(localPath))
+4 -9
View File
@@ -522,7 +522,7 @@ GetStatResult NativeFilesystem::getStat(AbsPathView path) const
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerate(AbsPathView path) const
cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerate(AbsPath path) const
{
TL_ASSERT(path.is_valid());
@@ -536,8 +536,6 @@ cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerate(AbsPathView path)
co_return;
}
//Note, the path might not survive after the first co_yield, if it came from a rvalue, in some conditions, so if you need it below, make a copy in a AbsPath
for (auto const& entry : std::filesystem::directory_iterator{ toStdPath(path) })
{
const auto status = entry.status();
@@ -557,7 +555,7 @@ cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerate(AbsPathView path)
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerateRecursively(AbsPathView path) const
cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerateRecursively(AbsPath path) const
{
TL_ASSERT(path.is_valid());
@@ -571,12 +569,9 @@ cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerateRecursively(AbsPat
co_return;
}
const AbsPath rootPath(path);
AbsPath eePath;
const auto stdRootPath = toStdPath(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
for (auto const& entry : std::filesystem::recursive_directory_iterator{ stdRootPath })
{
const auto status = entry.status();
@@ -584,12 +579,12 @@ cppcoro::generator<EnumerateEntry> NativeFilesystem::enumerateRecursively(AbsPat
if (type == std::filesystem::file_type::directory)
{
toFSAbsPath(eePath, entry.path());
co_yield{ rootPath.path_to(eePath), true };
co_yield{ path.path_to(eePath), true };
}
else if (type == std::filesystem::file_type::regular)
{
toFSAbsPath(eePath, entry.path());
co_yield{ rootPath.path_to(eePath), false };
co_yield{ path.path_to(eePath), false };
}
}
}
+2 -2
View File
@@ -441,7 +441,7 @@ GetStatResult ZipPack::getStat(AbsPathView path) const
//////////////////////////////////////////////////////////////////////////
cppcoro::generator<EnumerateEntry> ZipPack::enumerate(AbsPathView path) const
cppcoro::generator<EnumerateEntry> ZipPack::enumerate(AbsPath path) const
{
TL_ASSERT(path.is_valid());
@@ -508,7 +508,7 @@ void ZipPack::buildUpPath(RelPath& path, const File& file, uint32_t toIndex) con
}
cppcoro::generator<EnumerateEntry> ZipPack::enumerateRecursively(AbsPathView path) const
cppcoro::generator<EnumerateEntry> ZipPack::enumerateRecursively(AbsPath path) const
{
TL_ASSERT(path.is_valid());