diff options
author | Artemis Tosini <me@artem.ist> | 2024-07-27 19:40:40 +0000 |
---|---|---|
committer | Artemis Tosini <me@artem.ist> | 2024-07-27 19:40:40 +0000 |
commit | 3058029fbafb53563fcc31401546edce258d65ca (patch) | |
tree | 584d5933d9c3a050520b10afff9c17254c4a7497 /src/libutil/mount.cc | |
parent | 6abad7cb238c5c7bf59a83bed55e7590c544fc2e (diff) |
libutil: Add bindPath function from libstore
bindPath/doBind is a useful function in build that is used in several
parts of LocalDerivationGoal. Moving this function makes it easier to
split LocalDerivationGoal implementation between several files.
Change-Id: Ic5a0768479c153c1aa3ed425f12604b20bbf0f42
Diffstat (limited to 'src/libutil/mount.cc')
-rw-r--r-- | src/libutil/mount.cc | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/libutil/mount.cc b/src/libutil/mount.cc new file mode 100644 index 000000000..025ced5b9 --- /dev/null +++ b/src/libutil/mount.cc @@ -0,0 +1,43 @@ +#include "mount.hh" +#include "error.hh" +#include "file-system.hh" +#include "logging.hh" +#if __linux__ +#include <sys/mount.h> + +namespace nix { + +void bindPath(const Path & source, const Path & target, bool optional) { + debug("bind mounting '%1%' to '%2%'", source, target); + + 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); + }; + + 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(); + } +} + +} + +#endif |