You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/21 17:34:22 UTC

svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Author: mturk
Date: Mon Sep 21 15:34:20 2009
New Revision: 817291

URL: http://svn.apache.org/viewvc?rev=817291&view=rev
Log:
Use more powerfull temp dir creation. No need to use haks via temp file

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java?rev=817291&r1=817290&r2=817291&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java Mon Sep 21 15:34:20 2009
@@ -20,6 +20,7 @@
 import java.io.File;
 import java.io.PrintStream;
 import java.util.Random;
+import org.apache.commons.runtime.SystemId;
 
 /**
  * General purpose utilities.
@@ -152,6 +153,65 @@
         out.println(dumpBuffer(data));
     }
 
+    private static final String [] try_envs = {
+        "TMP",
+        "TEMP",
+        "TMPDIR",
+        "TEMPDIR"
+    };
+    private static final String [] win_trys = {
+        "C:\\TMP",
+        "C:\\TEMP",
+        "C:\\WINDOWS\\TEMP"
+    };
+    private static final String [] psx_trys = {
+        "/tmp",
+        "/usr/tmp",
+        "/var/tmp"
+    };
+
+
+    private static File checkTempDir(String path)
+    {
+        if (path == null)
+            return null;
+        try {
+            File d = new File(path);
+            File f = File.createTempFile("test", ".acr", new File(path));
+            f.delete();
+            return d;
+        } catch (Exception ex) {
+            // Ignore
+        }
+        return null;
+    }
+
+    private static File findTempDir()
+    {
+        File dir;
+        String [] try_path;
+
+        dir = checkTempDir(System.getProperty("java.io.tmpdir"));
+        if (dir != null)
+            return dir;
+        for (int i = 0; i < try_envs.length; i++) {
+            dir = checkTempDir(System.getenv(try_envs[i]));
+            if (dir != null)
+                return dir;
+        }
+        if (SystemId.getSysname().equals("windows"))
+            try_path = win_trys;
+        else
+            try_path = psx_trys;
+
+        for (int i = 0; i < try_path.length; i++) {
+            dir = checkTempDir(System.getenv(try_path[i]));
+            if (dir != null)
+                return dir;
+        }
+        return null;
+    }
+
     /**
      * Create temporary directory.
      * <p>
@@ -166,19 +226,23 @@
     public static File createTempDirectory(String prefix)
         throws IllegalArgumentException, IOException
     {
-        int tries = 0;
+        int tries  = 0;
+        byte [] rb = new byte[12];
+        char [] ec = new char[48];
+        File td    = findTempDir();
+
+        if (td == null) {
+            throw new IOException("Cannot find writable temporary directory");
+        }
         while(true) {
-            File tmpf = File.createTempFile(prefix, ".acr");
             try {
-                if (tmpf.delete()) {
-                    /* Create the directory with the same name
-                     * as the temp file was.
+                generateRandom(rb);
+                Base64.encode(rb, 0, ec, 0, 0, 0);
+                File tmpd = new File(td, prefix + new String(ec, 0, 8));
+                if (tmpd.mkdir()) {
+                    /* Directory is ceated
                      */
-                    if (tmpf.mkdir()) {
-                        /* Directory is ceated
-                         */
-                        return tmpf;
-                    }
+                    return tmpd;
                 }
             } catch (Exception ex) {
                 // Ignore



Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by sebb <se...@gmail.com>.
On 22/09/2009, Jim Jagielski <ji...@jagunet.com> wrote:
>
>  On Sep 21, 2009, at 11:51 AM, Jim Jagielski wrote:
>
>
> >
> > On Sep 21, 2009, at 11:34 AM, mturk@apache.org wrote:
> >
> > >
> > > +    private static final String [] try_envs = {
> > > +        "TMP",
> > > +        "TEMP",
> > > +        "TMPDIR",
> > > +        "TEMPDIR"
> > > +    };
> > >
> >
> > Not sure about that ordering, unless we want to favor Windows.
> > For Unix the canonical envvar is TMPDIR, so it should likely be
> > checked 1st (and this the 1st element), and then go down the others...
> >
> > Of course, we could even get more sophisticated and have 2; one for
> > Windows and another for non-Windows, so the ordering makes more sense
> > depending on the platform ;)
> >
> > OK if I fold something like that in?
> >
> >
>
>  Didn't hear back, so I'll be conservative and not do so...
>

Seems like a reasonable propsal to me, but I'm not directly involved -
why not raise a JIRA so it does not get forgotten?

>
> ---------------------------------------------------------------------
>  To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>  For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by Jim Jagielski <ji...@jaguNET.com>.
On Sep 22, 2009, at 12:33 PM, Mladen Turk wrote:

> On 22/09/09 18:05, Jim Jagielski wrote:
>>
>>
>> # First, try the environment.
>> for envname in 'TMPDIR', 'TEMP', 'TMP':
>
> OK. You've convinced me :)
>

*grin* :)

Cheers!

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by Mladen Turk <mt...@apache.org>.
On 22/09/09 18:05, Jim Jagielski wrote:
>
>
> # First, try the environment.
> for envname in 'TMPDIR', 'TEMP', 'TMP':

OK. You've convinced me :)


Regards
-- 
^TM


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by Jim Jagielski <ji...@jaguNET.com>.
On Sep 22, 2009, at 11:51 AM, Jim Jagielski wrote:

