Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libfetchers/git-lfs-fetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "nix/util/util.hh"
#include "nix/util/hash.hh"
#include "nix/store/ssh.hh"
#include "nix/util/deleter.hh"

#include <git2/attr.h>
#include <git2/config.h>
Expand Down
1 change: 1 addition & 0 deletions src/libfetchers/git-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "nix/util/thread-pool.hh"
#include "nix/util/pool.hh"
#include "nix/util/executable-path.hh"
#include "nix/util/deleter.hh"

#include <git2/attr.h>
#include <git2/blob.h>
Expand Down
11 changes: 0 additions & 11 deletions src/libfetchers/include/nix/fetchers/git-utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,6 @@ struct GitRepo
virtual Hash dereferenceSingletonDirectory(const Hash & oid) = 0;
};

// A helper to ensure that the `git_*_free` functions get called.
template<auto del>
struct Deleter
{
template<typename T>
void operator()(T * p) const
{
del(p);
};
};

// A helper to ensure that we don't leak objects returned by libgit2.
template<typename T>
struct Setter
Expand Down
19 changes: 19 additions & 0 deletions src/libutil/include/nix/util/deleter.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

namespace nix {

/**
* A helper for `std::unique_ptr` that ensures that C APIs that require manual memory management get properly freed. The
* template parameter `del` is a function that takes a pointer and frees it.
*/
template<auto del>
struct Deleter
{
template<typename T>
void operator()(T * p) const
{
del(p);
};
};

} // namespace nix
11 changes: 2 additions & 9 deletions src/libutil/include/nix/util/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "nix/util/types.hh"
#include "nix/util/file-descriptor.hh"
#include "nix/util/file-path.hh"
#include "nix/util/deleter.hh"

#include <filesystem>
#include <sys/types.h>
Expand Down Expand Up @@ -374,15 +375,7 @@ public:
}
};

struct DIRDeleter
{
void operator()(DIR * dir) const
{
closedir(dir);
}
};

typedef std::unique_ptr<DIR, DIRDeleter> AutoCloseDir;
typedef std::unique_ptr<DIR, Deleter<closedir>> AutoCloseDir;

/**
* Create a temporary directory.
Expand Down
1 change: 1 addition & 0 deletions src/libutil/include/nix/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ headers = [ config_pub_h ] + files(
'config-impl.hh',
'configuration.hh',
'current-process.hh',
'deleter.hh',
'demangle.hh',
'english.hh',
'environment-variables.hh',
Expand Down
23 changes: 16 additions & 7 deletions src/libutil/include/nix/util/signature/local-keys.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ struct Signature
auto operator<=>(const Signature &) const = default;
};

enum KeyType {
Ed25519,
MLDSA65,
};

KeyType parseKeyType(std::string_view s);

struct Key
{
KeyType type;
std::string name;
std::string key;

Expand All @@ -59,8 +67,9 @@ protected:
*/
Key(std::string_view s, bool sensitiveValue);

Key(std::string_view name, std::string && key)
: name(name)
Key(KeyType type, std::string_view name, std::string && key)
: type(type)
, name(name)
, key(std::move(key))
{
}
Expand All @@ -79,11 +88,11 @@ struct SecretKey : Key

PublicKey toPublicKey() const;

static SecretKey generate(std::string_view name);
static SecretKey generate(std::string_view name, KeyType type);

private:
SecretKey(std::string_view name, std::string && key)
: Key(name, std::move(key))
SecretKey(KeyType type, std::string_view name, std::string && key)
: Key(type, name, std::move(key))
{
}
};
Expand All @@ -107,8 +116,8 @@ struct PublicKey : Key
bool verifyDetachedAnon(std::string_view data, const Signature & sig) const;

private:
PublicKey(std::string_view name, std::string && key)
: Key(name, std::move(key))
PublicKey(KeyType type, std::string_view name, std::string && key)
: Key(type, name, std::move(key))
{
}
friend struct SecretKey;
Expand Down
Loading
Loading