2022-03-01 20:31:36 +02:00
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
///@file
|
2022-03-01 20:31:36 +02:00
|
|
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::unordered_map<StorePath, std::unordered_set<std::string>> Roots;
|
|
|
|
|
|
|
|
|
|
|
|
struct GCOptions
|
|
|
|
{
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Garbage collector operation:
|
|
|
|
*
|
|
|
|
* - `gcReturnLive`: return the set of paths reachable from
|
|
|
|
* (i.e. in the closure of) the roots.
|
|
|
|
*
|
|
|
|
* - `gcReturnDead`: return the set of paths not reachable from
|
|
|
|
* the roots.
|
|
|
|
*
|
|
|
|
* - `gcDeleteDead`: actually delete the latter set.
|
|
|
|
*
|
|
|
|
* - `gcDeleteSpecific`: delete the paths listed in
|
|
|
|
* `pathsToDelete`, insofar as they are not reachable.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
typedef enum {
|
|
|
|
gcReturnLive,
|
|
|
|
gcReturnDead,
|
|
|
|
gcDeleteDead,
|
|
|
|
gcDeleteSpecific,
|
|
|
|
} GCAction;
|
|
|
|
|
|
|
|
GCAction action{gcDeleteDead};
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* If `ignoreLiveness` is set, then reachability from the roots is
|
|
|
|
* ignored (dangerous!). However, the paths must still be
|
|
|
|
* unreferenced *within* the store (i.e., there can be no other
|
|
|
|
* store paths that depend on them).
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
bool ignoreLiveness{false};
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* For `gcDeleteSpecific`, the paths to delete.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
StorePathSet pathsToDelete;
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Stop after at least `maxFreed` bytes have been freed.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
uint64_t maxFreed{std::numeric_limits<uint64_t>::max()};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct GCResults
|
|
|
|
{
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Depending on the action, the GC roots, or the paths that would
|
|
|
|
* be or have been deleted.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
PathSet paths;
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* For `gcReturnDead`, `gcDeleteDead` and `gcDeleteSpecific`, the
|
|
|
|
* number of bytes that would be or was freed.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
uint64_t bytesFreed = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct GcStore : public virtual Store
|
|
|
|
{
|
2022-03-09 17:27:48 +02:00
|
|
|
inline static std::string operationName = "Garbage collection";
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Add an indirect root, which is merely a symlink to `path` from
|
|
|
|
* `/nix/var/nix/gcroots/auto/<hash of path>`. `path` is supposed
|
|
|
|
* to be a symlink to a store path. The garbage collector will
|
|
|
|
* automatically remove the indirect root when it finds that
|
|
|
|
* `path` has disappeared.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
virtual void addIndirectRoot(const Path & path) = 0;
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Find the roots of the garbage collector. Each root is a pair
|
|
|
|
* `(link, storepath)` where `link` is the path of the symlink
|
|
|
|
* outside of the Nix store that point to `storePath`. If
|
|
|
|
* `censor` is true, privacy-sensitive information about roots
|
|
|
|
* found in `/proc` is censored.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
virtual Roots findRoots(bool censor) = 0;
|
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Perform a garbage collection.
|
|
|
|
*/
|
2022-03-01 20:31:36 +02:00
|
|
|
virtual void collectGarbage(const GCOptions & options, GCResults & results) = 0;
|
2022-03-08 19:45:19 +02:00
|
|
|
};
|
2022-03-01 20:31:36 +02:00
|
|
|
|
|
|
|
}
|