Skip to content

Commit

Permalink
Fix permission denied when building symlink derivation which points t…
Browse files Browse the repository at this point in the history
…o a symlink out of the store

Bind-mounting symlinks is apparently not possible, which is why the
thing was failing.

Fortunately, symlinks are small, so we can fallback to copy them at no cost.

Fix #9579

Co-authored-by: Artturin <[email protected]>
(cherry picked from commit 913db9f)
  • Loading branch information
thufschmitt authored and github-actions[bot] committed Apr 11, 2024
1 parent f7146d2 commit ccb9779
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <regex>
#include <queue>

#include <sys/stat.h>
#include <sys/un.h>
#include <fcntl.h>
#include <termios.h>
Expand Down Expand Up @@ -395,20 +396,30 @@ void LocalDerivationGoal::cleanupPostOutputsRegisteredModeNonCheck()
static void doBind(const Path & source, const Path & target, bool optional = false) {
debug("bind mounting '%1%' to '%2%'", source, target);
struct stat st;
if (stat(source.c_str(), &st) == -1) {

auto bindMount = [&]() {
if (mount(source.c_str(), target.c_str(), "", MS_BIND | MS_REC, 0) == -1)
throw SysError("bind mount from '%1%' to '%2%' failed", source, target);
};

if (lstat(source.c_str(), &st) == -1) {
if (optional && errno == ENOENT)
return;
else
throw SysError("getting attributes of path '%1%'", source);
}
if (S_ISDIR(st.st_mode))
if (S_ISDIR(st.st_mode)) {
createDirs(target);
else {
bindMount();
} else if (S_ISLNK(st.st_mode)) {
// Symlinks can (apparently) not be bind-mounted, so just copy it
createDirs(dirOf(target));
copyFile(source, target, /* andDelete */ false);
} else {
createDirs(dirOf(target));
writeFile(target, "");
bindMount();
}
if (mount(source.c_str(), target.c_str(), "", MS_BIND | MS_REC, 0) == -1)
throw SysError("bind mount from '%1%' to '%2%' failed", source, target);
};
#endif

Expand Down

0 comments on commit ccb9779

Please sign in to comment.