summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2017-04-28 18:38:23 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2017-06-26 23:31:15 +0200
commit07815d4edc99585967b9f267e6f37c37008dcba5 (patch)
tree43fe60f0e11353e807cf8683ba2def6147b9b259
parent6829c5420b4c2e434489e837cad6f3fd09fa3ab3 (diff)
avoid changing directory in mirror method
-rw-r--r--methods/mirror.cc30
1 files 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 <apt-pkg/metaindex.h>
#include <apt-pkg/strutl.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <fstream>
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include <sys/utsname.h>
#include <dirent.h>
+#include <fcntl.h>
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;
}