summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGábor Németh <homar@riseup.net>2023-03-06 16:19:14 +0100
committerGábor Németh <homar@riseup.net>2023-11-22 11:05:28 +0100
commitbd7c126e3fb1b94e76e0e632c657cea854586844 (patch)
treee7e5140d1857d155a187f6392cf458c0237f6858
parent6ba6b29e37a7e7b867a88f1d74e7dcfd7f83c30e (diff)
Add 'dist-clean' command to remove packages and list files
We assume all files in the 'listsdir' are candidates. Keep only files ending with Release, Release.gpg, and InRelease. Closes: #959093
-rw-r--r--apt-pkg/acquire.cc54
-rw-r--r--apt-pkg/acquire.h23
-rw-r--r--apt-private/private-download.cc23
-rw-r--r--apt-private/private-download.h2
-rw-r--r--cmdline/apt.cc2
5 files changed, 86 insertions, 18 deletions
diff --git a/apt-pkg/acquire.cc b/apt-pkg/acquire.cc
index cdb2c5257..727880e71 100644
--- a/apt-pkg/acquire.cc
+++ b/apt-pkg/acquire.cc
@@ -29,6 +29,7 @@
#include <iostream>
#include <memory>
#include <numeric>
+#include <regex>
#include <sstream>
#include <string>
#include <tuple>
@@ -825,11 +826,11 @@ pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I)
return I->NextAcquire;
}
/*}}}*/
-// Acquire::Clean - Cleans a directory /*{{{*/
+// Acquire::CleanDir - Cleans a directory /*{{{*/
// ---------------------------------------------------------------------
/* This is a bit simplistic, it looks at every file in the dir and sees
- if it is part of the download set. */
-bool pkgAcquire::Clean(string Dir)
+ if it matches the predicate or not. */
+bool pkgAcquire::CleanDir(string Dir, std::function<bool(char const*)> const &Keep, char const * const Caller)
{
// non-existing directories are by definition clean…
if (DirectoryExists(Dir) == false)
@@ -853,22 +854,49 @@ bool pkgAcquire::Clean(string Dir)
strcmp(E->d_name, "auxfiles") == 0 ||
strcmp(E->d_name, "lost+found") == 0 ||
strcmp(E->d_name, ".") == 0 ||
- strcmp(E->d_name, "..") == 0)
+ strcmp(E->d_name, "..") == 0 ||
+ Keep(E->d_name) == true)
continue;
-
- // Look in the get list and if not found nuke
- if (std::any_of(Items.cbegin(), Items.cend(),
- [&E](pkgAcquire::Item const * const I) {
- return flNotDir(I->DestFile) == E->d_name;
- }) == false)
- {
- RemoveFileAt("pkgAcquire::Clean", dirfd, E->d_name);
- }
+ RemoveFileAt(Caller, dirfd, E->d_name);
}
closedir(D);
return true;
}
/*}}}*/
+// Acquire::Clean - Cleans a directory of downloaded files /*{{{*/
+// ---------------------------------------------------------------------
+/* This is a bit simplistic, it looks at every file in the dir and sees
+ if it is part of the download set. */
+bool pkgAcquire::Clean(string Dir)
+{
+ return CleanDir(
+ Dir,
+ // Look in the get list and if found then keep
+ [this](char const *FName) {
+ return std::any_of(Items.cbegin(), Items.cend(),
+ [FName](pkgAcquire::Item const * const I) {
+ return flNotDir(I->DestFile) == FName;
+ });
+ },
+ "pkgAcquire::Clean"
+ );
+}
+ /*}}}*/
+// Acquire::CleanLists - Cleans a directory of list files /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool pkgAcquire::CleanLists(string Dir)
+{
+ return CleanDir(
+ Dir,
+ [](char const *FName) noexcept {
+ static const std::regex KeepPattern(".*_(Release|Release.gpg|InRelease)");
+ return std::regex_match(FName, KeepPattern);
+ },
+ "pkgAcquire::CleanLists"
+ );
+}
+ /*}}}*/
// Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
// ---------------------------------------------------------------------
/* This is the total number of bytes needed */
diff --git a/apt-pkg/acquire.h b/apt-pkg/acquire.h
index 17b68768e..46115a27f 100644
--- a/apt-pkg/acquire.h
+++ b/apt-pkg/acquire.h
@@ -70,6 +70,7 @@
#include <apt-pkg/weakptr.h>
#include <chrono>
+#include <functional>
#include <string>
#include <vector>
@@ -330,6 +331,14 @@ class APT_PUBLIC pkgAcquire
* \return \b true if the directory exists and is readable.
*/
bool Clean(std::string Dir);
+
+ /** Deletes each package list and index file in the given directory.
+ *
+ * \param Dir The directory to be cleaned.
+ *
+ * \return \b true if the directory exists and is readable.
+ */
+ bool CleanLists(std::string Dir);
/** \return the total size in bytes of all the items included in
* this download.
@@ -372,6 +381,20 @@ class APT_PUBLIC pkgAcquire
private:
APT_HIDDEN void Initialize();
+
+ /** Delete each entry in the given directory whose name does \em not match
+ * a criterion.
+ *
+ * \param Dir The directory to be cleaned.
+ *
+ * \param KeepP A predicate telling to keep the named file; if it is
+ * non-empty and returns true, the file is kept.
+ *
+ * \param Caller Log name of the caller.
+ *
+ * \return \b true if the directory exists and is readable.
+ */
+ APT_HIDDEN static bool CleanDir(std::string Dir, std::function<bool(char const*)> const &KeepP, char const * const Caller);
};
/** \brief Represents a single download source from which an item
diff --git a/apt-private/private-download.cc b/apt-private/private-download.cc
index eddb901d0..b271ec03f 100644
--- a/apt-private/private-download.cc
+++ b/apt-private/private-download.cc
@@ -304,9 +304,20 @@ bool DoChangelog(CommandLine &CmdL)
}
/*}}}*/
-// DoClean - Remove download archives /*{{{*/
+// DoClean - Remove download archives /*{{{*/
bool DoClean(CommandLine &)
{
+ return DoDistClean(false);
+}
+ /*}}}*/
+// DoDistClean - Remove download archives and lists /*{{{*/
+bool DoDistClean(CommandLine &)
+{
+ return DoDistClean(true);
+}
+// DoDistClean - the worker
+bool DoDistClean(bool ListsToo)
+{
std::string const archivedir = _config->FindDir("Dir::Cache::archives");
std::string const listsdir = _config->FindDir("Dir::state::lists");
@@ -315,23 +326,25 @@ bool DoClean(CommandLine &)
std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
std::cout << "Del " << archivedir << "* " << archivedir << "partial/*"<< std::endl
- << "Del " << listsdir << "partial/*" << std::endl
- << "Del " << pkgcache << " " << srcpkgcache << std::endl;
+ << "Del " << listsdir << "partial/*" << (ListsToo ? " *_{Packages,Sources}{,.diff_Index}" : "") << std::endl
+ << "Del " << pkgcache << " " << srcpkgcache << std::endl;
return true;
}
pkgAcquire Fetcher;
if (archivedir.empty() == false && FileExists(archivedir) == true &&
- Fetcher.GetLock(archivedir) == true)
+ Fetcher.GetLock(archivedir) == true)
{
Fetcher.Clean(archivedir);
Fetcher.Clean(archivedir + "partial/");
}
if (listsdir.empty() == false && FileExists(listsdir) == true &&
- Fetcher.GetLock(listsdir) == true)
+ Fetcher.GetLock(listsdir) == true)
{
Fetcher.Clean(listsdir + "partial/");
+ if (ListsToo)
+ Fetcher.CleanLists(listsdir);
}
pkgCacheFile::RemoveCaches();
diff --git a/apt-private/private-download.h b/apt-private/private-download.h
index d829e8b24..918828241 100644
--- a/apt-private/private-download.h
+++ b/apt-private/private-download.h
@@ -33,6 +33,8 @@ APT_PUBLIC bool DoDownload(CommandLine &CmdL);
APT_PUBLIC bool DoChangelog(CommandLine &CmdL);
APT_PUBLIC bool DoClean(CommandLine &CmdL);
+APT_PUBLIC bool DoDistClean(CommandLine &CmdL);
+bool DoDistClean(bool ListsToo);
APT_PUBLIC bool DoAutoClean(CommandLine &CmdL);
#endif
diff --git a/cmdline/apt.cc b/cmdline/apt.cc
index f53d4e446..ef58bcebf 100644
--- a/cmdline/apt.cc
+++ b/cmdline/apt.cc
@@ -89,6 +89,8 @@ static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
{"policy",&Policy, nullptr},
{"build-dep", &DoBuildDep,nullptr},
{"clean", &DoClean, nullptr},
+ {"distclean", &DoDistClean, nullptr},
+ {"dist-clean", &DoDistClean, nullptr},
{"autoclean", &DoAutoClean, nullptr},
{"auto-clean", &DoAutoClean, nullptr},
{"source", &DoSource, nullptr},