From 429154b74c7f4b4d679ae79bbc939049f537bfaf Mon Sep 17 00:00:00 2001 From: Matthew O'Gorman Date: Wed, 6 Jan 2016 20:15:19 -0500 Subject: Implement --delete-generations + flag for keeping last N number of generations --- src/libstore/profiles.cc | 23 +++++++++++++++++++++++ src/libstore/profiles.hh | 2 ++ src/nix-env/nix-env.cc | 2 ++ 3 files changed, 27 insertions(+) (limited to 'src') diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index 4a607b584506..e6300cf05552 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -157,6 +157,29 @@ void deleteGenerations(const Path & profile, const std::set & gens } } +void deleteGenerationsGreaterThan(const Path & profile, const string & max, bool dryRun) +{ + int max_keep = 0; + PathLocks lock; + if(max.size() < 2) + throw Error(format("invalid number of generations ‘%1%’") % max); + string str_max = string(max, 1, max.size()); + if (!string2Int(str_max, max_keep) || max_keep == 0) + throw Error(format("invalid number of generations to keep ‘%1%’") % max); + + lockProfile(lock, profile); + + int curGen; + Generations gens = findGenerations(profile, curGen); + + for (auto i = gens.rbegin(); i != gens.rend(); ++i) { + if (max_keep) { + max_keep--; + continue; + } + deleteGeneration2(profile, i->number, dryRun); + } +} void deleteOldGenerations(const Path & profile, bool dryRun) { diff --git a/src/libstore/profiles.hh b/src/libstore/profiles.hh index 1d4e6d3037db..3cad08dc5265 100644 --- a/src/libstore/profiles.hh +++ b/src/libstore/profiles.hh @@ -39,6 +39,8 @@ void deleteGeneration(const Path & profile, unsigned int gen); void deleteGenerations(const Path & profile, const std::set & gensToDelete, bool dryRun); +void deleteGenerationsGreaterThan(const Path & profile, const string & max, bool dryRun); + void deleteOldGenerations(const Path & profile, bool dryRun); void deleteGenerationsOlderThan(const Path & profile, time_t t, bool dryRun); diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 97e66cbd937e..1440b7744fd6 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1284,6 +1284,8 @@ static void opDeleteGenerations(Globals & globals, Strings opFlags, Strings opAr deleteOldGenerations(globals.profile, globals.dryRun); } else if (opArgs.size() == 1 && opArgs.front().find('d') != string::npos) { deleteGenerationsOlderThan(globals.profile, opArgs.front(), globals.dryRun); + } else if (opArgs.size() == 1 && opArgs.front().find('+') != string::npos) { + deleteGenerationsGreaterThan(globals.profile, opArgs.front(), globals.dryRun); } else { std::set gens; for (auto & i : opArgs) { -- cgit 1.4.1 From 0312d30315a3a77ab659b742b76ec32685145715 Mon Sep 17 00:00:00 2001 From: Matthew O'Gorman Date: Thu, 19 May 2016 15:42:54 -0400 Subject: this updates issues that were addressed by people in pr --- src/libstore/profiles.cc | 17 ++++++----------- src/libstore/profiles.hh | 2 +- src/nix-env/nix-env.cc | 8 +++++++- 3 files changed, 14 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index e6300cf05552..44c3c6e0d478 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -157,15 +157,10 @@ void deleteGenerations(const Path & profile, const std::set & gens } } -void deleteGenerationsGreaterThan(const Path & profile, const string & max, bool dryRun) +void deleteGenerationsGreaterThan(const Path & profile, int max, bool dryRun) { int max_keep = 0; PathLocks lock; - if(max.size() < 2) - throw Error(format("invalid number of generations ‘%1%’") % max); - string str_max = string(max, 1, max.size()); - if (!string2Int(str_max, max_keep) || max_keep == 0) - throw Error(format("invalid number of generations to keep ‘%1%’") % max); lockProfile(lock, profile); @@ -173,11 +168,11 @@ void deleteGenerationsGreaterThan(const Path & profile, const string & max, bool Generations gens = findGenerations(profile, curGen); for (auto i = gens.rbegin(); i != gens.rend(); ++i) { - if (max_keep) { - max_keep--; - continue; - } - deleteGeneration2(profile, i->number, dryRun); + if (max) { + max--; + continue; + } + deleteGeneration2(profile, i->number, dryRun); } } diff --git a/src/libstore/profiles.hh b/src/libstore/profiles.hh index 3cad08dc5265..5fa1533de311 100644 --- a/src/libstore/profiles.hh +++ b/src/libstore/profiles.hh @@ -39,7 +39,7 @@ void deleteGeneration(const Path & profile, unsigned int gen); void deleteGenerations(const Path & profile, const std::set & gensToDelete, bool dryRun); -void deleteGenerationsGreaterThan(const Path & profile, const string & max, bool dryRun); +void deleteGenerationsGreaterThan(const Path & profile, const int max, bool dryRun); void deleteOldGenerations(const Path & profile, bool dryRun); diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 1440b7744fd6..0da31588d23b 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1285,7 +1285,13 @@ static void opDeleteGenerations(Globals & globals, Strings opFlags, Strings opAr } else if (opArgs.size() == 1 && opArgs.front().find('d') != string::npos) { deleteGenerationsOlderThan(globals.profile, opArgs.front(), globals.dryRun); } else if (opArgs.size() == 1 && opArgs.front().find('+') != string::npos) { - deleteGenerationsGreaterThan(globals.profile, opArgs.front(), globals.dryRun); + 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 { std::set gens; for (auto & i : opArgs) { -- cgit 1.4.1 From 12fe2249e1b32dc9033e2680017a4c7f236edaac Mon Sep 17 00:00:00 2001 From: Matt O'Gorman Date: Thu, 21 Sep 2017 21:41:19 -0400 Subject: Update nix-env.cc missing comma --- src/nix-env/nix-env.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc index 0da31588d23b..f6a9d9e7b7c2 100644 --- a/src/nix-env/nix-env.cc +++ b/src/nix-env/nix-env.cc @@ -1287,7 +1287,7 @@ static void opDeleteGenerations(Globals & globals, Strings opFlags, Strings opAr } 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()); + 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()); -- cgit 1.4.1 From 3c16044cb0acc87e128534cb3e2a2006b71059b6 Mon Sep 17 00:00:00 2001 From: Matthew O'Gorman Date: Thu, 1 Mar 2018 22:22:02 -0500 Subject: remove unused variable and make sure to check that the current generation is not the one we are deleting --- src/libstore/profiles.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index 44c3c6e0d478..b43ec66f60c7 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -159,7 +159,6 @@ void deleteGenerations(const Path & profile, const std::set & gens void deleteGenerationsGreaterThan(const Path & profile, int max, bool dryRun) { - int max_keep = 0; PathLocks lock; lockProfile(lock, profile); @@ -172,7 +171,8 @@ void deleteGenerationsGreaterThan(const Path & profile, int max, bool dryRun) max--; continue; } - deleteGeneration2(profile, i->number, dryRun); + if (i->number != curGen) + deleteGeneration2(profile, i->number, dryRun); } } -- cgit 1.4.1 From 467fdd8ca4d63972dbd94f0496918522c58916a0 Mon Sep 17 00:00:00 2001 From: Matthew O'Gorman Date: Thu, 1 Mar 2018 22:59:00 -0500 Subject: only delete things older than current gen and update logic in doc as well --- doc/manual/command-ref/nix-env.xml | 10 +++++----- src/libstore/profiles.cc | 13 +++++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/doc/manual/command-ref/nix-env.xml b/doc/manual/command-ref/nix-env.xml index f0e70a41c024..7eb9cc855bae 100644 --- a/doc/manual/command-ref/nix-env.xml +++ b/doc/manual/command-ref/nix-env.xml @@ -1349,9 +1349,9 @@ special value old to delete all non-current generations, a value such as 30d to delete all generations older than the specified number of days (except for the generation that was active at that point in time), or a value such as. -+5 to delete all but the number of items specified. -Periodically deleting old generations is important to make garbage -collection effective. ++5 to only keep the specified items older than the +current generation. Periodically deleting old generations is important +to make garbage collection effective. @@ -1461,7 +1461,7 @@ error: no generation older than the current (91) exists Environment variables - + NIX_PROFILE Location of the Nix profile. Defaults to the @@ -1475,6 +1475,6 @@ error: no generation older than the current (91) exists - + diff --git a/src/libstore/profiles.cc b/src/libstore/profiles.cc index b43ec66f60c7..4c6af567ae6f 100644 --- a/src/libstore/profiles.cc +++ b/src/libstore/profiles.cc @@ -160,19 +160,24 @@ void deleteGenerations(const Path & profile, const std::set & 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 (max) { + if (i->number == curGen) { + fromCurGen = true; max--; continue; } - if (i->number != curGen) + if (fromCurGen) { + if (max) { + max--; + continue; + } deleteGeneration2(profile, i->number, dryRun); + } } } -- cgit 1.4.1