ソースを参照

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 年 前
コミット
8780916333
共有2 個のファイルを変更した14 個の追加15 個の削除を含む
  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);
   close(arfd);
   if (taroption) close(p2[1]);
   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) {
   if (taroption) {
     c3 = subproc_fork();
     c3 = subproc_fork();
     if (!c3) {
     if (!c3) {
@@ -314,6 +301,18 @@ extracthalf(const char *debar, const char *dir, const char *taroption,
 
 
       unsetenv("TAR_OPTIONS");
       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);
       execlp(TAR, "tar", buffer, "-", NULL);
       ohshite(_("unable to execute %s (%s)"), "tar", TAR);
       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;
   struct stat stab;
 
 
   dir = argv[0];
   dir = argv[0];
-  if (chdir("/"))
-    ohshite(_("failed to chdir to `/' for cleanup"));
   if (lstat(dir, &stab) && errno == ENOENT)
   if (lstat(dir, &stab) && errno == ENOENT)
     return;
     return;
 
 
   pid = subproc_fork();
   pid = subproc_fork();
   if (pid == 0) {
   if (pid == 0) {
+    if (chdir("/"))
+      ohshite(_("failed to chdir to `/' for cleanup"));
     execlp(RM, "rm", "-rf", dir, NULL);
     execlp(RM, "rm", "-rf", dir, NULL);
     ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
     ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
   }
   }