Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f3de0f3e5 |
No files matched your search
@@ -1,6 +0,0 @@
|
|||||||
---
|
|
||||||
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,39 +35,35 @@ 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) {
|
||||||
state.forceValue(*args[0], pos);
|
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]);
|
||||||
|
|
||||||
LockFlags lockFlags{
|
callFlake(
|
||||||
.updateLockFile = false,
|
state,
|
||||||
.writeLockFile = false,
|
lockFlake(
|
||||||
.useRegistries = !state.settings.pureEval && settings.useRegistries,
|
settings,
|
||||||
.allowUnlocked = !state.settings.pureEval,
|
state,
|
||||||
};
|
flakeRef,
|
||||||
|
LockFlags{
|
||||||
if (args[0]->type() == nPath) {
|
.updateLockFile = false,
|
||||||
auto path = state.realisePath(pos, *args[0]);
|
.writeLockFile = false,
|
||||||
callFlake(state, lockFlake(settings, state, path, lockFlags), v);
|
.useRegistries = !state.settings.pureEval && settings.useRegistries,
|
||||||
} else {
|
.allowUnlocked = !state.settings.pureEval,
|
||||||
NixStringContext context;
|
}),
|
||||||
std::string flakeRefS(
|
v);
|
||||||
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 or a path, and return its output attributes and some metadata. For example:
|
Fetch a flake from a flake reference, 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
|
||||||
|
|||||||
+6
-18
@@ -416,8 +416,10 @@ static LockFile readLockFile(const fetchers::Settings & fetchSettings, const Sou
|
|||||||
: LockFile();
|
: LockFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
LockedFlake lockFlake(
|
/* Compute an in-memory lock file for the specified top-level flake,
|
||||||
const Settings & settings, EvalState & state, const FlakeRef & topRef, const LockFlags & lockFlags, Flake flake)
|
and optionally write it to file, if the flake is writable. */
|
||||||
|
LockedFlake
|
||||||
|
lockFlake(const Settings & settings, EvalState & state, const FlakeRef & topRef, const LockFlags & lockFlags)
|
||||||
{
|
{
|
||||||
experimentalFeatureSettings.require(Xp::Flakes);
|
experimentalFeatureSettings.require(Xp::Flakes);
|
||||||
|
|
||||||
@@ -425,6 +427,8 @@ LockedFlake lockFlake(
|
|||||||
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();
|
||||||
@@ -904,22 +908,6 @@ LockedFlake lockFlake(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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,16 +214,9 @@ 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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -56,26 +56,24 @@ bool lockFile(Descriptor desc, LockType lockType, bool wait)
|
|||||||
switch (lockType) {
|
switch (lockType) {
|
||||||
case ltNone: {
|
case ltNone: {
|
||||||
OVERLAPPED ov = {0};
|
OVERLAPPED ov = {0};
|
||||||
if (!UnlockFileEx(desc, 0, 2, 0, &ov)) {
|
if (!UnlockFileEx(desc, 0, 2, 0, &ov))
|
||||||
WinError winError("Failed to unlock file desc %s", desc);
|
throw WinError("Failed to unlock file desc %s", desc);
|
||||||
throw winError;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case ltRead: {
|
case ltRead: {
|
||||||
OVERLAPPED ov = {0};
|
OVERLAPPED ov = {0};
|
||||||
if (!LockFileEx(desc, wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &ov)) {
|
if (!LockFileEx(desc, wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &ov)) {
|
||||||
WinError winError("Failed to lock file desc %s", desc);
|
auto lastError = GetLastError();
|
||||||
if (winError.lastError == ERROR_LOCK_VIOLATION)
|
if (lastError == ERROR_LOCK_VIOLATION)
|
||||||
return false;
|
return false;
|
||||||
throw winError;
|
throw WinError(lastError, "Failed to lock file desc %s", desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
ov.Offset = 1;
|
ov.Offset = 1;
|
||||||
if (!UnlockFileEx(desc, 0, 1, 0, &ov)) {
|
if (!UnlockFileEx(desc, 0, 1, 0, &ov)) {
|
||||||
WinError winError("Failed to unlock file desc %s", desc);
|
auto lastError = GetLastError();
|
||||||
if (winError.lastError != ERROR_NOT_LOCKED)
|
if (lastError != ERROR_NOT_LOCKED)
|
||||||
throw winError;
|
throw WinError(lastError, "Failed to unlock file desc %s", desc);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -83,17 +81,17 @@ bool lockFile(Descriptor desc, LockType lockType, bool wait)
|
|||||||
OVERLAPPED ov = {0};
|
OVERLAPPED ov = {0};
|
||||||
ov.Offset = 1;
|
ov.Offset = 1;
|
||||||
if (!LockFileEx(desc, LOCKFILE_EXCLUSIVE_LOCK | (wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), 0, 1, 0, &ov)) {
|
if (!LockFileEx(desc, LOCKFILE_EXCLUSIVE_LOCK | (wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), 0, 1, 0, &ov)) {
|
||||||
WinError winError("Failed to lock file desc %s", desc);
|
auto lastError = GetLastError();
|
||||||
if (winError.lastError == ERROR_LOCK_VIOLATION)
|
if (lastError == ERROR_LOCK_VIOLATION)
|
||||||
return false;
|
return false;
|
||||||
throw winError;
|
throw WinError(lastError, "Failed to lock file desc %s", desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
ov.Offset = 0;
|
ov.Offset = 0;
|
||||||
if (!UnlockFileEx(desc, 0, 1, 0, &ov)) {
|
if (!UnlockFileEx(desc, 0, 1, 0, &ov)) {
|
||||||
WinError winError("Failed to unlock file desc %s", desc);
|
auto lastError = GetLastError();
|
||||||
if (winError.lastError != ERROR_NOT_LOCKED)
|
if (lastError != ERROR_NOT_LOCKED)
|
||||||
throw winError;
|
throw WinError(lastError, "Failed to unlock file desc %s", desc);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,16 +47,16 @@ TEST(fchmodatTryNoFollow, works)
|
|||||||
/* Check that symlinks are not followed and targets are not changed. */
|
/* Check that symlinks are not followed and targets are not changed. */
|
||||||
|
|
||||||
EXPECT_NO_THROW(
|
EXPECT_NO_THROW(
|
||||||
try { fchmodatTryNoFollow(dirFd.get(), CanonPath("filelink"), 0777); } catch (SysError & e) {
|
try { fchmodatTryNoFollow(dirFd.get(), CanonPath("filelink"), 0777); } catch (SystemError & e) {
|
||||||
if (e.errNo != EOPNOTSUPP)
|
if (!e.is(std::errc::operation_not_supported))
|
||||||
throw;
|
throw;
|
||||||
});
|
});
|
||||||
ASSERT_EQ(stat((tmpDir / "file").c_str(), &st), 0);
|
ASSERT_EQ(stat((tmpDir / "file").c_str(), &st), 0);
|
||||||
EXPECT_EQ(st.st_mode & 0777, 0644);
|
EXPECT_EQ(st.st_mode & 0777, 0644);
|
||||||
|
|
||||||
EXPECT_NO_THROW(
|
EXPECT_NO_THROW(
|
||||||
try { fchmodatTryNoFollow(dirFd.get(), CanonPath("dirlink"), 0777); } catch (SysError & e) {
|
try { fchmodatTryNoFollow(dirFd.get(), CanonPath("dirlink"), 0777); } catch (SystemError & e) {
|
||||||
if (e.errNo != EOPNOTSUPP)
|
if (!e.is(std::errc::operation_not_supported))
|
||||||
throw;
|
throw;
|
||||||
});
|
});
|
||||||
ASSERT_EQ(stat((tmpDir / "dir").c_str(), &st), 0);
|
ASSERT_EQ(stat((tmpDir / "dir").c_str(), &st), 0);
|
||||||
@@ -110,14 +110,14 @@ TEST(fchmodatTryNoFollow, fallbackWithoutProc)
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
fchmodatTryNoFollow(dirFd.get(), CanonPath("file"), 0600);
|
fchmodatTryNoFollow(dirFd.get(), CanonPath("file"), 0600);
|
||||||
} catch (SysError & e) {
|
} catch (SystemError & e) {
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fchmodatTryNoFollow(dirFd.get(), CanonPath("link"), 0777);
|
fchmodatTryNoFollow(dirFd.get(), CanonPath("link"), 0777);
|
||||||
} catch (SysError & e) {
|
} catch (SystemError & e) {
|
||||||
if (e.errNo == EOPNOTSUPP)
|
if (e.is(std::errc::operation_not_supported))
|
||||||
_exit(0); /* Success. */
|
_exit(0); /* Success. */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -250,18 +250,15 @@ class SystemError : public Error
|
|||||||
std::error_code errorCode;
|
std::error_code errorCode;
|
||||||
std::string errorDetails;
|
std::string errorDetails;
|
||||||
|
|
||||||
protected:
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Just here to allow derived classes to use the right constructor
|
* Just here to allow the static methods to use the right constructor
|
||||||
* (the protected one).
|
* (the private one).
|
||||||
*/
|
*/
|
||||||
struct Disambig
|
struct Disambig
|
||||||
{};
|
{};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Protected constructor for subclasses that provide their own error message.
|
* Private constructor with explicit error message string.
|
||||||
* The error message is appended to the formatted hint.
|
|
||||||
*/
|
*/
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
SystemError(Disambig, std::error_code errorCode, std::string_view errorDetails, Args &&... args)
|
SystemError(Disambig, std::error_code errorCode, std::string_view errorDetails, Args &&... args)
|
||||||
@@ -284,6 +281,79 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a POSIX error using the explicitly-provided error number.
|
||||||
|
* `strerror` will be used to try to add additional information to the message.
|
||||||
|
*/
|
||||||
|
template<typename... Args>
|
||||||
|
static SystemError fromPosixExplicit(int errNo, Args &&... args)
|
||||||
|
{
|
||||||
|
return SystemError(
|
||||||
|
Disambig{},
|
||||||
|
std::make_error_code(static_cast<std::errc>(errNo)),
|
||||||
|
strerror(errNo),
|
||||||
|
std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a POSIX error using the ambient `errno`.
|
||||||
|
*
|
||||||
|
* Be sure to not perform another `errno`-modifying operation before
|
||||||
|
* calling this!
|
||||||
|
*/
|
||||||
|
template<typename... Args>
|
||||||
|
static SystemError fromPosix(Args &&... args)
|
||||||
|
{
|
||||||
|
return fromPosixExplicit(errno, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
/**
|
||||||
|
* Construct a Windows error using the explicitly-provided error number.
|
||||||
|
* `FormatMessageA` will be used to try to add additional information
|
||||||
|
* to the message.
|
||||||
|
*/
|
||||||
|
template<typename... Args>
|
||||||
|
static SystemError fromWindowsExplicit(DWORD lastError, Args &&... args)
|
||||||
|
{
|
||||||
|
return SystemError(
|
||||||
|
Disambig{},
|
||||||
|
std::error_code(lastError, std::system_category()),
|
||||||
|
renderWindowsError(lastError),
|
||||||
|
std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a Windows error using `GetLastError()`.
|
||||||
|
*
|
||||||
|
* Be sure to not perform another last-error-modifying operation
|
||||||
|
* before calling this!
|
||||||
|
*/
|
||||||
|
template<typename... Args>
|
||||||
|
static SystemError fromWindows(Args &&... args)
|
||||||
|
{
|
||||||
|
return fromWindowsExplicit(GetLastError(), std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::string renderWindowsError(DWORD lastError);
|
||||||
|
|
||||||
|
public:
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct using the native error (errno on POSIX, GetLastError() on Windows).
|
||||||
|
*/
|
||||||
|
template<typename... Args>
|
||||||
|
static SystemError fromNative(Args &&... args)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
return fromWindows(std::forward<Args>(args)...);
|
||||||
|
#else
|
||||||
|
return fromPosix(std::forward<Args>(args)...);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
const std::error_code ec() const &
|
const std::error_code ec() const &
|
||||||
{
|
{
|
||||||
return errorCode;
|
return errorCode;
|
||||||
@@ -296,53 +366,58 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POSIX system error, created using `errno`, `strerror` friends.
|
* Wrapper to avoid churn
|
||||||
*
|
|
||||||
* Throw this, but prefer not to catch this, and catch `SystemError`
|
|
||||||
* instead. This allows implementations to freely switch between this
|
|
||||||
* and `windows::WinError` without breaking catch blocks.
|
|
||||||
*
|
|
||||||
* However, it is permissible to catch this and rethrow so long as
|
|
||||||
* certain conditions are not met (e.g. to catch only if `errNo =
|
|
||||||
* EFooBar`). In that case, try to also catch the equivalent `windows::WinError`
|
|
||||||
* code.
|
|
||||||
*
|
|
||||||
* @todo Rename this to `PosixError` or similar. At this point Windows
|
|
||||||
* support is too WIP to justify the code churn, but if it is finished
|
|
||||||
* then a better identifier becomes moe worth it.
|
|
||||||
*/
|
*/
|
||||||
class SysError : public SystemError
|
template<typename... Args>
|
||||||
|
SystemError SysError(int errNo, Args &&... args)
|
||||||
{
|
{
|
||||||
public:
|
return SystemError::fromPosixExplicit(errNo, std::forward<Args>(args)...);
|
||||||
int errNo;
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct using the explicitly-provided error number. `strerror`
|
* Wrapper to avoid churn
|
||||||
* will be used to try to add additional information to the message.
|
*/
|
||||||
*/
|
template<typename... Args>
|
||||||
template<typename... Args>
|
SystemError SysError(Args &&... args)
|
||||||
SysError(int errNo, Args &&... args)
|
{
|
||||||
: SystemError(
|
return SystemError::fromPosix(std::forward<Args>(args)...);
|
||||||
Disambig{},
|
}
|
||||||
std::make_error_code(static_cast<std::errc>(errNo)),
|
|
||||||
strerror(errNo),
|
|
||||||
std::forward<Args>(args)...)
|
|
||||||
, errNo(errNo)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
#ifdef _WIN32
|
||||||
* Construct using the ambient `errno`.
|
|
||||||
*
|
namespace windows {
|
||||||
* Be sure to not perform another `errno`-modifying operation before
|
|
||||||
* calling this constructor!
|
/**
|
||||||
*/
|
* Wrapper to avoid churn
|
||||||
template<typename... Args>
|
*/
|
||||||
SysError(Args &&... args)
|
template<typename... Args>
|
||||||
: SysError(errno, std::forward<Args>(args)...)
|
SystemError WinError(DWORD lastError, Args &&... args)
|
||||||
{
|
{
|
||||||
}
|
return SystemError::fromWindowsExplicit(lastError, std::forward<Args>(args)...);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper to avoid churn
|
||||||
|
*/
|
||||||
|
template<typename... Args>
|
||||||
|
SystemError WinError(Args &&... args)
|
||||||
|
{
|
||||||
|
return SystemError::fromWindows(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace windows
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convenience wrapper for when we use `errno`-based error handling
|
||||||
|
* on Unix, and `GetLastError()`-based error handling on Windows.
|
||||||
|
*/
|
||||||
|
template<typename... Args>
|
||||||
|
SystemError NativeSysError(Args &&... args)
|
||||||
|
{
|
||||||
|
return SystemError::fromNative(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throw an exception for the purpose of checking that exception
|
* Throw an exception for the purpose of checking that exception
|
||||||
@@ -382,69 +457,4 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)
|
|||||||
# define nixUnreachableWhenHardened std::unreachable
|
# define nixUnreachableWhenHardened std::unreachable
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
|
|
||||||
namespace windows {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Windows Error type.
|
|
||||||
*
|
|
||||||
* Unless you need to catch a specific error number, don't catch this in
|
|
||||||
* portable code. Catch `SystemError` instead.
|
|
||||||
*/
|
|
||||||
class WinError : public SystemError
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
DWORD lastError;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct using the explicitly-provided error number.
|
|
||||||
* `FormatMessageA` will be used to try to add additional
|
|
||||||
* information to the message.
|
|
||||||
*/
|
|
||||||
template<typename... Args>
|
|
||||||
WinError(DWORD lastError, Args &&... args)
|
|
||||||
: SystemError(
|
|
||||||
Disambig{},
|
|
||||||
std::error_code(lastError, std::system_category()),
|
|
||||||
renderError(lastError),
|
|
||||||
std::forward<Args>(args)...)
|
|
||||||
, lastError(lastError)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Construct using `GetLastError()` and the ambient "last error".
|
|
||||||
*
|
|
||||||
* Be sure to not perform another last-error-modifying operation
|
|
||||||
* before calling this constructor!
|
|
||||||
*/
|
|
||||||
template<typename... Args>
|
|
||||||
WinError(Args &&... args)
|
|
||||||
: WinError(GetLastError(), std::forward<Args>(args)...)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
static std::string renderError(DWORD lastError);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace windows
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convenience alias for when we use a `errno`-based error handling
|
|
||||||
* function on Unix, and `GetLastError()`-based error handling on on
|
|
||||||
* Windows.
|
|
||||||
*/
|
|
||||||
using NativeSysError =
|
|
||||||
#ifdef _WIN32
|
|
||||||
windows::WinError
|
|
||||||
#else
|
|
||||||
SysError
|
|
||||||
#endif
|
|
||||||
;
|
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
@@ -97,7 +97,7 @@ namespace unix {
|
|||||||
* AT_SYMLINK_NOFOLLOW, since it's the best we can do without failing.
|
* AT_SYMLINK_NOFOLLOW, since it's the best we can do without failing.
|
||||||
*
|
*
|
||||||
* @pre path.isRoot() is false
|
* @pre path.isRoot() is false
|
||||||
* @throws SysError if any operation fails
|
* @throws SystemError if any operation fails
|
||||||
*/
|
*/
|
||||||
void fchmodatTryNoFollow(Descriptor dirFd, const CanonPath & path, mode_t mode);
|
void fchmodatTryNoFollow(Descriptor dirFd, const CanonPath & path, mode_t mode);
|
||||||
|
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ std::filesystem::path readLink(const std::filesystem::path & path);
|
|||||||
*
|
*
|
||||||
* @note this function will clobber `errno` (Unix) / "last error"
|
* @note this function will clobber `errno` (Unix) / "last error"
|
||||||
* (Windows), so care must be used to get those error codes, then call
|
* (Windows), so care must be used to get those error codes, then call
|
||||||
* this, then build a `SysError` / `WinError` with the saved error code.
|
* this, then build a `SystemError` with the saved error code.
|
||||||
*/
|
*/
|
||||||
std::filesystem::path descriptorToPath(Descriptor fd);
|
std::filesystem::path descriptorToPath(Descriptor fd);
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ bool userNamespacesSupported()
|
|||||||
|
|
||||||
auto r = pid.wait();
|
auto r = pid.wait();
|
||||||
assert(!r);
|
assert(!r);
|
||||||
} catch (SysError & e) {
|
} catch (SystemError & e) {
|
||||||
debug("user namespaces do not work on this system: %s", e.msg());
|
debug("user namespaces do not work on this system: %s", e.msg());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ bool mountAndPidNamespacesSupported()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SysError & e) {
|
} catch (SystemError & e) {
|
||||||
debug("mount namespaces do not work on this system: %s", e.msg());
|
debug("mount namespaces do not work on this system: %s", e.msg());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,8 +78,8 @@ bindConnectProcHelper(std::string_view operationName, auto && operation, Socket
|
|||||||
if (operation(fd, psaddr, sizeof(addr)) == -1)
|
if (operation(fd, psaddr, sizeof(addr)) == -1)
|
||||||
throw SysError("cannot %s to socket at '%s'", operationName, path);
|
throw SysError("cannot %s to socket at '%s'", operationName, path);
|
||||||
writeFull(pipe.writeSide.get(), "0\n");
|
writeFull(pipe.writeSide.get(), "0\n");
|
||||||
} catch (SysError & e) {
|
} catch (SystemError & e) {
|
||||||
writeFull(pipe.writeSide.get(), fmt("%d\n", e.errNo));
|
writeFull(pipe.writeSide.get(), fmt("%d\n", e.ec().value()));
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
writeFull(pipe.writeSide.get(), "-1\n");
|
writeFull(pipe.writeSide.get(), "-1\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ void unix::closeExtraFDs()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} catch (SysError &) {
|
} catch (SystemError &) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -186,9 +186,9 @@ static void _deletePath(
|
|||||||
if ((st.st_mode & PERM_MASK) != PERM_MASK)
|
if ((st.st_mode & PERM_MASK) != PERM_MASK)
|
||||||
try {
|
try {
|
||||||
unix::fchmodatTryNoFollow(parentfd, CanonPath(name), st.st_mode | PERM_MASK);
|
unix::fchmodatTryNoFollow(parentfd, CanonPath(name), st.st_mode | PERM_MASK);
|
||||||
} catch (SysError & e) {
|
} catch (SystemError & e) {
|
||||||
e.addTrace({}, "while making directory %1% accessible for deletion", PathFmt(path));
|
e.addTrace({}, "while making directory %1% accessible for deletion", PathFmt(path));
|
||||||
if (e.errNo == EOPNOTSUPP)
|
if (e.is(std::errc::operation_not_supported))
|
||||||
e.addTrace({}, "%1% is now a symlink, expected directory", PathFmt(path));
|
e.addTrace({}, "%1% is now a symlink, expected directory", PathFmt(path));
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ inline void MonitorFdHup::runThread(int watchFd, int notifyFd)
|
|||||||
{
|
{
|
||||||
int kqResult = kqueue();
|
int kqResult = kqueue();
|
||||||
if (kqResult < 0) {
|
if (kqResult < 0) {
|
||||||
throw SysError("MonitorFdHup kqueue");
|
throw SystemError::fromPosix("MonitorFdHup kqueue");
|
||||||
}
|
}
|
||||||
AutoCloseFD kq{kqResult};
|
AutoCloseFD kq{kqResult};
|
||||||
|
|
||||||
@@ -78,14 +78,14 @@ inline void MonitorFdHup::runThread(int watchFd, int notifyFd)
|
|||||||
|
|
||||||
int result = kevent(kq.get(), kevs.data(), kevs.size(), nullptr, 0, nullptr);
|
int result = kevent(kq.get(), kevs.data(), kevs.size(), nullptr, 0, nullptr);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
throw SysError("MonitorFdHup kevent add");
|
throw SystemError::fromPosix("MonitorFdHup kevent add");
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
struct kevent event;
|
struct kevent event;
|
||||||
int numEvents = kevent(kq.get(), nullptr, 0, &event, 1, nullptr);
|
int numEvents = kevent(kq.get(), nullptr, 0, &event, 1, nullptr);
|
||||||
if (numEvents < 0) {
|
if (numEvents < 0) {
|
||||||
throw SysError("MonitorFdHup kevent watch");
|
throw SystemError::fromPosix("MonitorFdHup kevent watch");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (numEvents > 0 && (event.flags & EV_EOF)) {
|
if (numEvents > 0 && (event.flags & EV_EOF)) {
|
||||||
@@ -112,7 +112,7 @@ inline void MonitorFdHup::runThread(int watchFd, int notifyFd)
|
|||||||
if (errno == EINTR || errno == EAGAIN) {
|
if (errno == EINTR || errno == EAGAIN) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
throw SysError("in MonitorFdHup poll()");
|
throw SystemError::fromPosix("in MonitorFdHup poll()");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ std::filesystem::path getHome()
|
|||||||
auto st = maybeStat(homeDir->c_str());
|
auto st = maybeStat(homeDir->c_str());
|
||||||
if (st && st->st_uid != geteuid())
|
if (st && st->st_uid != geteuid())
|
||||||
unownedUserHomeDir.swap(homeDir);
|
unownedUserHomeDir.swap(homeDir);
|
||||||
} catch (SysError & e) {
|
} catch (SystemError & e) {
|
||||||
warn(
|
warn(
|
||||||
"couldn't stat $HOME ('%s') for reason other than not existing, falling back to the one defined in the 'passwd' file: %s",
|
"couldn't stat $HOME ('%s') for reason other than not existing, falling back to the one defined in the 'passwd' file: %s",
|
||||||
*homeDir,
|
*homeDir,
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
using namespace nix::windows;
|
||||||
|
|
||||||
std::chrono::microseconds getCpuUserTime()
|
std::chrono::microseconds getCpuUserTime()
|
||||||
{
|
{
|
||||||
FILETIME creationTime;
|
FILETIME creationTime;
|
||||||
@@ -17,7 +19,7 @@ std::chrono::microseconds getCpuUserTime()
|
|||||||
|
|
||||||
if (!GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime)) {
|
if (!GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime)) {
|
||||||
auto lastError = GetLastError();
|
auto lastError = GetLastError();
|
||||||
throw windows::WinError(lastError, "failed to get CPU time");
|
throw WinError(lastError, "failed to get CPU time");
|
||||||
}
|
}
|
||||||
|
|
||||||
ULARGE_INTEGER uLargeInt;
|
ULARGE_INTEGER uLargeInt;
|
||||||
|
|||||||
@@ -13,8 +13,6 @@
|
|||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
using namespace nix::windows;
|
|
||||||
|
|
||||||
namespace windows {
|
namespace windows {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
@@ -210,6 +208,8 @@ bool isReparsePoint(HANDLE handle)
|
|||||||
|
|
||||||
} // namespace windows
|
} // namespace windows
|
||||||
|
|
||||||
|
using namespace nix::windows;
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -259,9 +259,10 @@ Descriptor openFileEnsureBeneathNoSymlinks(
|
|||||||
FILE_TRAVERSE | SYNCHRONIZE, // Just need traversal rights
|
FILE_TRAVERSE | SYNCHRONIZE, // Just need traversal rights
|
||||||
FILE_DIRECTORY_FILE | FILE_OPEN_REPARSE_POINT // Open directory, don't follow symlinks
|
FILE_DIRECTORY_FILE | FILE_OPEN_REPARSE_POINT // Open directory, don't follow symlinks
|
||||||
);
|
);
|
||||||
} catch (WinError & e) {
|
} catch (SystemError & e) {
|
||||||
/* Check if this is because it's a symlink */
|
/* Check if this is because it's a symlink */
|
||||||
if (e.lastError == ERROR_CANT_ACCESS_FILE || e.lastError == ERROR_ACCESS_DENIED) {
|
auto err = e.ec().value();
|
||||||
|
if (err == ERROR_CANT_ACCESS_FILE || err == ERROR_ACCESS_DENIED) {
|
||||||
throwIfSymlink(wcomponent, pathUpTo(std::next(it)));
|
throwIfSymlink(wcomponent, pathUpTo(std::next(it)));
|
||||||
}
|
}
|
||||||
throw;
|
throw;
|
||||||
@@ -286,9 +287,9 @@ Descriptor openFileEnsureBeneathNoSymlinks(
|
|||||||
desiredAccess,
|
desiredAccess,
|
||||||
createOptions | FILE_OPEN_REPARSE_POINT, // Don't follow symlinks on final component either
|
createOptions | FILE_OPEN_REPARSE_POINT, // Don't follow symlinks on final component either
|
||||||
createDisposition);
|
createDisposition);
|
||||||
} catch (WinError & e) {
|
} catch (SystemError & e) {
|
||||||
/* Check if final component is a symlink when we requested to not follow it */
|
/* Check if final component is a symlink when we requested to not follow it */
|
||||||
if (e.lastError == ERROR_CANT_ACCESS_FILE) {
|
if (e.ec().value() == ERROR_CANT_ACCESS_FILE) {
|
||||||
throwIfSymlink(finalComponent, path);
|
throwIfSymlink(finalComponent, path);
|
||||||
}
|
}
|
||||||
throw;
|
throw;
|
||||||
|
|||||||
@@ -8,8 +8,6 @@
|
|||||||
|
|
||||||
namespace nix::windows::known_folders {
|
namespace nix::windows::known_folders {
|
||||||
|
|
||||||
using namespace nix::windows;
|
|
||||||
|
|
||||||
static std::filesystem::path getKnownFolder(REFKNOWNFOLDERID rfid)
|
static std::filesystem::path getKnownFolder(REFKNOWNFOLDERID rfid)
|
||||||
{
|
{
|
||||||
PWSTR str = nullptr;
|
PWSTR str = nullptr;
|
||||||
|
|||||||
@@ -7,15 +7,17 @@
|
|||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
using namespace nix::windows;
|
||||||
|
|
||||||
void MuxablePipePollState::poll(HANDLE ioport, std::optional<unsigned int> timeout)
|
void MuxablePipePollState::poll(HANDLE ioport, std::optional<unsigned int> timeout)
|
||||||
{
|
{
|
||||||
/* We are on at least Windows Vista / Server 2008 and can get many
|
/* We are on at least Windows Vista / Server 2008 and can get many
|
||||||
(countof(oentries)) statuses in one API call. */
|
(countof(oentries)) statuses in one API call. */
|
||||||
if (!GetQueuedCompletionStatusEx(
|
if (!GetQueuedCompletionStatusEx(
|
||||||
ioport, oentries, sizeof(oentries) / sizeof(*oentries), &removed, timeout ? *timeout : INFINITE, false)) {
|
ioport, oentries, sizeof(oentries) / sizeof(*oentries), &removed, timeout ? *timeout : INFINITE, false)) {
|
||||||
windows::WinError winError("GetQueuedCompletionStatusEx");
|
auto lastError = GetLastError();
|
||||||
if (winError.lastError != WAIT_TIMEOUT)
|
if (lastError != WAIT_TIMEOUT)
|
||||||
throw winError;
|
throw WinError(lastError, "GetQueuedCompletionStatusEx");
|
||||||
assert(removed == 0);
|
assert(removed == 0);
|
||||||
} else {
|
} else {
|
||||||
assert(0 < removed && removed <= sizeof(oentries) / sizeof(*oentries));
|
assert(0 < removed && removed <= sizeof(oentries) / sizeof(*oentries));
|
||||||
@@ -52,12 +54,12 @@ void MuxablePipePollState::iterate(
|
|||||||
// here is possible (but not obligatory) to call
|
// here is possible (but not obligatory) to call
|
||||||
// `handleRead` and repeat ReadFile immediately
|
// `handleRead` and repeat ReadFile immediately
|
||||||
} else {
|
} else {
|
||||||
windows::WinError winError("ReadFile(%s, ..)", (*p)->readSide.get());
|
auto lastError = GetLastError();
|
||||||
if (winError.lastError == ERROR_BROKEN_PIPE) {
|
if (lastError == ERROR_BROKEN_PIPE) {
|
||||||
handleEOF((*p)->readSide.get());
|
handleEOF((*p)->readSide.get());
|
||||||
nextp = channels.erase(p); // no need to maintain `channels` ?
|
nextp = channels.erase(p); // no need to maintain `channels` ?
|
||||||
} else if (winError.lastError != ERROR_IO_PENDING)
|
} else if (lastError != ERROR_IO_PENDING)
|
||||||
throw winError;
|
throw WinError(lastError, "ReadFile(%s, ..)", (*p)->readSide.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
# define WIN32_LEAN_AND_MEAN
|
# define WIN32_LEAN_AND_MEAN
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
|
|
||||||
namespace nix::windows {
|
namespace nix {
|
||||||
|
|
||||||
std::string WinError::renderError(DWORD lastError)
|
std::string SystemError::renderWindowsError(DWORD lastError)
|
||||||
{
|
{
|
||||||
LPSTR errorText = NULL;
|
LPSTR errorText = NULL;
|
||||||
|
|
||||||
@@ -32,5 +32,5 @@ std::string WinError::renderError(DWORD lastError)
|
|||||||
return fmt("CODE=%d", lastError);
|
return fmt("CODE=%d", lastError);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace nix::windows
|
} // namespace nix
|
||||||
#endif
|
#endif
|
||||||
@@ -17,7 +17,7 @@ void showManPage(const std::string & name)
|
|||||||
setEnv("MANPATH", (getNixManDir().string() + ":").c_str());
|
setEnv("MANPATH", (getNixManDir().string() + ":").c_str());
|
||||||
execlp("man", "man", name.c_str(), nullptr);
|
execlp("man", "man", name.c_str(), nullptr);
|
||||||
if (errno == ENOENT) {
|
if (errno == ENOENT) {
|
||||||
// Not SysError because we don't want to suffix the errno, aka No such file or directory.
|
// Not SystemError because we don't want to suffix the errno, aka No such file or directory.
|
||||||
throw Error(
|
throw Error(
|
||||||
"The '%1%' command was not found, but it is needed for '%2%' and some other '%3%' commands' help text. Perhaps you could install the '%1%' command?",
|
"The '%1%' command was not found, but it is needed for '%2%' and some other '%3%' commands' help text. Perhaps you could install the '%1%' command?",
|
||||||
"man",
|
"man",
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ writeSimpleFlake() {
|
|||||||
baseName = builtins.baseNameOf ./.;
|
baseName = builtins.baseNameOf ./.;
|
||||||
|
|
||||||
root = ./.;
|
root = ./.;
|
||||||
|
|
||||||
number = 123;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
#!/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,7 +34,6 @@ 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