]> granicus.if.org Git - php/commitdiff
Added System::mktemp() (Creates temporal files or directories)
authorTomas V.V.Cox <cox@php.net>
Sun, 16 Dec 2001 01:30:31 +0000 (01:30 +0000)
committerTomas V.V.Cox <cox@php.net>
Sun, 16 Dec 2001 01:30:31 +0000 (01:30 +0000)
pear/System.php

index ff61fbffe1ec54027d8b75b82799d53de3025c3d..a97cc5ceced05acfa9927a5eed272d204aacf49d 100644 (file)
@@ -21,7 +21,6 @@
 //
 
 // TODO:
-// - Test under Windows (help is really appreciated in this point)
 // - Build strong tests
 // - Error reporting (now shows standar php errors)
 // - Write doc
@@ -36,7 +35,7 @@ require_once 'Console/Getopt.php';
 * Unix and Windows. The names and usage has been taken from its repectively
 * GNU commands.
 *
-* Usage: System::rm('-r file1 dir1');
+* Example usage: System::rm('-r file1 dir1');
 *
 * ------------------- EXPERIMENTAL STATUS -------------------
 *
@@ -51,18 +50,17 @@ class System extends PEAR
     * returns the commandline arguments of a function
     *
     * @param    string  $argv           the commandline
-    * @param    string  $short_options  the allowed option short-tags 
+    * @param    string  $short_options  the allowed option short-tags
     * @param    string  $long_options   the allowed option long-tags
     * @return   array   the given options and there values
     * @access private
     */
     function _parseArgs($argv, $short_options, $long_options = null)
     {
-        if (!is_array($argv)) {
+        if (!is_array($argv) && $argv !== null) {
             $argv = preg_split('/\s+/', $argv);
         }
-        $options = Console_Getopt::getopt($argv, $short_options);
-        return $options;
+        return Console_Getopt::getopt($argv, $short_options);
     }
 
     /**
@@ -140,12 +138,12 @@ class System extends PEAR
     }
 
     /**
-    * The rm command for removing files. 
+    * The rm command for removing files.
     * Supports multiple files and dirs and also recursive deletes
     *
     * @param    string  $args   the arguments for rm
     * @return   mixed   PEAR_Error or true for success
-    * @access   public    
+    * @access   public
     */
     function rm($args)
     {
@@ -183,7 +181,7 @@ class System extends PEAR
     *
     * @param    string  $args    the name of the director(y|ies) to create
     * @return   mixed   PEAR_Error or true for success
-    * @access   public    
+    * @access   public
     */
     function mkDir($args)
     {
@@ -282,5 +280,62 @@ class System extends PEAR
         return $ret;
     }
 
+    /**
+    * Creates temporal files or directories
+    *
+    * Usage:
+    *   1) System::mktemp("prefix");
+    *   2) System::mktemp("-d prefix");
+    *   3) System::mktemp();
+    *   4) System::mktemp("-t /var/tmp prefix");
+    *
+    * prefix -> The string that will be prepended to the temp name
+    *           (defaults to "tmp").
+    * -d     -> A temporal dir will be created instead of a file.
+    * -t     -> The target dir where the temporal (file|dir) will be created. If
+    *           this param is missing by default the env vars TMP on Windows or
+    *           TMPDIR in Unix will be used. If these vars are also missing
+    *           c:\windows\temp or /tmp will be used.
+    *
+    * @param   string  $args  The arguments
+    * @return  mixed   PEAR_Error or true for success
+    * @access  public
+    */
+    function mktemp($args = null)
+    {
+        $opts = System::_parseArgs($args, 't:d');
+        if (PEAR::isError($opts)) {
+            return $opts;
+        }
+        foreach($opts[0] as $opt) {
+            if($opt[0] == 'd') {
+                $tmp_is_dir = true;
+            } elseif($opt[0] == 't') {
+                $tmpdir = $opt[1];
+            }
+        }
+        //print_r($opts);
+        $prefix = (isset($opts[1][0])) ? $opts[1][0] : 'tmp';
+        if(!isset($tmpdir)) {
+            if (OS_WINDOWS){
+                $tmpdir = getenv('TMP');
+            } else {
+                $tmpdir = getenv('TMPDIR');
+            }
+            if (empty($tmpdir)) {
+                $tmpdir = (OS_WINDOWS) ? 'c:\\windows\\temp' : '/tmp';
+            }
+        }
+        System::mkDir("-p $tmpdir");
+        $tmp = tempnam($tmpdir, $prefix);
+        if(isset($tmp_is_dir)) {
+            unlink($tmp); // be careful possible race condition here
+            if (!mkdir($tmp, 0700)) {
+                return $this->raiseError("Unable to create temporary directory $tmpdir");
+            }
+        }
+        return $tmp;
+    }
+
 }
 ?>
\ No newline at end of file