diff --git a/include/fs/CustomFilesystem.h b/include/fs/CustomFilesystem.h index 758d4eb..8cbb6ac 100644 --- a/include/fs/CustomFilesystem.h +++ b/include/fs/CustomFilesystem.h @@ -64,8 +64,8 @@ public: GetStatResult getStat(AbsPathView path) const override; - cppcoro::generator enumerate(AbsPathView path) const override; - cppcoro::generator enumerateRecursively(AbsPathView path) const override; + cppcoro::generator enumerate(AbsPath path) const override; + cppcoro::generator enumerateRecursively(AbsPath path) const override; private: struct PackData diff --git a/include/fs/FolderPack.h b/include/fs/FolderPack.h index 837858b..cea4a8b 100644 --- a/include/fs/FolderPack.h +++ b/include/fs/FolderPack.h @@ -25,8 +25,8 @@ public: GetStatResult getStat(AbsPathView path) const override; - cppcoro::generator enumerate(AbsPathView path) const override; - cppcoro::generator enumerateRecursively(AbsPathView path) const override; + cppcoro::generator enumerate(AbsPath path) const override; + cppcoro::generator enumerateRecursively(AbsPath path) const override; ConvertToNativePathResult convertToNativePath(AbsPathView path) const override; diff --git a/include/fs/IFilesystem.h b/include/fs/IFilesystem.h index 41899f0..1e6ce40 100644 --- a/include/fs/IFilesystem.h +++ b/include/fs/IFilesystem.h @@ -117,8 +117,12 @@ public: virtual SetWriteTimeResult setWriteTime(AbsPathView path, time_t time) = 0; - virtual cppcoro::generator enumerate(AbsPathView path) const = 0; - virtual cppcoro::generator 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 enumerate(AbsPath path) const = 0; + virtual cppcoro::generator enumerateRecursively(AbsPath path) const = 0; }; diff --git a/include/fs/IPack.h b/include/fs/IPack.h index 28c590e..08dfe4f 100644 --- a/include/fs/IPack.h +++ b/include/fs/IPack.h @@ -33,8 +33,8 @@ public: virtual GetStatResult getStat(AbsPathView i_path) const = 0; - virtual cppcoro::generator enumerate(AbsPathView i_path) const = 0; - virtual cppcoro::generator enumerateRecursively(AbsPathView i_path) const = 0; + virtual cppcoro::generator enumerate(AbsPath i_path) const = 0; + virtual cppcoro::generator enumerateRecursively(AbsPath i_path) const = 0; virtual ConvertToNativePathResult convertToNativePath(AbsPathView i_path) const = 0; diff --git a/include/fs/NativeFilesystem.h b/include/fs/NativeFilesystem.h index 2495c84..3c1b15b 100644 --- a/include/fs/NativeFilesystem.h +++ b/include/fs/NativeFilesystem.h @@ -50,8 +50,8 @@ public: GetStatResult getStat(AbsPathView path) const override; - cppcoro::generator enumerate(AbsPathView path) const override; - cppcoro::generator enumerateRecursively(AbsPathView path) const override; + cppcoro::generator enumerate(AbsPath path) const override; + cppcoro::generator enumerateRecursively(AbsPath path) const override; AbsPath getCurrentFolder() const; tl::result setCurrentFolder(AbsPathView path); diff --git a/include/fs/zip/ZipPack.h b/include/fs/zip/ZipPack.h index 6f6574e..748aadd 100644 --- a/include/fs/zip/ZipPack.h +++ b/include/fs/zip/ZipPack.h @@ -37,8 +37,8 @@ public: GetStatResult getStat(AbsPathView path) const override; - cppcoro::generator enumerate(AbsPathView path) const override; - cppcoro::generator enumerateRecursively(AbsPathView path) const override; + cppcoro::generator enumerate(AbsPath path) const override; + cppcoro::generator enumerateRecursively(AbsPath path) const override; tl::result convertToNativePath(AbsPathView path) const override; diff --git a/src/CustomFilesystem.cpp b/src/CustomFilesystem.cpp index 71012eb..1ed09e0 100644 --- a/src/CustomFilesystem.cpp +++ b/src/CustomFilesystem.cpp @@ -499,7 +499,7 @@ GetStatResult CustomFilesystem::getStat(AbsPathView path) const ////////////////////////////////////////////////////////////////////////// -cppcoro::generator CustomFilesystem::enumerate(AbsPathView path) const +cppcoro::generator CustomFilesystem::enumerate(AbsPath path) const { AbsPath basePackPath; for (const auto& pd : m_packsData) @@ -521,19 +521,16 @@ cppcoro::generator CustomFilesystem::enumerate(AbsPathView path) ////////////////////////////////////////////////////////////////////////// -cppcoro::generator CustomFilesystem::enumerateRecursively(AbsPathView path) const +cppcoro::generator CustomFilesystem::enumerateRecursively(AbsPath path) const { tl::unordered_set 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 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()))) diff --git a/src/FolderPack.cpp b/src/FolderPack.cpp index da7d3fa..edb48e9 100644 --- a/src/FolderPack.cpp +++ b/src/FolderPack.cpp @@ -78,7 +78,7 @@ GetStatResult FolderPack::getStat(AbsPathView path) const ////////////////////////////////////////////////////////////////////////// -cppcoro::generator FolderPack::enumerate(AbsPathView path) const +cppcoro::generator FolderPack::enumerate(AbsPath path) const { const AbsPath localPath = convertToUnderlyingPath(path); for (EnumerateEntry ee : m_filesystem->enumerate(localPath)) @@ -87,7 +87,7 @@ cppcoro::generator FolderPack::enumerate(AbsPathView path) const ////////////////////////////////////////////////////////////////////////// -cppcoro::generator FolderPack::enumerateRecursively(AbsPathView path) const +cppcoro::generator FolderPack::enumerateRecursively(AbsPath path) const { const AbsPath localPath = convertToUnderlyingPath(path); for (EnumerateEntry ee : m_filesystem->enumerateRecursively(localPath)) diff --git a/src/NativeFilesystem.cpp b/src/NativeFilesystem.cpp index f8c5753..e3741c1 100644 --- a/src/NativeFilesystem.cpp +++ b/src/NativeFilesystem.cpp @@ -522,7 +522,7 @@ GetStatResult NativeFilesystem::getStat(AbsPathView path) const ////////////////////////////////////////////////////////////////////////// -cppcoro::generator NativeFilesystem::enumerate(AbsPathView path) const +cppcoro::generator NativeFilesystem::enumerate(AbsPath path) const { TL_ASSERT(path.is_valid()); @@ -536,8 +536,6 @@ cppcoro::generator 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 NativeFilesystem::enumerate(AbsPathView path) ////////////////////////////////////////////////////////////////////////// -cppcoro::generator NativeFilesystem::enumerateRecursively(AbsPathView path) const +cppcoro::generator NativeFilesystem::enumerateRecursively(AbsPath path) const { TL_ASSERT(path.is_valid()); @@ -571,12 +569,9 @@ cppcoro::generator 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 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 }; } } } diff --git a/src/zip/ZipPack.cpp b/src/zip/ZipPack.cpp index 48fd87c..0089011 100644 --- a/src/zip/ZipPack.cpp +++ b/src/zip/ZipPack.cpp @@ -441,7 +441,7 @@ GetStatResult ZipPack::getStat(AbsPathView path) const ////////////////////////////////////////////////////////////////////////// -cppcoro::generator ZipPack::enumerate(AbsPathView path) const +cppcoro::generator 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 ZipPack::enumerateRecursively(AbsPathView path) const +cppcoro::generator ZipPack::enumerateRecursively(AbsPath path) const { TL_ASSERT(path.is_valid());