forked from lix-project/lix
primops: Move functions to primops/hash.cc
Moved builtins: hashFile, hashString
The function realisePath has also been declared in primops.hh
Change-Id: I295c2f1964b0496e449f4e23f0299ce972bbca04
This commit is contained in:
parent
7f9f2f7835
commit
95968c44eb
|
@ -89,6 +89,7 @@ libexpr_sources = files(
|
||||||
'primops/attrset.cc',
|
'primops/attrset.cc',
|
||||||
'primops/context.cc',
|
'primops/context.cc',
|
||||||
'primops/control.cc',
|
'primops/control.cc',
|
||||||
|
'primops/hash.cc',
|
||||||
'primops/fetchClosure.cc',
|
'primops/fetchClosure.cc',
|
||||||
'primops/fetchMercurial.cc',
|
'primops/fetchMercurial.cc',
|
||||||
'primops/fetchTree.cc',
|
'primops/fetchTree.cc',
|
||||||
|
|
|
@ -115,12 +115,7 @@ StringMap EvalState::realiseContext(const NixStringContext & context)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RealisePathFlags {
|
SourcePath realisePath(EvalState & state, const PosIdx pos, Value & v, const RealisePathFlags flags)
|
||||||
// Whether to check that the path is allowed in pure eval mode
|
|
||||||
bool checkForPureEval = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
static SourcePath realisePath(EvalState & state, const PosIdx pos, Value & v, const RealisePathFlags flags = {})
|
|
||||||
{
|
{
|
||||||
NixStringContext context;
|
NixStringContext context;
|
||||||
|
|
||||||
|
@ -1585,30 +1580,6 @@ static RegisterPrimOp primop_findFile(PrimOp {
|
||||||
.fun = prim_findFile,
|
.fun = prim_findFile,
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Return the cryptographic hash of a file in base-16. */
|
|
||||||
static void prim_hashFile(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
|
||||||
{
|
|
||||||
auto type = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.hashFile");
|
|
||||||
std::optional<HashType> ht = parseHashType(type);
|
|
||||||
if (!ht)
|
|
||||||
state.error<EvalError>("unknown hash type '%1%'", type).atPos(pos).debugThrow();
|
|
||||||
|
|
||||||
auto path = realisePath(state, pos, *args[1]);
|
|
||||||
|
|
||||||
v.mkString(hashString(*ht, path.readFile()).to_string(Base16, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
static RegisterPrimOp primop_hashFile({
|
|
||||||
.name = "__hashFile",
|
|
||||||
.args = {"type", "p"},
|
|
||||||
.doc = R"(
|
|
||||||
Return a base-16 representation of the cryptographic hash of the
|
|
||||||
file at path *p*. The hash algorithm specified by *type* must be one
|
|
||||||
of `"md5"`, `"sha1"`, `"sha256"` or `"sha512"`.
|
|
||||||
)",
|
|
||||||
.fun = prim_hashFile,
|
|
||||||
});
|
|
||||||
|
|
||||||
static std::string_view fileTypeToString(InputAccessor::Type type)
|
static std::string_view fileTypeToString(InputAccessor::Type type)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
|
@ -3103,31 +3074,6 @@ static RegisterPrimOp primop_lessThan({
|
||||||
*************************************************************/
|
*************************************************************/
|
||||||
|
|
||||||
|
|
||||||
/* Return the cryptographic hash of a string in base-16. */
|
|
||||||
static void prim_hashString(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
|
||||||
{
|
|
||||||
auto type = state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.hashString");
|
|
||||||
std::optional<HashType> ht = parseHashType(type);
|
|
||||||
if (!ht)
|
|
||||||
state.error<EvalError>("unknown hash algorithm '%1%'", type).atPos(pos).debugThrow();
|
|
||||||
|
|
||||||
NixStringContext context; // discarded
|
|
||||||
auto s = state.forceString(*args[1], context, pos, "while evaluating the second argument passed to builtins.hashString");
|
|
||||||
|
|
||||||
v.mkString(hashString(*ht, s).to_string(Base16, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
static RegisterPrimOp primop_hashString({
|
|
||||||
.name = "__hashString",
|
|
||||||
.args = {"type", "s"},
|
|
||||||
.doc = R"(
|
|
||||||
Return a base-16 representation of the cryptographic hash of string
|
|
||||||
*s*. The hash algorithm specified by *type* must be one of `"md5"`,
|
|
||||||
`"sha1"`, `"sha256"` or `"sha512"`.
|
|
||||||
)",
|
|
||||||
.fun = prim_hashString,
|
|
||||||
});
|
|
||||||
|
|
||||||
/*************************************************************
|
/*************************************************************
|
||||||
* Versions
|
* Versions
|
||||||
*************************************************************/
|
*************************************************************/
|
||||||
|
|
|
@ -86,4 +86,11 @@ struct RegexCache
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct RealisePathFlags {
|
||||||
|
// Whether to check that the path is allowed in pure eval mode
|
||||||
|
bool checkForPureEval = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
SourcePath realisePath(EvalState & state, const PosIdx pos, Value & v, const RealisePathFlags flags = {});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
69
src/libexpr/primops/hash.cc
Normal file
69
src/libexpr/primops/hash.cc
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
#include "hash.hh"
|
||||||
|
#include "primops.hh"
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* builtins.hashFile
|
||||||
|
*/
|
||||||
|
|
||||||
|
static void prim_hashFile(EvalState & state, const PosIdx pos, Value ** args, Value & v)
|
||||||
|
{
|
||||||
|
auto type = state.forceStringNoCtx(
|
||||||
|
*args[0], pos, "while evaluating the first argument passed to builtins.hashFile"
|
||||||
|
);
|
||||||
|
std::optional<HashType> ht = parseHashType(type);
|
||||||
|
if (!ht) {
|
||||||
|
state.error<EvalError>("unknown hash type '%1%'", type).atPos(pos).debugThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto path = realisePath(state, pos, *args[1]);
|
||||||
|
|
||||||
|
v.mkString(hashString(*ht, path.readFile()).to_string(Base16, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
static RegisterPrimOp primop_hashFile({
|
||||||
|
.name = "__hashFile",
|
||||||
|
.args = {"type", "p"},
|
||||||
|
.doc = R"(
|
||||||
|
Return a base-16 representation of the cryptographic hash of the
|
||||||
|
file at path *p*. The hash algorithm specified by *type* must be one
|
||||||
|
of `"md5"`, `"sha1"`, `"sha256"` or `"sha512"`.
|
||||||
|
)",
|
||||||
|
.fun = prim_hashFile,
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* builtins.hashString
|
||||||
|
*/
|
||||||
|
|
||||||
|
static void prim_hashString(EvalState & state, const PosIdx pos, Value ** args, Value & v)
|
||||||
|
{
|
||||||
|
auto type = state.forceStringNoCtx(
|
||||||
|
*args[0], pos, "while evaluating the first argument passed to builtins.hashString"
|
||||||
|
);
|
||||||
|
std::optional<HashType> ht = parseHashType(type);
|
||||||
|
if (!ht) {
|
||||||
|
state.error<EvalError>("unknown hash algorithm '%1%'", type).atPos(pos).debugThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
NixStringContext context; // discarded
|
||||||
|
auto s = state.forceString(
|
||||||
|
*args[1], context, pos, "while evaluating the second argument passed to builtins.hashString"
|
||||||
|
);
|
||||||
|
|
||||||
|
v.mkString(hashString(*ht, s).to_string(Base16, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
static RegisterPrimOp primop_hashString({
|
||||||
|
.name = "__hashString",
|
||||||
|
.args = {"type", "s"},
|
||||||
|
.doc = R"(
|
||||||
|
Return a base-16 representation of the cryptographic hash of string
|
||||||
|
*s*. The hash algorithm specified by *type* must be one of `"md5"`,
|
||||||
|
`"sha1"`, `"sha256"` or `"sha512"`.
|
||||||
|
)",
|
||||||
|
.fun = prim_hashString,
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue