2023-09-08 05:49:01 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "config.hh"
|
|
|
|
#include "fs-sink.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
2023-09-08 05:49:01 +03:00
|
|
|
void RestoreSink::createDirectory(const Path & path)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
|
|
|
if (mkdir(p.c_str(), 0777) == -1)
|
|
|
|
throw SysError("creating directory '%1%'", p);
|
|
|
|
};
|
2023-09-08 05:49:01 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
struct RestoreRegularFile : CreateRegularFileSink {
|
|
|
|
AutoCloseFD fd;
|
|
|
|
|
|
|
|
void operator () (std::string_view data) override;
|
|
|
|
void isExecutable() override;
|
|
|
|
void preallocateContents(uint64_t size) override;
|
|
|
|
};
|
2023-09-08 05:49:01 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
void RestoreSink::createRegularFile(const Path & path, std::function<void(CreateRegularFileSink &)> func)
|
2023-09-08 05:49:01 +03:00
|
|
|
{
|
2023-12-20 21:47:05 +02:00
|
|
|
Path p = dstPath + path;
|
|
|
|
RestoreRegularFile crf;
|
|
|
|
crf.fd = open(p.c_str(), O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC, 0666);
|
|
|
|
if (!crf.fd) throw SysError("creating file '%1%'", p);
|
|
|
|
func(crf);
|
2023-09-08 05:49:01 +03:00
|
|
|
}
|
2023-09-08 05:49:01 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
void RestoreRegularFile::isExecutable()
|
2023-09-08 05:49:01 +03:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
2023-09-08 05:49:01 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
void RestoreRegularFile::preallocateContents(uint64_t len)
|
2023-09-08 05:49:01 +03:00
|
|
|
{
|
|
|
|
if (!restoreSinkSettings.preallocateContents)
|
|
|
|
return;
|
2023-09-08 05:49:01 +03:00
|
|
|
|
|
|
|
#if HAVE_POSIX_FALLOCATE
|
2023-09-08 05:49:01 +03:00
|
|
|
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);
|
2023-09-08 05:49:01 +03:00
|
|
|
}
|
2023-09-08 05:49:01 +03:00
|
|
|
#endif
|
|
|
|
}
|
2023-09-08 05:49:01 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
void RestoreRegularFile::operator () (std::string_view data)
|
2023-09-08 05:49:01 +03:00
|
|
|
{
|
|
|
|
writeFull(fd.get(), data);
|
|
|
|
}
|
2023-09-08 05:49:01 +03:00
|
|
|
|
2023-09-08 05:49:01 +03:00
|
|
|
void RestoreSink::createSymlink(const Path & path, const std::string & target)
|
|
|
|
{
|
|
|
|
Path p = dstPath + path;
|
|
|
|
nix::createSymlink(target, p);
|
|
|
|
}
|
2023-09-08 05:49:01 +03:00
|
|
|
|
2023-12-20 21:47:05 +02:00
|
|
|
|
|
|
|
void RegularFileSink::createRegularFile(const Path & 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 Path & 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);
|
|
|
|
}
|
|
|
|
|
2023-09-08 05:49:01 +03:00
|
|
|
}
|