Skip to content

Commit

Permalink
Factor out StoreDirConfig
Browse files Browse the repository at this point in the history
More progress on #5729.

(cherry picked from commit e97ac09)
  • Loading branch information
Ericson2314 authored and github-actions[bot] committed Dec 7, 2023
1 parent 43d55dd commit 6d3655b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 119 deletions.
14 changes: 7 additions & 7 deletions src/libstore/path.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "store-api.hh"
#include "store-dir-config.hh"

#include <sodium.h>

Expand Down Expand Up @@ -54,15 +54,15 @@ StorePath StorePath::random(std::string_view name)
return StorePath(hash, name);
}

StorePath Store::parseStorePath(std::string_view path) const
StorePath StoreDirConfig::parseStorePath(std::string_view path) const
{
auto p = canonPath(std::string(path));
if (dirOf(p) != storeDir)
throw BadStorePath("path '%s' is not in the Nix store", p);
return StorePath(baseNameOf(p));
}

std::optional<StorePath> Store::maybeParseStorePath(std::string_view path) const
std::optional<StorePath> StoreDirConfig::maybeParseStorePath(std::string_view path) const
{
try {
return parseStorePath(path);
Expand All @@ -71,24 +71,24 @@ std::optional<StorePath> Store::maybeParseStorePath(std::string_view path) const
}
}

bool Store::isStorePath(std::string_view path) const
bool StoreDirConfig::isStorePath(std::string_view path) const
{
return (bool) maybeParseStorePath(path);
}

StorePathSet Store::parseStorePathSet(const PathSet & paths) const
StorePathSet StoreDirConfig::parseStorePathSet(const PathSet & paths) const
{
StorePathSet res;
for (auto & i : paths) res.insert(parseStorePath(i));
return res;
}

std::string Store::printStorePath(const StorePath & path) const
std::string StoreDirConfig::printStorePath(const StorePath & path) const
{
return (storeDir + "/").append(path.to_string());
}