>
> On Sep 22, 2009, at 10:56 AM, Mladen Turk wrote:
>
>> On 22/09/09 16:06, Jim Jagielski wrote:
>>>
>>> On Sep 21, 2009, at 11:51 AM, Jim Jagielski wrote:
>>>
>>>>
>>>> On Sep 21, 2009, at 11:34 AM, mturk@apache.org wrote:
>>>>>
>>>>> + private static final String [] try_envs = {
>>>>> + "TMP",
>>>>> + "TEMP",
>>>>> + "TMPDIR",
>>>>> + "TEMPDIR"
>>>>> + };
>>>>
>>>> Not sure about that ordering, unless we want to favor Windows.
>>>> For Unix the canonical envvar is TMPDIR, so it should likely be
>>>> checked 1st (and this the 1st element), and then go down the  
>>>> others...
>>>>
>>>> Of course, we could even get more sophisticated and have 2; one for
>>>> Windows and another for non-Windows, so the ordering makes more  
>>>> sense
>>>> depending on the platform ;)
>>>>
>>>> OK if I fold something like that in?
>>>>
>>>
>>> Didn't hear back, so I'll be conservative and not do so...
>>>
>>
>> It's just like with APR. See unix/tempdir.c
>> The order is: TMP, TEMP, TMPDIR (and I added TEMPDIR)
>
> which itself was based on Python 2.2 (earlier used TMPFILE) :)

FWIW, later versions of Python use "my" ordering:

     # First, try the environment.
     for envname in 'TMPDIR', 'TEMP', 'TMP':
         dirname = _os.getenv(envname)
         if dirname: dirlist.append(dirname)



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by Jim Jagielski <ji...@jagunet.com>.
On Sep 22, 2009, at 10:56 AM, Mladen Turk wrote:

> On 22/09/09 16:06, Jim Jagielski wrote:
>>
>> On Sep 21, 2009, at 11:51 AM, Jim Jagielski wrote:
>>
>>>
>>> On Sep 21, 2009, at 11:34 AM, mturk@apache.org wrote:
>>>>
>>>> + private static final String [] try_envs = {
>>>> + "TMP",
>>>> + "TEMP",
>>>> + "TMPDIR",
>>>> + "TEMPDIR"
>>>> + };
>>>
>>> Not sure about that ordering, unless we want to favor Windows.
>>> For Unix the canonical envvar is TMPDIR, so it should likely be
>>> checked 1st (and this the 1st element), and then go down the  
>>> others...
>>>
>>> Of course, we could even get more sophisticated and have 2; one for
>>> Windows and another for non-Windows, so the ordering makes more  
>>> sense
>>> depending on the platform ;)
>>>
>>> OK if I fold something like that in?
>>>
>>
>> Didn't hear back, so I'll be conservative and not do so...
>>
>
> It's just like with APR. See unix/tempdir.c
> The order is: TMP, TEMP, TMPDIR (and I added TEMPDIR)

which itself was based on Python 2.2 (earlier used TMPFILE) :)



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by Mladen Turk <mt...@apache.org>.
On 22/09/09 16:06, Jim Jagielski wrote:
>
> On Sep 21, 2009, at 11:51 AM, Jim Jagielski wrote:
>
>>
>> On Sep 21, 2009, at 11:34 AM, mturk@apache.org wrote:
>>>
>>> + private static final String [] try_envs = {
>>> + "TMP",
>>> + "TEMP",
>>> + "TMPDIR",
>>> + "TEMPDIR"
>>> + };
>>
>> Not sure about that ordering, unless we want to favor Windows.
>> For Unix the canonical envvar is TMPDIR, so it should likely be
>> checked 1st (and this the 1st element), and then go down the others...
>>
>> Of course, we could even get more sophisticated and have 2; one for
>> Windows and another for non-Windows, so the ordering makes more sense
>> depending on the platform ;)
>>
>> OK if I fold something like that in?
>>
>
> Didn't hear back, so I'll be conservative and not do so...
>

It's just like with APR. See unix/tempdir.c
The order is: TMP, TEMP, TMPDIR (and I added TEMPDIR)


Regards
-- 
^TM


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by Jim Jagielski <ji...@jaguNET.com>.
On Sep 21, 2009, at 11:51 AM, Jim Jagielski wrote:

>
> On Sep 21, 2009, at 11:34 AM, mturk@apache.org wrote:
>>
>> +    private static final String [] try_envs = {
>> +        "TMP",
>> +        "TEMP",
>> +        "TMPDIR",
>> +        "TEMPDIR"
>> +    };
>
> Not sure about that ordering, unless we want to favor Windows.
> For Unix the canonical envvar is TMPDIR, so it should likely be
> checked 1st (and this the 1st element), and then go down the others...
>
> Of course, we could even get more sophisticated and have 2; one for
> Windows and another for non-Windows, so the ordering makes more sense
> depending on the platform ;)
>
> OK if I fold something like that in?
>

Didn't hear back, so I'll be conservative and not do so...


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r817291 - /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java

Posted by Jim Jagielski <ji...@jaguNET.com>.
On Sep 21, 2009, at 11:34 AM, mturk@apache.org wrote:
>
> +    private static final String [] try_envs = {
> +        "TMP",
> +        "TEMP",
> +        "TMPDIR",
> +        "TEMPDIR"
> +    };

Not sure about that ordering, unless we want to favor Windows.
For Unix the canonical envvar is TMPDIR, so it should likely be
checked 1st (and this the 1st element), and then go down the others...

Of course, we could even get more sophisticated and have 2; one for
Windows and another for non-Windows, so the ordering makes more sense
depending on the platform ;)

OK if I fold something like that in?

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org