From 07815d4edc99585967b9f267e6f37c37008dcba5 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 28 Apr 2017 18:38:23 +0200 Subject: avoid changing directory in mirror method --- methods/mirror.cc | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/methods/mirror.cc b/methods/mirror.cc index 71faaf591..5047463d1 100644 --- a/methods/mirror.cc +++ b/methods/mirror.cc @@ -20,15 +20,18 @@ #include #include -#include -#include -#include #include #include #include + +#include +#include +#include #include +#include #include #include +#include using namespace std; @@ -89,16 +92,12 @@ bool MirrorMethod::Clean(string Dir) pkgSourceList list; list.ReadMainList(); - DIR *D = opendir(Dir.c_str()); - if (D == 0) - return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); - - string StartDir = SafeGetCWD(); - if (chdir(Dir.c_str()) != 0) - { - closedir(D); - return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str()); - } + 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()); + DIR * const D = fdopendir(dirfd); + if (D == nullptr) + return _error->Errno("fdopendir",_("Unable to read %s"),Dir.c_str()); for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D)) { @@ -122,12 +121,9 @@ bool MirrorMethod::Clean(string Dir) } // nothing found, nuke it if (I == list.end()) - RemoveFile("mirror", Dir->d_name); + RemoveFileAt("mirror", dirfd, Dir->d_name); } - closedir(D); - if (chdir(StartDir.c_str()) != 0) - return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str()); return true; } -- cgit v1.2.3