libutil/mount: accept copy flags for bind path

Sometimes, `bindPath` will detect the source is a symlink and we are not
using the new mount API which support symlinks (kernel ≥ 5.12 IIRC?).

In those instances, we copy the inode to the target.

But some callers may want to follow the symlink in such circumstances,
we add a new default argument to the previous value and let caller
decide for themselves.

Change-Id: I8505b613fc614ce539eb89258fbbb7eaecebe23b
Signed-off-by: Raito Bezarius <raito@lix.systems>
This commit is contained in:
Raito Bezarius
2025-07-27 00:20:09 +00:00
parent ef94901156
commit e854c5364a
2 changed files with 12 additions and 5 deletions
+3 -3
View File
@@ -7,7 +7,8 @@
namespace nix {
void bindPath(const Path & source, const Path & target, bool optional) {
void bindPath(const Path & source, const Path & target, bool optional, CopyFileFlags flags)
{
debug("bind mounting '%1%' to '%2%'", source, target);
auto bindMount = [&]() {
@@ -30,14 +31,13 @@ void bindPath(const Path & source, const Path & target, bool optional) {
} else if (S_ISLNK(st.st_mode)) {
// Symlinks can (apparently) not be bind-mounted, so just copy it
createDirs(dirOf(target));
copyFile(source, target, {});
copyFile(source, target, flags);
} else {
createDirs(dirOf(target));
writeFile(target, "");
bindMount();
}
}
}
#endif
+9 -2
View File
@@ -2,6 +2,7 @@
///@file
#include "lix/libutil/types.hh"
#include "lix/libutil/file-system.hh"
#if __linux__
namespace nix {
@@ -9,8 +10,14 @@ 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);
void bindPath(
const Path & source, const Path & target, bool optional = false, CopyFileFlags flags = {}
);
}
#endif