From 16591eb3cccf86da8cd3f20c56e2dd847576ff5e Mon Sep 17 00:00:00 2001
From: Shea Levy <shea@shealevy.com>
Date: Fri, 12 Jul 2013 09:35:33 -0400
Subject: [PATCH] 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>
---
 src/libstore/build.cc | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index f71601a77..30fbfb8a3 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);
             }
-- 
GitLab