summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2017-04-28 18:18:28 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2017-06-26 23:31:15 +0200
commit6829c5420b4c2e434489e837cad6f3fd09fa3ab3 (patch)
tree131c32bc0eae7b2f500351501e4a72628e4e6dc8
parente91dd46e3f65517ed9dbc6e431a56112a403cb3e (diff)
downloadapt-6829c5420b4c2e434489e837cad6f3fd09fa3ab3.tar.gz
clean archives without changing directory
Adopting this change in other frontends will require source changes as well similar to our own changes in apt-private/.
-rw-r--r--apt-pkg/clean.cc56
-rw-r--r--apt-pkg/clean.h14
-rw-r--r--apt-pkg/contrib/macros.h2
-rw-r--r--apt-private/private-download.cc6
4 files changed, 50 insertions, 28 deletions
diff --git a/apt-pkg/clean.cc b/apt-pkg/clean.cc
index fe57c26a2..9f408e46e 100644
--- a/apt-pkg/clean.cc
+++ b/apt-pkg/clean.cc
@@ -22,8 +22,10 @@
#include <string>
#include <string.h>
#include <dirent.h>
+#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <fcntl.h>
#include <apti18n.h>
/*}}}*/
@@ -43,17 +45,21 @@ bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
if (FileExists(Dir) == false)
return true;
- DIR *D = opendir(Dir.c_str());
- if (D == 0)
- return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
-
- std::string StartDir = SafeGetCWD();
- if (chdir(Dir.c_str()) != 0)
+ auto const withoutChangingDir = dynamic_cast<pkgArchiveCleaner2*>(this);
+ int const dirfd = open(Dir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (dirfd == -1)
+ return _error->Errno("open",_("Unable to read %s"),Dir.c_str());
+ std::string CWD;
+ if (withoutChangingDir == nullptr)
{
- closedir(D);
- return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
+ CWD = SafeGetCWD();
+ if (fchdir(dirfd) != 0)
+ return _error->Errno("fchdir",_("Unable to change to %s"),Dir.c_str());
}
-
+ DIR * const D = fdopendir(dirfd);
+ if (D == nullptr)
+ return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
+
for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
{
// Skip some files..
@@ -65,15 +71,13 @@ bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
continue;
struct stat St;
- if (stat(Dir->d_name,&St) != 0)
+ if (fstatat(dirfd, Dir->d_name,&St, 0) != 0)
{
_error->Errno("stat",_("Unable to stat %s."),Dir->d_name);
closedir(D);
- if (chdir(StartDir.c_str()) != 0)
- return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
return false;
}
-
+
// Grab the package name
const char *I = Dir->d_name;
for (; *I != 0 && *I != '_';I++);
@@ -87,7 +91,7 @@ bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
if (*I != '_')
continue;
std::string Ver = DeQuoteString(std::string(Start,I-Start));
-
+
// Grab the arch
Start = I + 1;
for (I = Start; *I != 0 && *I != '.' ;I++);
@@ -98,7 +102,7 @@ bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
// ignore packages of unconfigured architectures
if (APT::Configuration::checkArchitecture(Arch) == false)
continue;
-
+
// Lookup the package
pkgCache::PkgIterator P = Cache.FindPkg(Pkg, Arch);
if (P.end() != true)
@@ -117,23 +121,29 @@ bool pkgArchiveCleaner::Go(std::string Dir,pkgCache &Cache)
IsFetchable = true;
break;
}
-
+
// See if this version matches the file
if (IsFetchable == true && Ver == V.VerStr())
break;
}
-
+
// We found a match, keep the file
if (V.end() == false)
continue;
}
-
- Erase(Dir->d_name,Pkg,Ver,St);
- };
-
+
+ if (withoutChangingDir == nullptr)
+ {
+ APT_IGNORE_DEPRECATED_PUSH
+ Erase(Dir->d_name, Pkg, Ver, St);
+ APT_IGNORE_DEPRECATED_POP
+ }
+ else
+ withoutChangingDir->Erase(dirfd, Dir->d_name, Pkg, Ver, St);
+ }
closedir(D);
- if (chdir(StartDir.c_str()) != 0)
- return _error->Errno("chdir", _("Unable to change to %s"), StartDir.c_str());
+ if (withoutChangingDir == nullptr && chdir(CWD.c_str()) != 0)
+ return _error->Errno("chdir", _("Unable to change to %s"),Dir.c_str());
return true;
}
/*}}}*/
diff --git a/apt-pkg/clean.h b/apt-pkg/clean.h
index 6cedae086..2ba3932a0 100644
--- a/apt-pkg/clean.h
+++ b/apt-pkg/clean.h
@@ -16,6 +16,8 @@
#include <string>
+#include <apt-pkg/macros.h>
+
class pkgCache;
class pkgArchiveCleaner
@@ -25,7 +27,7 @@ class pkgArchiveCleaner
protected:
- virtual void Erase(const char * /*File*/,std::string /*Pkg*/,std::string /*Ver*/,struct stat & /*St*/) {};
+ APT_DEPRECATED_MSG("Use pkgArchiveCleaner2 to avoid CWD expectations and chdir") virtual void Erase(const char * /*File*/,std::string /*Pkg*/,std::string /*Ver*/,struct stat & /*St*/) {};
public:
@@ -34,5 +36,15 @@ class pkgArchiveCleaner
pkgArchiveCleaner();
virtual ~pkgArchiveCleaner();
};
+// TODO: merge classes and "erase" the old way
+class pkgArchiveCleaner2: public pkgArchiveCleaner
+{
+ friend class pkgArchiveCleaner;
+protected:
+ using pkgArchiveCleaner::Erase;
+ virtual void Erase(int const dirfd, char const * const File,
+ std::string const &Pkg,std::string const &Ver,
+ struct stat const &St) = 0;
+};
#endif
diff --git a/apt-pkg/contrib/macros.h b/apt-pkg/contrib/macros.h
index bc1f523ea..5e22a195d 100644
--- a/apt-pkg/contrib/macros.h
+++ b/apt-pkg/contrib/macros.h
@@ -165,7 +165,7 @@
// See also buildlib/libversion.mak
#define APT_PKG_MAJOR 5
#define APT_PKG_MINOR 0
-#define APT_PKG_RELEASE 1
+#define APT_PKG_RELEASE 2
#define APT_PKG_ABI ((APT_PKG_MAJOR * 100) + APT_PKG_MINOR)
#endif
diff --git a/apt-private/private-download.cc b/apt-private/private-download.cc
index ee477f4cb..c8d1b5a09 100644
--- a/apt-private/private-download.cc
+++ b/apt-private/private-download.cc
@@ -339,15 +339,15 @@ bool DoClean(CommandLine &)
// ---------------------------------------------------------------------
/* This is similar to clean but it only purges things that cannot be
downloaded, that is old versions of cached packages. */
- class LogCleaner : public pkgArchiveCleaner
+ class LogCleaner : public pkgArchiveCleaner2
{
protected:
- virtual void Erase(const char *File, std::string Pkg, std::string Ver,struct stat &St) APT_OVERRIDE
+ virtual void Erase(int const dirfd, char const * const File, std::string const &Pkg, std::string const &Ver,struct stat const &St) APT_OVERRIDE
{
c1out << "Del " << Pkg << " " << Ver << " [" << SizeToStr(St.st_size) << "B]" << std::endl;
if (_config->FindB("APT::Get::Simulate") == false)
- RemoveFile("Cleaner::Erase", File);
+ RemoveFileAt("Cleaner::Erase", dirfd, File);
};
};
bool DoAutoClean(CommandLine &)