Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using filesystem read_link to test if sym link already created. #417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/zimdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sstream>
#include <fstream>
#include <set>
#include <filesystem>

#define ZIM_PRIVATE
#include <zim/archive.h>
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testSymlink convey very few information. (What is tested ?)

It would be better to rename it to checkSymlinkTarget or symlinkPointsTo.

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<bool (const char c)> nsfilter)
{
unsigned int truncatedFiles = 0;
Expand Down Expand Up @@ -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
}
Expand Down