Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/libalpm/add.c
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-09-19 14:08:14 -0500
committerDan McGee <dan@archlinux.org>2011-09-19 14:11:08 -0500
commit288a81d8470b1fd2e8a8f0814abe4cbfed71497c (patch)
tree36f9c2ef8575926161315d5057504d00bdeac139 /lib/libalpm/add.c
parentbfe36c2ddf869e2ebc44bec1e35c0319263ac386 (diff)
Use more efficient way of restoring working directory
Rather than using a string-based path, we can restore the working directory via a file descriptor and use of fchdir(). From the getcwd manpage: Opening the current directory (".") and calling fchdir(2) to return is usually a faster and more reliable alternative when sufficiently many file descriptors are available. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm/add.c')
-rw-r--r--lib/libalpm/add.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index b526145a..eb05f841 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -25,6 +25,7 @@
#include <time.h>
#include <string.h>
#include <limits.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -528,8 +529,7 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
if(!(trans->flags & ALPM_TRANS_FLAG_DBONLY)) {
struct archive *archive;
struct archive_entry *entry;
- char cwd[PATH_MAX] = "";
- int restore_cwd = 0;
+ int cwdfd;
_alpm_log(handle, ALPM_LOG_DEBUG, "extracting files\n");
@@ -551,10 +551,11 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
}
/* save the cwd so we can restore it later */
- if(getcwd(cwd, PATH_MAX) == NULL) {
+ do {
+ cwdfd = open(".", O_RDONLY);
+ } while(cwdfd == -1 && errno == EINTR);
+ if(cwdfd < 0) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not get current working directory\n"));
- } else {
- restore_cwd = 1;
}
/* libarchive requires this for extracting hard links */
@@ -607,8 +608,12 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
archive_read_finish(archive);
/* restore the old cwd if we have it */
- if(restore_cwd && chdir(cwd) != 0) {
- _alpm_log(handle, ALPM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
+ if(cwdfd >= 0) {
+ if(fchdir(cwdfd) != 0) {
+ _alpm_log(handle, ALPM_LOG_ERROR,
+ _("could not restore working directory (%s)\n"), strerror(errno));
+ }
+ close(cwdfd);
}
if(errors) {