Skip to content

Commit 67975bf

Browse files
committed
using filesystem read_link to test if sym link already created.
1 parent 1b2e64c commit 67975bf

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/zimdump.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <sstream>
2222
#include <fstream>
2323
#include <set>
24+
#include <filesystem>
2425

2526
#define ZIM_PRIVATE
2627
#include <zim/archive.h>
@@ -291,6 +292,16 @@ void ZimDumper::writeHttpRedirect(const std::string& directory, const std::strin
291292
write_to_file(directory + SEPARATOR, outputPath, content.c_str(), content.size());
292293
}
293294

295+
inline static bool testSymLink(const std::string& sym, const std::string& target) {
296+
namespace fs = std::filesystem;
297+
std::error_code ec;
298+
fs::path p = fs::read_symlink(fs::path(sym), ec);
299+
if (!ec) {
300+
return p.string() == target;
301+
}
302+
return false;
303+
}
304+
294305
void ZimDumper::dumpFiles(const std::string& directory, bool symlinkdump, std::function<bool (const char c)> nsfilter)
295306
{
296307
unsigned int truncatedFiles = 0;
@@ -341,8 +352,11 @@ void ZimDumper::dumpFiles(const std::string& directory, bool symlinkdump, std::f
341352
write_to_file(directory + SEPARATOR, relative_path, blob.data(), blob.size());
342353
#else
343354
if (symlink(redirectPath.c_str(), full_path.c_str()) != 0) {
344-
throw std::runtime_error(
345-
std::string("Error creating symlink from ") + full_path + " to " + redirectPath);
355+
// let's double check if the symlink is already created
356+
if (!testSymLink(full_path, redirectPath)) {
357+
throw std::runtime_error(
358+
std::string("Error creating symlink from ") + full_path + " to " + redirectPath);
359+
}
346360
}
347361
#endif
348362
}

0 commit comments

Comments
 (0)