From 67975bfcc6da764c0add354e87fa67099c8f4fcd Mon Sep 17 00:00:00 2001 From: nick huang Date: Sat, 22 Jun 2024 00:52:17 +0800 Subject: [PATCH] using filesystem read_link to test if sym link already created. --- src/zimdump.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/zimdump.cpp b/src/zimdump.cpp index 08e95dc0..1a9b319d 100644 --- a/src/zimdump.cpp +++ b/src/zimdump.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #define ZIM_PRIVATE #include @@ -291,6 +292,16 @@ void ZimDumper::writeHttpRedirect(const std::string& directory, const std::strin write_to_file(directory + SEPARATOR, outputPath, content.c_str(), content.size()); } +inline static bool testSymLink(const std::string& sym, const std::string& target) { + namespace fs = std::filesystem; + std::error_code ec; + fs::path p = fs::read_symlink(fs::path(sym), ec); + if (!ec) { + return p.string() == target; + } + return false; +} + void ZimDumper::dumpFiles(const std::string& directory, bool symlinkdump, std::function nsfilter) { unsigned int truncatedFiles = 0; @@ -341,8 +352,11 @@ void ZimDumper::dumpFiles(const std::string& directory, bool symlinkdump, std::f write_to_file(directory + SEPARATOR, relative_path, blob.data(), blob.size()); #else if (symlink(redirectPath.c_str(), full_path.c_str()) != 0) { - throw std::runtime_error( - std::string("Error creating symlink from ") + full_path + " to " + redirectPath); + // let's double check if the symlink is already created + if (!testSymLink(full_path, redirectPath)) { + throw std::runtime_error( + std::string("Error creating symlink from ") + full_path + " to " + redirectPath); + } } #endif }