Skip to content

Commit

Permalink
Factor out StoreDirConfig
Browse files Browse the repository at this point in the history
More progress on NixOS#5729.
  • Loading branch information
Ericson2314 committed Mar 18, 2022
1 parent fef26ed commit e725d25
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 72 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"

namespace nix {

Expand Down Expand Up @@ -41,15 +41,15 @@ bool StorePath::isDerivation() const

StorePath StorePath::dummy("ffffffffffffffffffffffffffffffff-x");

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 @@ -58,24 +58,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
20 changes: 10 additions & 10 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
namespace nix {


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


std::pair<StorePath, Path> Store::toStorePath(const Path & path) const
std::pair<StorePath, Path> StoreDirConfig::toStorePath(const Path & path) const
{
if (!isInStore(path))
throw Error("path '%1%' is not in the Nix store", path);
Expand Down Expand Up @@ -135,7 +135,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 @@ -146,22 +146,22 @@ 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(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));
}


static std::string makeType(
const Store & store,
const StoreDirConfig & store,
std::string && type,
const StorePathSet & references,
bool hasSelfReference = false)
Expand All @@ -175,7 +175,7 @@ static std::string makeType(
}


StorePath Store::makeFixedOutputPath(
StorePath StoreDirConfig::makeFixedOutputPath(
FileIngestionMethod method,
const Hash & hash,
std::string_view name,
Expand All @@ -195,7 +195,7 @@ StorePath Store::makeFixedOutputPath(
}
}

StorePath Store::makeFixedOutputPathFromCA(std::string_view name, ContentAddress ca,
StorePath StoreDirConfig::makeFixedOutputPathFromCA(std::string_view name, ContentAddress ca,
const StorePathSet & references, bool hasSelfReference) const
{
// New template
Expand All @@ -209,7 +209,7 @@ StorePath Store::makeFixedOutputPathFromCA(std::string_view name, ContentAddress
}, ca);
}

StorePath Store::makeTextPath(std::string_view name, const Hash & hash,
StorePath StoreDirConfig::makeTextPath(std::string_view name, const Hash & hash,
const StorePathSet & references) const
{
assert(hash.type == htSHA256);
Expand Down Expand Up @@ -1166,7 +1166,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
58 changes: 3 additions & 55 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "config.hh"
#include "path-info.hh"
#include "repair-flag.hh"
#include "store-dir-config.hh"

#include <atomic>
#include <limits>
Expand Down Expand Up @@ -58,7 +59,6 @@ MakeError(InvalidPath, Error);
MakeError(Unsupported, Error);
MakeError(SubstituteGone, Error);
MakeError(SubstituterDisabled, Error);
MakeError(BadStorePath, Error);

MakeError(InvalidStoreURI, Error);

Expand All @@ -83,9 +83,9 @@ enum BuildMode { bmNormal, bmRepair, bmCheck };
struct BuildResult;


struct StoreConfig : public Config
struct StoreConfig : public StoreDirConfig
{
using Config::Config;
using StoreDirConfig::StoreDirConfig;

StoreConfig() = delete;

Expand All @@ -95,10 +95,6 @@ struct StoreConfig : public Config

virtual const std::string name() = 0;

const PathSetting storeDir_{this->dodgeGcc80431(), false, settings.nixStore,
"store", "path to the Nix store"};
const Path storeDir = storeDir_;

const Setting<int> pathInfoCacheSize{this->dodgeGcc80431(), 65536, "path-info-cache-size", "size of the in-memory store path information cache"};

const Setting<bool> isTrusted{this->dodgeGcc80431(), false, "trusted", "whether paths from this store can be used as substitutes even when they lack trusted signatures"};
Expand Down Expand Up @@ -171,61 +167,13 @@ 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;

// FIXME: 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(const Path & 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(const Path & path) const;

/* Follow symlinks until we end up with a path in the Nix store. */
Path followLinksToStore(std::string_view path) const;

/* Same as followLinksToStore(), but apply toStorePath() to the
result. */
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(FileIngestionMethod method,
const Hash & hash, std::string_view name,
const StorePathSet & references = {},
bool hasSelfReference = false) const;

StorePath makeTextPath(std::string_view name, const Hash & hash,
const StorePathSet & references = {}) const;

StorePath makeFixedOutputPathFromCA(std::string_view name, ContentAddress ca,
const StorePathSet & references = {},
bool hasSelfReference = false) const;

/* This is the preparatory part of addToStore(); it computes the
store path to which srcPath is to be copied. Returns the store
path and the cryptographic hash of the contents of srcPath. */
Expand Down
81 changes: 81 additions & 0 deletions src/libstore/store-dir-config.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include "path.hh"
#include "hash.hh"
#include "content-address.hh"
#include "globals.hh"
#include "config.hh"

#include <map>
#include <string>
#include <variant>


namespace nix {

MakeError(BadStorePath, Error);

struct StoreDirConfig : public Config
{
using Config::Config;

StoreDirConfig() = delete;

virtual ~StoreDirConfig() = default;

const PathSetting storeDir_{static_cast<Config &>(*this), false, settings.nixStore,
"store", "path to the Nix store"};
const Path storeDir = storeDir_;

// pure methods

StorePath parseStorePath(std::string_view path) const;

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

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

// FIXME: 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(const Path & 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(const Path & 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(FileIngestionMethod method,
const Hash & hash, std::string_view name,
const StorePathSet & references = {},
bool hasSelfReference = false) const;

StorePath makeTextPath(std::string_view name, const Hash & hash,
const StorePathSet & references = {}) const;

StorePath makeFixedOutputPathFromCA(std::string_view name, ContentAddress ca,
const StorePathSet & references = {},
bool hasSelfReference = false) const;
};

}

0 comments on commit e725d25

Please sign in to comment.