Browse Source

dpkg-deb: Always chdir(2) in the childs to not change the global state

To not disturb the global program state we should always chdir(2) in the
childs that actually need it. This will allow in the future to switch
this code to be part of a library.
Guillem Jover 15 years ago
parent
commit
8780916333
2 changed files with 14 additions and 15 deletions
  1. 12 13
      dpkg-deb/extract.c
  2. 2 2
      dpkg-deb/info.c

+ 12 - 13
dpkg-deb/extract.c

@@ -288,19 +288,6 @@ extracthalf(const char *debar, const char *dir, const char *taroption,
   close(arfd);
   if (taroption) close(p2[1]);
 
-  if (taroption && dir) {
-    if (chdir(dir)) {
-      if (errno == ENOENT) {
-        if (mkdir(dir, 0777))
-          ohshite(_("failed to create directory"));
-        if (chdir(dir))
-          ohshite(_("failed to chdir to directory after creating it"));
-      } else {
-        ohshite(_("failed to chdir to directory"));
-      }
-    }
-  }
-
   if (taroption) {
     c3 = subproc_fork();
     if (!c3) {
@@ -314,6 +301,18 @@ extracthalf(const char *debar, const char *dir, const char *taroption,
 
       unsetenv("TAR_OPTIONS");
 
+      if (dir) {
+        if (chdir(dir)) {
+          if (errno != ENOENT)
+            ohshite(_("failed to chdir to directory"));
+
+          if (mkdir(dir, 0777))
+            ohshite(_("failed to create directory"));
+          if (chdir(dir))
+            ohshite(_("failed to chdir to directory after creating it"));
+        }
+      }
+
       execlp(TAR, "tar", buffer, "-", NULL);
       ohshite(_("unable to execute %s (%s)"), "tar", TAR);
     }

+ 2 - 2
dpkg-deb/info.c

@@ -54,13 +54,13 @@ static void cu_info_prepare(int argc, void **argv) {
   struct stat stab;
 
   dir = argv[0];
-  if (chdir("/"))
-    ohshite(_("failed to chdir to `/' for cleanup"));
   if (lstat(dir, &stab) && errno == ENOENT)
     return;
 
   pid = subproc_fork();
   if (pid == 0) {
+    if (chdir("/"))
+      ohshite(_("failed to chdir to `/' for cleanup"));
     execlp(RM, "rm", "-rf", dir, NULL);
     ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
   }