Skip to content

Commit 19db567

Browse files
authored
Merge pull request #14744 from NixOS/gc-actions-daemon-check
daemon: Add WorkerProto serialiser for GCAction
2 parents afc2b96 + f2465bc commit 19db567

File tree

5 files changed

+70
-22
lines changed

5 files changed

+70
-22
lines changed

src/libstore/daemon.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ static void performOp(
743743

744744
case WorkerProto::Op::CollectGarbage: {
745745
GCOptions options;
746-
options.action = (GCOptions::GCAction) readInt(conn.from);
746+
options.action = WorkerProto::Serialise<GCOptions::GCAction>::read(*store, rconn);
747747
options.pathsToDelete = WorkerProto::Serialise<StorePathSet>::read(*store, rconn);
748748
conn.from >> options.ignoreLiveness >> options.maxFreed;
749749
// obsolete fields

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,31 @@ typedef boost::unordered_flat_map<
1313
std::hash<StorePath>>
1414
Roots;
1515

16+
/**
17+
* Garbage collector operation:
18+
*
19+
* - `gcReturnLive`: return the set of paths reachable from
20+
* (i.e. in the closure of) the roots.
21+
*
22+
* - `gcReturnDead`: return the set of paths not reachable from
23+
* the roots.
24+
*
25+
* - `gcDeleteDead`: actually delete the latter set.
26+
*
27+
* - `gcDeleteSpecific`: delete the paths listed in
28+
* `pathsToDelete`, insofar as they are not reachable.
29+
*/
30+
enum class GCAction {
31+
gcReturnLive,
32+
gcReturnDead,
33+
gcDeleteDead,
34+
gcDeleteSpecific,
35+
};
36+
1637
struct GCOptions
1738
{
18-
/**
19-
* Garbage collector operation:
20-
*
21-
* - `gcReturnLive`: return the set of paths reachable from
22-
* (i.e. in the closure of) the roots.
23-
*
24-
* - `gcReturnDead`: return the set of paths not reachable from
25-
* the roots.
26-
*
27-
* - `gcDeleteDead`: actually delete the latter set.
28-
*
29-
* - `gcDeleteSpecific`: delete the paths listed in
30-
* `pathsToDelete`, insofar as they are not reachable.
31-
*/
32-
typedef enum {
33-
gcReturnLive,
34-
gcReturnDead,
35-
gcDeleteDead,
36-
gcDeleteSpecific,
37-
} GCAction;
39+
using GCAction = nix::GCAction;
40+
using enum GCAction;
3841

3942
GCAction action{gcDeleteDead};
4043

src/libstore/include/nix/store/worker-protocol.hh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct ValidPathInfo;
3737
struct UnkeyedValidPathInfo;
3838
enum BuildMode : uint8_t;
3939
enum TrustedFlag : bool;
40+
enum class GCAction;
4041

4142
/**
4243
* The "worker protocol", used by unix:// and ssh-ng:// stores.
@@ -263,6 +264,8 @@ DECLARE_WORKER_SERIALISER(UnkeyedValidPathInfo);
263264
template<>
264265
DECLARE_WORKER_SERIALISER(BuildMode);
265266
template<>
267+
DECLARE_WORKER_SERIALISER(GCAction);
268+
template<>
266269
DECLARE_WORKER_SERIALISER(std::optional<TrustedFlag>);
267270
template<>
268271
DECLARE_WORKER_SERIALISER(std::optional<std::chrono::microseconds>);

src/libstore/remote-store.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ void RemoteStore::collectGarbage(const GCOptions & options, GCResults & results)
694694
{
695695
auto conn(getConnection());
696696

697-
conn->to << WorkerProto::Op::CollectGarbage << options.action;
697+
conn->to << WorkerProto::Op::CollectGarbage;
698+
WorkerProto::write(*this, *conn, options.action);
698699
WorkerProto::write(*this, *conn, options.pathsToDelete);
699700
conn->to << options.ignoreLiveness
700701
<< options.maxFreed

src/libstore/worker-protocol.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "nix/util/serialise.hh"
22
#include "nix/store/path-with-outputs.hh"
33
#include "nix/store/store-api.hh"
4+
#include "nix/store/gc-store.hh"
45
#include "nix/store/build-result.hh"
56
#include "nix/store/worker-protocol.hh"
67
#include "nix/store/worker-protocol-impl.hh"
@@ -47,6 +48,46 @@ void WorkerProto::Serialise<BuildMode>::write(
4748
};
4849
}
4950

51+
GCAction WorkerProto::Serialise<GCAction>::read(const StoreDirConfig & store, WorkerProto::ReadConn conn)
52+
{
53+
auto temp = readNum<unsigned>(conn.from);
54+
using enum GCAction;
55+
switch (temp) {
56+
case 0:
57+
return gcReturnLive;
58+
case 1:
59+
return gcReturnDead;
60+
case 2:
61+
return gcDeleteDead;
62+
case 3:
63+
return gcDeleteSpecific;
64+
default:
65+
throw Error("Invalid GC action");
66+
}
67+
}
68+
69+
void WorkerProto::Serialise<GCAction>::write(
70+
const StoreDirConfig & store, WorkerProto::WriteConn conn, const GCAction & action)
71+
{
72+
using enum GCAction;
73+
switch (action) {
74+
case gcReturnLive:
75+
conn.to << unsigned{0};
76+
break;
77+
case gcReturnDead:
78+
conn.to << unsigned{1};
79+
break;
80+
case gcDeleteDead:
81+
conn.to << unsigned{2};
82+
break;
83+
case gcDeleteSpecific:
84+
conn.to << unsigned{3};
85+
break;
86+
default:
87+
assert(false);
88+
}
89+
}
90+
5091
std::optional<TrustedFlag>
5192
WorkerProto::Serialise<std::optional<TrustedFlag>>::read(const StoreDirConfig & store, WorkerProto::ReadConn conn)
5293
{

0 commit comments

Comments
 (0)