From f5c1271ddbc8789208bf69d35e5760804fc926f4 Mon Sep 17 00:00:00 2001 From: nick huang Date: Wed, 19 Jun 2024 12:27:23 +0800 Subject: [PATCH] test if symlink already created before to avoid unnecessary exception throw --- src/zimdump.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/zimdump.cpp b/src/zimdump.cpp index 08e95dc0..d7738d47 100644 --- a/src/zimdump.cpp +++ b/src/zimdump.cpp @@ -291,6 +291,18 @@ void ZimDumper::writeHttpRedirect(const std::string& directory, const std::strin write_to_file(directory + SEPARATOR, outputPath, content.c_str(), content.size()); } +static bool testSymlink(const std::string& sym, const std::string& target) { + char buf[256]; + ssize_t n = readlink(sym.c_str(), buf, 255); + if (n > 0){ + buf[n] = '\0'; + if (target == buf) { + return true; + } + } + return false; +} + void ZimDumper::dumpFiles(const std::string& directory, bool symlinkdump, std::function nsfilter) { unsigned int truncatedFiles = 0; @@ -340,9 +352,12 @@ void ZimDumper::dumpFiles(const std::string& directory, bool symlinkdump, std::f auto blob = redirectItem.getData(); 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); + // There is a chance of symlink already created before. i.e. repeat run + if (!testSymlink(full_path, redirectPath)) { + if (symlink(redirectPath.c_str(), full_path.c_str()) != 0) { + throw std::runtime_error( + std::string("Error creating symlink from ") + full_path + " to " + redirectPath); + } } #endif }