Skip to content

Commit 1d3f0ca

Browse files
authored
Merge pull request #14383 from obsidiansystems/misc-cleanups
Two misc cleanups
2 parents e177f42 + 234f029 commit 1d3f0ca

File tree

9 files changed

+36
-31
lines changed

9 files changed

+36
-31
lines changed

src/libstore/dummy-store.cc

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,6 @@ struct DummyStoreImpl : DummyStore
258258
});
259259
}
260260

261-
void narFromPath(const StorePath & path, Sink & sink) override
262-
{
263-
bool visited = contents.cvisit(path, [&](const auto & kv) {
264-
const auto & [info, accessor] = kv.second;
265-
SourcePath sourcePath(accessor);
266-
dumpPath(sourcePath, sink, FileSerialisationMethod::NixArchive);
267-
});
268-
269-
if (!visited)
270-
throw Error("path '%s' is not valid", printStorePath(path));
271-
}
272-
273261
void queryRealisationUncached(
274262
const DrvOutput & drvOutput, Callback<std::shared_ptr<const UnkeyedRealisation>> callback) noexcept override
275263
{

src/libstore/include/nix/store/local-fs-store.hh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ struct LocalFSStore : virtual Store, virtual GcStore, virtual LogStore
7878

7979
LocalFSStore(const Config & params);
8080

81-
void narFromPath(const StorePath & path, Sink & sink) override;
8281
ref<SourceAccessor> getFSAccessor(bool requireValidPath = true) override;
8382
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath = true) override;
8483

src/libstore/include/nix/store/store-api.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public:
609609
/**
610610
* Write a NAR dump of a store path.
611611
*/
612-
virtual void narFromPath(const StorePath & path, Sink & sink) = 0;
612+
virtual void narFromPath(const StorePath & path, Sink & sink);
613613

614614
/**
615615
* For each path, if it's a derivation, build it. Building a

src/libstore/include/nix/store/uds-remote-store.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct UDSRemoteStore : virtual IndirectRootStore, virtual RemoteStore
6868

6969
void narFromPath(const StorePath & path, Sink & sink) override
7070
{
71-
LocalFSStore::narFromPath(path, sink);
71+
Store::narFromPath(path, sink);
7272
}
7373

7474
/**

src/libstore/local-fs-store.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ std::shared_ptr<SourceAccessor> LocalFSStore::getFSAccessor(const StorePath & pa
112112
return std::make_shared<PosixSourceAccessor>(std::move(absPath));
113113
}
114114

115-
void LocalFSStore::narFromPath(const StorePath & path, Sink & sink)
116-
{
117-
if (!isValidPath(path))
118-
throw Error("path '%s' is not valid", printStorePath(path));
119-
dumpPath(getRealStoreDir() + std::string(printStorePath(path), storeDir.size()), sink);
120-
}
121-
122115
const std::string LocalFSStore::drvsLogDir = "drvs";
123116

124117
std::optional<std::string> LocalFSStore::getBuildLogExact(const StorePath & path)

src/libstore/restricted-store.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void RestrictedStore::narFromPath(const StorePath & path, Sink & sink)
226226
{
227227
if (!goal.isAllowed(path))
228228
throw InvalidPath("cannot dump unknown path '%s' in recursive Nix", printStorePath(path));
229-
LocalFSStore::narFromPath(path, sink);
229+
Store::narFromPath(path, sink);
230230
}
231231

232232
void RestrictedStore::ensurePath(const StorePath & path)

src/libstore/ssh-store.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct MountedSSHStore : virtual SSHStore, virtual LocalFSStore
143143

144144
void narFromPath(const StorePath & path, Sink & sink) override
145145
{
146-
return LocalFSStore::narFromPath(path, sink);
146+
return Store::narFromPath(path, sink);
147147
}
148148

149149
ref<SourceAccessor> getFSAccessor(bool requireValidPath) override

src/libstore/store-api.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,13 @@ ValidPathInfo Store::addToStoreSlow(
300300
return info;
301301
}
302302

303+
void Store::narFromPath(const StorePath & path, Sink & sink)
304+
{
305+
auto accessor = requireStoreObjectAccessor(path);
306+
SourcePath sourcePath{accessor};
307+
dumpPath(sourcePath, sink, FileSerialisationMethod::NixArchive);
308+
}
309+
303310
StringSet Store::Config::getDefaultSystemFeatures()
304311
{
305312
auto res = settings.systemFeatures.get();

src/libutil/include/nix/util/ref.hh

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,32 @@ private:
1717

1818
std::shared_ptr<T> p;
1919

20+
void assertNonNull()
21+
{
22+
if (!p)
23+
throw std::invalid_argument("null pointer cast to ref");
24+
}
25+
2026
public:
2127

2228
using element_type = T;
2329

2430
explicit ref(const std::shared_ptr<T> & p)
2531
: p(p)
2632
{
27-
if (!p)
28-
throw std::invalid_argument("null pointer cast to ref");
33+
assertNonNull();
34+
}
35+
36+
explicit ref(std::shared_ptr<T> && p)
37+
: p(std::move(p))
38+
{
39+
assertNonNull();
2940
}
3041

3142
explicit ref(T * p)
3243
: p(p)
3344
{
34-
if (!p)
35-
throw std::invalid_argument("null pointer cast to ref");
45+
assertNonNull();
3646
}
3747

3848
T * operator->() const
@@ -45,14 +55,22 @@ public:
4555
return *p;
4656
}
4757

48-
operator std::shared_ptr<T>() const
58+
std::shared_ptr<T> get_ptr() const &
4959
{
5060
return p;
5161
}
5262

53-
std::shared_ptr<T> get_ptr() const
63+
std::shared_ptr<T> get_ptr() &&
5464
{
55-
return p;
65+
return std::move(p);
66+
}
67+
68+
/**
69+
* Convenience to avoid explicit `get_ptr()` call in some cases.
70+
*/
71+
operator std::shared_ptr<T>(this auto && self)
72+
{
73+
return std::forward<decltype(self)>(self).get_ptr();
5674
}
5775

5876
template<typename T2>

0 commit comments

Comments
 (0)