about summary refs log tree commit diff
diff options
context:
space:
mode:
authorShea Levy <shea@shealevy.com>2013-07-12T13·35-0400
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-07-15T14·01+0200
commit16591eb3cccf86da8cd3f20c56e2dd847576ff5e (patch)
treecc77f031f689af1f67e8b3c37d6cf72f36e9d30c
parentc3f5413e806a22d3e664416649687e331b14f8b9 (diff)
Allow bind-mounting regular files into the chroot
mount(2) with MS_BIND allows mounting a regular file on top of a regular
file, so there's no reason to only bind directories. This allows finer
control over just which files are and aren't included in the chroot
without having to build symlink trees or the like.

Signed-off-by: Shea Levy <shea@shealevy.com>
-rw-r--r--src/libstore/build.cc10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index f71601a77c55..30fbfb8a3a54 100644
--- a/src/libstore/build.cc
+++ b/src/libstore/build.cc
@@ -2117,11 +2117,19 @@ void DerivationGoal::initChild()
                filesystem that we want in the chroot
                environment. */
             foreach (DirsInChroot::iterator, i, dirsInChroot) {
+                struct stat st;
                 Path source = i->second;
                 Path target = chrootRootDir + i->first;
                 if (source == "/proc") continue; // backwards compatibility
                 debug(format("bind mounting `%1%' to `%2%'") % source % target);
-                createDirs(target);
+                if (stat(source.c_str(), &st) == -1)
+                    throw SysError(format("getting attributes of path `%1%'") % source);
+                if (S_ISDIR(st.st_mode))
+                    createDirs(target);
+                else {
+                    createDirs(dirOf(target));
+                    writeFile(target, "");
+                }
                 if (mount(source.c_str(), target.c_str(), "", MS_BIND, 0) == -1)
                     throw SysError(format("bind mount from `%1%' to `%2%' failed") % source % target);
             }