Compare commits

..
Author SHA1 Message Date
John Ericson 6f867cce2c Move cgroup functionality inside nix::linux namespace
This is what we do for other OS-specific functionality.
2026-02-17 14:58:19 -05:00
John Ericson eb826a5a03 Factor out chroot logic from ChrootDerivationBuilder into its own file
This shrinks the big derivation builder compilation unit, and hopefully
puts on on the road to being able to do all derivation builder cleanup
with RAII.
2026-02-17 14:58:19 -05:00
144 changed files with 831 additions and 1736 deletions

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ jobs:
# required to find all branches
fetch-depth: 0
- name: Create backport PRs
uses: korthout/backport-action@01619ebc9a6e3f6820274221b9956b3e7365000a # v4.1.0
uses: korthout/backport-action@c656f5d5851037b2b38fb5db2691a03fa229e3b2 # v4.0.1
id: backport
with:
# Config README: https://github.com/korthout/backport-action#backport-action
+2 -2
View File
@@ -174,7 +174,7 @@ jobs:
echo "installer-url=file://$GITHUB_WORKSPACE/out" >> "$GITHUB_OUTPUT"
TARBALL_PATH="$(find "$GITHUB_WORKSPACE/out" -name 'nix*.tar.xz' -print | head -n 1)"
echo "tarball-path=file://$TARBALL_PATH" >> "$GITHUB_OUTPUT"
- uses: cachix/install-nix-action@2126ae7fc54c9df00dd18f7f18754393182c73cd # v31.9.1
- uses: cachix/install-nix-action@4e002c8ec80594ecd40e759629461e26c8abed15 # v31.9.0
if: ${{ !matrix.experimental-installer }}
with:
install_url: ${{ format('{0}/install', steps.installer-tarball-url.outputs.installer-url) }}
@@ -222,7 +222,7 @@ jobs:
id: installer-tarball-url
run: |
echo "installer-url=file://$GITHUB_WORKSPACE/out" >> "$GITHUB_OUTPUT"
- uses: cachix/install-nix-action@2126ae7fc54c9df00dd18f7f18754393182c73cd # v31.9.1
- uses: cachix/install-nix-action@4e002c8ec80594ecd40e759629461e26c8abed15 # v31.9.0
with:
install_url: ${{ format('{0}/install', steps.installer-tarball-url.outputs.installer-url) }}
install_options: ${{ format('--tarball-url-prefix {0}', steps.installer-tarball-url.outputs.installer-url) }}
@@ -1,23 +0,0 @@
---
synopsis: "C API: Errors returned from your primops are not treated as recoverable by default"
prs: [15286, 13930]
---
Nix 2.34 by default remembers the error in the thunk that triggered it.
Previously the following sequence of events worked:
1. Have a thunk that invokes a primop that's defined through the C API
2. The primop returns an error
3. Force the thunk again
4. The primop returns a value
5. The thunk evaluated successfully
**Resolution**
C API consumers that rely on this must change their recoverable error calls:
```diff
-nix_set_err_msg(context, NIX_ERR_*, msg);
+nix_set_err_msg(context, NIX_ERR_RECOVERABLE, msg);
```
@@ -1,6 +0,0 @@
---
synopsis: New setting `narinfo-cache-meta-ttl`
prs: [15287]
---
The new setting `narinfo-cache-meta-ttl` controls how long binary cache metadata (i.e. `/nix-cache-info`) is cached locally, in seconds. This was previously hard-coded to 7 days, which is still the default. As a result, you can now use `nix store info --refresh` to check whether a binary cache is still valid.
+1
View File
@@ -358,6 +358,7 @@ dockerTools.buildLayeredImageWithNixDb {
extraCommands = ''
rm -rf nix-support
ln -s /nix/var/nix/profiles nix/var/nix/gcroots/profiles
'';
fakeRootCommands = ''
chmod 1777 tmp
@@ -9,3 +9,9 @@ endif
if 'address' in get_option('b_sanitize')
deps_other += declare_dependency(sources : 'asan-options.cc')
endif
if 'undefined' in get_option('b_sanitize')
add_project_arguments('-DNIX_UBSAN_ENABLED=1', language : 'cpp')
else
add_project_arguments('-DNIX_UBSAN_ENABLED=0', language : 'cpp')
endif
+1 -1
View File
@@ -710,7 +710,7 @@ void NixRepl::loadFlake(const std::string & flakeRefS)
try {
cwd = std::filesystem::current_path();
} catch (std::filesystem::filesystem_error & e) {
throw SystemError(e.code(), "cannot determine current working directory");
throw SysError("cannot determine current working directory");
}
auto flakeRef = parseFlakeRef(fetchSettings, flakeRefS, cwd.string(), true);
+1 -3
View File
@@ -185,9 +185,7 @@ EvalState * nix_eval_state_build(nix_c_context * context, nix_eval_state_builder
return EvalState{
.fetchSettings = std::move(builder->fetchSettings),
.settings = std::move(builder->settings),
.statePtr = std::make_shared<nix::EvalState>(
builder->lookupPath, builder->store, self->fetchSettings, self->settings),
.state = *self->statePtr,
.state = nix::EvalState(builder->lookupPath, builder->store, self->fetchSettings, self->settings),
};
});
}
+1 -2
View File
@@ -24,8 +24,7 @@ struct EvalState
{
nix::fetchers::Settings fetchSettings;
nix::EvalSettings settings;
std::shared_ptr<nix::EvalState> statePtr;
nix::EvalState & state;
nix::EvalState state;
};
struct BindingsBuilder
+2 -10
View File
@@ -1,5 +1,4 @@
#include "nix/expr/attr-set.hh"
#include "nix/expr/eval-error.hh"
#include "nix/util/configuration.hh"
#include "nix/expr/eval.hh"
#include "nix/store/globals.hh"
@@ -108,13 +107,8 @@ static void nix_c_primop_wrapper(
f(userdata, &ctx, (EvalState *) &state, external_args.data(), vTmpPtr);
if (ctx.last_err_code != NIX_OK) {
if (ctx.last_err_code == NIX_ERR_RECOVERABLE) {
state.error<nix::RecoverableEvalError>("Recoverable error from custom function: %s", *ctx.last_err)
.atPos(pos)
.debugThrow();
} else {
state.error<nix::EvalError>("Error from custom function: %s", *ctx.last_err).atPos(pos).debugThrow();
}
/* TODO: Throw different errors depending on the error code */
state.error<nix::EvalError>("Error from custom function: %s", *ctx.last_err).atPos(pos).debugThrow();
}
if (!vTmp.isValid()) {
@@ -200,8 +194,6 @@ ValueType nix_get_type(nix_c_context * context, const nix_value * value)
switch (v.type()) {
case nThunk:
return NIX_TYPE_THUNK;
case nFailed:
return NIX_TYPE_FAILED;
case nInt:
return NIX_TYPE_INT;
case nFloat:
+1 -2
View File
@@ -100,8 +100,7 @@ typedef enum {
/** @brief External value from C++ plugins or C API
* @see Externals
*/
NIX_TYPE_EXTERNAL,
NIX_TYPE_FAILED,
NIX_TYPE_EXTERNAL
} ValueType;
// forward declarations
+1 -2
View File
@@ -37,8 +37,7 @@ static void BM_EvalDynamicAttrs(benchmark::State & state)
EvalSettings evalSettings{readOnlyMode};
evalSettings.nixPath = {};
auto stPtr = std::make_shared<EvalState>(LookupPath{}, store, fetchSettings, evalSettings, nullptr);
auto & st = *stPtr;
EvalState st({}, store, fetchSettings, evalSettings, nullptr);
Expr * expr = st.parseExprFromString(exprStr, st.rootPath(CanonPath::root));
Value v;
+2 -4
View File
@@ -16,8 +16,7 @@ struct GetDerivationsEnv
fetchers::Settings fetchSettings{};
bool readOnlyMode = true;
EvalSettings evalSettings{readOnlyMode};
std::shared_ptr<EvalState> statePtr;
EvalState & state;
EvalState state;
Bindings * autoArgs = nullptr;
Value attrsValue;
@@ -28,8 +27,7 @@ struct GetDerivationsEnv
settings.nixPath = {};
return settings;
}())
, statePtr(std::make_shared<EvalState>(LookupPath{}, store, fetchSettings, evalSettings, nullptr))
, state(*statePtr)
, state({}, store, fetchSettings, evalSettings, nullptr)
{
autoArgs = state.buildBindings(0).finish();
-102
View File
@@ -517,106 +517,4 @@ TEST_F(nix_api_expr_test, nix_expr_attrset_update)
assert_ctx_ok();
}
// The following is a test case for retryable thunks. This is a requirement
// for the current way in which NixOps4 evaluates its deployment expressions.
// An alternative strategy could be implemented, but unwinding the stack may
// be a more efficient way to deal with many suspensions/resumptions, compared
// to e.g. using a thread or coroutine stack for each suspended dependency.
// This test models the essential bits of a deployment tool that uses such
// a strategy.
// State for the retryable primop - simulates deployment resource availability
struct DeploymentResourceState
{
bool vm_created = false;
};
static void primop_load_resource_input(
void * user_data, nix_c_context * context, EvalState * state, nix_value ** args, nix_value * ret)
{
assert(context);
assert(state);
auto * resource_state = static_cast<DeploymentResourceState *>(user_data);
// Get the resource input name argument
std::string input_name;
if (nix_get_string(context, args[0], OBSERVE_STRING(input_name)) != NIX_OK)
return;
// Only handle "vm_id" input - throw for anything else
if (input_name != "vm_id") {
std::string error_msg = "unknown resource input: " + input_name;
nix_set_err_msg(context, NIX_ERR_NIX_ERROR, error_msg.c_str());
return;
}
if (resource_state->vm_created) {
// VM has been created, return the ID
nix_init_string(context, ret, "vm-12345");
} else {
// VM not created yet, fail with dependency error
nix_set_err_msg(context, NIX_ERR_RECOVERABLE, "VM not yet created");
}
}
TEST_F(nix_api_expr_test, nix_expr_thunk_re_evaluation_after_deployment)
{
// This test demonstrates NixOps4's requirement: a thunk calling a primop should be
// re-evaluable when deployment resources become available that were not available initially.
DeploymentResourceState resource_state;
PrimOp * primop = nix_alloc_primop(
ctx,
primop_load_resource_input,
1,
"loadResourceInput",
nullptr,
"load a deployment resource input",
&resource_state);
assert_ctx_ok();
nix_value * primopValue = nix_alloc_value(ctx, state);
assert_ctx_ok();
nix_init_primop(ctx, primopValue, primop);
assert_ctx_ok();
nix_value * inputName = nix_alloc_value(ctx, state);
assert_ctx_ok();
nix_init_string(ctx, inputName, "vm_id");
assert_ctx_ok();
// Create a single thunk by using nix_init_apply instead of nix_value_call
// This creates a lazy application that can be forced multiple times
nix_value * thunk = nix_alloc_value(ctx, state);
assert_ctx_ok();
nix_init_apply(ctx, thunk, primopValue, inputName);
assert_ctx_ok();
// First force: VM not created yet, should fail
nix_value_force(ctx, state, thunk);
ASSERT_EQ(NIX_ERR_NIX_ERROR, nix_err_code(ctx));
ASSERT_THAT(nix_err_msg(nullptr, ctx, nullptr), testing::HasSubstr("VM not yet created"));
// Clear the error context for the next attempt
nix_c_context_free(ctx);
ctx = nix_c_context_create();
// Simulate deployment process: VM gets created
resource_state.vm_created = true;
// Second force of the SAME thunk: this is where the "failed" value issue appears
// With failed value caching, this should fail because the thunk is marked as permanently failed
// Without failed value caching (or with retryable failures), this should succeed
nix_value_force(ctx, state, thunk);
// If we get here without error, the thunk was successfully re-evaluated
assert_ctx_ok();
std::string result;
nix_get_string(ctx, thunk, OBSERVE_STRING(result));
assert_ctx_ok();
ASSERT_STREQ("vm-12345", result.c_str());
}
} // namespace nixC
+1 -2
View File
@@ -27,8 +27,7 @@ static void BM_EvalManyBuiltinsMatchSameRegex(benchmark::State & state)
EvalSettings evalSettings{readOnlyMode};
evalSettings.nixPath = {};
auto stPtr = std::make_shared<EvalState>(LookupPath{}, store, fetchSettings, evalSettings, nullptr);
auto & st = *stPtr;
EvalState st({}, store, fetchSettings, evalSettings, nullptr);
Expr * expr = st.parseExprFromString(std::string(exprStr), st.rootPath(CanonPath::root));
Value v;
-16
View File
@@ -188,22 +188,6 @@ TEST_F(ValuePrintingTests, vBlackhole)
test(vBlackhole, "«potential infinite recursion»");
}
TEST_F(ValuePrintingTests, vFailed)
{
Value v;
try {
throw Error("nope");
} catch (...) {
v.mkFailed(std::current_exception(), nullptr);
}
// Historically, a tried and then ignored value (e.g. through tryEval) was
// reverted to the original thunk.
test(v, "«thunk»");
test(v, ANSI_MAGENTA "«thunk»" ANSI_NORMAL, PrintOptions{.ansiColors = true});
}
TEST_F(ValuePrintingTests, depthAttrs)
{
Value vOne;
+2 -2
View File
@@ -11,7 +11,7 @@
namespace nix::eval_cache {
CachedEvalError::CachedEvalError(ref<AttrCursor> cursor, Symbol attr)
: CloneableError(cursor->root->state, "cached failure of attribute '%s'", cursor->getAttrPathStr(attr))
: EvalError(cursor->root->state, "cached failure of attribute '%s'", cursor->getAttrPathStr(attr))
, cursor(cursor)
, attr(attr)
{
@@ -70,7 +70,7 @@ struct AttrDb
{
auto state(_state->lock());
auto cacheDir = getCacheDir() / "eval-cache-v6";
auto cacheDir = std::filesystem::path(getCacheDir()) / "eval-cache-v6";
createDirs(cacheDir);
auto dbPath = cacheDir / (fingerprint.to_string(HashFormat::Base16, false) + ".sqlite");
-1
View File
@@ -114,6 +114,5 @@ template class EvalErrorBuilder<InfiniteRecursionError>;
template class EvalErrorBuilder<StackOverflowError>;
template class EvalErrorBuilder<InvalidPathError>;
template class EvalErrorBuilder<IFDError>;
template class EvalErrorBuilder<RecoverableEvalError>;
} // namespace nix
+1 -1
View File
@@ -70,7 +70,7 @@ Strings EvalSettings::getDefaultNixPath()
}
};
add(getNixDefExpr() / "channels");
add(std::filesystem::path{getNixDefExpr()} / "channels");
auto profilesDirOpts = settings.getProfileDirsOptions();
add(rootChannelsDir(profilesDirOpts) / "nixpkgs", "nixpkgs");
add(rootChannelsDir(profilesDirOpts));
+7 -70
View File
@@ -1,5 +1,4 @@
#include "nix/expr/eval.hh"
#include "nix/expr/eval-error.hh"
#include "nix/expr/eval-settings.hh"
#include "nix/expr/primops.hh"
#include "nix/expr/print-options.hh"
@@ -32,7 +31,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <sstream>
#include <cstring>
@@ -157,8 +155,6 @@ std::string_view showType(ValueType type, bool withArticle)
return WA("a", "float");
case nThunk:
return WA("a", "thunk");
case nFailed:
return WA("an", "error");
}
unreachable();
}
@@ -1991,15 +1987,11 @@ void ExprOpUpdate::eval(EvalState & state, Env & env, Value & v)
UpdateQueue q;
evalForUpdate(state, env, q);
Value vTmp;
vTmp.mkAttrs(&Bindings::emptyBindings);
v.mkAttrs(&Bindings::emptyBindings);
for (auto & rhs : std::views::reverse(q)) {
/* Remember that queue is sorted rightmost attrset first. */
eval(state, /*v=*/vTmp, /*v1=*/vTmp, /*v2=*/rhs);
eval(state, /*v=*/v, /*v1=*/v, /*v2=*/rhs);
}
v = vTmp;
}
void Expr::evalForUpdate(EvalState & state, Env & env, UpdateQueue & q, std::string_view errorCtx)
@@ -2182,54 +2174,6 @@ void ExprBlackHole::eval(EvalState & state, [[maybe_unused]] Env & env, Value &
// always force this to be separate, otherwise forceValue may inline it and take
// a massive perf hit
[[gnu::noinline]]
void EvalState::handleEvalExceptionForThunk(Env * env, Expr * expr, Value & v, const PosIdx pos)
{
if (!env)
tryFixupBlackHolePos(v, pos);
auto e = std::current_exception();
Value * recovery = nullptr;
try {
std::rethrow_exception(e);
} catch (const RecoverableEvalError & e) {
recovery = allocValue();
} catch (...) {
}
if (recovery) {
recovery->mkThunk(env, expr);
}
v.mkFailed(e, recovery);
}
[[gnu::noinline]]
void EvalState::handleEvalExceptionForApp(Value & v, const Value & savedApp)
{
auto e = std::current_exception();
Value * recovery = nullptr;
try {
std::rethrow_exception(e);
} catch (const RecoverableEvalError & e) {
recovery = allocValue();
} catch (...) {
}
if (recovery) {
*recovery = savedApp;
}
v.mkFailed(e, recovery);
}
[[gnu::noinline]]
void EvalState::handleEvalFailed(Value & v, const PosIdx pos)
{
assert(v.isFailed());
if (auto recoveryValue = v.failed().recoveryValue) {
v = *recoveryValue;
forceValue(v, pos);
} else {
v.failed().rethrow();
}
}
void EvalState::tryFixupBlackHolePos(Value & v, PosIdx pos)
{
if (!v.isBlackhole())
@@ -2238,8 +2182,7 @@ void EvalState::tryFixupBlackHolePos(Value & v, PosIdx pos)
try {
std::rethrow_exception(e);
} catch (InfiniteRecursionError & e) {
if (!e.hasPos())
e.atPos(positions[pos]);
e.atPos(positions[pos]);
} catch (...) {
}
}
@@ -2878,11 +2821,8 @@ void EvalState::assertEqValues(Value & v1, Value & v2, const PosIdx pos, std::st
}
return;
// Cannot be returned by forceValue().
case nThunk:
case nFailed:
unreachable();
case nThunk: // Must not be left by forceValue
assert(false);
default: // Note that we pass compiler flags that should make `default:` unreachable.
// Also note that this probably ran after `eqValues`, which implements
// the same logic more efficiently (without having to unwind stacks),
@@ -2976,11 +2916,8 @@ bool EvalState::eqValues(Value & v1, Value & v2, const PosIdx pos, std::string_v
// !!!
return v1.fpoint() == v2.fpoint();
// Cannot be returned by forceValue().
case nThunk:
case nFailed:
unreachable();
case nThunk: // Must not be left by forceValue
assert(false);
default: // Note that we pass compiler flags that should make `default:` unreachable.
error<EvalError>("eqValues: cannot compare %1% with %2%", showType(v1), showType(v2))
.withTrace(pos, errorCtx)
+1 -1
View File
@@ -14,7 +14,7 @@ namespace nix::eval_cache {
struct AttrDb;
class AttrCursor;
struct CachedEvalError : CloneableError<CachedEvalError, EvalError>
struct CachedEvalError : EvalError
{
const ref<AttrCursor> cursor;
const Symbol attr;
+7 -15
View File
@@ -18,7 +18,7 @@ class EvalErrorBuilder;
*
* Most subclasses should inherit from `EvalError` instead of this class.
*/
class EvalBaseError : public CloneableError<EvalBaseError, Error>
class EvalBaseError : public Error
{
template<class T>
friend class EvalErrorBuilder;
@@ -26,14 +26,14 @@ public:
EvalState & state;
EvalBaseError(EvalState & state, ErrorInfo && errorInfo)
: CloneableError(errorInfo)
: Error(errorInfo)
, state(state)
{
}
template<typename... Args>
explicit EvalBaseError(EvalState & state, const std::string & formatString, const Args &... formatArgs)
: CloneableError(formatString, formatArgs...)
: Error(formatString, formatArgs...)
, state(state)
{
}
@@ -60,31 +60,23 @@ MakeError(InfiniteRecursionError, EvalError);
* Inherits from EvalBaseError (not EvalError) because resource exhaustion
* should not be cached.
*/
struct StackOverflowError : public CloneableError<StackOverflowError, EvalBaseError>
struct StackOverflowError : public EvalBaseError
{
StackOverflowError(EvalState & state)
: CloneableError(state, "stack overflow; max-call-depth exceeded")
: EvalBaseError(state, "stack overflow; max-call-depth exceeded")
{
}
};
MakeError(IFDError, EvalBaseError);
/**
* An evaluation error which should be retried instead of rethrown.
*
* A RecoverableEvalError is not an EvalError, because we shouldn't cache it in
* the eval cache, as it should be retried anyway.
*/
MakeError(RecoverableEvalError, EvalBaseError);
struct InvalidPathError : public CloneableError<InvalidPathError, EvalError>
struct InvalidPathError : public EvalError
{
public:
Path path;
InvalidPathError(EvalState & state, const Path & path)
: CloneableError(state, "path '%s' is not valid", path)
: EvalError(state, "path '%s' is not valid", path)
{
}
};
-3
View File
@@ -33,9 +33,6 @@ using gc_allocator = std::allocator<T>;
struct gc
{};
struct gc_cleanup
{};
#endif
namespace nix {
+5 -13
View File
@@ -5,7 +5,6 @@
#include "nix/expr/eval.hh"
#include "nix/expr/eval-error.hh"
#include "nix/expr/eval-settings.hh"
#include <exception>
namespace nix {
@@ -92,25 +91,18 @@ void EvalState::forceValue(Value & v, const PosIdx pos)
Expr * expr = v.thunk().expr;
try {
v.mkBlackhole();
// checkInterrupt();
if (env) [[likely]]
expr->eval(*this, *env, v);
else
ExprBlackHole::throwInfiniteRecursionError(*this, v);
} catch (...) {
handleEvalExceptionForThunk(env, expr, v, pos);
v.mkThunk(env, expr);
tryFixupBlackHolePos(v, pos);
throw;
}
} else if (v.isApp()) {
Value savedApp = v;
try {
callFunction(*v.app().left, *v.app().right, v, pos);
} catch (...) {
handleEvalExceptionForApp(v, savedApp);
throw;
}
} else if (v.isFailed()) {
handleEvalFailed(v, pos);
}
} else if (v.isApp())
callFunction(*v.app().left, *v.app().right, v, pos);
}
[[gnu::always_inline]]
-20
View File
@@ -652,28 +652,8 @@ public:
*/
inline void forceValue(Value & v, const PosIdx pos);
private:
/**
* Internal support function for forceValue
*
* This code is factored out so that it's not in the heavily inlined hot path.
*/
void handleEvalExceptionForThunk(Env * env, Expr * expr, Value & v, const PosIdx pos);
/**
* Internal support function for forceValue
*
* This code is factored out so that it's not in the heavily inlined hot path.
*/
void handleEvalExceptionForApp(Value & v, const Value & savedApp);
void handleEvalFailed(Value & v, PosIdx pos);
void tryFixupBlackHolePos(Value & v, PosIdx pos);
public:
/**
* Force a value, then recursively force list elements and
* attributes.
+5 -67
View File
@@ -7,7 +7,6 @@
#include <cstring>
#include <memory>
#include <memory_resource>
#include <exception>
#include <span>
#include <string_view>
#include <type_traits>
@@ -43,7 +42,6 @@ enum InternalType {
tBool,
tNull,
tFloat,
tFailed,
tExternal,
tPrimOp,
tAttrs,
@@ -70,7 +68,6 @@ enum InternalType {
*/
typedef enum {
nThunk,
nFailed,
nInt,
nFloat,
nBool,
@@ -427,36 +424,6 @@ struct ValueBase
size_t size;
Value * const * elems;
};
struct Failed : gc_cleanup
{
std::exception_ptr ex;
/**
* Optional value for recovering `RecoverableEvalError`
* Must be set iff `ex` is an instance of `RecoverableEvalError`.
*/
Value * recoveryValue;
Failed(std::exception_ptr ex, Value * recoveryValue)
: ex(ex)
, recoveryValue(recoveryValue)
{
}
[[noreturn]] void rethrow() const
{
try {
std::rethrow_exception(ex);
} catch (BaseError & e) {
/* Rethrow the copy of the exception - not the original one.
Stack tracing mechanisms rely on being able to modify the exceptions
they catch by reference. */
e.throwClone();
} catch (...) {
throw;
}
}
};
};
template<typename T>
@@ -483,7 +450,6 @@ struct PayloadTypeToInternalType
MACRO(PrimOp *, primOp, tPrimOp) \
MACRO(ValueBase::PrimOpApplicationThunk, primOpApp, tPrimOpApp) \
MACRO(ExternalValueBase *, external, tExternal) \
MACRO(ValueBase::Failed *, failed, tFailed) \
MACRO(NixFloat, fpoint, tFloat)
#define NIX_VALUE_PAYLOAD_TYPE(T, FIELD_NAME, DISCRIMINATOR) \
@@ -788,11 +754,6 @@ protected:
path.path = std::bit_cast<const StringData *>(payload[1]);
}
void getStorage(Failed *& failed) const noexcept
{
failed = std::bit_cast<Failed *>(payload[1]);
}
void setStorage(NixInt integer) noexcept
{
setSingleDWordPayload<tInt>(integer.value);
@@ -842,11 +803,6 @@ protected:
{
setUntaggablePayload<pdPath>(path.accessor, path.path);
}
void setStorage(Failed * failed) noexcept
{
setSingleDWordPayload<tFailed>(std::bit_cast<PackedPointer>(failed));
}
};
/**
@@ -1098,12 +1054,12 @@ public:
inline bool isThunk() const
{
return isa<tThunk>();
}
};
inline bool isApp() const
{
return isa<tApp>();
}
};
inline bool isBlackhole() const;
@@ -1111,22 +1067,17 @@ public:
inline bool isLambda() const
{
return isa<tLambda>();
}
};
inline bool isPrimOp() const
{
return isa<tPrimOp>();
}
};
inline bool isPrimOpApp() const
{
return isa<tPrimOpApp>();
}
inline bool isFailed() const
{
return isa<tFailed>();
}
};
/**
* Returns the normal type of a Value. This only returns nThunk if
@@ -1147,7 +1098,6 @@ public:
t[tBool] = nBool;
t[tNull] = nNull;
t[tFloat] = nFloat;
t[tFailed] = nFailed;
t[tExternal] = nExternal;
t[tAttrs] = nAttrs;
t[tPrimOp] = nFunction;
@@ -1285,11 +1235,6 @@ public:
setStorage(n);
}
inline void mkFailed(std::exception_ptr e, Value * recovery) noexcept
{
setStorage(new Value::Failed(e, recovery));
}
bool isList() const noexcept
{
return isa<tListSmall, tListN>();
@@ -1404,13 +1349,6 @@ public:
{
return getStorage<Path>().accessor;
}
Failed & failed() const noexcept
{
auto p = getStorage<Failed *>();
assert(p);
return *p;
}
};
extern ExprBlackHole eBlackHole;
@@ -9,14 +9,14 @@
namespace nix {
class BadNixStringContextElem final : public CloneableError<BadNixStringContextElem, Error>
class BadNixStringContextElem : public Error
{
public:
std::string_view raw;
template<typename... Args>
BadNixStringContextElem(std::string_view raw_, const Args &... args)
: CloneableError("")
: Error("")
{
raw = raw_;
auto hf = HintFmt(args...);
+5 -7
View File
@@ -562,7 +562,6 @@ static void prim_typeOf(EvalState & state, const PosIdx pos, Value ** args, Valu
v.mkStringNoCopy("float"_sds);
break;
case nThunk:
case nFailed:
unreachable();
}
}
@@ -1313,12 +1312,11 @@ static void prim_warn(EvalState & state, const PosIdx pos, Value ** args, Value
state.forceString(*args[0], pos, "while evaluating the first argument; the message passed to builtins.warn");
{
ErrorInfo info{
.level = lvlWarn,
.msg = HintFmt(std::string(msgStr)),
.pos = state.positions[pos],
.isFromExpr = true,
};
BaseError msg(std::string{msgStr});
msg.atPos(state.positions[pos]);
auto info = msg.info();
info.level = lvlWarn;
info.isFromExpr = true;
logWarning(info);
}
-5
View File
@@ -73,11 +73,6 @@ void printAmbiguous(EvalState & state, Value & v, std::ostream & str, std::set<c
str << "«potential infinite recursion»";
}
break;
case nFailed:
// Historically, a tried and then ignored value (e.g. through tryEval) was
// reverted to the original thunk.
str << "<CODE>";
break;
case nFunction:
if (v.isLambda()) {
str << "<LAMBDA>";
-15
View File
@@ -509,17 +509,6 @@ private:
}
}
void printFailed()
{
if (options.ansiColors)
output << ANSI_MAGENTA;
// Historically, a tried and then ignored value (e.g. through tryEval) was
// reverted to the original thunk.
output << "«thunk»";
if (options.ansiColors)
output << ANSI_NORMAL;
}
void printExternal(Value & v)
{
v.external()->print(output);
@@ -601,10 +590,6 @@ private:
printFunction(v);
break;
case nFailed:
printFailed();
break;
case nThunk:
printThunk(v);
break;
-1
View File
@@ -98,7 +98,6 @@ json printValueAsJSON(
break;
case nThunk:
case nFailed:
case nFunction:
state.error<TypeError>("cannot convert %1% to JSON", showType(v)).atPos(v.determinePos(pos)).debugThrow();
}
-4
View File
@@ -171,11 +171,7 @@ static void printValueAsXML(
break;
case nThunk:
// Historically, a tried and then ignored value (e.g. through tryEval) was
// reverted to the original thunk.
case nFailed:
doc.writeEmptyElement("unevaluated");
break;
}
}
+2 -2
View File
@@ -44,8 +44,8 @@ struct CacheImpl : Cache
{
auto state(_state.lock());
auto dbPath = getCacheDir() / "fetcher-cache-v4.sqlite";
createDirs(dbPath.parent_path());
auto dbPath = (getCacheDir() / "fetcher-cache-v4.sqlite").string();
createDirs(dirOf(dbPath));
state->db = SQLite(dbPath, {.useWAL = nix::settings.useSQLiteWAL});
state->db.isCache();
+2 -2
View File
@@ -268,10 +268,10 @@ void Fetch::fetch(
return;
}
auto cacheDir = getCacheDir() / "git-lfs";
std::filesystem::path cacheDir = getCacheDir() / "git-lfs";
std::string key = hashString(HashAlgorithm::SHA256, pointerFilePath.rel()).to_string(HashFormat::Base16, false)
+ "/" + pointer->oid;
auto cachePath = cacheDir / key;
std::filesystem::path cachePath = cacheDir / key;
if (pathExists(cachePath)) {
debug("using cache entry %s -> %s", key, PathFmt(cachePath));
sink(readFile(cachePath));
+3 -4
View File
@@ -74,11 +74,11 @@ namespace nix {
struct GitSourceAccessor;
struct GitError final : public CloneableError<GitError, Error>
struct GitError : public Error
{
template<typename... Ts>
GitError(const git_error & error, Ts &&... args)
: CloneableError("")
: Error("")
{
auto hf = HintFmt(std::forward<Ts>(args)...);
err.msg = HintFmt("%1%: %2% (libgit2 error code = %3%)", Uncolored(hf.str()), error.message, error.klass);
@@ -247,8 +247,7 @@ static void initRepoAtomically(std::filesystem::path & path, GitRepo::Options op
|| e.code() == std::errc::directory_not_empty) {
return;
} else
throw SystemError(
e.code(), "moving temporary git repository from %s to %s", PathFmt(tmpDir), PathFmt(path));
throw SysError("moving temporary git repository from %s to %s", PathFmt(tmpDir), PathFmt(path));
}
// we successfully moved the repository, so the temporary directory no longer exists.
delTmpDir.cancel();
+2 -3
View File
@@ -44,9 +44,8 @@ static bool isCacheFileWithinTtl(const Settings & settings, time_t now, const Po
std::filesystem::path getCachePath(std::string_view key, bool shallow)
{
auto name =
hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) + (shallow ? "-shallow" : "");
return getCacheDir() / "gitv3" / std::move(name);
return getCacheDir() / "gitv3"
/ (hashString(HashAlgorithm::SHA256, key).to_string(HashFormat::Nix32, false) + (shallow ? "-shallow" : ""));
}
// Returns the name of the HEAD branch.
+1 -1
View File
@@ -31,7 +31,7 @@ namespace nix::flake {
// setting name -> setting value -> allow or ignore.
typedef std::map<std::string, std::map<std::string, bool>> TrustedList;
static std::filesystem::path trustedListPath()
std::filesystem::path trustedListPath()
{
return getDataDir() / "trusted-settings.json";
}
+1 -1
View File
@@ -73,7 +73,7 @@ std::function<void(siginfo_t * info, void * ctx)> stackOverflowHandler(defaultSt
void defaultStackOverflowHandler(siginfo_t * info, void * ctx)
{
char msg[] = "error: stack overflow (possible infinite recursion)\n";
[[gnu::unused]] auto res = ::write(2, msg, strlen(msg));
[[gnu::unused]] auto res = write(2, msg, strlen(msg));
_exit(1); // maybe abort instead?
}
@@ -1 +0,0 @@
C:\foo\bar\baz?trusted=true
+4
View File
@@ -13,6 +13,10 @@ using testing::Eq;
using testing::Field;
using testing::SizeIs;
namespace nix::fs {
using namespace std::filesystem;
}
using namespace nix;
TEST(machines, getMachinesWithEmptyBuilders)
+1 -19
View File
@@ -85,20 +85,6 @@ static StoreReference localExample_2{
},
};
#ifdef _WIN32
static StoreReference localExample_windows{
.variant =
StoreReference::Specified{
.scheme = "local",
.authority = "/C:/foo/bar/baz",
},
.params =
{
{"trusted", "true"},
},
};
#endif
static StoreReference localExample_3{
.variant =
StoreReference::Specified{
@@ -122,11 +108,7 @@ URI_TEST_READ(local_3_no_percent, localExample_3)
URI_TEST_READ(local_shorthand_1, localExample_1)
#ifndef _WIN32
URI_TEST_READ(local_shorthand_path_unix, localExample_2)
#else
URI_TEST_READ(local_shorthand_path_windows, localExample_windows)
#endif
URI_TEST_READ(local_shorthand_2, localExample_2)
URI_TEST(
local_shorthand_3,
+1 -1
View File
@@ -28,7 +28,7 @@
namespace nix {
AwsAuthError::AwsAuthError(int errorCode)
: CloneableError("AWS authentication error: '%s' (%d)", aws_error_str(errorCode), errorCode)
: Error("AWS authentication error: '%s' (%d)", aws_error_str(errorCode), errorCode)
, errorCode(errorCode)
{
}
+65 -120
View File
@@ -362,17 +362,25 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths)
return LocalBuildCapability{*localStoreP, ext};
}();
auto acquireResources = [&](bool & done, PathLocks & outputLocks) -> Goal::Co {
/**
* Activity that denotes waiting for a lock.
*/
std::unique_ptr<Activity> actLock;
/**
* Locks on (fixed) output paths.
*/
PathLocks outputLocks;
while (true) {
trace("trying to build");
/**
* Output paths to acquire locks on, if known a priori.
*
* The locks are automatically released when the caller's `PathLocks` goes
* out of scope, including on exception unwinding. If we can't acquire the lock, then
* continue; hopefully some other goal can start a build, and if not, the
* main loop will sleep a few seconds and then retry this goal.
*/
/* Obtain locks on all output paths, if the paths are known a priori.
The locks are automatically released when we exit this function or Nix
crashes. If we can't acquire the lock, then continue; hopefully some
other goal can start a build, and if not, the main loop will sleep a few
seconds and then retry this goal. */
std::set<std::filesystem::path> lockFiles;
/* FIXME: Should lock something like the drv itself so we don't build same
CA drv concurrently */
@@ -416,8 +424,7 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths)
debug("skipping build of derivation '%s', someone beat us to it", worker.store.printStorePath(drvPath));
outputLocks.setDeletion(true);
outputLocks.unlock();
done = true;
co_return Return{};
co_return doneSuccess(BuildResult::Success::AlreadyValid, std::move(validOutputs));
}
/* If any of the outputs already exist but are not valid, delete
@@ -432,133 +439,71 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths)
}
}
co_return Return{};
};
// Check and repair modes are only supported for local builds.
if (buildMode != bmNormal) {
// can try hook: false
// can try local: true
// preference: doesn't matter because it's only one option
break;
} else {
// can try hook: true
// can try local: true
// preference: unknown
auto tryHookLoop = [&](bool & valid) -> Goal::Co {
{
PathLocks outputLocks;
co_await acquireResources(valid, outputLocks);
if (valid)
co_return doneSuccess(BuildResult::Success::AlreadyValid, checkPathValidity(initialOutputs).second);
if (drvOptions.preferLocalBuild) {
// preference: local
if (std::holds_alternative<LocalBuildCapability>(localBuildResult)) {
// Can build locally as preferred, so do it.
break;
}
// We cannot build locally, but we can maybe do the hook.
}
// If we don't prefer a local build, OR we do but the local build was rejected, then we try the hook if
// possible.
switch (tryBuildHook(drvOptions)) {
case rpAccept:
/* Yes, it has started doing so. Wait until we get
EOF from the hook. */
valid = true;
actLock.reset();
co_return buildWithHook(
std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
case rpDecline:
// We should do it ourselves.
co_return Return{};
break;
case rpPostpone:
/* Not now; wait until at least one child finishes or
the wake-up timeout expires. */
break;
}
}
PathLocks outputLocks;
{
// First attempt was postponed. Retry in a loop with an activity
// that lives until accept or decline.
Activity act(
*logger,
lvlWarn,
actBuildWaiting,
fmt("waiting for a machine to build '%s'", Magenta(worker.store.printStorePath(drvPath))));
while (true) {
if (!actLock)
actLock = std::make_unique<Activity>(
*logger,
lvlWarn,
actBuildWaiting,
fmt("waiting for a machine to build '%s'", Magenta(worker.store.printStorePath(drvPath))));
outputLocks.unlock();
co_await waitForAWhile();
co_await acquireResources(valid, outputLocks);
if (valid)
break;
switch (tryBuildHook(drvOptions)) {
case rpAccept:
/* Yes, it has started doing so. Wait until we get
EOF from the hook. */
break;
case rpPostpone:
/* Not now; wait until at least one child finishes or
the wake-up timeout expires. */
outputLocks.unlock();
continue;
case rpDecline:
// We should do it ourselves.
co_return Return{};
}
continue;
case rpDecline:
// We should do it ourselves.
break;
}
}
if (valid) {
co_return doneSuccess(BuildResult::Success::AlreadyValid, checkPathValidity(initialOutputs).second);
} else {
co_return buildWithHook(
std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
}
};
auto tryBuildLocally = [&](bool & valid) -> Goal::Co {
if (auto * cap = std::get_if<LocalBuildCapability>(&localBuildResult)) {
PathLocks outputLocks;
co_await acquireResources(valid, outputLocks);
if (valid)
co_return doneSuccess(BuildResult::Success::AlreadyValid, checkPathValidity(initialOutputs).second);
valid = true;
co_return buildLocally(
*cap, std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
}
co_return Return{};
};
if (buildMode != bmNormal) {
// Check and repair modes operate on the state of this store specifically,
// so they must always build locally.
bool valid = false;
co_await tryBuildLocally(valid);
if (valid)
co_return Return{};
} else if (drvOptions.preferLocalBuild) {
// Local is preferred, so try it first. If it's not available, fall back to the hook.
{
bool valid = false;
co_await tryBuildLocally(valid);
if (valid)
co_return Return{};
}
{
bool valid = false;
co_await tryHookLoop(valid);
if (valid)
co_return Return{};
}
} else {
// Default preference is a remote build: they tend to be faster and preserve local
// resources for other tasks. Fall back to local if no remote is available.
{
bool valid = false;
co_await tryHookLoop(valid);
if (valid)
co_return Return{};
}
{
bool valid = false;
co_await tryBuildLocally(valid);
if (valid)
co_return Return{};
}
break;
}
std::string storePath = worker.store.printStorePath(drvPath);
auto * rejection = std::get_if<LocalBuildRejection>(&localBuildResult);
assert(rejection);
co_return doneFailure(reject(*rejection, storePath));
// can do hook: false (either we can't try, or we tried and were declined)
// can try local: true
// preference: doesn't matter
actLock.reset();
if (auto * cap = std::get_if<LocalBuildCapability>(&localBuildResult)) {
co_return buildLocally(
*cap, std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
} else {
outputLocks.unlock();
std::string storePath = worker.store.printStorePath(drvPath);
co_return doneFailure(reject(std::get<LocalBuildRejection>(localBuildResult), storePath));
}
}
Goal::Co DerivationBuildingGoal::buildWithHook(
+2 -2
View File
@@ -1,11 +1,11 @@
#include "nix/store/build/goal.hh"
#include "nix/store/build/worker.hh"
#include "nix/store/worker-settings.hh"
#include "nix/store/globals.hh"
namespace nix {
TimedOut::TimedOut(time_t maxDuration)
: CloneableError(BuildResult::Failure::TimedOut, "timed out after %1% seconds", maxDuration)
: BuildError(BuildResult::Failure::TimedOut, "timed out after %1% seconds", maxDuration)
, maxDuration(maxDuration)
{
}
-7
View File
@@ -20,9 +20,6 @@ Worker::Worker(Store & store, Store & evalStore)
: act(*logger, actRealise)
, actDerivations(*logger, actBuilds)
, actSubstitutions(*logger, actCopyPaths)
#ifdef _WIN32
, ioport{CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0)}
#endif
, store(store)
, evalStore(evalStore)
, settings(nix::settings.getWorkerSettings())
@@ -30,10 +27,6 @@ Worker::Worker(Store & store, Store & evalStore)
return nix::settings.getWorkerSettings().useSubstitutes ? getDefaultSubstituters() : std::list<ref<Store>>{};
}}
{
#ifdef _WIN32
if (!ioport)
throw windows::WinError("CreateIoCompletionPort");
#endif
nrLocalBuilds = 0;
nrSubstitutions = 0;
lastWokenUp = steady_time_point::min();
+2 -4
View File
@@ -27,8 +27,6 @@ static void builtinUnpackChannel(const BuiltinBuilderContext & ctx)
size_t fileCount;
std::string fileName;
auto entries = DirectoryIterator{out};
if (entries == DirectoryIterator{})
throw Error("channel tarball '%s' is empty", src);
fileName = entries->path().string();
fileCount = std::distance(entries.begin(), entries.end());
@@ -38,8 +36,8 @@ static void builtinUnpackChannel(const BuiltinBuilderContext & ctx)
auto target = out / channelName;
try {
std::filesystem::rename(fileName, target);
} catch (std::filesystem::filesystem_error & e) {
throw SystemError(e.code(), "failed to rename %1% to %2%", fileName, target.string());
} catch (std::filesystem::filesystem_error &) {
throw SysError("failed to rename %1% to %2%", fileName, target.string());
}
}
+3 -3
View File
@@ -60,12 +60,12 @@ namespace {
using curlSList = std::unique_ptr<::curl_slist, decltype([](::curl_slist * list) { ::curl_slist_free_all(list); })>;
using curlMulti = std::unique_ptr<::CURLM, decltype([](::CURLM * multi) { ::curl_multi_cleanup(multi); })>;
struct curlMultiError final : CloneableError<curlMultiError, Error>
struct curlMultiError : Error
{
::CURLMcode code;
curlMultiError(::CURLMcode code)
: CloneableError{"unexpected curl multi error: %s", ::curl_multi_strerror(code)}
: Error{"unexpected curl multi error: %s", ::curl_multi_strerror(code)}
{
assert(code != CURLM_OK);
}
@@ -1212,7 +1212,7 @@ void FileTransfer::download(
template<typename... Args>
FileTransferError::FileTransferError(
FileTransfer::Error error, std::optional<std::string> response, const Args &... args)
: CloneableError(args...)
: Error(args...)
, error(error)
, response(response)
{
+1 -1
View File
@@ -272,7 +272,7 @@ void LocalStore::findRoots(const Path & path, std::filesystem::file_type type, R
|| e.code() == std::errc::not_a_directory)
printInfo("cannot read potential root '%1%'", path);
else
throw SystemError(e.code(), "finding GC roots in '%1%'", path);
throw;
}
catch (SystemError & e) {
+2 -3
View File
@@ -34,13 +34,12 @@ struct AwsCredentials
}
};
class AwsAuthError final : public CloneableError<AwsAuthError, Error>
class AwsAuthError : public Error
{
std::optional<int> errorCode;
public:
using CloneableError::CloneableError;
using Error::Error;
AwsAuthError(int errorCode);
std::optional<int> getErrorCode() const
@@ -58,7 +58,7 @@ enum struct BuildResultFailureStatus : uint8_t {
* This is both an exception type (inherits from Error) and serves as
* the failure variant in BuildResult::inner.
*/
struct BuildError : public CloneableError<BuildError, Error>
struct BuildError : public Error
{
using Status = BuildResultFailureStatus;
using enum Status;
@@ -80,7 +80,7 @@ public:
*/
template<typename... Args>
BuildError(Status status, const Args &... args)
: CloneableError(args...)
: Error(args...)
, status{status}
{
}
@@ -97,7 +97,7 @@ public:
* Also used for deserialization.
*/
BuildError(Args args)
: CloneableError(std::move(args.msg))
: Error(std::move(args.msg))
, status{args.status}
, isNonDeterministic{args.isNonDeterministic}
@@ -108,7 +108,7 @@ public:
* Default constructor for deserialization.
*/
BuildError()
: CloneableError("")
: Error("")
{
}
@@ -20,14 +20,14 @@ namespace nix {
* Denotes a build failure that stemmed from the builder exiting with a
* failing exist status.
*/
struct BuilderFailureError final : CloneableError<BuilderFailureError, BuildError>
struct BuilderFailureError : BuildError
{
int builderStatus;
std::string extraMsgAfter;
BuilderFailureError(BuildResult::Failure::Status status, int builderStatus, std::string extraMsgAfter)
: CloneableError{
: BuildError{
status,
/* No message for now, because the caller will make for
us, with extra context */
+1 -1
View File
@@ -10,7 +10,7 @@
namespace nix {
struct TimedOut final : CloneableError<TimedOut, BuildError>
struct TimedOut : BuildError
{
time_t maxDuration;
@@ -22,7 +22,7 @@ struct Package
}
};
class BuildEnvFileConflictError final : public CloneableError<BuildEnvFileConflictError, Error>
class BuildEnvFileConflictError : public Error
{
public:
const Path fileA;
@@ -30,7 +30,7 @@ public:
int priority;
BuildEnvFileConflictError(const Path fileA, const Path fileB, int priority)
: CloneableError(
: Error(
"Unable to build profile. There is a conflict for the following files:\n"
"\n"
" %1%\n"
@@ -403,7 +403,7 @@ ref<FileTransfer> getFileTransfer();
*/
ref<FileTransfer> makeFileTransfer(const FileTransferSettings & settings = fileTransferSettings);
class FileTransferError final : public CloneableError<FileTransferError, Error>
class FileTransferError : public Error
{
public:
FileTransfer::Error error;
-11
View File
@@ -86,17 +86,6 @@ struct NarInfoDiskCacheSettings : public virtual Config
would prevent trying to pull the path again and failing with a hash
mismatch if the build isn't reproducible.
)"};
Setting<unsigned int> ttlMeta{
this,
7 * 24 * 3600,
"narinfo-cache-meta-ttl",
R"(
The TTL in seconds for caching binary cache metadata (i.e.
`/nix-cache-info`). This determines how long information about a
binary cache (such as its store directory, priority, and whether it
wants mass queries) is considered valid before being refreshed.
)"};
};
class Settings : public virtual Config,
@@ -14,7 +14,7 @@ struct LocalBinaryCacheStoreConfig : std::enable_shared_from_this<LocalBinaryCac
*/
LocalBinaryCacheStoreConfig(std::string_view scheme, PathView binaryCacheDir, const Params & params);
std::filesystem::path binaryCacheDir;
Path binaryCacheDir;
static const std::string name()
{
@@ -146,7 +146,7 @@ struct RealisedPath
auto operator<=>(const RealisedPath &) const = default;
};
class MissingRealisation final : public CloneableError<MissingRealisation, Error>
class MissingRealisation : public Error
{
public:
MissingRealisation(DrvOutput & outputId)
@@ -155,7 +155,7 @@ public:
}
MissingRealisation(std::string_view drv, OutputName outputName)
: CloneableError(
: Error(
"cannot operate on output '%s' of the "
"unbuilt derivation '%s'",
outputName,
+1 -1
View File
@@ -166,7 +166,7 @@ struct SQLiteTxn
~SQLiteTxn();
};
struct SQLiteError : CloneableError<SQLiteError, Error>
struct SQLiteError : Error
{
std::string path;
std::string errMsg;
+8 -31
View File
@@ -3,32 +3,11 @@
#include "nix/store/nar-info-disk-cache.hh"
#include "nix/util/signals.hh"
#include "nix/store/store-registration.hh"
#include "nix/util/url.hh"
#include <atomic>
namespace nix {
static std::filesystem::path checkBinaryCachePath(const std::filesystem::path & root, std::string_view path)
{
/* Note: these checks aren't complete and don't guard against symlink shenanigans. */
auto p = std::filesystem::path(path);
if (p.is_absolute())
/* Never happens unless the caller is messed up. */
throw Error("binary cache path '%s' must be relative", path);
auto result = (root / p).lexically_normal();
/* NB: lexically_normal() only does textual normalization and does
not resolve symlinks. This is acceptable because store/substituter
paths are already trusted, and this check is defense-in-depth
against ".." traversal. */
if (!isInDir(result, root.lexically_normal()))
throw Error("binary cache path '%s' escapes cache directory", path);
return result;
}
LocalBinaryCacheStoreConfig::LocalBinaryCacheStoreConfig(
std::string_view scheme, PathView binaryCacheDir, const StoreReference::Params & params)
: Store::Config{params}
@@ -50,7 +29,7 @@ StoreReference LocalBinaryCacheStoreConfig::getReference() const
.variant =
StoreReference::Specified{
.scheme = "file",
.authority = encodeUrlPath(pathToUrlPath(binaryCacheDir)),
.authority = binaryCacheDir,
},
};
}
@@ -77,9 +56,7 @@ protected:
void upsertFile(
const std::string & path, RestartableSource & source, const std::string & mimeType, uint64_t sizeHint) override
{
/* TODO: Maybe use RestoreSink for writing stuff? It would have to gain the ability to write files
atomically (maybe with O_TMPFILE + linkat + AT_EMPTY_PATH when available or fallback to rename). */
auto path2 = checkBinaryCachePath(config->binaryCacheDir, path);
auto path2 = std::filesystem::path{config->binaryCacheDir} / path;
static std::atomic<int> counter{0};
createDirs(path2.parent_path());
auto tmp = path2;
@@ -93,7 +70,7 @@ protected:
void getFile(const std::string & path, Sink & sink) override
{
try {
readFile(checkBinaryCachePath(config->binaryCacheDir, path), sink);
readFile(config->binaryCacheDir + "/" + path, sink);
} catch (SystemError & e) {
if (e.is(std::errc::no_such_file_or_directory))
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path);
@@ -124,17 +101,17 @@ protected:
void LocalBinaryCacheStore::init()
{
createDirs(config->binaryCacheDir / "nar");
createDirs(config->binaryCacheDir / realisationsPrefix);
createDirs(config->binaryCacheDir + "/nar");
createDirs(config->binaryCacheDir + "/" + realisationsPrefix);
if (config->writeDebugInfo)
createDirs(config->binaryCacheDir / "debuginfo");
createDirs(config->binaryCacheDir / "log");
createDirs(config->binaryCacheDir + "/debuginfo");
createDirs(config->binaryCacheDir + "/log");
BinaryCacheStore::init();
}
bool LocalBinaryCacheStore::fileExists(const std::string & path)
{
return pathExists(checkBinaryCachePath(config->binaryCacheDir, path));
return pathExists(config->binaryCacheDir + "/" + path);
}
StringSet LocalBinaryCacheStoreConfig::uriSchemes()
+1 -1
View File
@@ -35,7 +35,7 @@ static void readProcLink(const std::filesystem::path & file, UncheckedRoots & ro
if (e.code() == std::errc::no_such_file_or_directory || e.code() == std::errc::permission_denied
|| e.code() == std::errc::no_such_process)
return;
throw SystemError(e.code(), "reading symlink '%s'", PathFmt(file));
throw;
}
if (buf.is_absolute())
roots[buf.string()].emplace(file.string());
+4 -1
View File
@@ -144,7 +144,10 @@ LocalStore::LocalStore(ref<const Config> config)
Path gcRootsDir = config->stateDir + "/gcroots";
const auto & localSettings = config->getLocalSettings();
const auto & gcSettings = localSettings.getGCSettings();
createDirs(gcRootsDir);
if (!pathExists(gcRootsDir)) {
createDirs(gcRootsDir);
replaceSymlink(profilesDir, gcRootsDir + "/profiles");
}
for (auto & perUserDir : {profilesDir + "/per-user", gcRootsDir + "/per-user"}) {
createDirs(perUserDir);
+2 -2
View File
@@ -111,8 +111,8 @@ static std::vector<std::string> expandBuilderLines(const std::string & builders)
std::string text;
try {
text = readFile(path);
} catch (const SystemError & e) {
if (!e.is(std::errc::no_such_file_or_directory))
} catch (const SysError & e) {
if (e.errNo != ENOENT)
throw;
debug("cannot find machines file '%s'", path);
continue;
+4 -1
View File
@@ -63,6 +63,9 @@ struct NarInfoDiskCacheImpl : NarInfoDiskCache
/* How often to purge expired entries from the cache. */
const int purgeInterval = 24 * 3600;
/* How long to cache binary cache info (i.e. /nix-cache-info) */
const int cacheInfoTtl = 7 * 24 * 3600;
struct Cache
{
int id;
@@ -181,7 +184,7 @@ private:
{
auto i = state.caches.find(uri);
if (i == state.caches.end()) {
auto queryCache(state.queryCache.use()(uri)(time(0) - settings.ttlMeta));
auto queryCache(state.queryCache.use()(uri)(time(0) - cacheInfoTtl));
if (!queryCache.next())
return std::nullopt;
auto cache = Cache{
+5 -4
View File
@@ -202,13 +202,14 @@ void LocalStore::optimisePath_(
full. When that happens, it's fine to ignore it: we
just effectively disable deduplication of this
file.
TODO: Get rid of errno, use error code.
*/
printInfo("cannot link %s to '%s': %s", PathFmt(linkPath), path, e.code().message());
printInfo("cannot link %s to '%s': %s", PathFmt(linkPath), path, strerror(errno));
return;
}
else
throw SystemError(e.code(), "creating hard link from %1% to %2%", PathFmt(linkPath), path);
throw;
}
}
@@ -249,7 +250,7 @@ void LocalStore::optimisePath_(
printInfo("%1% has maximum number of links", PathFmt(linkPath));
return;
}
throw SystemError(e.code(), "creating hard link from %1% to %2%", PathFmt(linkPath), PathFmt(tempLink));
throw;
}
/* Atomically replace the old file with the new hard link. */
@@ -270,7 +271,7 @@ void LocalStore::optimisePath_(
debug("%s has reached maximum number of links", PathFmt(linkPath));
return;
}
throw SystemError(e.code(), "renaming %1% to %2%", PathFmt(tempLink), path);
throw;
}
stats.filesLinked++;
+8 -4
View File
@@ -103,7 +103,7 @@ static void removeFile(const std::filesystem::path & path)
try {
std::filesystem::remove(path);
} catch (std::filesystem::filesystem_error & e) {
throw SystemError(e.code(), "removing file %1%", PathFmt(path));
throw SysError("removing file %1%", PathFmt(path));
}
}
@@ -293,7 +293,8 @@ std::string optimisticLockProfile(const std::filesystem::path & profile)
std::filesystem::path profilesDir(ProfileDirsOptions settings)
{
auto profileRoot = isRootUser() ? rootProfilesDir(settings) : createNixStateDir() / "profiles";
auto profileRoot =
isRootUser() ? rootProfilesDir(settings) : std::filesystem::path{createNixStateDir()} / "profiles";
createDirs(profileRoot);
return profileRoot;
}
@@ -305,8 +306,9 @@ std::filesystem::path rootProfilesDir(ProfileDirsOptions settings)
std::filesystem::path getDefaultProfile(ProfileDirsOptions settings)
{
std::filesystem::path profileLink =
settings.useXDGBaseDirectories ? createNixStateDir() / "profile" : getHome() / ".nix-profile";
std::filesystem::path profileLink = settings.useXDGBaseDirectories
? std::filesystem::path{createNixStateDir()} / "profile"
: std::filesystem::path{getHome()} / ".nix-profile";
try {
auto profile = profilesDir(settings) / "profile";
if (!pathExists(profileLink)) {
@@ -322,6 +324,8 @@ std::filesystem::path getDefaultProfile(ProfileDirsOptions settings)
return absPath(readLink(profileLink), &linkDir);
} catch (Error &) {
return profileLink;
} catch (std::filesystem::filesystem_error &) {
return profileLink;
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ namespace nix {
SQLiteError::SQLiteError(
const char * path, const char * errMsg, int errNo, int extendedErrNo, int offset, HintFmt && hf)
: CloneableError("")
: Error("")
, path(path)
, errMsg(errMsg)
, errNo(errNo)
+3 -3
View File
@@ -18,11 +18,11 @@ static std::string parsePublicHostKey(std::string_view host, std::string_view ss
}
}
class InvalidSSHAuthority final : public CloneableError<InvalidSSHAuthority, Error>
class InvalidSSHAuthority : public Error
{
public:
InvalidSSHAuthority(const ParsedURL::Authority & authority, std::string_view reason)
: CloneableError("invalid SSH authority: '%s': %s", authority.to_string(), reason)
: Error("invalid SSH authority: '%s': %s", authority.to_string(), reason)
{
}
};
@@ -98,7 +98,7 @@ void SSHMaster::addCommonSSHOpts(Strings & args)
args.insert(args.end(), {"-i", keyFile});
if (!sshPublicHostKey.empty()) {
std::filesystem::path fileName = tmpDir->path() / "host-key";
writeFile(fileName, authority.host + " " + sshPublicHostKey + "\n");
writeFile(fileName.string(), authority.host + " " + sshPublicHostKey + "\n");
args.insert(args.end(), {"-oUserKnownHostsFile=" + fileName.string()});
}
if (compress)
+1 -7
View File
@@ -1190,14 +1190,8 @@ Derivation Store::derivationFromPath(const StorePath & drvPath)
static Derivation readDerivationCommon(Store & store, const StorePath & drvPath, bool requireValidPath)
{
auto accessor = store.requireStoreObjectAccessor(drvPath, requireValidPath);
auto contents = accessor->readFile(CanonPath::root);
try {
/* Special case for an empty file to show the user a better message */
if (contents.empty())
throw FormatError("file is empty (possible filesystem corruption)");
return parseDerivation(store, std::move(contents), Derivation::nameFromPath(drvPath));
return parseDerivation(store, accessor->readFile(CanonPath::root), Derivation::nameFromPath(drvPath));
} catch (FormatError & e) {
throw Error("error parsing derivation '%s': %s", store.printStorePath(drvPath), e.msg());
}
+2 -3
View File
@@ -1,5 +1,4 @@
#include "nix/util/error.hh"
#include "nix/util/file-path-impl.hh"
#include "nix/util/split.hh"
#include "nix/util/url.hh"
#include "nix/store/store-reference.hh"
@@ -18,7 +17,7 @@ static bool isNonUriPath(const std::string & spec)
spec.find("://") == std::string::npos
// Has at least one path separator, and so isn't a single word that
// might be special like "auto"
&& OsPathTrait<char>::findPathSep(spec) != std::string::npos;
&& spec.find("/") != std::string::npos;
}
std::string StoreReference::render(bool withParams) const
@@ -112,7 +111,7 @@ StoreReference StoreReference::parse(const std::string & uri, const StoreReferen
.variant =
Specified{
.scheme = "local",
.authority = encodeUrlPath(pathToUrlPath(absPath(std::filesystem::path{baseURI}))),
.authority = absPath(baseURI),
},
.params = std::move(params),
};
+4 -5
View File
@@ -42,18 +42,17 @@ ref<StoreConfig> resolveStoreConfig(StoreReference && storeURI)
/* If /nix doesn't exist, there is no daemon socket, and
we're not root, then automatically set up a chroot
store in ~/.local/share/nix/root. */
auto chrootStore = getDataDir() / "root";
auto chrootStore = getDataDir() + "/root";
if (!pathExists(chrootStore)) {
try {
createDirs(chrootStore);
} catch (SystemError & e) {
return make_ref<LocalStore::Config>(params);
}
warn("%s does not exist, so Nix will use %s as a chroot store", stateDir, PathFmt(chrootStore));
warn("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore);
} else
debug(
"%s does not exist, so Nix will use %s as a chroot store", stateDir, PathFmt(chrootStore));
return make_ref<LocalStore::Config>("local", chrootStore.string(), params);
debug("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore);
return make_ref<LocalStore::Config>("local", chrootStore, params);
}
#endif
else
@@ -1,5 +1,7 @@
#ifdef __linux__
# include "chroot.hh"
namespace nix {
struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
@@ -11,14 +13,14 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
}
/**
* The root of the chroot environment.
* The chroot root directory.
*/
std::filesystem::path chrootRootDir;
/**
* RAII object to delete the chroot directory.
* RAII cleanup for the chroot directory.
*/
std::shared_ptr<AutoDelete> autoDelChroot;
std::optional<AutoDelete> autoDelChroot;
PathsInChroot pathsInChroot;
@@ -54,78 +56,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
void prepareSandbox() override
{
/* Create a temporary directory in which we set up the chroot
environment using bind-mounts. We put it in the Nix store
so that the build outputs can be moved efficiently from the
chroot to their final location. */
std::filesystem::path chrootParentDir = store.toRealPath(drvPath) + ".chroot";
deletePath(chrootParentDir);
/* Clean up the chroot directory automatically. */
autoDelChroot = std::make_shared<AutoDelete>(chrootParentDir);
printMsg(lvlChatty, "setting up chroot environment in %1%", PathFmt(chrootParentDir));
if (mkdir(chrootParentDir.c_str(), 0700) == -1)
throw SysError("cannot create %s", PathFmt(chrootRootDir));
chrootRootDir = chrootParentDir / "root";
if (mkdir(chrootRootDir.c_str(), buildUser && buildUser->getUIDCount() != 1 ? 0755 : 0750) == -1)
throw SysError("cannot create %1%", PathFmt(chrootRootDir));
if (buildUser
&& chown(
chrootRootDir.c_str(), buildUser->getUIDCount() != 1 ? buildUser->getUID() : 0, buildUser->getGID())
== -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootRootDir));
/* Create a writable /tmp in the chroot. Many builders need
this. (Of course they should really respect $TMPDIR
instead.) */
std::filesystem::path chrootTmpDir = chrootRootDir / "tmp";
createDirs(chrootTmpDir);
chmod(chrootTmpDir, 01777);
/* Create a /etc/passwd with entries for the build user and the
nobody account. The latter is kind of a hack to support
Samba-in-QEMU. */
createDirs(chrootRootDir / "etc");
if (drvOptions.useUidRange(drv))
chownToBuilder(chrootRootDir / "etc");
if (drvOptions.useUidRange(drv) && (!buildUser || buildUser->getUIDCount() < 65536))
throw Error(
"feature 'uid-range' requires the setting '%s' to be enabled",
store.config->getLocalSettings().autoAllocateUids.name);
/* Declare the build user's group so that programs get a consistent
view of the system (e.g., "id -gn"). */
writeFile(
chrootRootDir / "etc" / "group",
fmt("root:x:0:\n"
"nixbld:!:%1%:\n"
"nogroup:x:65534:\n",
sandboxGid()));
/* Create /etc/hosts with localhost entry. */
if (derivationType.isSandboxed())
writeFile(chrootRootDir / "etc" / "hosts", "127.0.0.1 localhost\n::1 localhost\n");
/* Make the closure of the inputs available in the chroot,
rather than the whole Nix store. This prevents any access
to undeclared dependencies. Directories are bind-mounted,
while other inputs are hard-linked (since only directories
can be bind-mounted). !!! As an extra security
precaution, make the fake Nix store only writable by the
build user. */
std::filesystem::path chrootStoreDir = chrootRootDir / std::filesystem::path(store.storeDir).relative_path();
createDirs(chrootStoreDir);
chmod(chrootStoreDir, 01775);
if (buildUser && chown(chrootStoreDir.c_str(), 0, buildUser->getGID()) == -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootStoreDir));
// Start with the default sandbox paths
pathsInChroot = getPathsInSandbox();
for (auto & i : inputPaths) {
@@ -147,6 +78,22 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
if (i.second.second)
pathsInChroot.erase(store.printStorePath(*i.second.second));
}
// Set up chroot parameters
BuildChrootParams params{
.chrootParentDir = store.toRealPath(drvPath) + ".chroot",
.useUidRange = drvOptions.useUidRange(drv),
.isSandboxed = derivationType.isSandboxed(),
.buildUser = buildUser.get(),
.storeDir = store.storeDir,
.chownToBuilder = [this](const std::filesystem::path & path) { this->chownToBuilder(path); },
.getSandboxGid = [this]() { return this->sandboxGid(); },
};
// Create the chroot
auto [rootDir, cleanup] = setupBuildChroot(params);
chrootRootDir = std::move(rootDir);
autoDelChroot.emplace(std::move(cleanup));
}
Strings getPreBuildHookArgs() override
@@ -166,21 +113,23 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
{
DerivationBuilderImpl::cleanupBuild(force);
/* Move paths out of the chroot for easier debugging of
build failures. */
if (!force && buildMode == bmNormal)
for (auto & [_, status] : initialOutputs) {
if (!status.known)
continue;
if (buildMode != bmCheck && status.known->isValid())
continue;
std::filesystem::path p = store.toRealPath(status.known->path);
std::filesystem::path chrootPath = chrootRootDir / p.relative_path();
if (pathExists(chrootPath))
std::filesystem::rename(chrootPath, p);
}
if (autoDelChroot) {
/* Move paths out of the chroot for easier debugging of
build failures. */
if (!force && buildMode == bmNormal)
for (auto & [_, status] : initialOutputs) {
if (!status.known)
continue;
if (buildMode != bmCheck && status.known->isValid())
continue;
std::filesystem::path p = store.toRealPath(status.known->path);
std::filesystem::path chrootPath = chrootRootDir / p.relative_path();
if (pathExists(chrootPath))
std::filesystem::rename(chrootPath, p);
}
autoDelChroot.reset(); /* this runs the destructor */
autoDelChroot.reset();
}
}
std::pair<std::filesystem::path, std::filesystem::path> addDependencyPrep(const StorePath & path)
+87
View File
@@ -0,0 +1,87 @@
#include "chroot.hh"
#include "nix/util/file-system.hh"
#include "nix/store/globals.hh"
#include "nix/util/logging.hh"
#include "nix/util/error.hh"
#include <sys/stat.h>
namespace nix {
std::pair<std::filesystem::path, AutoDelete> setupBuildChroot(const BuildChrootParams & params)
{
/* Create a temporary directory in which we set up the chroot
environment using bind-mounts. We put it in the Nix store
so that the build outputs can be moved efficiently from the
chroot to their final location. */
auto chrootParentDir = params.chrootParentDir;
deletePath(chrootParentDir);
printMsg(lvlChatty, "setting up chroot environment in %1%", PathFmt(chrootParentDir));
if (mkdir(chrootParentDir.c_str(), 0700) == -1)
throw SysError("cannot create %s", PathFmt(chrootParentDir));
std::filesystem::path chrootRootDir = chrootParentDir / "root";
if (mkdir(chrootRootDir.c_str(), params.buildUser && params.buildUser->getUIDCount() != 1 ? 0755 : 0750) == -1)
throw SysError("cannot create %1%", PathFmt(chrootRootDir));
if (params.buildUser
&& chown(
chrootRootDir.c_str(),
params.buildUser->getUIDCount() != 1 ? params.buildUser->getUID() : 0,
params.buildUser->getGID())
== -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootRootDir));
/* Create a writable /tmp in the chroot. Many builders need
this. (Of course they should really respect $TMPDIR
instead.) */
std::filesystem::path chrootTmpDir = chrootRootDir / "tmp";
createDirs(chrootTmpDir);
chmod(chrootTmpDir, 01777);
/* Create a /etc/passwd with entries for the build user and the
nobody account. The latter is kind of a hack to support
Samba-in-QEMU. */
createDirs(chrootRootDir / "etc");
if (params.useUidRange)
params.chownToBuilder(chrootRootDir / "etc");
if (params.useUidRange && (!params.buildUser || params.buildUser->getUIDCount() < 65536))
throw Error(
"feature 'uid-range' requires the setting '%s' to be enabled",
settings.getLocalSettings().autoAllocateUids.name);
/* Declare the build user's group so that programs get a consistent
view of the system (e.g., "id -gn"). */
writeFile(
chrootRootDir / "etc" / "group",
fmt("root:x:0:\n"
"nixbld:!:%1%:\n"
"nogroup:x:65534:\n",
params.getSandboxGid()));
/* Create /etc/hosts with localhost entry. */
if (params.isSandboxed)
writeFile(chrootRootDir / "etc" / "hosts", "127.0.0.1 localhost\n::1 localhost\n");
/* Make the closure of the inputs available in the chroot,
rather than the whole Nix store. This prevents any access
to undeclared dependencies. Directories are bind-mounted,
while other inputs are hard-linked (since only directories
can be bind-mounted). !!! As an extra security
precaution, make the fake Nix store only writable by the
build user. */
std::filesystem::path chrootStoreDir = chrootRootDir / std::filesystem::path(params.storeDir).relative_path();
createDirs(chrootStoreDir);
chmod(chrootStoreDir, 01775);
if (params.buildUser && chown(chrootStoreDir.c_str(), 0, params.buildUser->getGID()) == -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootStoreDir));
return {std::move(chrootRootDir), AutoDelete(chrootParentDir)};
}
} // namespace nix
+55
View File
@@ -0,0 +1,55 @@
#pragma once
///@file
#include <filesystem>
#include <map>
#include <memory>
#include <functional>
#include "nix/store/build/derivation-builder.hh"
#include "nix/util/file-system.hh"
#include "nix/store/user-lock.hh"
#include "nix/store/store-api.hh"
namespace nix {
/**
* Parameters for setting up a chroot environment.
*/
struct BuildChrootParams
{
/** The directory where the chroot will be created */
std::filesystem::path chrootParentDir;
/** Whether the derivation uses UID range feature */
bool useUidRange;
/** Whether the derivation type is sandboxed */
bool isSandboxed;
/** Build user (may be null if not using a build user) */
UserLock * buildUser;
/** The store directory (e.g., "/nix/store") */
std::string storeDir;
/** Callback to change ownership of a path to the build user */
std::function<void(const std::filesystem::path &)> chownToBuilder;
/** Function to get the sandbox GID */
std::function<gid_t()> getSandboxGid;
};
/**
* Set up a chroot build environment.
*
* Creates the chroot directory structure and sets up necessary directories
* (/tmp, /etc, store directory). Returns the chroot root path and an AutoDelete
* that will clean up the chroot directory when destroyed.
*
* @param params Parameters for chroot setup
* @return Pair of (chroot root path, cleanup object)
*/
std::pair<std::filesystem::path, AutoDelete> setupBuildChroot(const BuildChrootParams & params);
} // namespace nix
@@ -55,10 +55,10 @@
namespace nix {
struct NotDeterministic final : CloneableError<NotDeterministic, BuildError>
struct NotDeterministic : BuildError
{
NotDeterministic(auto &&... args)
: CloneableError(BuildResult::Failure::NotDeterministic, args...)
: BuildError(BuildResult::Failure::NotDeterministic, args...)
{
isNonDeterministic = true;
}
@@ -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)};
if (!fd)
throw SysError("creating file %s", PathFmt(path));
writeFile(fd.get(), contents);
writeFile(fd, path, contents);
chownToBuilder(fd.get(), path);
}
@@ -26,6 +26,8 @@
namespace nix {
using namespace nix::linux;
static void setupSeccomp(const LocalSettings & localSettings)
{
if (!localSettings.filterSyscalls)
+1
View File
@@ -1,5 +1,6 @@
sources += files(
'build/child.cc',
'build/chroot.cc',
'build/derivation-builder.cc',
'build/hook-instance.cc',
'pathlocks.cc',
+16 -30
View File
@@ -1,9 +1,7 @@
#include "nix/util/file-system.hh"
#include "nix/util/logging.hh"
#include "nix/store/pathlocks.hh"
#include "nix/util/signals.hh"
#include "nix/util/util.hh"
#include "nix/util/windows-environment.hh"
#ifdef _WIN32
# include <errhandlingapi.h>
@@ -53,43 +51,31 @@ AutoCloseFD openLockFile(const std::filesystem::path & path, bool create)
return desc;
}
/**
* Throw a WinError, or if running under Wine, just warn and return true.
* Wine has incomplete file locking support, so we degrade gracefully.
*/
template<typename... Args>
static bool warnOrThrowWine(DWORD lastError, const std::string & fs, const Args &... args)
{
if (isWine()) {
warn(fs + ": %s (ignored under Wine)", args..., lastError);
return true;
}
throw WinError(lastError, fs, args...);
}
bool lockFile(Descriptor desc, LockType lockType, bool wait)
{
switch (lockType) {
case ltNone: {
OVERLAPPED ov = {0};
if (!UnlockFileEx(desc, 0, 2, 0, &ov))
return warnOrThrowWine(GetLastError(), "Failed to unlock file %s", PathFmt(descriptorToPath(desc)));
if (!UnlockFileEx(desc, 0, 2, 0, &ov)) {
WinError winError("Failed to unlock file desc %s", desc);
throw winError;
}
return true;
}
case ltRead: {
OVERLAPPED ov = {0};
if (!LockFileEx(desc, wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &ov)) {
auto lastError = GetLastError();
if (lastError == ERROR_LOCK_VIOLATION)
WinError winError("Failed to lock file desc %s", desc);
if (winError.lastError == ERROR_LOCK_VIOLATION)
return false;
return warnOrThrowWine(lastError, "Failed to lock file %s", PathFmt(descriptorToPath(desc)));
throw winError;
}
ov.Offset = 1;
if (!UnlockFileEx(desc, 0, 1, 0, &ov)) {
auto lastError = GetLastError();
if (lastError != ERROR_NOT_LOCKED)
return warnOrThrowWine(lastError, "Failed to unlock file %s", PathFmt(descriptorToPath(desc)));
WinError winError("Failed to unlock file desc %s", desc);
if (winError.lastError != ERROR_NOT_LOCKED)
throw winError;
}
return true;
}
@@ -97,17 +83,17 @@ bool lockFile(Descriptor desc, LockType lockType, bool wait)
OVERLAPPED ov = {0};
ov.Offset = 1;
if (!LockFileEx(desc, LOCKFILE_EXCLUSIVE_LOCK | (wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), 0, 1, 0, &ov)) {
auto lastError = GetLastError();
if (lastError == ERROR_LOCK_VIOLATION)
WinError winError("Failed to lock file desc %s", desc);
if (winError.lastError == ERROR_LOCK_VIOLATION)
return false;
return warnOrThrowWine(lastError, "Failed to lock file %s", PathFmt(descriptorToPath(desc)));
throw winError;
}
ov.Offset = 0;
if (!UnlockFileEx(desc, 0, 1, 0, &ov)) {
auto lastError = GetLastError();
if (lastError != ERROR_NOT_LOCKED)
return warnOrThrowWine(lastError, "Failed to unlock file %s", PathFmt(descriptorToPath(desc)));
WinError winError("Failed to unlock file desc %s", desc);
if (winError.lastError != ERROR_NOT_LOCKED)
throw winError;
}
return true;
}
-7
View File
@@ -109,13 +109,6 @@ enum nix_err {
*/
NIX_ERR_NIX_ERROR = -4,
/**
* @brief A recoverable error occurred.
*
* This is used primarily by C API *consumers* to communicate that a failed
* primop call should be retried on the next evaluation attempt.
*/
NIX_ERR_RECOVERABLE = -5,
};
typedef enum nix_err nix_err;
-76
View File
@@ -3,15 +3,9 @@
#include "nix/util/file-descriptor.hh"
#include "nix/util/serialise.hh"
#include "nix/util/signals.hh"
#include <cstring>
#ifndef _WIN32
# include <fcntl.h>
# include <stdlib.h>
#endif
namespace nix {
// BufferedSource with configurable small buffer for precise boundary testing.
@@ -119,55 +113,6 @@ TEST(ReadLine, LineWithNullBytes)
3));
}
#ifndef _WIN32
TEST(ReadLine, TreatsEioAsEof)
{
// Open a pty master. When the slave side is closed (or never opened),
// reading from the master returns EIO, which readLine should treat as EOF.
int master = posix_openpt(O_RDWR | O_NOCTTY);
ASSERT_NE(master, -1);
ASSERT_EQ(grantpt(master), 0);
ASSERT_EQ(unlockpt(master), 0);
// Open and immediately close the slave to trigger EIO on the master.
int slave = open(ptsname(master), O_RDWR | O_NOCTTY);
ASSERT_NE(slave, -1);
close(slave);
// With eofOk=true, readLine should return empty string (treating EIO as EOF).
EXPECT_EQ(readLine(master, /*eofOk=*/true), "");
// With eofOk=false, readLine should throw EndOfFile.
EXPECT_THROW(readLine(master), EndOfFile);
close(master);
}
// macOS (BSD) discards buffered pty data on slave close and returns normal
// EOF (0) instead of EIO, so partial data never reaches the master.
# ifdef __linux__
TEST(ReadLine, PartialLineBeforeEio)
{
int master = posix_openpt(O_RDWR | O_NOCTTY);
ASSERT_NE(master, -1);
ASSERT_EQ(grantpt(master), 0);
ASSERT_EQ(unlockpt(master), 0);
int slave = open(ptsname(master), O_RDWR | O_NOCTTY);
ASSERT_NE(slave, -1);
// Write a partial line (no terminator) from the slave, then close it.
ASSERT_EQ(::write(slave, "partial", 7), 7);
close(slave);
// readLine should return the partial data when eofOk=true.
EXPECT_EQ(readLine(master, /*eofOk=*/true), "partial");
close(master);
}
# endif
#endif
TEST(BufferedSourceReadLine, ReadsLinesFromPipe)
{
Pipe pipe;
@@ -298,25 +243,4 @@ TEST(BufferedSourceReadLine, BufferExhaustedThenEof)
EXPECT_EQ(source.readLine(/*eofOk=*/true), "");
}
TEST(WriteFull, RespectsAllowInterrupts)
{
Pipe pipe;
pipe.create();
setInterrupted(true);
// Must not throw Interrupted even though the interrupt flag is set.
EXPECT_NO_THROW(writeFull(pipe.writeSide.get(), "hello", /*allowInterrupts=*/false));
// Must throw Interrupted when allowInterrupts is true.
EXPECT_THROW(writeFull(pipe.writeSide.get(), "hello", /*allowInterrupts=*/true), Interrupted);
setInterrupted(false);
pipe.writeSide.close();
// Verify the data from the first write was actually written.
FdSource source(pipe.readSide.get());
EXPECT_EQ(source.readLine(/*eofOk=*/true), "hello");
}
} // namespace nix
+10 -14
View File
@@ -84,18 +84,14 @@ TEST(openFileEnsureBeneathNoSymlinks, works)
dirSink.createDirectory(CanonPath("d"));
dirSink.createSymlink(CanonPath("c"), "./d");
});
#ifdef _WIN32
EXPECT_THROW(sink.createDirectory(CanonPath("a/b/c/e")), SymlinkNotAllowed);
#else
// FIXME: This still follows symlinks on Unix (incorrectly succeeds)
sink.createDirectory(CanonPath("a/b/c/e"));
#endif
// Test that symlinks in intermediate path are detected during nested operations
EXPECT_THROW(
ASSERT_THROW(
sink.createDirectory(
CanonPath("a/b/c/f"), [](FileSystemObjectSink & dirSink, const CanonPath & relPath) {}),
SymlinkNotAllowed);
EXPECT_THROW(
ASSERT_THROW(
sink.createRegularFile(
CanonPath("a/b/c/regular"), [](CreateRegularFileSink & crf) { crf("some contents"); }),
SymlinkNotAllowed);
@@ -104,7 +100,7 @@ TEST(openFileEnsureBeneathNoSymlinks, works)
AutoCloseFD dirFd = openDirectory(tmpDir);
// Helper to open files with platform-specific arguments
auto openRead = [&](std::string_view path) -> AutoCloseFD {
auto openRead = [&](std::string_view path) -> Descriptor {
return openFileEnsureBeneathNoSymlinks(
dirFd.get(),
CanonPath(path),
@@ -118,7 +114,7 @@ TEST(openFileEnsureBeneathNoSymlinks, works)
);
};
auto openReadDir = [&](std::string_view path) -> AutoCloseFD {
auto openReadDir = [&](std::string_view path) -> Descriptor {
return openFileEnsureBeneathNoSymlinks(
dirFd.get(),
CanonPath(path),
@@ -132,7 +128,7 @@ TEST(openFileEnsureBeneathNoSymlinks, works)
);
};
auto openCreateExclusive = [&](std::string_view path) -> AutoCloseFD {
auto openCreateExclusive = [&](std::string_view path) -> Descriptor {
return openFileEnsureBeneathNoSymlinks(
dirFd.get(),
CanonPath(path),
@@ -158,19 +154,19 @@ TEST(openFileEnsureBeneathNoSymlinks, works)
#if !defined(_WIN32) && !defined(__CYGWIN__)
// This returns ELOOP on cygwin when O_NOFOLLOW is used
EXPECT_FALSE(openCreateExclusive("a/broken_symlink"));
EXPECT_EQ(openCreateExclusive("a/broken_symlink"), INVALID_DESCRIPTOR);
/* Sanity check, no symlink shenanigans and behaves the same as regular openat with O_EXCL | O_CREAT. */
EXPECT_EQ(errno, EEXIST);
#endif
EXPECT_THROW(openCreateExclusive("a/absolute_symlink/broken_symlink"), SymlinkNotAllowed);
// Test invalid paths
EXPECT_FALSE(openRead("c/d/regular/a"));
EXPECT_FALSE(openReadDir("c/d/regular"));
EXPECT_EQ(openRead("c/d/regular/a"), INVALID_DESCRIPTOR);
EXPECT_EQ(openReadDir("c/d/regular"), INVALID_DESCRIPTOR);
// Test valid paths work
EXPECT_TRUE(openRead("c/d/regular"));
EXPECT_TRUE(openCreateExclusive("a/regular"));
EXPECT_TRUE(AutoCloseFD{openRead("c/d/regular")});
EXPECT_TRUE(AutoCloseFD{openCreateExclusive("a/regular")});
}
} // namespace nix
+19 -25
View File
@@ -16,12 +16,10 @@ using namespace std::string_view_literals;
#ifdef _WIN32
# define FS_SEP L"\\"
# define FS_ROOT_NO_TRAILING_SLASH L"C:" // Need a mounted one, C drive is likely
# define FS_ROOT FS_ROOT_NO_TRAILING_SLASH FS_SEP
# define FS_ROOT L"C:" FS_SEP // Need a mounted one, C drive is likely
#else
# define FS_SEP "/"
# define FS_ROOT_NO_TRAILING_SLASH FS_SEP
# define FS_ROOT FS_ROOT_NO_TRAILING_SLASH
# define FS_ROOT FS_SEP
#endif
#ifndef PATH_MAX
@@ -46,7 +44,7 @@ TEST(absPath, doesntChangeRoot)
{
auto p = absPath(std::filesystem::path{FS_ROOT});
ASSERT_EQ(p, FS_ROOT_NO_TRAILING_SLASH);
ASSERT_EQ(p, FS_ROOT);
}
TEST(absPath, turnsEmptyPathIntoCWD)
@@ -198,42 +196,42 @@ TEST(baseNameOf, absoluteNothingSlashNothing)
TEST(isInDir, trivialCase)
{
EXPECT_TRUE(isInDir(FS_ROOT "foo" FS_SEP "bar", FS_ROOT "foo"));
EXPECT_TRUE(isInDir("/foo/bar", "/foo"));
}
TEST(isInDir, notInDir)
{
EXPECT_FALSE(isInDir(FS_ROOT "zes" FS_SEP "foo" FS_SEP "bar", FS_ROOT "foo"));
EXPECT_FALSE(isInDir("/zes/foo/bar", "/foo"));
}
TEST(isInDir, emptyDir)
{
EXPECT_FALSE(isInDir(FS_ROOT "zes" FS_SEP "foo" FS_SEP "bar", ""));
EXPECT_FALSE(isInDir("/zes/foo/bar", ""));
}
TEST(isInDir, hiddenSubdirectory)
{
EXPECT_TRUE(isInDir(FS_ROOT "foo" FS_SEP ".ssh", FS_ROOT "foo"));
EXPECT_TRUE(isInDir("/foo/.ssh", "/foo"));
}
TEST(isInDir, ellipsisEntry)
{
EXPECT_TRUE(isInDir(FS_ROOT "foo" FS_SEP "...", FS_ROOT "foo"));
EXPECT_TRUE(isInDir("/foo/...", "/foo"));
}
TEST(isInDir, sameDir)
{
EXPECT_FALSE(isInDir(FS_ROOT "foo", FS_ROOT "foo"));
EXPECT_FALSE(isInDir("/foo", "/foo"));
}
TEST(isInDir, sameDirDot)
{
EXPECT_FALSE(isInDir(FS_ROOT "foo" FS_SEP ".", FS_ROOT "foo"));
EXPECT_FALSE(isInDir("/foo/.", "/foo"));
}
TEST(isInDir, dotDotPrefix)
{
EXPECT_FALSE(isInDir(FS_ROOT "foo" FS_SEP ".." FS_SEP "bar", FS_ROOT "foo"));
EXPECT_FALSE(isInDir("/foo/../bar", "/foo"));
}
/* ----------------------------------------------------------------------------
@@ -242,8 +240,8 @@ TEST(isInDir, dotDotPrefix)
TEST(isDirOrInDir, trueForSameDirectory)
{
ASSERT_EQ(isDirOrInDir(FS_ROOT "nix", FS_ROOT "nix"), true);
ASSERT_EQ(isDirOrInDir(FS_ROOT, FS_ROOT), true);
ASSERT_EQ(isDirOrInDir("/nix", "/nix"), true);
ASSERT_EQ(isDirOrInDir("/", "/"), true);
}
TEST(isDirOrInDir, trueForEmptyPaths)
@@ -253,17 +251,17 @@ TEST(isDirOrInDir, trueForEmptyPaths)
TEST(isDirOrInDir, falseForDisjunctPaths)
{
ASSERT_EQ(isDirOrInDir(FS_ROOT "foo", FS_ROOT "bar"), false);
ASSERT_EQ(isDirOrInDir("/foo", "/bar"), false);
}
TEST(isDirOrInDir, relativePaths)
{
ASSERT_EQ(isDirOrInDir(FS_ROOT "foo" FS_SEP "..", FS_ROOT "foo"), false);
ASSERT_EQ(isDirOrInDir("/foo/..", "/foo"), false);
}
TEST(isDirOrInDir, relativePathsTwice)
{
ASSERT_EQ(isDirOrInDir(FS_ROOT "foo" FS_SEP "..", FS_ROOT "foo" FS_SEP "."), false);
ASSERT_EQ(isDirOrInDir("/foo/..", "/foo/."), false);
}
/* ----------------------------------------------------------------------------
@@ -296,16 +294,13 @@ TEST(makeParentCanonical, noParent)
TEST(makeParentCanonical, root)
{
ASSERT_EQ(makeParentCanonical(FS_ROOT), FS_ROOT_NO_TRAILING_SLASH);
ASSERT_EQ(makeParentCanonical("/"), "/");
}
/* ----------------------------------------------------------------------------
* chmodIfNeeded
* --------------------------------------------------------------------------*/
#ifndef _WIN32
// Windows doesn't support Unix-style permission bits - lstat always
// returns the same mode regardless of what chmod sets.
TEST(chmodIfNeeded, works)
{
auto [autoClose_, tmpFile] = nix::createTempFile();
@@ -321,11 +316,10 @@ TEST(chmodIfNeeded, works)
}
}
}
#endif
TEST(chmodIfNeeded, nonexistent)
{
ASSERT_THROW(chmodIfNeeded("/schnitzel/darmstadt/pommes", 0755), SystemError);
ASSERT_THROW(chmodIfNeeded("/schnitzel/darmstadt/pommes", 0755), SysError);
}
/* ----------------------------------------------------------------------------
@@ -346,7 +340,7 @@ TEST(DirectoryIterator, works)
TEST(DirectoryIterator, nonexistent)
{
ASSERT_THROW(DirectoryIterator("/schnitzel/darmstadt/pommes"), SystemError);
ASSERT_THROW(DirectoryIterator("/schnitzel/darmstadt/pommes"), SysError);
}
/* ----------------------------------------------------------------------------
-30
View File
@@ -137,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
-104
View File
@@ -956,108 +956,4 @@ TEST(nix, isValidSchemeName)
ASSERT_FALSE(isValidSchemeName("http "));
}
/* ----------------------------------------------------------------------------
* pathToUrlPath / urlPathToPath
* --------------------------------------------------------------------------*/
struct UrlPathTestCase
{
std::string_view urlString;
ParsedURL urlParsed;
std::filesystem::path path;
std::string description;
};
class UrlPathTest : public ::testing::TestWithParam<UrlPathTestCase>
{};
TEST_P(UrlPathTest, pathToUrlPath)
{
const auto & testCase = GetParam();
auto urlPath = pathToUrlPath(testCase.path);
EXPECT_EQ(urlPath, testCase.urlParsed.path);
}
TEST_P(UrlPathTest, urlPathToPath)
{
const auto & testCase = GetParam();
auto path = urlPathToPath(testCase.urlParsed.path);
EXPECT_EQ(path, testCase.path);
}
TEST_P(UrlPathTest, urlToString)
{
const auto & testCase = GetParam();
EXPECT_EQ(testCase.urlParsed.to_string(), testCase.urlString);
}
TEST_P(UrlPathTest, stringToUrl)
{
const auto & testCase = GetParam();
auto parsed = parseURL(std::string{testCase.urlString});
EXPECT_EQ(parsed, testCase.urlParsed);
}
#ifndef _WIN32
INSTANTIATE_TEST_SUITE_P(
Unix,
UrlPathTest,
::testing::Values(
UrlPathTestCase{
.urlString = "file:///foo/bar/baz",
.urlParsed =
ParsedURL{
.scheme = "file",
.authority = ParsedURL::Authority{},
.path = {"", "foo", "bar", "baz"},
},
.path = "/foo/bar/baz",
.description = "absolute_path",
},
UrlPathTestCase{
.urlString = "file:///",
.urlParsed =
ParsedURL{
.scheme = "file",
.authority = ParsedURL::Authority{},
.path = {"", ""},
},
.path = "/",
.description = "root_path",
}),
[](const auto & info) { return info.param.description; });
#else // _WIN32
INSTANTIATE_TEST_SUITE_P(
Windows,
UrlPathTest,
::testing::Values(
UrlPathTestCase{
.urlString = "file:///C:/foo/bar/baz",
.urlParsed =
ParsedURL{
.scheme = "file",
.authority = ParsedURL::Authority{},
.path = {"", "C:", "foo", "bar", "baz"},
},
.path = L"C:\\foo\\bar\\baz",
.description = "absolute_path",
},
UrlPathTestCase{
.urlString = "file:///C:/",
.urlParsed =
ParsedURL{
.scheme = "file",
.authority = ParsedURL::Authority{},
.path = {"", "C:", ""},
},
.path = L"C:\\",
.description = "drive_root",
}),
[](const auto & info) { return info.param.description; });
#endif // _WIN32
} // namespace nix
+2 -2
View File
@@ -31,11 +31,11 @@ unsigned int getMaxCPU()
{
#ifdef __linux__
try {
auto cgroupFS = getCgroupFS();
auto cgroupFS = linux::getCgroupFS();
if (!cgroupFS)
return 0;
auto cpuFile = *cgroupFS / getCurrentCgroup().rel() / "cpu.max";
auto cpuFile = *cgroupFS / linux::getCurrentCgroup().rel() / "cpu.max";
auto cpuMax = readFile(cpuFile);
auto cpuMaxParts = tokenizeString<std::vector<std::string>>(cpuMax, " \n");
+1 -13
View File
@@ -41,11 +41,6 @@ const std::string & BaseError::calcWhat() const
}
}
bool BaseError::hasPos() const
{
return err.pos.get() && *err.pos.get();
}
std::optional<std::string> ErrorInfo::programName = std::nullopt;
std::ostream & operator<<(std::ostream & os, const HintFmt & hf)
@@ -427,20 +422,13 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s
*/
static void writeErr(std::string_view buf)
{
Descriptor fd = getStandardError();
while (!buf.empty()) {
#ifdef _WIN32
DWORD n;
if (!WriteFile(fd, buf.data(), buf.size(), &n, NULL))
abort();
#else
auto n = ::write(fd, buf.data(), buf.size());
auto n = write(STDERR_FILENO, buf.data(), buf.size());
if (n < 0) {
if (errno == EINTR)
continue;
abort();
}
#endif
buf = buf.substr(n);
}
}
+5 -9
View File
@@ -20,23 +20,17 @@ ExecutablePath ExecutablePath::load()
}
ExecutablePath ExecutablePath::parse(const OsString & path)
{
ExecutablePath ret;
ret.parseAppend(path);
return ret;
}
void ExecutablePath::parseAppend(const OsString & path)
{
auto strings = path.empty() ? (std::list<OsString>{})
: basicSplitString<std::list<OsString>, OsChar>(path, path_var_separator);
directories.reserve(directories.size() + strings.size());
std::vector<std::filesystem::path> ret;
ret.reserve(strings.size());
std::transform(
std::make_move_iterator(strings.begin()),
std::make_move_iterator(strings.end()),
std::back_inserter(directories),
std::back_inserter(ret),
[](OsString && str) {
return std::filesystem::path{
str.empty()
@@ -51,6 +45,8 @@ void ExecutablePath::parseAppend(const OsString & path)
: std::move(str),
};
});
return {ret};
}
OsString ExecutablePath::render() const
+1 -1
View File
@@ -378,7 +378,7 @@ std::set<ExperimentalFeature> parseFeatures(const StringSet & rawFeatures)
}
MissingExperimentalFeature::MissingExperimentalFeature(ExperimentalFeature feature, std::string reason)
: CloneableError(
: Error(
"experimental Nix feature '%1%' is disabled%2%; add '--extra-experimental-features %1%' to enable it",
showExperimentalFeature(feature),
Uncolored(optionalBracket(" (", reason, ")")))
+31 -103
View File
@@ -8,112 +8,10 @@
#ifdef _WIN32
# include <winnt.h>
# include <fileapi.h>
#else
# include <poll.h>
#endif
namespace nix {
namespace {
enum class PollDirection { In, Out };
/**
* Retry an I/O operation if it fails with EAGAIN/EWOULDBLOCK.
*
* On Unix, polls the fd and retries. On Windows, just calls `f` once.
*
* This retry logic is needed to handle non-blocking reads/writes. This
* is needed in the buildhook, because somehow the json logger file
* descriptor ends up being non-blocking and breaks remote-building.
*
* @todo Get rid of buildhook and remove this logic again
* (https://github.com/NixOS/nix/issues/12688)
*/
template<typename F>
auto retryOnBlock([[maybe_unused]] Descriptor fd, [[maybe_unused]] PollDirection dir, F && f) -> decltype(f())
{
#ifndef _WIN32
while (true) {
try {
return std::forward<F>(f)();
} catch (SystemError & e) {
if (e.is(std::errc::resource_unavailable_try_again) || e.is(std::errc::operation_would_block)) {
struct pollfd pfd;
pfd.fd = fd;
pfd.events = dir == PollDirection::In ? POLLIN : POLLOUT;
if (poll(&pfd, 1, -1) == -1)
throw SysError("poll on file descriptor failed");
continue;
}
throw;
}
}
#else
return std::forward<F>(f)();
#endif
}
} // namespace
void readFull(Descriptor fd, char * buf, size_t count)
{
while (count) {
checkInterrupt();
auto res = retryOnBlock(
fd, PollDirection::In, [&]() { return read(fd, {reinterpret_cast<std::byte *>(buf), count}); });
if (res == 0)
throw EndOfFile("unexpected end-of-file");
count -= res;
buf += res;
}
}
std::string readLine(Descriptor fd, bool eofOk, char terminator)
{
std::string s;
while (1) {
checkInterrupt();
char ch;
// FIXME: inefficient
auto rd = retryOnBlock(fd, PollDirection::In, [&]() -> size_t {
try {
return read(fd, {reinterpret_cast<std::byte *>(&ch), 1});
} catch (SystemError & e) {
// On pty masters, EIO signals that the slave side closed,
// which is semantically EOF. Map it to a zero-length read
// so the existing EOF path handles it.
if (e.is(std::errc::io_error))
return 0;
throw;
}
});
if (rd == 0) {
if (eofOk)
return s;
else
throw EndOfFile("unexpected EOF reading a line");
} else {
if (ch == terminator)
return s;
s += ch;
}
}
}
void writeFull(Descriptor fd, std::string_view s, bool allowInterrupts)
{
while (!s.empty()) {
if (allowInterrupts)
checkInterrupt();
auto res = retryOnBlock(fd, PollDirection::Out, [&]() {
return write(fd, {reinterpret_cast<const std::byte *>(s.data()), s.size()}, allowInterrupts);
});
if (res > 0)
s.remove_prefix(res);
}
}
void writeLine(Descriptor fd, std::string s)
{
s += '\n';
@@ -169,6 +67,8 @@ void drainFD(Descriptor fd, Sink & sink, DrainFdSinkOpts opts)
&& (e.is(std::errc::resource_unavailable_try_again) || e.is(std::errc::operation_would_block)))
break;
#endif
if (e.is(std::errc::interrupted))
continue;
throw;
}
@@ -205,8 +105,18 @@ void copyFdRange(Descriptor fd, off_t offset, size_t nbytes, Sink & sink)
std::array<std::byte, 64 * 1024> buf;
while (left) {
checkInterrupt();
auto limit = std::min<size_t>(left, buf.size());
auto n = readOffset(fd, offset, std::span(buf.data(), limit));
/* Should be initialized before we read, because the `catch`
block either throws or continues. */
size_t n;
try {
n = readOffset(fd, offset, std::span(buf.data(), limit));
} catch (SystemError & e) {
if (e.is(std::errc::interrupted))
continue;
throw;
}
if (n == 0)
throw EndOfFile("unexpected end-of-file");
assert(n <= left);
@@ -274,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
{
#ifdef __linux__
+21 -29
View File
@@ -21,9 +21,7 @@
#include <sys/time.h>
#include <unistd.h>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/filesystem/path.hpp>
#ifdef __FreeBSD__
# include <sys/param.h>
@@ -42,9 +40,9 @@ DirectoryIterator::DirectoryIterator(const std::filesystem::path & p)
// **Attempt to create the underlying directory_iterator**
it_ = std::filesystem::directory_iterator(p);
} catch (const std::filesystem::filesystem_error & e) {
// **Catch filesystem_error and throw SystemError**
// Adapt the error message as needed for SystemError
throw SystemError(e.code(), "cannot read directory %s", PathFmt(p));
// **Catch filesystem_error and throw SysError**
// Adapt the error message as needed for SysError
throw SysError("cannot read directory %s", PathFmt(p));
}
}
@@ -264,13 +262,13 @@ std::optional<PosixStat> maybeLstat(const std::filesystem::path & path)
bool pathExists(const std::filesystem::path & path)
{
return maybeLstat(path).has_value();
return maybeLstat(path.string()).has_value();
}
bool pathAccessible(const std::filesystem::path & path)
{
try {
return pathExists(path);
return pathExists(path.string());
} catch (SystemError & e) {
// swallow EPERM
if (e.is(std::errc::operation_not_permitted))
@@ -282,11 +280,7 @@ bool pathAccessible(const std::filesystem::path & path)
std::filesystem::path readLink(const std::filesystem::path & path)
{
checkInterrupt();
try {
return std::filesystem::read_symlink(path);
} catch (std::filesystem::filesystem_error & e) {
throw SystemError(e.code(), "reading symbolic link '%s'", PathFmt(path));
}
return std::filesystem::read_symlink(path);
}
Path readLink(const Path & path)
@@ -307,27 +301,25 @@ std::string readFile(const std::filesystem::path & path)
return readFile(fd.get());
}
void readFile(const std::filesystem::path & path, Sink & sink, bool memory_map)
void readFile(const Path & path, Sink & sink, bool memory_map)
{
// Memory-map the file for faster processing where possible.
if (memory_map) {
try {
// mapped_file_source can't be constructed from std::filesystem::path with wide paths. Go
// through boost::filesystem::path.
boost::iostreams::mapped_file_source mmap(boost::filesystem::path{path.native()});
boost::iostreams::mapped_file_source mmap(path);
if (mmap.is_open()) {
sink({mmap.data(), mmap.size()});
return;
}
} catch (const boost::exception & e) {
debug("memory-mapping failed for path: %s: %s", PathFmt(path), boost::diagnostic_information(e));
}
debug("memory-mapping failed for path: %s", path);
}
// Stream the file instead if memory-mapping fails or is disabled.
AutoCloseFD fd = openFileReadonly(path);
AutoCloseFD fd = openFileReadonly(std::filesystem::path(path));
if (!fd)
throw NativeSysError("opening file %s", PathFmt(path));
throw NativeSysError("opening file %s", path);
drainFD(fd.get(), sink);
}
@@ -344,23 +336,23 @@ void writeFile(const Path & path, std::string_view s, mode_t mode, FsSync sync)
if (!fd)
throw SysError("opening file '%1%'", path);
writeFile(fd.get(), s, sync, &path);
writeFile(fd, path, s, mode, sync);
/* Close explicitly to propagate the exceptions. */
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 {
writeFull(fd, s);
writeFull(fd.get(), s);
if (sync == FsSync::Yes)
syncDescriptor(fd);
fd.fsync();
} catch (Error & e) {
e.addTrace({}, "writing file '%1%'", origPath ? *origPath : descriptorToPath(fd).string());
e.addTrace({}, "writing file '%1%'", origPath);
throw;
}
}
@@ -471,7 +463,7 @@ void createDirs(const std::filesystem::path & path)
try {
std::filesystem::create_directories(path);
} catch (std::filesystem::filesystem_error & e) {
throw SystemError(e.code(), "creating directory '%1%'", path.string());
throw SysError("creating directory '%1%'", path.string());
}
}
@@ -672,7 +664,7 @@ void replaceSymlink(const std::filesystem::path & target, const std::filesystem:
} catch (std::filesystem::filesystem_error & e) {
if (e.code() == std::errc::file_exists)
continue;
throw SystemError(e.code(), "creating symlink %1% -> %2%", PathFmt(tmp), PathFmt(target));
throw SysError("creating symlink %1% -> %2%", PathFmt(tmp), PathFmt(target));
}
try {
@@ -680,7 +672,7 @@ void replaceSymlink(const std::filesystem::path & target, const std::filesystem:
} catch (std::filesystem::filesystem_error & e) {
if (e.code() == std::errc::file_exists)
continue;
throw SystemError(e.code(), "renaming %1% to %2%", PathFmt(tmp), PathFmt(link));
throw SysError("renaming %1% to %2%", PathFmt(tmp), PathFmt(link));
}
break;
@@ -781,7 +773,7 @@ std::filesystem::path makeParentCanonical(const std::filesystem::path & rawPath)
}
return std::filesystem::canonical(parent) / path.filename();
} catch (std::filesystem::filesystem_error & e) {
throw SystemError(e.code(), "canonicalising parent path of %1%", PathFmt(path));
throw SysError("canonicalising parent path of %1%", PathFmt(path));
}
}
+1 -1
View File
@@ -181,7 +181,7 @@ void RestoreSink::createRegularFile(const CanonPath & path, std::function<void(C
component are not followed. */
constexpr int flags = O_CREAT | O_EXCL | O_WRONLY | O_CLOEXEC;
if (!dirFd)
return AutoCloseFD{::open(p.c_str(), flags, 0666)};
return ::open(p.c_str(), flags, 0666);
return openFileEnsureBeneathNoSymlinks(dirFd.get(), path, flags, 0666);
}()
#endif
+39 -84
View File
@@ -17,7 +17,6 @@
#include "nix/util/suggestions.hh"
#include "nix/util/fmt.hh"
#include "nix/util/config.hh"
#include <cstring>
#include <list>
@@ -127,20 +126,20 @@ public:
BaseError & operator=(BaseError &&) = default;
template<typename... Args>
BaseError(unsigned int status, Args &&... args)
: err{.level = lvlError, .msg = HintFmt(std::forward<Args>(args)...), .pos = {}, .status = status}
BaseError(unsigned int status, const Args &... args)
: err{.level = lvlError, .msg = HintFmt(args...), .pos = {}, .status = status}
{
}
template<typename... Args>
explicit BaseError(const std::string & fs, Args &&... args)
: err{.level = lvlError, .msg = HintFmt(fs, std::forward<Args>(args)...), .pos = {}}
explicit BaseError(const std::string & fs, const Args &... args)
: err{.level = lvlError, .msg = HintFmt(fs, args...), .pos = {}}
{
}
template<typename... Args>
BaseError(const Suggestions & sug, Args &&... args)
: err{.level = lvlError, .msg = HintFmt(std::forward<Args>(args)...), .pos = {}, .suggestions = sug}
BaseError(const Suggestions & sug, const Args &... args)
: err{.level = lvlError, .msg = HintFmt(args...), .pos = {}, .suggestions = sug}
{
}
@@ -191,8 +190,6 @@ public:
err.pos = pos;
}
bool hasPos() const;
void pushTrace(Trace trace)
{
err.traces.push_front(trace);
@@ -206,9 +203,9 @@ public:
* @param args... Format string arguments.
*/
template<typename... Args>
void addTrace(std::shared_ptr<const Pos> && pos, std::string_view fs, Args &&... args)
void addTrace(std::shared_ptr<const Pos> && pos, std::string_view fs, const Args &... args)
{
addTrace(std::move(pos), HintFmt(std::string(fs), std::forward<Args>(args)...));
addTrace(std::move(pos), HintFmt(std::string(fs), args...));
}
/**
@@ -229,31 +226,13 @@ public:
{
return err;
};
[[noreturn]] virtual void throwClone() const = 0;
};
template<typename Derived, typename Base>
class CloneableError : public Base
{
public:
using Base::Base;
/**
* Rethrow a copy of this exception. Useful when the exception can get
* modified when appending traces.
*/
[[noreturn]] void throwClone() const override
{
throw Derived(static_cast<const Derived &>(*this));
}
};
#define MakeError(newClass, superClass) \
class newClass : public CloneableError<newClass, superClass> \
{ \
public: \
using CloneableError<newClass, superClass>::CloneableError; \
#define MakeError(newClass, superClass) \
class newClass : public superClass \
{ \
public: \
using superClass::superClass; \
}
MakeError(Error, BaseError);
@@ -265,42 +244,22 @@ MakeError(UnimplementedError, Error);
* std::error_code. Use when you want to catch and check an error condition like
* no_such_file_or_directory (ENOENT) without ifdefs.
*/
class SystemError : public CloneableError<SystemError, Error>
class SystemError : public Error
{
std::error_code errorCode;
std::string errorDetails;
protected:
/**
* Just here to allow derived classes to use the right constructor
* (the protected one).
*/
struct Disambig
{};
/**
* Protected constructor for subclasses that provide their own error message.
* The error message is appended to the formatted hint.
*/
template<typename... Args>
SystemError(Disambig, std::error_code errorCode, std::string_view errorDetails, Args &&... args)
: CloneableError("")
, errorCode(errorCode)
, errorDetails(errorDetails)
{
auto hf = HintFmt(std::forward<Args>(args)...);
err.msg = HintFmt("%s: %s", Uncolored(hf.str()), errorDetails);
}
public:
/**
* Construct with an error code. The error code's message is automatically
* appended to the error message.
*/
template<typename... Args>
SystemError(std::errc posixErrNo, Args &&... args)
: Error(std::forward<Args>(args)...)
, errorCode(std::make_error_code(posixErrNo))
{
}
template<typename... Args>
SystemError(std::error_code errorCode, Args &&... args)
: SystemError(Disambig{}, errorCode, errorCode.message(), std::forward<Args>(args)...)
: Error(std::forward<Args>(args)...)
, errorCode(errorCode)
{
}
@@ -331,7 +290,7 @@ public:
* support is too WIP to justify the code churn, but if it is finished
* then a better identifier becomes moe worth it.
*/
class SysError final : public CloneableError<SysError, SystemError>
class SysError : public SystemError
{
public:
int errNo;
@@ -341,14 +300,12 @@ public:
* will be used to try to add additional information to the message.
*/
template<typename... Args>
SysError(int errNo, Args &&... args)
: CloneableError(
Disambig{},
std::make_error_code(static_cast<std::errc>(errNo)),
strerror(errNo),
std::forward<Args>(args)...)
SysError(int errNo, const Args &... args)
: SystemError(static_cast<std::errc>(errNo), "")
, errNo(errNo)
{
auto hf = HintFmt(args...);
err.msg = HintFmt("%1%: %2%", Uncolored(hf.str()), strerror(errNo));
}
/**
@@ -358,8 +315,8 @@ public:
* calling this constructor!
*/
template<typename... Args>
SysError(Args &&... args)
: SysError(errno, std::forward<Args>(args)...)
SysError(const Args &... args)
: SysError(errno, args...)
{
}
};
@@ -393,7 +350,7 @@ int handleExceptions(const std::string & programName, std::function<void()> fun)
*/
[[gnu::noinline, gnu::cold, noreturn]] void unreachable(std::source_location loc = std::source_location::current());
#if NIX_UBSAN_ENABLED
#if NIX_UBSAN_ENABLED == 1
/* When building with sanitizers, also enable expensive unreachable checks. In
optimised builds this explicitly invokes UB with std::unreachable for better
optimisations. */
@@ -412,7 +369,7 @@ namespace windows {
* Unless you need to catch a specific error number, don't catch this in
* portable code. Catch `SystemError` instead.
*/
class WinError : public CloneableError<WinError, SystemError>
class WinError : public SystemError
{
public:
DWORD lastError;
@@ -423,14 +380,12 @@ public:
* information to the message.
*/
template<typename... Args>
WinError(DWORD lastError, Args &&... args)
: CloneableError(
Disambig{},
std::error_code(lastError, std::system_category()),
renderError(lastError),
std::forward<Args>(args)...)
WinError(DWORD lastError, const Args &... args)
: SystemError(std::error_code(lastError, std::system_category()), "")
, lastError(lastError)
{
auto hf = HintFmt(args...);
err.msg = HintFmt("%1%: %2%", Uncolored(hf.str()), renderError(lastError));
}
/**
@@ -440,14 +395,14 @@ public:
* before calling this constructor!
*/
template<typename... Args>
WinError(Args &&... args)
: WinError(GetLastError(), std::forward<Args>(args)...)
WinError(const Args &... args)
: WinError(GetLastError(), args...)
{
}
private:
static std::string renderError(DWORD lastError);
std::string renderError(DWORD lastError);
};
} // namespace windows
@@ -33,12 +33,6 @@ struct ExecutablePath
*/
static ExecutablePath parse(const OsString & path);
/**
* Like `parse` but appends new entries to the end of an existing
* `ExecutablePath`.
*/
void parseAppend(const OsString & path);
/**
* Load the `PATH` environment variable and `parse` it.
*/
@@ -80,7 +80,7 @@ std::set<ExperimentalFeature> parseFeatures(const StringSet &);
* An experimental feature was required for some (experimental)
* operation, but was not enabled.
*/
class MissingExperimentalFeature final : public CloneableError<MissingExperimentalFeature, Error>
class MissingExperimentalFeature : public Error
{
public:
/**
@@ -65,7 +65,7 @@ std::string readFile(Descriptor fd);
* Platform-specific read into a buffer.
*
* Thin wrapper around ::read (Unix) or ReadFile (Windows).
* Handles EINTR on Unix. Treats ERROR_BROKEN_PIPE as EOF on Windows.
* Does NOT handle EINTR on Unix - caller must catch and retry if needed.
*
* @param fd The file descriptor to read from
* @param buffer The buffer to read into
@@ -74,19 +74,6 @@ std::string readFile(Descriptor fd);
*/
size_t read(Descriptor fd, std::span<std::byte> buffer);
/**
* Platform-specific write from a buffer.
*
* Thin wrapper around ::write (Unix) or WriteFile (Windows).
* Handles EINTR on Unix.
*
* @param fd The file descriptor to write to
* @param buffer The buffer to write from
* @return The number of bytes actually written
* @throws SystemError on failure
*/
size_t write(Descriptor fd, std::span<const std::byte> buffer, bool allowInterrupts);
/**
* Get the size of a file.
*
@@ -146,11 +133,6 @@ std::string readLine(Descriptor fd, bool eofOk = false, char terminator = '\n');
*/
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.
*/
@@ -271,11 +253,7 @@ public:
/**
* Perform a blocking fsync operation.
*/
void fsync() const
{
if (fd != INVALID_DESCRIPTOR)
nix::syncDescriptor(fd);
}
void fsync() const;
/**
* Asynchronously flush to disk without blocking, if available on
@@ -54,7 +54,7 @@ OsString readLinkAt(Descriptor dirFd, const CanonPath & path);
* @throws SymlinkNotAllowed if any path components are symlinks
* @throws SystemError on other errors
*/
AutoCloseFD openFileEnsureBeneathNoSymlinks(
Descriptor openFileEnsureBeneathNoSymlinks(
Descriptor dirFd,
const CanonPath & path,
#ifdef _WIN32
@@ -75,7 +75,7 @@ namespace linux {
*
* @see https://man7.org/linux/man-pages/man2/openat2.2.html
* @see https://man7.org/linux/man-pages/man2/open_how.2type.html
*
v*
* @param flags O_* flags
* @param mode Mode for O_{CREAT,TMPFILE}
* @param resolve RESOLVE_* flags
+11 -9
View File
@@ -12,7 +12,6 @@
#include "nix/util/file-descriptor.hh"
#include "nix/util/file-path.hh"
#include <filesystem>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
@@ -190,19 +189,21 @@ Path readLink(const Path & path);
*/
std::filesystem::path readLink(const std::filesystem::path & path);
#ifdef _WIN32
namespace windows {
/**
* Get the path associated with a file descriptor.
* Get the path associated with a file handle.
*
* @note One MUST only use this for error handling, because it creates
* TOCTOU issues. We don't mind if error messages point to out of date
* paths (that is a rather trivial TOCTOU --- the error message is best
* effort) but for anything else we do.
*
* @note this function will clobber `errno` (Unix) / "last error"
* (Windows), so care must be used to get those error codes, then call
* this, then build a `SysError` / `WinError` with the saved error code.
*/
std::filesystem::path descriptorToPath(Descriptor fd);
std::filesystem::path handleToPath(Descriptor handle);
} // namespace windows
#endif
/**
* Open a `Descriptor` with read-only access to the given directory.
@@ -244,7 +245,7 @@ Descriptor openNewFileForWrite(const std::filesystem::path & path, mode_t mode,
*/
std::string readFile(const Path & path);
std::string readFile(const std::filesystem::path & path);
void readFile(const std::filesystem::path & path, Sink & sink, bool memory_map = true);
void readFile(const Path & path, Sink & sink, bool memory_map = true);
enum struct FsSync { Yes, No };
@@ -267,7 +268,8 @@ writeFile(const std::filesystem::path & path, Source & source, mode_t mode = 066
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.
+1 -6
View File
@@ -2,12 +2,7 @@
include_dirs = [ include_directories('../..') ]
config_pub_h = configure_file(
configuration : configdata_pub,
output : 'config.hh',
)
headers = [ config_pub_h ] + files(
headers = files(
'abstract-setting-to-json.hh',
'alignment.hh',
'ansicolor.hh',
+2 -2
View File
@@ -133,14 +133,14 @@ std::pair<int, std::string> runProgram(RunOptions && options);
void runProgram2(const RunOptions & options);
class ExecError final : public CloneableError<ExecError, Error>
class ExecError : public Error
{
public:
int status;
template<typename... Args>
ExecError(int status, const Args &... args)
: CloneableError(args...)
: Error(args...)
, status(status)
{
}
@@ -231,19 +231,19 @@ ref<SourceAccessor> makeEmptySourceAccessor();
*/
MakeError(RestrictedPathError, Error);
struct SymlinkNotAllowed final : public CloneableError<SymlinkNotAllowed, Error>
struct SymlinkNotAllowed : public Error
{
CanonPath path;
SymlinkNotAllowed(CanonPath path)
: CloneableError("relative path '%s' points to a symlink, which is not allowed", path.rel())
: Error("relative path '%s' points to a symlink, which is not allowed", path.rel())
, path(std::move(path))
{
}
template<typename... Args>
SymlinkNotAllowed(CanonPath path, const std::string & fs, Args &&... args)
: CloneableError(fs, std::forward<Args>(args)...)
: Error(fs, std::forward<Args>(args)...)
, path(std::move(path))
{
}
Loaded 100 of 144 files, more files were not shown because too many files have changed in this diff. Show more