]> granicus.if.org Git - php/commitdiff
avoid chdir() calls
authorTomas V.V.Cox <cox@php.net>
Fri, 10 Oct 2003 12:22:59 +0000 (12:22 +0000)
committerTomas V.V.Cox <cox@php.net>
Fri, 10 Oct 2003 12:22:59 +0000 (12:22 +0000)
remove duplicated validation checks
(fix bug #66 contributed by Roman)

pear/PEAR/Packager.php

index 24c3ed5465b83e8a7b40ab774c7c4f3cd9cc56dc..c0f5ca0c6026ed11129518fff32503755d3a8afd 100644 (file)
@@ -54,34 +54,21 @@ class PEAR_Packager extends PEAR_Common
 
     function package($pkgfile = null, $compress = true)
     {
+        // {{{ validate supplied package.xml file
         if (empty($pkgfile)) {
             $pkgfile = 'package.xml';
         }
+        // $this->pkginfo gets populated inside
         $pkginfo = $this->infoFromDescriptionFile($pkgfile);
         if (PEAR::isError($pkginfo)) {
             return $this->raiseError($pkginfo);
         }
-        if (empty($this->pkginfo['version'])) {
-            return $this->raiseError("No version info found in $pkgfile");
-        }
-        // TMP DIR -------------------------------------------------
-        // We allow calls like "pear package /home/user/mypack/package.xml"
-        $oldcwd = getcwd();
-        $dir = dirname($pkgfile);
-        if (!@chdir($dir)) {
-            return $this->raiseError('Could not chdir to '.$dir);
-        }
+
+        $pkgdir = dirname(realpath($pkgfile));
         $pkgfile = basename($pkgfile);
-        if (@$this->pkginfo['release_state'] == 'snapshot' && empty($this->pkginfo['version'])) {
-            $this->pkginfo['version'] = date('Ymd');
-        }
-        // don't want strange characters
-        $pkgname    = preg_replace('/[^a-z0-9._]/i', '_', $this->pkginfo['package']);
-        $pkgversion = preg_replace('/[^a-z0-9._-]/i', '_', $this->pkginfo['version']);
-        $pkgver = $pkgname . '-' . $pkgversion;
 
         $errors = $warnings = array();
-        $this->validatePackageInfo($this->pkginfo, $errors, $warnings, $dir);
+        $this->validatePackageInfo($pkginfo, $errors, $warnings, $pkgdir);
         foreach ($warnings as $w) {
             $this->log(1, "Warning: $w");
         }
@@ -89,75 +76,73 @@ class PEAR_Packager extends PEAR_Common
             $this->log(0, "Error: $e");
         }
         if (sizeof($errors) > 0) {
-            chdir($oldcwd);
             return $this->raiseError('Errors in package');
         }
+        // }}}
 
-        // ----- Create the package file list
+        $pkgver = $pkginfo['package'] . '-' . $pkginfo['version'];
+
+        // {{{ Create the package file list
         $filelist = array();
         $i = 0;
 
-        // {{{ Copy files -----------------------------------------------
-        foreach ($this->pkginfo['filelist'] as $fname => $atts) {
-            if (!file_exists($fname)) {
-                chdir($oldcwd);
+        foreach ($pkginfo['filelist'] as $fname => $atts) {
+            $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
+            if (!file_exists($file)) {
                 return $this->raiseError("File does not exist: $fname");
             } else {
-                $filelist[$i++] = $fname;
-                if (empty($this->pkginfo['filelist'][$fname]['md5sum'])) {
-                    $md5sum = md5_file($fname);
-                    $this->pkginfo['filelist'][$fname]['md5sum'] = $md5sum;
+                $filelist[$i++] = $file;
+                if (empty($pkginfo['filelist'][$fname]['md5sum'])) {
+                    $md5sum = md5_file($file);
+                    $tpkginfo['filelist'][$fname]['md5sum'] = $md5sum;
                 }
                 $this->log(2, "Adding file $fname");
             }
         }
         // }}}
-        $new_xml = $this->xmlFromInfo($this->pkginfo);
+
+        // {{{ regenerate package.xml
+        $new_xml = $this->xmlFromInfo($pkginfo);
         if (PEAR::isError($new_xml)) {
-            chdir($oldcwd);
             return $this->raiseError($new_xml);
         }
-        if (!($tmpdir = System::mktemp(array('-t', getcwd(), '-d')))) {
-            chdir($oldcwd);
+        if (!($tmpdir = System::mktemp(array('-d')))) {
             return $this->raiseError("PEAR_Packager: mktemp failed");
         }
         $newpkgfile = $tmpdir . DIRECTORY_SEPARATOR . 'package.xml';
-        $np = @fopen($newpkgfile, "wb");
+        $np = @fopen($newpkgfile, 'wb');
         if (!$np) {
-            chdir($oldcwd);
             return $this->raiseError("PEAR_Packager: unable to rewrite $pkgfile as $newpkgfile");
         }
         fwrite($np, $new_xml);
         fclose($np);
+        // }}}
 
         // {{{ TAR the Package -------------------------------------------
         $ext = $compress ? '.tgz' : '.tar';
-        $dest_package = $oldcwd . DIRECTORY_SEPARATOR . $pkgver . $ext;
+        $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
         $tar =& new Archive_Tar($dest_package, $compress);
         $tar->setErrorHandling(PEAR_ERROR_RETURN); // XXX Don't print errors
         // ----- Creates with the package.xml file
         $ok = $tar->createModify(array($newpkgfile), '', $tmpdir);
         if (PEAR::isError($ok)) {
-            chdir($oldcwd);
             return $this->raiseError($ok);
         } elseif (!$ok) {
-            chdir($oldcwd);
             return $this->raiseError('PEAR_Packager: tarball creation failed');
         }
         // ----- Add the content of the package
-        if (!$tar->addModify($filelist, $pkgver)) {
-            chdir($oldcwd);
+        if (!$tar->addModify($filelist, $pkgver, $pkgdir)) {
             return $this->raiseError('PEAR_Packager: tarball creation failed');
         }
         $this->log(1, "Package $dest_package done");
-        if (file_exists("CVS/Root")) {
-            $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $pkgversion);
+        if (file_exists("$pkgdir/CVS/Root")) {
+            $cvsversion = preg_replace('/[^a-z0-9]/i', '_', $pkginfo['version']);
             $cvstag = "RELEASE_$cvsversion";
             $this->log(1, "Tag the released code with `pear cvstag $pkgfile'");
             $this->log(1, "(or set the CVS tag $cvstag by hand)");
         }
         // }}}
-        chdir($oldcwd);
+
         return $dest_package;
     }