Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
490cb842cc | ||
|
|
6992698ac5 | ||
|
|
9868310d6f | ||
|
|
08ce8dbfba |
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
synopsis: "`builtins.getFlake` now supports path values"
|
||||||
|
prs: [15290]
|
||||||
|
---
|
||||||
|
|
||||||
|
`builtins.getFlake` now accepts path values in addition to flakerefs, allowing you to write `builtins.getFlake ./subflake` instead of having to use ugly workarounds to construct a pure flakeref.
|
||||||
@@ -35,35 +35,39 @@ namespace nix::flake::primops {
|
|||||||
PrimOp getFlake(const Settings & settings)
|
PrimOp getFlake(const Settings & settings)
|
||||||
{
|
{
|
||||||
auto prim_getFlake = [&settings](EvalState & state, const PosIdx pos, Value ** args, Value & v) {
|
auto prim_getFlake = [&settings](EvalState & state, const PosIdx pos, Value ** args, Value & v) {
|
||||||
std::string flakeRefS(
|
state.forceValue(*args[0], pos);
|
||||||
state.forceStringNoCtx(*args[0], pos, "while evaluating the argument passed to builtins.getFlake"));
|
|
||||||
auto flakeRef = nix::parseFlakeRef(state.fetchSettings, flakeRefS, {}, true);
|
|
||||||
if (state.settings.pureEval && !flakeRef.input.isLocked(state.fetchSettings))
|
|
||||||
throw Error(
|
|
||||||
"cannot call 'getFlake' on unlocked flake reference '%s', at %s (use --impure to override)",
|
|
||||||
flakeRefS,
|
|
||||||
state.positions[pos]);
|
|
||||||
|
|
||||||
callFlake(
|
LockFlags lockFlags{
|
||||||
state,
|
.updateLockFile = false,
|
||||||
lockFlake(
|
.writeLockFile = false,
|
||||||
settings,
|
.useRegistries = !state.settings.pureEval && settings.useRegistries,
|
||||||
state,
|
.allowUnlocked = !state.settings.pureEval,
|
||||||
flakeRef,
|
};
|
||||||
LockFlags{
|
|
||||||
.updateLockFile = false,
|
if (args[0]->type() == nPath) {
|
||||||
.writeLockFile = false,
|
auto path = state.realisePath(pos, *args[0]);
|
||||||
.useRegistries = !state.settings.pureEval && settings.useRegistries,
|
callFlake(state, lockFlake(settings, state, path, lockFlags), v);
|
||||||
.allowUnlocked = !state.settings.pureEval,
|
} else {
|
||||||
}),
|
NixStringContext context;
|
||||||
v);
|
std::string flakeRefS(
|
||||||
|
state.forceStringNoCtx(*args[0], pos, "while evaluating the argument passed to builtins.getFlake"));
|
||||||
|
|
||||||
|
auto flakeRef = nix::parseFlakeRef(state.fetchSettings, flakeRefS, {}, true);
|
||||||
|
if (state.settings.pureEval && !flakeRef.input.isLocked(state.fetchSettings))
|
||||||
|
throw Error(
|
||||||
|
"cannot call 'getFlake' on unlocked flake reference '%s', at %s (use --impure to override)",
|
||||||
|
flakeRefS,
|
||||||
|
state.positions[pos]);
|
||||||
|
|
||||||
|
callFlake(state, lockFlake(settings, state, flakeRef, lockFlags), v);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return PrimOp{
|
return PrimOp{
|
||||||
.name = "__getFlake",
|
.name = "__getFlake",
|
||||||
.args = {"args"},
|
.args = {"args"},
|
||||||
.doc = R"(
|
.doc = R"(
|
||||||
Fetch a flake from a flake reference, and return its output attributes and some metadata. For example:
|
Fetch a flake from a flake reference or a path, and return its output attributes and some metadata. For example:
|
||||||
|
|
||||||
```nix
|
```nix
|
||||||
(builtins.getFlake "nix/55bc52401966fbffa525c574c14f67b00bc4fb3a").packages.x86_64-linux.nix
|
(builtins.getFlake "nix/55bc52401966fbffa525c574c14f67b00bc4fb3a").packages.x86_64-linux.nix
|
||||||
|
|||||||
+18
-6
@@ -416,10 +416,8 @@ static LockFile readLockFile(const fetchers::Settings & fetchSettings, const Sou
|
|||||||
: LockFile();
|
: LockFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Compute an in-memory lock file for the specified top-level flake,
|
LockedFlake lockFlake(
|
||||||
and optionally write it to file, if the flake is writable. */
|
const Settings & settings, EvalState & state, const FlakeRef & topRef, const LockFlags & lockFlags, Flake flake)
|
||||||
LockedFlake
|
|
||||||
lockFlake(const Settings & settings, EvalState & state, const FlakeRef & topRef, const LockFlags & lockFlags)
|
|
||||||
{
|
{
|
||||||
experimentalFeatureSettings.require(Xp::Flakes);
|
experimentalFeatureSettings.require(Xp::Flakes);
|
||||||
|
|
||||||
@@ -427,8 +425,6 @@ lockFlake(const Settings & settings, EvalState & state, const FlakeRef & topRef,
|
|||||||
auto useRegistriesTop = useRegistries ? fetchers::UseRegistries::All : fetchers::UseRegistries::No;
|
auto useRegistriesTop = useRegistries ? fetchers::UseRegistries::All : fetchers::UseRegistries::No;
|
||||||
auto useRegistriesInputs = useRegistries ? fetchers::UseRegistries::Limited : fetchers::UseRegistries::No;
|
auto useRegistriesInputs = useRegistries ? fetchers::UseRegistries::Limited : fetchers::UseRegistries::No;
|
||||||
|
|
||||||
auto flake = getFlake(state, topRef, useRegistriesTop, {});
|
|
||||||
|
|
||||||
if (lockFlags.applyNixConfig) {
|
if (lockFlags.applyNixConfig) {
|
||||||
flake.config.apply(settings);
|
flake.config.apply(settings);
|
||||||
state.store->setOptions();
|
state.store->setOptions();
|
||||||
@@ -908,6 +904,22 @@ lockFlake(const Settings & settings, EvalState & state, const FlakeRef & topRef,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LockedFlake
|
||||||
|
lockFlake(const Settings & settings, EvalState & state, const FlakeRef & topRef, const LockFlags & lockFlags)
|
||||||
|
{
|
||||||
|
auto useRegistries = lockFlags.useRegistries.value_or(settings.useRegistries);
|
||||||
|
auto useRegistriesTop = useRegistries ? fetchers::UseRegistries::All : fetchers::UseRegistries::No;
|
||||||
|
return lockFlake(settings, state, topRef, lockFlags, getFlake(state, topRef, useRegistriesTop, {}));
|
||||||
|
}
|
||||||
|
|
||||||
|
LockedFlake
|
||||||
|
lockFlake(const Settings & settings, EvalState & state, const SourcePath & flakeDir, const LockFlags & lockFlags)
|
||||||
|
{
|
||||||
|
/* We need a fake flakeref to put in the `Flake` struct, but it's not used for anything. */
|
||||||
|
auto fakeRef = parseFlakeRef(state.fetchSettings, "flake:get-flake");
|
||||||
|
return lockFlake(settings, state, fakeRef, lockFlags, readFlake(state, fakeRef, fakeRef, fakeRef, flakeDir, {}));
|
||||||
|
}
|
||||||
|
|
||||||
static ref<SourceAccessor> makeInternalFS()
|
static ref<SourceAccessor> makeInternalFS()
|
||||||
{
|
{
|
||||||
auto internalFS = make_ref<MemorySourceAccessor>(MemorySourceAccessor{});
|
auto internalFS = make_ref<MemorySourceAccessor>(MemorySourceAccessor{});
|
||||||
|
|||||||
@@ -214,9 +214,16 @@ struct LockFlags
|
|||||||
std::set<NonEmptyInputAttrPath> inputUpdates;
|
std::set<NonEmptyInputAttrPath> inputUpdates;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compute an in-memory lock file for the specified top-level flake, and optionally write it to file, if the flake is
|
||||||
|
* writable.
|
||||||
|
*/
|
||||||
LockedFlake
|
LockedFlake
|
||||||
lockFlake(const Settings & settings, EvalState & state, const FlakeRef & flakeRef, const LockFlags & lockFlags);
|
lockFlake(const Settings & settings, EvalState & state, const FlakeRef & flakeRef, const LockFlags & lockFlags);
|
||||||
|
|
||||||
|
LockedFlake
|
||||||
|
lockFlake(const Settings & settings, EvalState & state, const SourcePath & flakeDir, const LockFlags & lockFlags);
|
||||||
|
|
||||||
void callFlake(EvalState & state, const LockedFlake & lockedFlake, Value & v);
|
void callFlake(EvalState & state, const LockedFlake & lockedFlake, Value & v);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "nix/util/archive.hh"
|
#include "nix/util/archive.hh"
|
||||||
#include "nix/util/posix-source-accessor.hh"
|
#include "nix/util/posix-source-accessor.hh"
|
||||||
#include "nix/util/posix-source-accessor.hh"
|
|
||||||
#include "nix/store/store-api.hh"
|
#include "nix/store/store-api.hh"
|
||||||
#include "nix/store/local-fs-store.hh"
|
#include "nix/store/local-fs-store.hh"
|
||||||
#include "nix/store/globals.hh"
|
#include "nix/store/globals.hh"
|
||||||
@@ -110,7 +109,7 @@ std::shared_ptr<SourceAccessor> LocalFSStore::getFSAccessor(const StorePath & pa
|
|||||||
if (!pathExists(absPath))
|
if (!pathExists(absPath))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return makeFSSourceAccessor(std::move(absPath));
|
return std::make_shared<PosixSourceAccessor>(std::move(absPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string LocalFSStore::drvsLogDir = "drvs";
|
const std::string LocalFSStore::drvsLogDir = "drvs";
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "nix/util/util.hh"
|
#include "nix/util/util.hh"
|
||||||
#include "nix/store/store-api.hh"
|
#include "nix/store/store-api.hh"
|
||||||
#include "nix/store/globals.hh"
|
#include "nix/store/globals.hh"
|
||||||
#include "nix/util/file-system-at.hh"
|
|
||||||
#include "store-config-private.hh"
|
#include "store-config-private.hh"
|
||||||
|
|
||||||
#if NIX_SUPPORT_ACL
|
#if NIX_SUPPORT_ACL
|
||||||
|
|||||||
@@ -758,7 +758,7 @@ std::optional<Descriptor> DerivationBuilderImpl::startBuild()
|
|||||||
|
|
||||||
/* The TOCTOU between the previous mkdir call and this open call is unavoidable due to
|
/* The TOCTOU between the previous mkdir call and this open call is unavoidable due to
|
||||||
POSIX semantics.*/
|
POSIX semantics.*/
|
||||||
tmpDirFd = openDirectory(tmpDir, /*followFinalSymlink=*/false);
|
tmpDirFd = AutoCloseFD{open(tmpDir.c_str(), O_RDONLY | O_NOFOLLOW | O_DIRECTORY)};
|
||||||
if (!tmpDirFd)
|
if (!tmpDirFd)
|
||||||
throw SysError("failed to open the build temporary directory descriptor %1%", PathFmt(tmpDir));
|
throw SysError("failed to open the build temporary directory descriptor %1%", PathFmt(tmpDir));
|
||||||
|
|
||||||
@@ -1269,7 +1269,7 @@ void DerivationBuilderImpl::writeBuilderFile(const std::string & name, std::stri
|
|||||||
openat(tmpDirFd.get(), name.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC | O_EXCL | O_NOFOLLOW, 0666)};
|
openat(tmpDirFd.get(), name.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC | O_EXCL | O_NOFOLLOW, 0666)};
|
||||||
if (!fd)
|
if (!fd)
|
||||||
throw SysError("creating file %s", PathFmt(path));
|
throw SysError("creating file %s", PathFmt(path));
|
||||||
writeFile(fd.get(), contents);
|
writeFile(fd, path, contents);
|
||||||
chownToBuilder(fd.get(), path);
|
chownToBuilder(fd.get(), path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include "nix/store/pathlocks.hh"
|
#include "nix/store/pathlocks.hh"
|
||||||
#include "nix/util/file-system-at.hh"
|
#include "nix/util/file-system.hh"
|
||||||
#include "nix/util/util.hh"
|
#include "nix/util/util.hh"
|
||||||
#include "nix/util/sync.hh"
|
#include "nix/util/sync.hh"
|
||||||
#include "nix/util/signals.hh"
|
#include "nix/util/signals.hh"
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
|
||||||
#include "nix/util/file-system-at.hh"
|
#include "nix/util/file-system-at.hh"
|
||||||
#include "nix/util/file-system.hh"
|
#include "nix/util/file-system.hh"
|
||||||
#include "nix/util/fs-sink.hh"
|
#include "nix/util/fs-sink.hh"
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------------
|
/* ----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -91,7 +91,9 @@ TEST_F(FSSourceAccessorTest, works)
|
|||||||
{
|
{
|
||||||
RestoreSink sink(false);
|
RestoreSink sink(false);
|
||||||
sink.dstPath = tmpDir;
|
sink.dstPath = tmpDir;
|
||||||
|
#ifndef _WIN32
|
||||||
sink.dirFd = openDirectory(tmpDir);
|
sink.dirFd = openDirectory(tmpDir);
|
||||||
|
#endif
|
||||||
sink.createDirectory(CanonPath("subdir"));
|
sink.createDirectory(CanonPath("subdir"));
|
||||||
sink.createRegularFile(CanonPath("file1"), [](CreateRegularFileSink & crf) { crf("content1"); });
|
sink.createRegularFile(CanonPath("file1"), [](CreateRegularFileSink & crf) { crf("content1"); });
|
||||||
sink.createRegularFile(CanonPath("subdir/file2"), [](CreateRegularFileSink & crf) { crf("content2"); });
|
sink.createRegularFile(CanonPath("subdir/file2"), [](CreateRegularFileSink & crf) { crf("content2"); });
|
||||||
@@ -135,34 +137,4 @@ TEST_F(FSSourceAccessorTest, works)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------------
|
|
||||||
* RestoreSink non-directory at root (no dirFd)
|
|
||||||
* --------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
TEST_F(FSSourceAccessorTest, RestoreSinkRegularFileAtRoot)
|
|
||||||
{
|
|
||||||
auto filePath = tmpDir / "rootfile";
|
|
||||||
{
|
|
||||||
RestoreSink sink(false);
|
|
||||||
sink.dstPath = filePath;
|
|
||||||
// No dirFd set - this tests the !dirFd path
|
|
||||||
sink.createRegularFile(CanonPath::root, [](CreateRegularFileSink & crf) { crf("root content"); });
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_THAT(makeFSSourceAccessor(filePath), HasContents(CanonPath::root, "root content"));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_F(FSSourceAccessorTest, RestoreSinkSymlinkAtRoot)
|
|
||||||
{
|
|
||||||
auto linkPath = tmpDir / "rootlink";
|
|
||||||
{
|
|
||||||
RestoreSink sink(false);
|
|
||||||
sink.dstPath = linkPath;
|
|
||||||
// No dirFd set - this tests the !dirFd path
|
|
||||||
sink.createSymlink(CanonPath::root, "symlink_target");
|
|
||||||
}
|
|
||||||
|
|
||||||
EXPECT_THAT(makeFSSourceAccessor(linkPath), HasSymlink(CanonPath::root, "symlink_target"));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -1,285 +0,0 @@
|
|||||||
// FIXME: detect in meson
|
|
||||||
#define HAVE_OPENAT2 1
|
|
||||||
#define HAVE_OPENAT 1
|
|
||||||
|
|
||||||
#include "util-config-private.hh"
|
|
||||||
#include "nix/util/signals.hh"
|
|
||||||
#include "nix/util/directory-source-accessor.hh"
|
|
||||||
#include "nix/util/posix-source-accessor.hh"
|
|
||||||
|
|
||||||
#if defined(__linux__) && HAVE_OPENAT2
|
|
||||||
# include <sys/syscall.h>
|
|
||||||
# include <linux/openat2.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <ranges>
|
|
||||||
|
|
||||||
namespace nix {
|
|
||||||
|
|
||||||
#if HAVE_OPENAT
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
class DirFdSourceAccessor : public SourceAccessor
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* File descriptor of the root directory.
|
|
||||||
*/
|
|
||||||
AutoCloseFD dirFd;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Path corresponding to the accessor.
|
|
||||||
* @warning Do not use for any file operations!
|
|
||||||
*/
|
|
||||||
std::filesystem::path root;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The most recent mtime seen by lstat(). This is a hack to
|
|
||||||
* support dumpPathAndGetMtime(). Should remove this eventually.
|
|
||||||
*/
|
|
||||||
time_t mtime = 0;
|
|
||||||
|
|
||||||
bool trackLastModified = false;
|
|
||||||
|
|
||||||
AutoCloseFD openFile(const CanonPath & path, int flags)
|
|
||||||
{
|
|
||||||
if (path.isRoot())
|
|
||||||
return ::openat(dirFd.get(), ".", flags);
|
|
||||||
|
|
||||||
# if defined(__linux__) && HAVE_OPENAT2
|
|
||||||
/* Cache the result of whether openat2 is not supported. */
|
|
||||||
static std::atomic_flag openat2Unsupported = ATOMIC_FLAG_INIT;
|
|
||||||
|
|
||||||
if (!openat2Unsupported.test()) {
|
|
||||||
/* No glibc wrapper yet, but there's a patch:
|
|
||||||
* https://patchwork.sourceware.org/project/glibc/patch/20251029200519.3203914-1-adhemerval.zanella@linaro.org/
|
|
||||||
*/
|
|
||||||
auto how = ::open_how{
|
|
||||||
.flags = static_cast<decltype(::open_how::flags)>(flags),
|
|
||||||
/* Symlinks are disallowed. RESOLVE_BENEATH is a bit overkill, since
|
|
||||||
CanonPath has the invariant of not having any `..` components, but
|
|
||||||
that's good practice anyway. */
|
|
||||||
.resolve = RESOLVE_NO_SYMLINKS | RESOLVE_BENEATH,
|
|
||||||
};
|
|
||||||
|
|
||||||
auto res = ::syscall(__NR_openat2, dirFd.get(), path.rel_c_str(), &how, sizeof(how));
|
|
||||||
if (res < 0 && errno == ENOSYS) {
|
|
||||||
/* Cache that the syscall is not supported and fall through to openat. */
|
|
||||||
openat2Unsupported.test_and_set();
|
|
||||||
} else {
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
# endif
|
|
||||||
|
|
||||||
AutoCloseFD parentFd;
|
|
||||||
auto nrComponents = std::ranges::distance(path);
|
|
||||||
auto components = std::views::take(path, nrComponents - 1); /* Everything but last component */
|
|
||||||
auto getParentFd = [&]() { return parentFd ? parentFd.get() : dirFd.get(); };
|
|
||||||
|
|
||||||
/* This rather convoluted loop is necessary to avoid TOCTOU when validating that
|
|
||||||
no inner path component is a symlink. */
|
|
||||||
for (auto it = components.begin(); it != components.end(); ++it) {
|
|
||||||
std::string_view component = *it;
|
|
||||||
parentFd = ::openat(
|
|
||||||
getParentFd(), /* First iteration uses dirFd. */
|
|
||||||
std::string(component).c_str(), /* Copy into a string to make NUL terminated. */
|
|
||||||
O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC |
|
|
||||||
# ifdef __linux__
|
|
||||||
O_PATH /* Linux-specific optimization. Files are open only for path resolution purposes. */
|
|
||||||
# endif
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!parentFd) {
|
|
||||||
/* Construct the CanonPath for error message. */
|
|
||||||
auto path2 = std::ranges::fold_left(components.begin(), ++it, CanonPath::root, [](auto lhs, auto rhs) {
|
|
||||||
lhs.push(rhs);
|
|
||||||
return lhs;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (errno == ELOOP)
|
|
||||||
/* Do not allow any simlinks in internal path components.
|
|
||||||
This is what is necessary to emulate it without RESOLVE_NO_SYMLINKS. */
|
|
||||||
throw Error("path '%s' is a symlink", showPath(path2));
|
|
||||||
|
|
||||||
return AutoCloseFD();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ::openat(getParentFd(), std::string(path.baseName().value()).c_str(), flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<struct ::stat> maybeLstatImpl(const CanonPath & path)
|
|
||||||
{
|
|
||||||
std::optional<struct ::stat> st{std::in_place};
|
|
||||||
|
|
||||||
if (path.isRoot()) {
|
|
||||||
if (::fstat(dirFd.get(), &*st)) /* Is already open. */
|
|
||||||
throw SysError("getting status of '%s'", showPath(path));
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto parentPath = path.parent().value();
|
|
||||||
auto fd = openFile(parentPath, O_PATH | O_NOFOLLOW | O_CLOEXEC | O_DIRECTORY);
|
|
||||||
if (!fd) {
|
|
||||||
if (errno == ENOENT || errno == ENOTDIR)
|
|
||||||
return std::nullopt;
|
|
||||||
throw SysError("opening parent path of '%s'", showPath(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Should have the same semantics as lstat on a path. */
|
|
||||||
if (::fstatat(fd.get(), std::string(path.baseName().value()).c_str(), &*st, AT_SYMLINK_NOFOLLOW)) {
|
|
||||||
if (errno == ENOENT || errno == ENOTDIR)
|
|
||||||
st.reset();
|
|
||||||
else
|
|
||||||
throw SysError("getting status of '%s'", showPath(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The contract is that trackLastModified implies that the caller uses the accessor
|
|
||||||
from a single thread. Thus this is not a CAS loop. */
|
|
||||||
if (trackLastModified)
|
|
||||||
mtime = std::max(mtime, st->st_mtime);
|
|
||||||
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
DirFdSourceAccessor(AutoCloseFD rootFd_, std::filesystem::path root_, bool trackLastModified)
|
|
||||||
: dirFd(std::move(rootFd_))
|
|
||||||
, root(std::move(root_))
|
|
||||||
, trackLastModified(trackLastModified)
|
|
||||||
{
|
|
||||||
if (root != root.root_path()) /* Don't prefix root directory. */
|
|
||||||
displayPrefix = root.string();
|
|
||||||
}
|
|
||||||
|
|
||||||
void readFile(const CanonPath & path, Sink & sink, std::function<void(uint64_t)> sizeCallback) override
|
|
||||||
{
|
|
||||||
auto fd = openFile(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
|
||||||
|
|
||||||
auto size = getFileSize(fd.get());
|
|
||||||
sizeCallback(size);
|
|
||||||
drainFD(fd.get(), sink, {.expectedSize = size});
|
|
||||||
}
|
|
||||||
|
|
||||||
bool pathExists(const CanonPath & path) override
|
|
||||||
{
|
|
||||||
return maybeLstatImpl(path).has_value();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<Stat> maybeLstat(const CanonPath & path) override
|
|
||||||
{
|
|
||||||
// TODO: Deduplicate with PosixSourceAccessor.
|
|
||||||
|
|
||||||
auto st = maybeLstatImpl(path);
|
|
||||||
if (!st)
|
|
||||||
return std::nullopt;
|
|
||||||
|
|
||||||
/* The contract is that trackLastModified implies that the caller uses the accessor
|
|
||||||
from a single thread. Thus this is not a CAS loop. */
|
|
||||||
if (trackLastModified)
|
|
||||||
mtime = std::max(mtime, st->st_mtime);
|
|
||||||
|
|
||||||
return PosixSourceAccessor::makeStat(*st);
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntries readDirectory(const CanonPath & path) override
|
|
||||||
{
|
|
||||||
auto fd = openFile(path, O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
|
||||||
if (!fd)
|
|
||||||
throw SysError("opening directory '%1%'", path);
|
|
||||||
|
|
||||||
auto dir = AutoCloseDir{::fdopendir(fd.get())};
|
|
||||||
if (!dir)
|
|
||||||
throw SysError("opening directory '%1%'", path);
|
|
||||||
|
|
||||||
fd.release();
|
|
||||||
|
|
||||||
DirEntries entries;
|
|
||||||
struct dirent * dirent;
|
|
||||||
|
|
||||||
while (errno = 0, dirent = ::readdir(dir.get())) {
|
|
||||||
checkInterrupt();
|
|
||||||
std::string name = dirent->d_name;
|
|
||||||
if (name == "." || name == "..")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
auto type = [&]() -> std::optional<Type> {
|
|
||||||
switch (dirent->d_type) {
|
|
||||||
case DT_REG:
|
|
||||||
return tRegular;
|
|
||||||
case DT_DIR:
|
|
||||||
return tDirectory;
|
|
||||||
case DT_LNK:
|
|
||||||
return tSymlink;
|
|
||||||
case DT_BLK:
|
|
||||||
return tBlock;
|
|
||||||
case DT_CHR:
|
|
||||||
return tChar;
|
|
||||||
case DT_FIFO:
|
|
||||||
return tFifo;
|
|
||||||
case DT_SOCK:
|
|
||||||
return tSocket;
|
|
||||||
case DT_UNKNOWN:
|
|
||||||
default:
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
}();
|
|
||||||
|
|
||||||
entries.emplace(std::move(name), type);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errno)
|
|
||||||
throw SysError("reading directory '%1%'", showPath(path));
|
|
||||||
|
|
||||||
return entries;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string readLink(const CanonPath & path) override
|
|
||||||
{
|
|
||||||
if (path.isRoot())
|
|
||||||
throw Error("file '%s' is not a symbolic link", path);
|
|
||||||
|
|
||||||
auto parentPath = path.parent().value();
|
|
||||||
auto basename = std::string(path.baseName().value());
|
|
||||||
|
|
||||||
auto parentFd = openFile(
|
|
||||||
parentPath,
|
|
||||||
O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC |
|
|
||||||
# ifdef __linux__
|
|
||||||
O_PATH
|
|
||||||
# endif
|
|
||||||
);
|
|
||||||
if (!parentFd)
|
|
||||||
throw SysError("opening path '%1%'", showPath(parentPath));
|
|
||||||
|
|
||||||
std::string target;
|
|
||||||
target.resize(PATH_MAX);
|
|
||||||
auto len = ::readlinkat(parentFd.get(), basename.c_str(), target.data(), target.size());
|
|
||||||
if (len < 0)
|
|
||||||
throw SysError("reading link '%1%'", showPath(path));
|
|
||||||
target.resize(len);
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<std::filesystem::path> getPhysicalPath(const CanonPath & path) override
|
|
||||||
{
|
|
||||||
return root / path.rel();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
ref<SourceAccessor> makeDirectorySourceAccessor(AutoCloseFD fd, std::filesystem::path root, bool trackLastModified)
|
|
||||||
{
|
|
||||||
#if HAVE_OPENAT
|
|
||||||
return make_ref<DirFdSourceAccessor>(std::move(fd), std::move(root), trackLastModified);
|
|
||||||
#else
|
|
||||||
return make_ref<PosixSourceAccessor>(std::move(root), trackLastModified);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nix
|
|
||||||
@@ -184,6 +184,24 @@ void AutoCloseFD::close()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AutoCloseFD::fsync() const
|
||||||
|
{
|
||||||
|
if (fd != INVALID_DESCRIPTOR) {
|
||||||
|
int result;
|
||||||
|
result =
|
||||||
|
#ifdef _WIN32
|
||||||
|
::FlushFileBuffers(fd)
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
::fcntl(fd, F_FULLFSYNC)
|
||||||
|
#else
|
||||||
|
::fsync(fd)
|
||||||
|
#endif
|
||||||
|
;
|
||||||
|
if (result == -1)
|
||||||
|
throw NativeSysError("fsync file descriptor %1%", fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void AutoCloseFD::startFsync() const
|
void AutoCloseFD::startFsync() const
|
||||||
{
|
{
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
#include "nix/util/file-system-at.hh"
|
|
||||||
|
|
||||||
namespace nix {
|
|
||||||
|
|
||||||
std::optional<PosixStat> maybeFstatat(Descriptor dirFd, const CanonPath & path)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return fstatat(dirFd, path);
|
|
||||||
} catch (SystemError & e) {
|
|
||||||
if (e.is(std::errc::no_such_file_or_directory) || e.is(std::errc::not_a_directory))
|
|
||||||
return std::nullopt;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nix
|
|
||||||
+51
-22
@@ -195,42 +195,71 @@ bool isDirOrInDir(const std::filesystem::path & path, const std::filesystem::pat
|
|||||||
return path == dir || isInDir(path, dir);
|
return path == dir || isInDir(path, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
# define STAT _wstat64
|
||||||
|
# define LSTAT _wstat64
|
||||||
|
#else
|
||||||
|
# define STAT stat
|
||||||
|
# define LSTAT lstat
|
||||||
|
#endif
|
||||||
|
|
||||||
PosixStat stat(const std::filesystem::path & path)
|
PosixStat stat(const std::filesystem::path & path)
|
||||||
|
{
|
||||||
|
PosixStat st;
|
||||||
|
if (STAT(path.c_str(), &st))
|
||||||
|
throw SysError("getting status of %s", PathFmt(path));
|
||||||
|
return st;
|
||||||
|
}
|
||||||
|
|
||||||
|
PosixStat lstat(const std::filesystem::path & path)
|
||||||
|
{
|
||||||
|
PosixStat st;
|
||||||
|
if (LSTAT(path.c_str(), &st))
|
||||||
|
throw SysError("getting status of %s", PathFmt(path));
|
||||||
|
return st;
|
||||||
|
}
|
||||||
|
|
||||||
|
PosixStat fstat(int fd)
|
||||||
{
|
{
|
||||||
PosixStat st;
|
PosixStat st;
|
||||||
if (
|
if (
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
_wstat64
|
_fstat64
|
||||||
#else
|
#else
|
||||||
::stat
|
::fstat
|
||||||
#endif
|
#endif
|
||||||
(path.c_str(), &st))
|
(fd, &st))
|
||||||
throw SysError("getting status of %s", PathFmt(path));
|
throw SysError("getting status of fd %d", fd);
|
||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<PosixStat> maybeStat(const std::filesystem::path & path)
|
std::optional<PosixStat> maybeStat(const std::filesystem::path & path)
|
||||||
{
|
{
|
||||||
try {
|
std::optional<PosixStat> st{std::in_place};
|
||||||
return stat(path);
|
if (STAT(path.c_str(), &*st)) {
|
||||||
} catch (SystemError & e) {
|
if (errno == ENOENT || errno == ENOTDIR)
|
||||||
if (e.is(std::errc::no_such_file_or_directory) || e.is(std::errc::not_a_directory))
|
st.reset();
|
||||||
return std::nullopt;
|
else
|
||||||
throw;
|
throw SysError("getting status of %s", PathFmt(path));
|
||||||
}
|
}
|
||||||
|
return st;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<PosixStat> maybeLstat(const std::filesystem::path & path)
|
std::optional<PosixStat> maybeLstat(const std::filesystem::path & path)
|
||||||
{
|
{
|
||||||
try {
|
std::optional<PosixStat> st{std::in_place};
|
||||||
return lstat(path);
|
if (LSTAT(path.c_str(), &*st)) {
|
||||||
} catch (SystemError & e) {
|
if (errno == ENOENT || errno == ENOTDIR)
|
||||||
if (e.is(std::errc::no_such_file_or_directory) || e.is(std::errc::not_a_directory))
|
st.reset();
|
||||||
return std::nullopt;
|
else
|
||||||
throw;
|
throw SysError("getting status of %s", PathFmt(path));
|
||||||
}
|
}
|
||||||
|
return st;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#undef STAT
|
||||||
|
#undef LSTAT
|
||||||
|
|
||||||
bool pathExists(const std::filesystem::path & path)
|
bool pathExists(const std::filesystem::path & path)
|
||||||
{
|
{
|
||||||
return maybeLstat(path.string()).has_value();
|
return maybeLstat(path.string()).has_value();
|
||||||
@@ -311,23 +340,23 @@ void writeFile(const Path & path, std::string_view s, mode_t mode, FsSync sync)
|
|||||||
if (!fd)
|
if (!fd)
|
||||||
throw SysError("opening file '%1%'", path);
|
throw SysError("opening file '%1%'", path);
|
||||||
|
|
||||||
writeFile(fd.get(), s, sync, &path);
|
writeFile(fd, path, s, mode, sync);
|
||||||
|
|
||||||
/* Close explicitly to propagate the exceptions. */
|
/* Close explicitly to propagate the exceptions. */
|
||||||
fd.close();
|
fd.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(Descriptor fd, std::string_view s, FsSync sync, const Path * origPath)
|
void writeFile(AutoCloseFD & fd, const Path & origPath, std::string_view s, mode_t mode, FsSync sync)
|
||||||
{
|
{
|
||||||
assert(fd != INVALID_DESCRIPTOR);
|
assert(fd);
|
||||||
try {
|
try {
|
||||||
writeFull(fd, s);
|
writeFull(fd.get(), s);
|
||||||
|
|
||||||
if (sync == FsSync::Yes)
|
if (sync == FsSync::Yes)
|
||||||
syncDescriptor(fd);
|
fd.fsync();
|
||||||
|
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
e.addTrace({}, "writing file '%1%'", origPath ? *origPath : descriptorToPath(fd).string());
|
e.addTrace({}, "writing file '%1%'", origPath);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+36
-28
@@ -105,26 +105,34 @@ void RestoreSink::createDirectory(const CanonPath & path)
|
|||||||
{
|
{
|
||||||
auto p = append(dstPath, path);
|
auto p = append(dstPath, path);
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
if (dirFd) {
|
if (dirFd) {
|
||||||
if (path.isRoot())
|
if (path.isRoot())
|
||||||
/* Trying to create a directory that we already have a file descriptor for. */
|
/* Trying to create a directory that we already have a file descriptor for. */
|
||||||
throw Error("path %s already exists", PathFmt(p));
|
throw Error("path %s already exists", PathFmt(p));
|
||||||
|
|
||||||
createDirectoryAt(dirFd.get(), path);
|
if (::mkdirat(dirFd.get(), path.rel_c_str(), 0777) == -1)
|
||||||
|
throw SysError("creating directory %s", PathFmt(p));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (!std::filesystem::create_directory(p))
|
if (!std::filesystem::create_directory(p))
|
||||||
throw Error("path '%s' already exists", p.string());
|
throw Error("path '%s' already exists", p.string());
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
if (path.isRoot()) {
|
if (path.isRoot()) {
|
||||||
assert(!dirFd); // Handled above
|
assert(!dirFd); // Handled above
|
||||||
|
|
||||||
/* Open directory for further *at operations relative to the sink root
|
/* Open directory for further *at operations relative to the sink root
|
||||||
directory. */
|
directory. */
|
||||||
dirFd = openDirectory(p, /*followFinalSymlink=*/false);
|
dirFd = open(p.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
|
||||||
|
if (!dirFd)
|
||||||
|
throw SysError("creating directory %1%", PathFmt(p));
|
||||||
}
|
}
|
||||||
}
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
struct RestoreRegularFile : CreateRegularFileSink, FdSink
|
struct RestoreRegularFile : CreateRegularFileSink, FdSink
|
||||||
{
|
{
|
||||||
@@ -156,26 +164,28 @@ void RestoreSink::createRegularFile(const CanonPath & path, std::function<void(C
|
|||||||
{
|
{
|
||||||
auto p = append(dstPath, path);
|
auto p = append(dstPath, path);
|
||||||
|
|
||||||
auto crf = RestoreRegularFile(startFsync, [&]() -> Descriptor {
|
auto crf = RestoreRegularFile(
|
||||||
AutoCloseFD parentFd;
|
startFsync,
|
||||||
if (!dirFd) {
|
|
||||||
assert(path.isRoot());
|
|
||||||
assert(p.has_parent_path());
|
|
||||||
parentFd = openDirectory(p.parent_path());
|
|
||||||
}
|
|
||||||
return openFileEnsureBeneathNoSymlinks(
|
|
||||||
dirFd ? dirFd.get() : parentFd.get(),
|
|
||||||
dirFd ? path : CanonPath::root / p.filename().string(),
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
CreateFileW(
|
||||||
|
p.c_str(),
|
||||||
GENERIC_READ | GENERIC_WRITE,
|
GENERIC_READ | GENERIC_WRITE,
|
||||||
FILE_NON_DIRECTORY_FILE,
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||||
FILE_CREATE
|
NULL,
|
||||||
|
CREATE_NEW,
|
||||||
|
FILE_ATTRIBUTE_NORMAL,
|
||||||
|
NULL)
|
||||||
#else
|
#else
|
||||||
O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC,
|
[&]() {
|
||||||
0666
|
/* O_EXCL together with O_CREAT ensures symbolic links in the last
|
||||||
|
component are not followed. */
|
||||||
|
constexpr int flags = O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC;
|
||||||
|
if (!dirFd)
|
||||||
|
return ::open(p.c_str(), flags, 0666);
|
||||||
|
return openFileEnsureBeneathNoSymlinks(dirFd.get(), path, flags, 0666);
|
||||||
|
}()
|
||||||
#endif
|
#endif
|
||||||
);
|
);
|
||||||
}());
|
|
||||||
if (!crf.fd)
|
if (!crf.fd)
|
||||||
throw NativeSysError("creating file %1%", PathFmt(p));
|
throw NativeSysError("creating file %1%", PathFmt(p));
|
||||||
func(crf);
|
func(crf);
|
||||||
@@ -214,16 +224,14 @@ void RestoreRegularFile::preallocateContents(uint64_t len)
|
|||||||
void RestoreSink::createSymlink(const CanonPath & path, const std::string & target)
|
void RestoreSink::createSymlink(const CanonPath & path, const std::string & target)
|
||||||
{
|
{
|
||||||
auto p = append(dstPath, path);
|
auto p = append(dstPath, path);
|
||||||
AutoCloseFD parentFd;
|
#ifndef _WIN32
|
||||||
if (!dirFd) {
|
if (dirFd) {
|
||||||
assert(path.isRoot());
|
if (::symlinkat(requireCString(target), dirFd.get(), path.rel_c_str()) == -1)
|
||||||
assert(p.has_parent_path());
|
throw SysError("creating symlink from %1% -> '%2%'", PathFmt(p), target);
|
||||||
parentFd = openDirectory(p.parent_path());
|
return;
|
||||||
}
|
}
|
||||||
createUnknownSymlinkAt(
|
#endif
|
||||||
dirFd ? dirFd.get() : parentFd.get(),
|
nix::createSymlink(target, p.string());
|
||||||
dirFd ? path : CanonPath::root / p.filename().string(),
|
|
||||||
string_to_os_string(target));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegularFileSink::createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func)
|
void RegularFileSink::createRegularFile(const CanonPath & path, std::function<void(CreateRegularFileSink &)> func)
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
/// @file
|
|
||||||
|
|
||||||
#include "nix/util/source-accessor.hh"
|
|
||||||
#include "nix/util/ref.hh"
|
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
|
|
||||||
namespace nix {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a directory accessor rooted at @param root.
|
|
||||||
*
|
|
||||||
* On unix accesses are performed with openat. Linux uses openat2 if supported
|
|
||||||
* by the kernel (>= 5.6).
|
|
||||||
*
|
|
||||||
* On Windows returns a PosixSourceAccessor.
|
|
||||||
*
|
|
||||||
* @param fd Descriptor of the directory.
|
|
||||||
* @param root Filesystem path corresponding to fd. Must correspond to a directory.
|
|
||||||
*/
|
|
||||||
ref<SourceAccessor>
|
|
||||||
makeDirectorySourceAccessor(AutoCloseFD fd, std::filesystem::path root, bool trackLastModified = false);
|
|
||||||
|
|
||||||
} // namespace nix
|
|
||||||
@@ -133,11 +133,6 @@ std::string readLine(Descriptor fd, bool eofOk = false, char terminator = '\n');
|
|||||||
*/
|
*/
|
||||||
void writeLine(Descriptor fd, std::string s);
|
void writeLine(Descriptor fd, std::string s);
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a blocking fsync operation on a file descriptor.
|
|
||||||
*/
|
|
||||||
void syncDescriptor(Descriptor fd);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Options for draining a file descriptor to a sink.
|
* Options for draining a file descriptor to a sink.
|
||||||
*/
|
*/
|
||||||
@@ -258,11 +253,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Perform a blocking fsync operation.
|
* Perform a blocking fsync operation.
|
||||||
*/
|
*/
|
||||||
void fsync() const
|
void fsync() const;
|
||||||
{
|
|
||||||
if (fd != INVALID_DESCRIPTOR)
|
|
||||||
nix::syncDescriptor(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asynchronously flush to disk without blocking, if available on
|
* Asynchronously flush to disk without blocking, if available on
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "nix/util/file-descriptor.hh"
|
#include "nix/util/file-descriptor.hh"
|
||||||
#include "nix/util/file-system.hh"
|
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
@@ -34,90 +33,6 @@ namespace nix {
|
|||||||
*/
|
*/
|
||||||
OsString readLinkAt(Descriptor dirFd, const CanonPath & path);
|
OsString readLinkAt(Descriptor dirFd, const CanonPath & path);
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a symlink to a file relative to a directory file descriptor.
|
|
||||||
*
|
|
||||||
* On Windows, creates a file symlink. On Unix, equivalent to symlinkat.
|
|
||||||
*
|
|
||||||
* @param dirFd Directory file descriptor
|
|
||||||
* @param path Relative path for the new symlink
|
|
||||||
* @param target The symlink target (what it points to)
|
|
||||||
*
|
|
||||||
* @throws SystemError on any I/O errors.
|
|
||||||
*/
|
|
||||||
void createFileSymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a symlink to a directory relative to a directory file descriptor.
|
|
||||||
*
|
|
||||||
* On Windows, creates a directory symlink. On Unix, equivalent to symlinkat.
|
|
||||||
*
|
|
||||||
* @param dirFd Directory file descriptor
|
|
||||||
* @param path Relative path for the new symlink
|
|
||||||
* @param target The symlink target (what it points to)
|
|
||||||
*
|
|
||||||
* @throws SystemError on any I/O errors.
|
|
||||||
*/
|
|
||||||
void createDirectorySymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a symlink relative to a directory file descriptor, detecting target type.
|
|
||||||
*
|
|
||||||
* On Windows, stats the target to determine whether to create a file or
|
|
||||||
* directory symlink. Falls back to file symlink if the target does not exist.
|
|
||||||
* On Unix, equivalent to symlinkat.
|
|
||||||
*
|
|
||||||
* @param dirFd Directory file descriptor
|
|
||||||
* @param path Relative path for the new symlink
|
|
||||||
* @param target The symlink target (what it points to)
|
|
||||||
*
|
|
||||||
* @throws SystemError on any I/O errors.
|
|
||||||
*/
|
|
||||||
void createUnknownSymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a directory relative to a directory file descriptor.
|
|
||||||
*
|
|
||||||
* @param dirFd Directory file descriptor
|
|
||||||
* @param path Relative path for the new directory
|
|
||||||
*
|
|
||||||
* @throws SystemError on any I/O errors.
|
|
||||||
*/
|
|
||||||
void createDirectoryAt(Descriptor dirFd, const CanonPath & path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get status of an open file/directory handle.
|
|
||||||
*
|
|
||||||
* @param fd File descriptor/handle
|
|
||||||
* @throws SystemError on I/O errors.
|
|
||||||
*/
|
|
||||||
PosixStat fstat(Descriptor fd);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get status of a file relative to a directory file descriptor.
|
|
||||||
*
|
|
||||||
* @param dirFd Directory file descriptor
|
|
||||||
* @param path Relative path to stat
|
|
||||||
*
|
|
||||||
* @return nullopt if the path does not exist.
|
|
||||||
* @throws SystemError on other I/O errors.
|
|
||||||
*
|
|
||||||
* @pre path.isRoot() is false
|
|
||||||
*/
|
|
||||||
std::optional<PosixStat> maybeFstatat(Descriptor dirFd, const CanonPath & path);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get status of a file relative to a directory file descriptor.
|
|
||||||
*
|
|
||||||
* @param dirFd Directory file descriptor
|
|
||||||
* @param path Relative path to stat
|
|
||||||
*
|
|
||||||
* @throws SystemError if the path does not exist or on other I/O errors.
|
|
||||||
*
|
|
||||||
* @pre path.isRoot() is false
|
|
||||||
*/
|
|
||||||
PosixStat fstatat(Descriptor dirFd, const CanonPath & path);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Safe(r) function to open a file relative to dirFd, while
|
* Safe(r) function to open a file relative to dirFd, while
|
||||||
* disallowing escaping from a directory and any symlinks in the process.
|
* disallowing escaping from a directory and any symlinks in the process.
|
||||||
|
|||||||
@@ -28,20 +28,11 @@
|
|||||||
* Polyfill for MinGW
|
* Polyfill for MinGW
|
||||||
*
|
*
|
||||||
* Windows does in fact support symlinks, but the C runtime interfaces predate this.
|
* Windows does in fact support symlinks, but the C runtime interfaces predate this.
|
||||||
* We define S_IFLNK and S_ISLNK so that our lstat implementation can properly
|
*
|
||||||
* indicate symlinks by setting these mode bits when it detects a reparse point.
|
* @todo get rid of this, and stop using `stat` when we want `lstat` too.
|
||||||
*/
|
*/
|
||||||
#ifndef S_IFLNK
|
|
||||||
# ifndef _WIN32
|
|
||||||
# error "S_IFLNK should be defined on non-Windows platforms"
|
|
||||||
# endif
|
|
||||||
# define S_IFLNK 0120000
|
|
||||||
#endif
|
|
||||||
#ifndef S_ISLNK
|
#ifndef S_ISLNK
|
||||||
# ifndef _WIN32
|
# define S_ISLNK(m) false
|
||||||
# error "S_ISLNK should be defined on non-Windows platforms"
|
|
||||||
# endif
|
|
||||||
# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
@@ -134,38 +125,6 @@ using PosixStat =
|
|||||||
#endif
|
#endif
|
||||||
;
|
;
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
namespace windows {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert Windows FILETIME to Unix time_t.
|
|
||||||
*/
|
|
||||||
time_t fileTimeToUnixTime(const FILETIME & ft);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fill a PosixStat structure from file attributes and timestamps.
|
|
||||||
*
|
|
||||||
* @param dwFileAttributes File attributes (FILE_ATTRIBUTE_*)
|
|
||||||
* @param ftCreationTime Creation time
|
|
||||||
* @param ftLastAccessTime Last access time
|
|
||||||
* @param ftLastWriteTime Last write time
|
|
||||||
* @param nFileSizeHigh High 32 bits of file size
|
|
||||||
* @param nFileSizeLow Low 32 bits of file size
|
|
||||||
* @param nNumberOfLinks Number of hard links (default 1)
|
|
||||||
*/
|
|
||||||
void statFromFileInfo(
|
|
||||||
PosixStat & st,
|
|
||||||
DWORD dwFileAttributes,
|
|
||||||
const FILETIME & ftCreationTime,
|
|
||||||
const FILETIME & ftLastAccessTime,
|
|
||||||
const FILETIME & ftLastWriteTime,
|
|
||||||
DWORD nFileSizeHigh,
|
|
||||||
DWORD nFileSizeLow,
|
|
||||||
DWORD nNumberOfLinks = 1);
|
|
||||||
|
|
||||||
} // namespace windows
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get status of `path`.
|
* Get status of `path`.
|
||||||
*/
|
*/
|
||||||
@@ -174,6 +133,10 @@ PosixStat lstat(const std::filesystem::path & path);
|
|||||||
* Get status of `path` following symlinks.
|
* Get status of `path` following symlinks.
|
||||||
*/
|
*/
|
||||||
PosixStat stat(const std::filesystem::path & path);
|
PosixStat stat(const std::filesystem::path & path);
|
||||||
|
/**
|
||||||
|
* Get status of an open file descriptor.
|
||||||
|
*/
|
||||||
|
PosixStat fstat(int fd);
|
||||||
/**
|
/**
|
||||||
* `lstat` the given path if it exists.
|
* `lstat` the given path if it exists.
|
||||||
* @return std::nullopt if the path doesn't exist, or an optional containing the result of `lstat` otherwise
|
* @return std::nullopt if the path doesn't exist, or an optional containing the result of `lstat` otherwise
|
||||||
@@ -242,10 +205,8 @@ std::filesystem::path descriptorToPath(Descriptor fd);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a `Descriptor` with read-only access to the given directory.
|
* Open a `Descriptor` with read-only access to the given directory.
|
||||||
*
|
|
||||||
* @param followSymlinks If false, fail if the path is a symlink.
|
|
||||||
*/
|
*/
|
||||||
Descriptor openDirectory(const std::filesystem::path & path, bool followFinalSymlink = true);
|
Descriptor openDirectory(const std::filesystem::path & path);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a `Descriptor` with read-only access to the given file.
|
* Open a `Descriptor` with read-only access to the given file.
|
||||||
@@ -305,7 +266,8 @@ writeFile(const std::filesystem::path & path, Source & source, mode_t mode = 066
|
|||||||
return writeFile(path.string(), source, mode, sync);
|
return writeFile(path.string(), source, mode, sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(Descriptor fd, std::string_view s, FsSync sync = FsSync::No, const Path * origPath = nullptr);
|
void writeFile(
|
||||||
|
AutoCloseFD & fd, const Path & origPath, std::string_view s, mode_t mode = 0666, FsSync sync = FsSync::No);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flush a path's parent directory to disk.
|
* Flush a path's parent directory to disk.
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ headers = [ config_pub_h ] + files(
|
|||||||
'configuration.hh',
|
'configuration.hh',
|
||||||
'current-process.hh',
|
'current-process.hh',
|
||||||
'demangle.hh',
|
'demangle.hh',
|
||||||
'directory-source-accessor.hh',
|
|
||||||
'english.hh',
|
'english.hh',
|
||||||
'environment-variables.hh',
|
'environment-variables.hh',
|
||||||
'error.hh',
|
'error.hh',
|
||||||
|
|||||||
@@ -82,8 +82,6 @@ public:
|
|||||||
|
|
||||||
void invalidateCache(const CanonPath & path) override;
|
void invalidateCache(const CanonPath & path) override;
|
||||||
|
|
||||||
static Stat makeStat(const struct ::stat st);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -139,7 +139,6 @@ sources = [ config_priv_h ] + files(
|
|||||||
'config-global.cc',
|
'config-global.cc',
|
||||||
'configuration.cc',
|
'configuration.cc',
|
||||||
'current-process.cc',
|
'current-process.cc',
|
||||||
'directory-source-accessor.cc',
|
|
||||||
'english.cc',
|
'english.cc',
|
||||||
'environment-variables.cc',
|
'environment-variables.cc',
|
||||||
'error.cc',
|
'error.cc',
|
||||||
@@ -148,7 +147,6 @@ sources = [ config_priv_h ] + files(
|
|||||||
'experimental-features.cc',
|
'experimental-features.cc',
|
||||||
'file-content-address.cc',
|
'file-content-address.cc',
|
||||||
'file-descriptor.cc',
|
'file-descriptor.cc',
|
||||||
'file-system-at.cc',
|
|
||||||
'file-system.cc',
|
'file-system.cc',
|
||||||
'fs-sink.cc',
|
'fs-sink.cc',
|
||||||
'git.cc',
|
'git.cc',
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "nix/util/posix-source-accessor.hh"
|
#include "nix/util/posix-source-accessor.hh"
|
||||||
#include "nix/util/directory-source-accessor.hh"
|
|
||||||
#include "nix/util/source-path.hh"
|
#include "nix/util/source-path.hh"
|
||||||
#include "nix/util/signals.hh"
|
#include "nix/util/signals.hh"
|
||||||
#include "nix/util/sync.hh"
|
#include "nix/util/sync.hh"
|
||||||
@@ -109,7 +108,22 @@ std::optional<SourceAccessor::Stat> PosixSourceAccessor::maybeLstat(const CanonP
|
|||||||
if (trackLastModified)
|
if (trackLastModified)
|
||||||
mtime = std::max(mtime, st->st_mtime);
|
mtime = std::max(mtime, st->st_mtime);
|
||||||
|
|
||||||
return makeStat(*st);
|
return Stat{
|
||||||
|
.type = S_ISREG(st->st_mode) ? tRegular
|
||||||
|
: S_ISDIR(st->st_mode) ? tDirectory
|
||||||
|
: S_ISLNK(st->st_mode) ? tSymlink
|
||||||
|
: S_ISCHR(st->st_mode) ? tChar
|
||||||
|
: S_ISBLK(st->st_mode) ? tBlock
|
||||||
|
:
|
||||||
|
#ifdef S_ISSOCK
|
||||||
|
S_ISSOCK(st->st_mode) ? tSocket
|
||||||
|
:
|
||||||
|
#endif
|
||||||
|
S_ISFIFO(st->st_mode) ? tFifo
|
||||||
|
: tUnknown,
|
||||||
|
.fileSize = S_ISREG(st->st_mode) ? std::optional<uint64_t>(st->st_size) : std::nullopt,
|
||||||
|
.isExecutable = S_ISREG(st->st_mode) && st->st_mode & S_IXUSR,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath & path)
|
SourceAccessor::DirEntries PosixSourceAccessor::readDirectory(const CanonPath & path)
|
||||||
@@ -186,26 +200,6 @@ void PosixSourceAccessor::assertNoSymlinks(CanonPath path)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PosixSourceAccessor::Stat PosixSourceAccessor::makeStat(const struct ::stat st)
|
|
||||||
{
|
|
||||||
return Stat{
|
|
||||||
.type = S_ISREG(st.st_mode) ? tRegular
|
|
||||||
: S_ISDIR(st.st_mode) ? tDirectory
|
|
||||||
: S_ISLNK(st.st_mode) ? tSymlink
|
|
||||||
: S_ISCHR(st.st_mode) ? tChar
|
|
||||||
: S_ISBLK(st.st_mode) ? tBlock
|
|
||||||
:
|
|
||||||
#ifdef S_ISSOCK
|
|
||||||
S_ISSOCK(st.st_mode) ? tSocket
|
|
||||||
:
|
|
||||||
#endif
|
|
||||||
S_ISFIFO(st.st_mode) ? tFifo
|
|
||||||
: tUnknown,
|
|
||||||
.fileSize = S_ISREG(st.st_mode) ? std::optional<uint64_t>(st.st_size) : std::nullopt,
|
|
||||||
.isExecutable = S_ISREG(st.st_mode) && st.st_mode & S_IXUSR,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
ref<SourceAccessor> getFSSourceAccessor()
|
ref<SourceAccessor> getFSSourceAccessor()
|
||||||
{
|
{
|
||||||
static auto rootFS = make_ref<PosixSourceAccessor>();
|
static auto rootFS = make_ref<PosixSourceAccessor>();
|
||||||
@@ -214,30 +208,6 @@ ref<SourceAccessor> getFSSourceAccessor()
|
|||||||
|
|
||||||
ref<SourceAccessor> makeFSSourceAccessor(std::filesystem::path root, bool trackLastModified)
|
ref<SourceAccessor> makeFSSourceAccessor(std::filesystem::path root, bool trackLastModified)
|
||||||
{
|
{
|
||||||
AutoCloseFD fd = toDescriptor(
|
|
||||||
::open(
|
|
||||||
root.c_str(),
|
|
||||||
O_RDONLY |
|
|
||||||
#ifndef _WIN32
|
|
||||||
O_NOFOLLOW | O_CLOEXEC
|
|
||||||
#endif
|
|
||||||
));
|
|
||||||
|
|
||||||
if (!fd) {
|
|
||||||
if (errno == ELOOP)
|
|
||||||
/* Use a plain old PosixSourceAccessor for symlinks. Should probably just
|
|
||||||
read the link here into a MemorySourceAccessor. */
|
|
||||||
return make_ref<PosixSourceAccessor>(std::move(root), trackLastModified);
|
|
||||||
throw SysError("opening file %s", PathFmt(root));
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ::stat st;
|
|
||||||
if (::fstat(fd.get(), &st))
|
|
||||||
throw SysError("getting status of %s", PathFmt(root));
|
|
||||||
|
|
||||||
if (S_ISDIR(st.st_mode))
|
|
||||||
return makeDirectorySourceAccessor(std::move(fd), std::move(root), trackLastModified);
|
|
||||||
|
|
||||||
return make_ref<PosixSourceAccessor>(std::move(root), trackLastModified);
|
return make_ref<PosixSourceAccessor>(std::move(root), trackLastModified);
|
||||||
}
|
}
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#include "nix/util/file-system.hh"
|
#include "nix/util/file-system.hh"
|
||||||
#include "nix/util/file-system-at.hh"
|
|
||||||
#include "nix/util/signals.hh"
|
#include "nix/util/signals.hh"
|
||||||
#include "nix/util/finally.hh"
|
#include "nix/util/finally.hh"
|
||||||
#include "nix/util/serialise.hh"
|
#include "nix/util/serialise.hh"
|
||||||
@@ -208,17 +207,4 @@ void unix::closeOnExec(int fd)
|
|||||||
throw SysError("setting close-on-exec flag");
|
throw SysError("setting close-on-exec flag");
|
||||||
}
|
}
|
||||||
|
|
||||||
void syncDescriptor(Descriptor fd)
|
|
||||||
{
|
|
||||||
int result =
|
|
||||||
#if defined(__APPLE__)
|
|
||||||
::fcntl(fd, F_FULLFSYNC)
|
|
||||||
#else
|
|
||||||
::fsync(fd)
|
|
||||||
#endif
|
|
||||||
;
|
|
||||||
if (result == -1)
|
|
||||||
throw NativeSysError("fsync file descriptor %1%", fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -23,8 +23,6 @@
|
|||||||
# define HAVE_FCHMODAT2 0
|
# define HAVE_FCHMODAT2 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "util-unix-config-private.hh"
|
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
@@ -92,9 +90,10 @@ void unix::fchmodatTryNoFollow(Descriptor dirFd, const CanonPath & path, mode_t
|
|||||||
PathFmt(descriptorToPath(dirFd) / path.rel()));
|
PathFmt(descriptorToPath(dirFd) / path.rel()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Possible to use with O_PATH fd since
|
struct ::stat st;
|
||||||
* https://github.com/torvalds/linux/commit/55815f70147dcfa3ead5738fd56d3574e2e3c1c2 (3.6) */
|
/* Possible since https://github.com/torvalds/linux/commit/55815f70147dcfa3ead5738fd56d3574e2e3c1c2 (3.6) */
|
||||||
auto st = fstat(pathFd.get());
|
if (::fstat(pathFd.get(), &st) == -1)
|
||||||
|
throw SysError("statting '%s' relative to parent directory via O_PATH file descriptor", path.rel());
|
||||||
|
|
||||||
if (S_ISLNK(st.st_mode))
|
if (S_ISLNK(st.st_mode))
|
||||||
throw SysError(EOPNOTSUPP, "can't change mode of symlink %s", PathFmt(descriptorToPath(dirFd) / path.rel()));
|
throw SysError(EOPNOTSUPP, "can't change mode of symlink %s", PathFmt(descriptorToPath(dirFd) / path.rel()));
|
||||||
@@ -172,7 +171,8 @@ openFileEnsureBeneathNoSymlinksIterative(Descriptor dirFd, const CanonPath & pat
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (errno == ENOTDIR) /* Path component might be a symlink. */ {
|
if (errno == ENOTDIR) /* Path component might be a symlink. */ {
|
||||||
if (auto st = maybeFstatat(getParentFd(), CanonPath(component)); st && S_ISLNK(st->st_mode))
|
struct ::stat st;
|
||||||
|
if (::fstatat(getParentFd(), component.c_str(), &st, AT_SYMLINK_NOFOLLOW) == 0 && S_ISLNK(st.st_mode))
|
||||||
throw SymlinkNotAllowed(path2);
|
throw SymlinkNotAllowed(path2);
|
||||||
errno = ENOTDIR; /* Restore the errno. */
|
errno = ENOTDIR; /* Restore the errno. */
|
||||||
} else if (errno == ELOOP) {
|
} else if (errno == ELOOP) {
|
||||||
@@ -224,61 +224,4 @@ OsString readLinkAt(Descriptor dirFd, const CanonPath & path)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void symlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
|
||||||
if (::symlinkat(target.c_str(), dirFd, path.rel_c_str()) == -1) {
|
|
||||||
auto savedErrno = errno;
|
|
||||||
throw SysError(
|
|
||||||
savedErrno, "creating symlink %1% -> %2%", PathFmt(descriptorToPath(dirFd) / path.rel()), target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void createFileSymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target)
|
|
||||||
{
|
|
||||||
symlinkAt(dirFd, path, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
void createDirectorySymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target)
|
|
||||||
{
|
|
||||||
symlinkAt(dirFd, path, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
void createUnknownSymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target)
|
|
||||||
{
|
|
||||||
symlinkAt(dirFd, path, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
void createDirectoryAt(Descriptor dirFd, const CanonPath & path)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
|
||||||
if (::mkdirat(dirFd, path.rel_c_str(), 0777) == -1) {
|
|
||||||
auto savedErrno = errno;
|
|
||||||
throw SysError(savedErrno, "creating directory %1%", PathFmt(descriptorToPath(dirFd) / path.rel()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
PosixStat fstat(Descriptor fd)
|
|
||||||
{
|
|
||||||
PosixStat st;
|
|
||||||
if (::fstat(fd, &st)) {
|
|
||||||
auto savedErrno = errno;
|
|
||||||
throw SysError(savedErrno, "getting status of %s", PathFmt(descriptorToPath(fd)));
|
|
||||||
}
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
PosixStat fstatat(Descriptor dirFd, const CanonPath & path)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
PosixStat st;
|
|
||||||
if (::fstatat(dirFd, path.rel_c_str(), &st, AT_SYMLINK_NOFOLLOW)) {
|
|
||||||
auto savedErrno = errno;
|
|
||||||
throw SysError(savedErrno, "getting status of %s", PathFmt(descriptorToPath(dirFd) / path.rel()));
|
|
||||||
}
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -23,9 +23,9 @@
|
|||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
Descriptor openDirectory(const std::filesystem::path & path, bool followFinalSymlink)
|
Descriptor openDirectory(const std::filesystem::path & path)
|
||||||
{
|
{
|
||||||
return open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | (followFinalSymlink ? 0 : O_NOFOLLOW));
|
return open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
Descriptor openFileReadonly(const std::filesystem::path & path)
|
Descriptor openFileReadonly(const std::filesystem::path & path)
|
||||||
@@ -76,14 +76,6 @@ std::filesystem::path defaultTempDir()
|
|||||||
return getEnvNonEmpty("TMPDIR").value_or("/tmp");
|
return getEnvNonEmpty("TMPDIR").value_or("/tmp");
|
||||||
}
|
}
|
||||||
|
|
||||||
PosixStat lstat(const std::filesystem::path & path)
|
|
||||||
{
|
|
||||||
PosixStat st;
|
|
||||||
if (::lstat(path.c_str(), &st))
|
|
||||||
throw SysError("getting status of %s", PathFmt(path));
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setWriteTime(
|
void setWriteTime(
|
||||||
const std::filesystem::path & path, time_t accessedTime, time_t modificationTime, std::optional<bool> optIsSymlink)
|
const std::filesystem::path & path, time_t accessedTime, time_t modificationTime, std::optional<bool> optIsSymlink)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -148,10 +148,4 @@ off_t lseek(HANDLE h, off_t offset, int whence)
|
|||||||
return newPos.QuadPart;
|
return newPos.QuadPart;
|
||||||
}
|
}
|
||||||
|
|
||||||
void syncDescriptor(Descriptor fd)
|
|
||||||
{
|
|
||||||
if (!::FlushFileBuffers(fd))
|
|
||||||
throw WinError("FlushFileBuffers file descriptor %1%", fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -4,9 +4,8 @@
|
|||||||
#include "nix/util/file-path.hh"
|
#include "nix/util/file-path.hh"
|
||||||
#include "nix/util/source-accessor.hh"
|
#include "nix/util/source-accessor.hh"
|
||||||
|
|
||||||
#include <span>
|
|
||||||
|
|
||||||
#include <fileapi.h>
|
#include <fileapi.h>
|
||||||
|
#include <error.h>
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <winioctl.h>
|
#include <winioctl.h>
|
||||||
@@ -89,8 +88,8 @@ HANDLE openSymlinkAt(Descriptor dirFd, const CanonPath & path)
|
|||||||
assert(!path.isRoot());
|
assert(!path.isRoot());
|
||||||
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
||||||
|
|
||||||
auto wpath = std::filesystem::path(path.rel()).make_preferred();
|
std::wstring wpath = string_to_os_string(path.rel());
|
||||||
return ntOpenAt(dirFd, wpath.c_str(), FILE_READ_ATTRIBUTES | SYNCHRONIZE, FILE_OPEN_REPARSE_POINT);
|
return ntOpenAt(dirFd, wpath, FILE_READ_ATTRIBUTES | SYNCHRONIZE, FILE_OPEN_REPARSE_POINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,54 +191,6 @@ OsString readSymlinkTarget(HANDLE linkHandle)
|
|||||||
return {target_start, target_len};
|
return {target_start, target_len};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Write symlink target to an open file handle using reparse point.
|
|
||||||
*
|
|
||||||
* @param handle Open file handle (must have GENERIC_WRITE access)
|
|
||||||
* @param target The symlink target (what it points to)
|
|
||||||
*/
|
|
||||||
void writeSymlinkTarget(HANDLE handle, const std::filesystem::path & target)
|
|
||||||
{
|
|
||||||
/* Build the reparse data buffer for a symbolic link.
|
|
||||||
Layout: SubstituteName and PrintName stored consecutively in PathBuffer.
|
|
||||||
We use the same string for both. */
|
|
||||||
size_t targetBytes = target.native().size() * sizeof(wchar_t);
|
|
||||||
size_t bufSize = offsetof(ReparseDataBuffer, SymbolicLinkReparseBuffer.PathBuffer) + targetBytes * 2;
|
|
||||||
std::vector<uint8_t> buf(bufSize, 0);
|
|
||||||
|
|
||||||
auto * reparse = reinterpret_cast<ReparseDataBuffer *>(buf.data());
|
|
||||||
reparse->ReparseTag = IO_REPARSE_TAG_SYMLINK;
|
|
||||||
reparse->ReparseDataLength =
|
|
||||||
static_cast<unsigned short>(bufSize - offsetof(ReparseDataBuffer, SymbolicLinkReparseBuffer));
|
|
||||||
reparse->Reserved = 0;
|
|
||||||
|
|
||||||
auto & symlink = reparse->SymbolicLinkReparseBuffer;
|
|
||||||
/* SubstituteName comes first */
|
|
||||||
symlink.SubstituteNameOffset = 0;
|
|
||||||
symlink.SubstituteNameLength = static_cast<unsigned short>(targetBytes);
|
|
||||||
/* PrintName follows SubstituteName */
|
|
||||||
symlink.PrintNameOffset = static_cast<unsigned short>(targetBytes);
|
|
||||||
symlink.PrintNameLength = static_cast<unsigned short>(targetBytes);
|
|
||||||
/* SYMLINK_FLAG_RELATIVE = 1 for relative symlinks, 0 for absolute */
|
|
||||||
symlink.Flags = target.is_relative() ? 1 : 0;
|
|
||||||
|
|
||||||
/* Copy target into PathBuffer twice (SubstituteName and PrintName) */
|
|
||||||
memcpy(symlink.PathBuffer, target.c_str(), targetBytes);
|
|
||||||
memcpy(reinterpret_cast<char *>(symlink.PathBuffer) + targetBytes, target.c_str(), targetBytes);
|
|
||||||
|
|
||||||
DWORD bytesReturned;
|
|
||||||
if (!DeviceIoControl(
|
|
||||||
handle,
|
|
||||||
FSCTL_SET_REPARSE_POINT,
|
|
||||||
buf.data(),
|
|
||||||
static_cast<DWORD>(bufSize),
|
|
||||||
nullptr,
|
|
||||||
0,
|
|
||||||
&bytesReturned,
|
|
||||||
nullptr))
|
|
||||||
throw WinError("setting reparse point for symlink");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a handle refers to a reparse point (e.g., symlink).
|
* Check if a handle refers to a reparse point (e.g., symlink).
|
||||||
*
|
*
|
||||||
@@ -259,115 +210,6 @@ bool isReparsePoint(HANDLE handle)
|
|||||||
|
|
||||||
} // namespace windows
|
} // namespace windows
|
||||||
|
|
||||||
OsString readLinkAt(Descriptor dirFd, const CanonPath & path)
|
|
||||||
{
|
|
||||||
AutoCloseFD linkHandle(openSymlinkAt(dirFd, path));
|
|
||||||
return readSymlinkTarget(linkHandle.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
void createFileSymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
|
||||||
|
|
||||||
auto wpath = std::filesystem::path(path.rel()).make_preferred();
|
|
||||||
|
|
||||||
/* Create the file that will become the symlink */
|
|
||||||
AutoCloseFD handle(ntOpenAt(
|
|
||||||
dirFd, wpath.c_str(), GENERIC_WRITE | DELETE, FILE_NON_DIRECTORY_FILE | FILE_OPEN_REPARSE_POINT, FILE_CREATE));
|
|
||||||
|
|
||||||
writeSymlinkTarget(handle.get(), target);
|
|
||||||
}
|
|
||||||
|
|
||||||
void createDirectorySymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
|
||||||
|
|
||||||
auto wpath = std::filesystem::path(path.rel()).make_preferred();
|
|
||||||
|
|
||||||
/* Create the directory that will become the symlink */
|
|
||||||
AutoCloseFD handle(ntOpenAt(
|
|
||||||
dirFd, wpath.c_str(), GENERIC_WRITE | DELETE, FILE_DIRECTORY_FILE | FILE_OPEN_REPARSE_POINT, FILE_CREATE));
|
|
||||||
|
|
||||||
writeSymlinkTarget(handle.get(), target);
|
|
||||||
}
|
|
||||||
|
|
||||||
void createUnknownSymlinkAt(Descriptor dirFd, const CanonPath & path, const OsString & target)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
|
||||||
|
|
||||||
/* Resolve target path to determine if it's a directory.
|
|
||||||
Target is relative to the symlink's parent directory. */
|
|
||||||
std::filesystem::path targetPath(target);
|
|
||||||
std::filesystem::path resolvedTarget;
|
|
||||||
|
|
||||||
if (targetPath.is_absolute()) {
|
|
||||||
resolvedTarget = targetPath;
|
|
||||||
} else {
|
|
||||||
/* Get the symlink's parent directory path and resolve target relative to it */
|
|
||||||
auto symlinkDir = descriptorToPath(dirFd);
|
|
||||||
if (auto parent = path.parent())
|
|
||||||
symlinkDir /= parent->rel();
|
|
||||||
resolvedTarget = symlinkDir / targetPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check if target is a directory. If we can't stat it (e.g., dangling symlink),
|
|
||||||
fall back to file symlink. */
|
|
||||||
std::error_code ec;
|
|
||||||
auto status = std::filesystem::status(resolvedTarget, ec);
|
|
||||||
bool isDirectory = !ec && std::filesystem::is_directory(status);
|
|
||||||
|
|
||||||
if (isDirectory)
|
|
||||||
createDirectorySymlinkAt(dirFd, path, target);
|
|
||||||
else
|
|
||||||
createFileSymlinkAt(dirFd, path, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
void createDirectoryAt(Descriptor dirFd, const CanonPath & path)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
assert(!path.rel().starts_with('/')); /* Just in case the invariant is somehow broken. */
|
|
||||||
|
|
||||||
auto wpath = std::filesystem::path(path.rel()).make_preferred();
|
|
||||||
|
|
||||||
/* Create the directory. The handle is immediately closed. */
|
|
||||||
AutoCloseFD handle(ntOpenAt(dirFd, wpath.c_str(), FILE_TRAVERSE | SYNCHRONIZE, FILE_DIRECTORY_FILE, FILE_CREATE));
|
|
||||||
}
|
|
||||||
|
|
||||||
PosixStat fstat(Descriptor fd)
|
|
||||||
{
|
|
||||||
BY_HANDLE_FILE_INFORMATION info;
|
|
||||||
if (!GetFileInformationByHandle(fd, &info))
|
|
||||||
throw WinError("getting file information for %s", PathFmt(descriptorToPath(fd)));
|
|
||||||
|
|
||||||
PosixStat st;
|
|
||||||
windows::statFromFileInfo(
|
|
||||||
st,
|
|
||||||
info.dwFileAttributes,
|
|
||||||
info.ftCreationTime,
|
|
||||||
info.ftLastAccessTime,
|
|
||||||
info.ftLastWriteTime,
|
|
||||||
info.nFileSizeHigh,
|
|
||||||
info.nFileSizeLow,
|
|
||||||
info.nNumberOfLinks);
|
|
||||||
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
PosixStat fstatat(Descriptor dirFd, const CanonPath & path)
|
|
||||||
{
|
|
||||||
assert(!path.isRoot());
|
|
||||||
|
|
||||||
auto wpath = std::filesystem::path(path.rel()).make_preferred();
|
|
||||||
|
|
||||||
/* Open the file without following symlinks */
|
|
||||||
AutoCloseFD handle(ntOpenAt(dirFd, wpath.c_str(), FILE_READ_ATTRIBUTES | SYNCHRONIZE, FILE_OPEN_REPARSE_POINT));
|
|
||||||
|
|
||||||
return fstat(handle.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
Descriptor openFileEnsureBeneathNoSymlinks(
|
Descriptor openFileEnsureBeneathNoSymlinks(
|
||||||
Descriptor dirFd, const CanonPath & path, ACCESS_MASK desiredAccess, ULONG createOptions, ULONG createDisposition)
|
Descriptor dirFd, const CanonPath & path, ACCESS_MASK desiredAccess, ULONG createOptions, ULONG createDisposition)
|
||||||
{
|
{
|
||||||
@@ -459,4 +301,10 @@ Descriptor openFileEnsureBeneathNoSymlinks(
|
|||||||
return finalHandle;
|
return finalHandle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OsString readLinkAt(Descriptor dirFd, const CanonPath & path)
|
||||||
|
{
|
||||||
|
AutoCloseFD linkHandle(windows::openSymlinkAt(dirFd, path));
|
||||||
|
return windows::readSymlinkTarget(linkHandle.get());
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -23,7 +23,7 @@ void setWriteTime(
|
|||||||
warn("Changing file times is not yet implemented on Windows, path is %s", PathFmt(path));
|
warn("Changing file times is not yet implemented on Windows, path is %s", PathFmt(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
Descriptor openDirectory(const std::filesystem::path & path, bool followFinalSymlink)
|
Descriptor openDirectory(const std::filesystem::path & path)
|
||||||
{
|
{
|
||||||
return CreateFileW(
|
return CreateFileW(
|
||||||
path.c_str(),
|
path.c_str(),
|
||||||
@@ -31,7 +31,7 @@ Descriptor openDirectory(const std::filesystem::path & path, bool followFinalSym
|
|||||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||||
/*lpSecurityAttributes=*/nullptr,
|
/*lpSecurityAttributes=*/nullptr,
|
||||||
OPEN_EXISTING,
|
OPEN_EXISTING,
|
||||||
FILE_FLAG_BACKUP_SEMANTICS | (followFinalSymlink ? 0 : FILE_FLAG_OPEN_REPARSE_POINT),
|
FILE_FLAG_BACKUP_SEMANTICS,
|
||||||
/*hTemplateFile=*/nullptr);
|
/*hTemplateFile=*/nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,67 +105,4 @@ std::filesystem::path descriptorToPath(Descriptor handle)
|
|||||||
return std::filesystem::path{std::wstring{buf.data(), dw}};
|
return std::filesystem::path{std::wstring{buf.data(), dw}};
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t windows::fileTimeToUnixTime(const FILETIME & ft)
|
|
||||||
{
|
|
||||||
/* FILETIME is 100-nanosecond intervals since January 1, 1601 UTC.
|
|
||||||
Unix time is seconds since January 1, 1970 UTC.
|
|
||||||
Difference is 11644473600 seconds. */
|
|
||||||
ULARGE_INTEGER ull;
|
|
||||||
ull.LowPart = ft.dwLowDateTime;
|
|
||||||
ull.HighPart = ft.dwHighDateTime;
|
|
||||||
return static_cast<time_t>(ull.QuadPart / 10000000ULL - 11644473600ULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void windows::statFromFileInfo(
|
|
||||||
PosixStat & st,
|
|
||||||
DWORD dwFileAttributes,
|
|
||||||
const FILETIME & ftCreationTime,
|
|
||||||
const FILETIME & ftLastAccessTime,
|
|
||||||
const FILETIME & ftLastWriteTime,
|
|
||||||
DWORD nFileSizeHigh,
|
|
||||||
DWORD nFileSizeLow,
|
|
||||||
DWORD nNumberOfLinks)
|
|
||||||
{
|
|
||||||
memset(&st, 0, sizeof(st));
|
|
||||||
|
|
||||||
/* Determine file type */
|
|
||||||
if (dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
|
|
||||||
st.st_mode = S_IFLNK | 0777;
|
|
||||||
} else if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
||||||
st.st_mode = S_IFDIR | 0755;
|
|
||||||
} else {
|
|
||||||
st.st_mode = S_IFREG | 0644;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* File size (only meaningful for regular files) */
|
|
||||||
st.st_size = (static_cast<int64_t>(nFileSizeHigh) << 32) | nFileSizeLow;
|
|
||||||
|
|
||||||
/* Timestamps */
|
|
||||||
st.st_atime = fileTimeToUnixTime(ftLastAccessTime);
|
|
||||||
st.st_mtime = fileTimeToUnixTime(ftLastWriteTime);
|
|
||||||
st.st_ctime = fileTimeToUnixTime(ftCreationTime);
|
|
||||||
|
|
||||||
st.st_nlink = nNumberOfLinks;
|
|
||||||
st.st_uid = 0;
|
|
||||||
st.st_gid = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
PosixStat lstat(const std::filesystem::path & path)
|
|
||||||
{
|
|
||||||
WIN32_FILE_ATTRIBUTE_DATA attrData;
|
|
||||||
if (!GetFileAttributesExW(path.c_str(), GetFileExInfoStandard, &attrData))
|
|
||||||
throw WinError("getting status of %s", PathFmt(path));
|
|
||||||
|
|
||||||
PosixStat st;
|
|
||||||
windows::statFromFileInfo(
|
|
||||||
st,
|
|
||||||
attrData.dwFileAttributes,
|
|
||||||
attrData.ftCreationTime,
|
|
||||||
attrData.ftLastAccessTime,
|
|
||||||
attrData.ftLastWriteTime,
|
|
||||||
attrData.nFileSizeHigh,
|
|
||||||
attrData.nFileSizeLow);
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -32,6 +32,8 @@ writeSimpleFlake() {
|
|||||||
baseName = builtins.baseNameOf ./.;
|
baseName = builtins.baseNameOf ./.;
|
||||||
|
|
||||||
root = ./.;
|
root = ./.;
|
||||||
|
|
||||||
|
number = 123;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
source ./common.sh
|
||||||
|
|
||||||
|
createFlake1
|
||||||
|
|
||||||
|
mkdir -p "$flake1Dir/subflake"
|
||||||
|
cat > "$flake1Dir/subflake/flake.nix" <<EOF
|
||||||
|
{
|
||||||
|
outputs = { self }:
|
||||||
|
let
|
||||||
|
# Bad, legacy way of getting a flake from an input.
|
||||||
|
parentFlake = builtins.getFlake (builtins.flakeRefToString { type = "path"; path = self.sourceInfo.outPath; narHash = self.narHash; });
|
||||||
|
# Better way using a path value.
|
||||||
|
parentFlake2 = builtins.getFlake ./..;
|
||||||
|
in {
|
||||||
|
x = parentFlake.number;
|
||||||
|
y = parentFlake2.number;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
git -C "$flake1Dir" add subflake/flake.nix
|
||||||
|
|
||||||
|
[[ $(nix eval "$flake1Dir/subflake#x") = 123 ]]
|
||||||
|
|
||||||
|
[[ $(nix eval "$flake1Dir/subflake#y") = 123 ]]
|
||||||
@@ -34,6 +34,7 @@ suites += {
|
|||||||
'source-paths.sh',
|
'source-paths.sh',
|
||||||
'old-lockfiles.sh',
|
'old-lockfiles.sh',
|
||||||
'trace-ifd.sh',
|
'trace-ifd.sh',
|
||||||
|
'get-flake.sh',
|
||||||
],
|
],
|
||||||
'workdir' : meson.current_source_dir(),
|
'workdir' : meson.current_source_dir(),
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user