diff options
Diffstat (limited to 'apt-pkg/clean.cc')
-rw-r--r-- | apt-pkg/clean.cc | 67 |
1 files changed, 38 insertions, 29 deletions
diff --git a/apt-pkg/clean.cc b/apt-pkg/clean.cc index fe57c26a2..e9bf73b6c 100644 --- a/apt-pkg/clean.cc +++ b/apt-pkg/clean.cc @@ -8,21 +8,22 @@ ##################################################################### */ /*}}}*/ // Includes /*{{{*/ -#include<config.h> +#include <config.h> +#include <apt-pkg/aptconfiguration.h> #include <apt-pkg/clean.h> -#include <apt-pkg/strutl.h> -#include <apt-pkg/error.h> #include <apt-pkg/configuration.h> -#include <apt-pkg/aptconfiguration.h> +#include <apt-pkg/error.h> #include <apt-pkg/fileutl.h> #include <apt-pkg/pkgcache.h> -#include <apt-pkg/cacheiterators.h> +#include <apt-pkg/strutl.h> #include <string> -#include <string.h> #include <dirent.h> +#include <fcntl.h> +#include <string.h> #include <sys/stat.h> +#include <sys/types.h> #include <unistd.h> #include <apti18n.h> @@ -43,17 +44,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 +70,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 +90,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 +101,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 +120,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; } /*}}}*/ |