mirror of
https://github.com/NixOS/nix.git
synced 2025-12-19 07:21:07 +01:00
There are two big changes: 1. Public and private config is now separated. Configuration variables that are only used internally do not go in a header which is installed. (Additionally, libutil has a unix-specific private config header, which should only be used in unix-specific code. This keeps things a bit more organized, in a purely private implementation-internal way.) 2. Secondly, there is no more `-include`. There are very few config items that need to be publically exposed, so now it is feasible to just make the headers that need them just including the (public) configuration header. And there are also a few more small cleanups on top of those: - The configuration files have better names. - The few CPP variables that remain exposed in the public headers are now also renamed to always start with `NIX_`. This ensures they should not conflict with variables defined elsewhere. - We now always use `#if` and not `#ifdef`/`#ifndef` for our configuration variables, which helps avoid bugs by requiring that variables must be defined in all cases.
196 lines
5.3 KiB
C++
196 lines
5.3 KiB
C++
#include <fcntl.h>
|
|
|
|
#include "nix/error.hh"
|
|
#include "nix/config-global.hh"
|
|
#include "nix/fs-sink.hh"
|
|
|
|
#if _WIN32
|
|
# include <fileapi.h>
|
|
# include "nix/file-path.hh"
|
|
# include "nix/windows-error.hh"
|
|
#endif
|
|
|
|
#include "util-config-private.hh"
|
|
|
|
namespace nix {
|
|
|
|
void copyRecursive(
|
|
SourceAccessor & accessor, const CanonPath & from,
|
|
FileSystemObjectSink & sink, const CanonPath & to)
|
|
{
|
|
auto stat = accessor.lstat(from);
|
|
|
|
switch (stat.type) {
|
|
case SourceAccessor::tSymlink:
|
|
{
|
|
sink.createSymlink(to, accessor.readLink(from));
|
|
break;
|
|
}
|
|
|
|
case SourceAccessor::tRegular:
|
|
{
|
|
sink.createRegularFile(to, [&](CreateRegularFileSink & crf) {
|
|
if (stat.isExecutable)
|
|
crf.isExecutable();
|
|
accessor.readFile(from, crf, [&](uint64_t size) {
|
|
crf.preallocateContents(size);
|
|
});
|
|
});
|
|
break;
|
|
}
|
|
|
|
case SourceAccessor::tDirectory:
|
|
{
|
|
sink.createDirectory(to);
|
|
for (auto & [name, _] : accessor.readDirectory(from)) {
|
|
copyRecursive(
|
|
accessor, from / name,
|
|
sink, to / name);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SourceAccessor::tChar:
|
|
case SourceAccessor::tBlock:
|
|
case SourceAccessor::tSocket:
|
|
case SourceAccessor::tFifo:
|
|
case SourceAccessor::tUnknown:
|
|
default:
|
|
throw Error("file '%1%' has an unsupported type of %2%", from, stat.typeString());
|
|
}
|
|
}
|
|
|
|
|
|
struct RestoreSinkSettings : Config
|
|
{
|
|
Setting<bool> preallocateContents{this, false, "preallocate-contents",
|
|
"Whether to preallocate files when writing objects with known size."};
|
|
};
|
|
|
|
static RestoreSinkSettings restoreSinkSettings;
|
|
|
|
static GlobalConfig::Register r1(&restoreSinkSettings);
|
|
|
|
static std::filesystem::path append(const std::filesystem::path & src, const CanonPath & path)
|
|
{
|
|
auto dst = src;
|
|
if (!path.rel().empty())
|
|
dst /= path.rel();
|
|
return dst;
|
|
}
|
|
|
|
void RestoreSink::createDirectory(const CanonPath & path)
|
|
{
|
|
auto p = append(dstPath, path);
|
|
if (!std::filesystem::create_directory(p))
|
|
throw Error("path '%s' already exists", p.string());
|
|
};
|
|
|
|
struct RestoreRegularFile : CreateRegularFileSink {
|
|
AutoCloseFD fd;
|
|
bool startFsync = false;
|
|
|
|
~RestoreRegularFile()
|
|
{
|
|
/* Initiate an fsync operation without waiting for the
|
|
result. The real fsync should be run before registering a
|
|
store path, but this is a performance optimization to allow
|
|
the disk write to start early. */
|
|
if (fd && startFsync)
|
|
fd.startFsync();
|
|
}
|
|
|
|
void operator () (std::string_view data) override;
|
|
void isExecutable() override;
|
|
void preallocateContents(uint64_t size) override;
|
|
};
|
|
|
|
void RestoreSink::createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func)
|
|
{
|
|
auto p = append(dstPath, path);
|
|
|
|
RestoreRegularFile crf;
|
|
crf.startFsync = startFsync;
|
|
crf.fd =
|
|
#ifdef _WIN32
|
|
CreateFileW(p.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL)
|
|
#else
|
|
open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0666)
|
|
#endif
|
|
;
|
|
if (!crf.fd) throw NativeSysError("creating file '%1%'", p);
|
|
func(crf);
|
|
}
|
|
|
|
void RestoreRegularFile::isExecutable()
|
|
{
|
|
// Windows doesn't have a notion of executable file permissions we
|
|
// care about here, right?
|
|
#ifndef _WIN32
|
|
struct stat st;
|
|
if (fstat(fd.get(), &st) == -1)
|
|
throw SysError("fstat");
|
|
if (fchmod(fd.get(), st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH)) == -1)
|
|
throw SysError("fchmod");
|
|
#endif
|
|
}
|
|
|
|
void RestoreRegularFile::preallocateContents(uint64_t len)
|
|
{
|
|
if (!restoreSinkSettings.preallocateContents)
|
|
return;
|
|
|
|
#if HAVE_POSIX_FALLOCATE
|
|
if (len) {
|
|
errno = posix_fallocate(fd.get(), 0, len);
|
|
/* Note that EINVAL may indicate that the underlying
|
|
filesystem doesn't support preallocation (e.g. on
|
|
OpenSolaris). Since preallocation is just an
|
|
optimisation, ignore it. */
|
|
if (errno && errno != EINVAL && errno != EOPNOTSUPP && errno != ENOSYS)
|
|
throw SysError("preallocating file of %1% bytes", len);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void RestoreRegularFile::operator () (std::string_view data)
|
|
{
|
|
writeFull(fd.get(), data);
|
|
}
|
|
|
|
void RestoreSink::createSymlink(const CanonPath & path, const std::string & target)
|
|
{
|
|
auto p = append(dstPath, path);
|
|
nix::createSymlink(target, p.string());
|
|
}
|
|
|
|
|
|
void RegularFileSink::createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func)
|
|
{
|
|
struct CRF : CreateRegularFileSink {
|
|
RegularFileSink & back;
|
|
CRF(RegularFileSink & back) : back(back) {}
|
|
void operator () (std::string_view data) override
|
|
{
|
|
back.sink(data);
|
|
}
|
|
void isExecutable() override {}
|
|
} crf { *this };
|
|
func(crf);
|
|
}
|
|
|
|
|
|
void NullFileSystemObjectSink::createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func)
|
|
{
|
|
struct : CreateRegularFileSink {
|
|
void operator () (std::string_view data) override {}
|
|
void isExecutable() override {}
|
|
} crf;
|
|
// Even though `NullFileSystemObjectSink` doesn't do anything, it's important
|
|
// that we call the function, to e.g. advance the parser using this
|
|
// sink.
|
|
func(crf);
|
|
}
|
|
|
|
}
|