Merge pull request #767 from mogorman/garbage_collect_keep_last_few

Implement --delete-generations + flag for keeping last N number of gens
This commit is contained in:
Peter Simons 2018-05-31 10:00:21 +02:00 committed by GitHub
commit 93aa3bea2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 6 deletions

View file

@ -1346,11 +1346,12 @@ $ nix-env --list-generations
<para>This operation deletes the specified generations of the current <para>This operation deletes the specified generations of the current
profile. The generations can be a list of generation numbers, the profile. The generations can be a list of generation numbers, the
special value <literal>old</literal> to delete all non-current special value <literal>old</literal> to delete all non-current
generations, or a value such as <literal>30d</literal> to delete all generations, a value such as <literal>30d</literal> to delete all
generations older than the specified number of days (except for the generations older than the specified number of days (except for the
generation that was active at that point in time). generation that was active at that point in time), or a value such as.
Periodically deleting old generations is important to make garbage <literal>+5</literal> to only keep the specified items older than the
collection effective.</para> current generation. Periodically deleting old generations is important
to make garbage collection effective.</para>
</refsection> </refsection>
@ -1359,6 +1360,8 @@ collection effective.</para>
<screen> <screen>
$ nix-env --delete-generations 3 4 8 $ nix-env --delete-generations 3 4 8
$ nix-env --delete-generations +5
$ nix-env --delete-generations 30d $ nix-env --delete-generations 30d
$ nix-env -p other_profile --delete-generations old</screen> $ nix-env -p other_profile --delete-generations old</screen>

View file

@ -157,6 +157,29 @@ void deleteGenerations(const Path & profile, const std::set<unsigned int> & gens
} }
} }
void deleteGenerationsGreaterThan(const Path & profile, int max, bool dryRun)
{
PathLocks lock;
lockProfile(lock, profile);
int curGen;
bool fromCurGen = false;
Generations gens = findGenerations(profile, curGen);
for (auto i = gens.rbegin(); i != gens.rend(); ++i) {
if (i->number == curGen) {
fromCurGen = true;
max--;
continue;
}
if (fromCurGen) {
if (max) {
max--;
continue;
}
deleteGeneration2(profile, i->number, dryRun);
}
}
}
void deleteOldGenerations(const Path & profile, bool dryRun) void deleteOldGenerations(const Path & profile, bool dryRun)
{ {

View file

@ -39,6 +39,8 @@ void deleteGeneration(const Path & profile, unsigned int gen);
void deleteGenerations(const Path & profile, const std::set<unsigned int> & gensToDelete, bool dryRun); void deleteGenerations(const Path & profile, const std::set<unsigned int> & gensToDelete, bool dryRun);
void deleteGenerationsGreaterThan(const Path & profile, const int max, bool dryRun);
void deleteOldGenerations(const Path & profile, bool dryRun); void deleteOldGenerations(const Path & profile, bool dryRun);
void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun); void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun);

View file

@ -1284,6 +1284,14 @@ static void opDeleteGenerations(Globals & globals, Strings opFlags, Strings opAr
deleteOldGenerations(globals.profile, globals.dryRun); deleteOldGenerations(globals.profile, globals.dryRun);
} else if (opArgs.size() == 1 && opArgs.front().find('d') != string::npos) { } else if (opArgs.size() == 1 && opArgs.front().find('d') != string::npos) {
deleteGenerationsOlderThan(globals.profile, opArgs.front(), globals.dryRun); deleteGenerationsOlderThan(globals.profile, opArgs.front(), globals.dryRun);
} else if (opArgs.size() == 1 && opArgs.front().find('+') != string::npos) {
if(opArgs.front().size() < 2)
throw Error(format("invalid number of generations %1%") % opArgs.front());
string str_max = string(opArgs.front(), 1, opArgs.front().size());
int max;
if (!string2Int(str_max, max) || max == 0)
throw Error(format("invalid number of generations to keep %1%") % opArgs.front());
deleteGenerationsGreaterThan(globals.profile, max, globals.dryRun);
} else { } else {
std::set<unsigned int> gens; std::set<unsigned int> gens;
for (auto & i : opArgs) { for (auto & i : opArgs) {