PathSet Store::printStorePathSet(const StorePathSet & paths) const
PathSet StoreDirConfig::printStorePathSet(const StorePathSet & paths) const
{
PathSet res;
for (auto & i : paths) res.insert(printStorePath(i));
Expand Down
24 changes: 12 additions & 12 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ using json = nlohmann::json;
namespace nix {


bool Store::isInStore(PathView path) const
bool StoreDirConfig::isInStore(PathView path) const
{
return isInDir(path, storeDir);
}


std::pair<StorePath, Path> Store::toStorePath(PathView path) const
std::pair<StorePath, Path> StoreDirConfig::toStorePath(PathView path) const
{
if (!isInStore(path))
throw Error("path '%1%' is not in the Nix store", path);
Expand Down Expand Up @@ -145,7 +145,7 @@ StorePath Store::followLinksToStorePath(std::string_view path) const
*/


StorePath Store::makeStorePath(std::string_view type,
StorePath StoreDirConfig::makeStorePath(std::string_view type,
std::string_view hash, std::string_view name) const
{
/* e.g., "source:sha256:1abc...:/nix/store:foo.tar.gz" */
Expand All @@ -156,14 +156,14 @@ StorePath Store::makeStorePath(std::string_view type,
}


StorePath Store::makeStorePath(std::string_view type,
StorePath StoreDirConfig::makeStorePath(std::string_view type,
const Hash & hash, std::string_view name) const
{
return makeStorePath(type, hash.to_string(HashFormat::Base16, true), name);
}


StorePath Store::makeOutputPath(std::string_view id,
StorePath StoreDirConfig::makeOutputPath(std::string_view id,
const Hash & hash, std::string_view name) const
{
return makeStorePath("output:" + std::string { id }, hash, outputPathName(name, id));
Expand All @@ -174,7 +174,7 @@ StorePath Store::makeOutputPath(std::string_view id,
hacky, but we can't put them in, say, <s2> (per the grammar above)
since that would be ambiguous. */
static std::string makeType(
const Store & store,
const StoreDirConfig & store,
std::string && type,
const StoreReferences & references)
{
Expand All @@ -187,7 +187,7 @@ static std::string makeType(
}


StorePath Store::makeFixedOutputPath(std::string_view name, const FixedOutputInfo & info) const
StorePath StoreDirConfig::makeFixedOutputPath(std::string_view name, const FixedOutputInfo & info) const
{
if (info.hash.type == htSHA256 && info.method == FileIngestionMethod::Recursive) {
return makeStorePath(makeType(*this, "source", info.references), info.hash, name);
Expand All @@ -203,7 +203,7 @@ StorePath Store::makeFixedOutputPath(std::string_view name, const FixedOutputInf
}


StorePath Store::makeTextPath(std::string_view name, const TextInfo & info) const
StorePath StoreDirConfig::makeTextPath(std::string_view name, const TextInfo & info) const
{
assert(info.hash.type == htSHA256);
return makeStorePath(
Expand All @@ -216,7 +216,7 @@ StorePath Store::makeTextPath(std::string_view name, const TextInfo & info) cons
}


StorePath Store::makeFixedOutputPathFromCA(std::string_view name, const ContentAddressWithReferences & ca) const
StorePath StoreDirConfig::makeFixedOutputPathFromCA(std::string_view name, const ContentAddressWithReferences & ca) const
{
// New template
return std::visit(overloaded {
Expand All @@ -230,7 +230,7 @@ StorePath Store::makeFixedOutputPathFromCA(std::string_view name, const ContentA
}


std::pair<StorePath, Hash> Store::computeStorePathFromDump(
std::pair<StorePath, Hash> StoreDirConfig::computeStorePathFromDump(
Source & dump,
std::string_view name,
FileIngestionMethod method,
Expand All @@ -249,7 +249,7 @@ std::pair<StorePath, Hash> Store::computeStorePathFromDump(
}


StorePath Store::computeStorePathForText(
StorePath StoreDirConfig::computeStorePathForText(
std::string_view name,
std::string_view s,
const StorePathSet & references) const
Expand Down Expand Up @@ -1227,7 +1227,7 @@ std::optional<ValidPathInfo> decodeValidPathInfo(const Store & store, std::istre
}


std::string Store::showPaths(const StorePathSet & paths)
std::string StoreDirConfig::showPaths(const StorePathSet & paths)
{
std::string s;
for (auto & i : paths) {
Expand Down
103 changes: 3 additions & 100 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "config.hh"
#include "path-info.hh"
#include "repair-flag.hh"
#include "store-dir-config.hh"

#include <nlohmann/json_fwd.hpp>
#include <atomic>
Expand Down Expand Up @@ -64,7 +65,6 @@ MakeError(InvalidPath, Error);
MakeError(Unsupported, Error);
MakeError(SubstituteGone, Error);
MakeError(SubstituterDisabled, Error);
MakeError(BadStorePath, Error);

MakeError(InvalidStoreURI, Error);

Expand Down Expand Up @@ -96,11 +96,11 @@ struct KeyedBuildResult;

typedef std::map<StorePath, std::optional<ContentAddress>> StorePathCAMap;

struct StoreConfig : public Config
struct StoreConfig : public StoreDirConfig
{
typedef std::map<std::string, std::string> Params;

using Config::Config;
using StoreDirConfig::StoreDirConfig;

StoreConfig() = delete;

Expand Down Expand Up @@ -130,15 +130,6 @@ struct StoreConfig : public Config
return std::nullopt;
}

const PathSetting storeDir_{this, settings.nixStore,
"store",
R"(
Logical location of the Nix store, usually
`/nix/store`. Note that you can only copy store paths
between stores if they have the same `store` setting.
)"};
const Path storeDir = storeDir_;

const Setting<int> pathInfoCacheSize{this, 65536, "path-info-cache-size",
"Size of the in-memory store path metadata cache."};

Expand Down Expand Up @@ -223,45 +214,6 @@ public:

virtual std::string getUri() = 0;

StorePath parseStorePath(std::string_view path) const;

std::optional<StorePath> maybeParseStorePath(std::string_view path) const;

std::string printStorePath(const StorePath & path) const;

/**
* Deprecated
*
* \todo remove
*/
StorePathSet parseStorePathSet(const PathSet & paths) const;

PathSet printStorePathSet(const StorePathSet & path) const;

/**
* Display a set of paths in human-readable form (i.e., between quotes
* and separated by commas).
*/
std::string showPaths(const StorePathSet & paths);

/**
* @return true if ‘path’ is in the Nix store (but not the Nix
* store itself).
*/
bool isInStore(PathView path) const;

/**
* @return true if ‘path’ is a store path, i.e. a direct child of the
* Nix store.
*/
bool isStorePath(std::string_view path) const;

/**
* Split a path like /nix/store/<hash>-<name>/<bla> into
* /nix/store/<hash>-<name> and /<bla>.
*/
std::pair<StorePath, Path> toStorePath(PathView path) const;

/**
* Follow symlinks until we end up with a path in the Nix store.
*/
Expand All @@ -273,55 +225,6 @@ public:
*/
StorePath followLinksToStorePath(std::string_view path) const;

/**
* Constructs a unique store path name.
*/
StorePath makeStorePath(std::string_view type,
std::string_view hash, std::string_view name) const;
StorePath makeStorePath(std::string_view type,
const Hash & hash, std::string_view name) const;

StorePath makeOutputPath(std::string_view id,
const Hash & hash, std::string_view name) const;

StorePath makeFixedOutputPath(std::string_view name, const FixedOutputInfo & info) const;

StorePath makeTextPath(std::string_view name, const TextInfo & info) const;

StorePath makeFixedOutputPathFromCA(std::string_view name, const ContentAddressWithReferences & ca) const;

/**
* Read-only variant of addToStoreFromDump(). It returns the store
* path to which a NAR or flat file would be written.
*/
std::pair<StorePath, Hash> computeStorePathFromDump(
Source & dump,
std::string_view name,
FileIngestionMethod method = FileIngestionMethod::Recursive,
HashType hashAlgo = htSHA256,
const StorePathSet & references = {}) const;

/**
* Preparatory part of addTextToStore().
*
* !!! Computation of the path should take the references given to
* addTextToStore() into account, otherwise we have a (relatively
* minor) security hole: a caller can register a source file with
* bogus references. If there are too many references, the path may
* not be garbage collected when it has to be (not really a problem,
* the caller could create a root anyway), or it may be garbage
* collected when it shouldn't be (more serious).
*
* Hashing the references would solve this (bogus references would
* simply yield a different store path, so other users wouldn't be
* affected), but it has some backwards compatibility issues (the
* hashing scheme changes), so I'm not doing that for now.
*/
StorePath computeStorePathForText(
std::string_view name,
std::string_view s,
const StorePathSet & references) const;

/**
* Check whether a path is valid.
*/
Expand Down
Loading

0 comments on commit 6d3655b

Please sign in to comment.