libutil, libstore: move bindPath to libstore linux platform bits

it's only used for linux sandbox setup, and can't be used elsewhere anyway.

Change-Id: I3560e141e5250cf9e72dc1576f87384a8e01f446
This commit is contained in:
eldritch horrors
2026-02-02 14:20:54 +00:00
parent dd54f45bc7
commit 52590089df
5 changed files with 34 additions and 72 deletions
@@ -29,7 +29,6 @@
#include "lix/libutil/namespaces.hh"
#include "lix/libutil/types.hh"
#include "lix/libutil/unix-domain-socket.hh"
#include "lix/libutil/mount.hh"
#include "lix/libutil/strings.hh"
#include "lix/libutil/thread-name.hh"
#include "lix/libstore/platform/linux.hh"
+34 -1
View File
@@ -9,7 +9,6 @@
#include "lix/libutil/file-system.hh"
#include "lix/libutil/finally.hh"
#include "lix/libstore/gc-store.hh"
#include "lix/libutil/mount.hh"
#include "lix/libutil/processes.hh"
#include "lix/libutil/result.hh"
#include "lix/libutil/signals.hh"
@@ -1234,6 +1233,40 @@ std::string LinuxLocalDerivationGoal::rewriteResolvConf(std::string fromHost)
return std::regex_replace(fromHost, lineRegex, "") + nsInSandbox;
}
static void bindPath(const Path & source, const Path & target, bool optional = false)
{
debug("bind mounting '%1%' to '%2%'", source, target);
auto bindMount = [&]() {
if (sys::mount(source, target, "", MS_BIND | MS_REC, 0) == -1) {
throw SysError("bind mount from '%1%' to '%2%' failed", source, target);
}
};
auto maybeSt = maybeLstat(source);
if (!maybeSt) {
if (optional) {
return;
} else {
throw SysError("getting attributes of path '%1%'", source);
}
}
auto st = *maybeSt;
if (S_ISDIR(st.st_mode)) {
createDirs(target);
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, {});
} else {
createDirs(dirOf(target));
writeFile(target, "");
bindMount();
}
}
bool LinuxLocalDerivationGoal::prepareChildSetup()
{
// Set the NO_NEW_PRIVS prctl flag.
-2
View File
@@ -31,7 +31,6 @@ liblix_sources += files(
'logging-rpc.cc',
'logging.cc',
'monitor-fd.cc',
'mount.cc',
'namespaces.cc',
'position.cc',
'print-elided.cc',
@@ -114,7 +113,6 @@ libutil_headers = files(
'lru-cache.hh',
'manually-drop.hh',
'monitor-fd.hh',
'mount.hh',
'namespaces.hh',
'notifying-counter.hh',
'pool.hh',
-45
View File
@@ -1,45 +0,0 @@
#include "lix/libutil/mount.hh"
#include "c-calls.hh"
#include "lix/libutil/error.hh"
#include "lix/libutil/file-system.hh"
#include "lix/libutil/logging.hh"
#if __linux__
#include <sys/mount.h>
namespace nix {
void bindPath(const Path & source, const Path & target, bool optional, CopyFileFlags flags)
{
debug("bind mounting '%1%' to '%2%'", source, target);
auto bindMount = [&]() {
if (sys::mount(source, target, "", MS_BIND | MS_REC, 0) == -1) {
throw SysError("bind mount from '%1%' to '%2%' failed", source, target);
}
};
auto maybeSt = maybeLstat(source);
if (!maybeSt) {
if (optional)
return;
else
throw SysError("getting attributes of path '%1%'", source);
}
auto st = *maybeSt;
if (S_ISDIR(st.st_mode)) {
createDirs(target);
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, flags);
} else {
createDirs(dirOf(target));
writeFile(target, "");
bindMount();
}
}
}
#endif
-23
View File
@@ -1,23 +0,0 @@
#pragma once
///@file
#include "lix/libutil/types.hh"
#include "lix/libutil/file-system.hh"
#if __linux__
namespace nix {
/**
* Bind-mount file or directory from `source` to `destination`.
* If source does not exist this will fail unless `optional` is set
*
* If `source` is a symlink, it will perform a copy instead of a bind mount
* because symlinks cannot be bind mounted on all versions of the Linux kernel.
*
* If a copy is performed, extra flags to the copy can be passed using `flags`.
*/
void bindPath(
const Path & source, const Path & target, bool optional = false, CopyFileFlags flags = {}
);
}
#endif