Factor out linkOrCopy()

This commit is contained in:
Eelco Dolstra 2018-10-02 11:22:13 +02:00
parent f1c0b2c0e1
commit f7ce80f90a
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -1874,6 +1874,21 @@ static void preloadNSS() {
}); });
} }
void linkOrCopy(const Path & from, const Path & to)
{
if (link(from.c_str(), to.c_str()) == -1) {
/* Hard-linking fails if we exceed the maximum link count on a
file (e.g. 32000 of ext3), which is quite possible after a
'nix-store --optimise'. FIXME: actually, why don't we just
bind-mount in this case? */
if (errno != EMLINK)
throw SysError("linking '%s' to '%s'", to, from);
copyPath(from, to);
}
}
void DerivationGoal::startBuilder() void DerivationGoal::startBuilder()
{ {
/* Right platform? */ /* Right platform? */
@ -2117,22 +2132,8 @@ void DerivationGoal::startBuilder()
throw SysError(format("getting attributes of path '%1%'") % i); throw SysError(format("getting attributes of path '%1%'") % i);
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
dirsInChroot[i] = r; dirsInChroot[i] = r;
else { else
Path p = chrootRootDir + i; linkOrCopy(r, chrootRootDir + i);
debug("linking '%1%' to '%2%'", p, r);
if (link(r.c_str(), p.c_str()) == -1) {
/* Hard-linking fails if we exceed the maximum
link count on a file (e.g. 32000 of ext3),
which is quite possible after a `nix-store
--optimise'. */
if (errno != EMLINK)
throw SysError(format("linking '%1%' to '%2%'") % p % i);
StringSink sink;
dumpPath(r, sink);
StringSource source(*sink.s);
restorePath(p, source);
}
}
} }
/* If we're repairing, checking or rebuilding part of a /* If we're repairing, checking or rebuilding part of a