re GNATS java.io/203 (File.createTempFile doesn't close descriptor)
Fix for PR java.io/203: * java/io/File.java (createTempFile): Obey directory argument. Use java.io.tmpdir if needed. Don't leave FileDescripators open. * java/lang/natSystem.cc (init_properties): Use TMPDIR environment variable to set java.io.tmpdir on non-WIN32 systems. From-SVN: r36143
This commit is contained in:
parent
40255aaf54
commit
a1fa0b27e7
3 changed files with 53 additions and 16 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
2000-09-04 Anthony Green <green@redhat.com>
|
||||||
|
|
||||||
|
Fix for PR java.io/203:
|
||||||
|
* java/io/File.java (createTempFile): Obey directory argument.
|
||||||
|
Use java.io.tmpdir if needed. Don't leave FileDescripators open.
|
||||||
|
* java/lang/natSystem.cc (init_properties): Use TMPDIR environment
|
||||||
|
variable to set java.io.tmpdir on non-WIN32 systems.
|
||||||
|
|
||||||
2000-09-04 Anthony Green <green@redhat.com>
|
2000-09-04 Anthony Green <green@redhat.com>
|
||||||
|
|
||||||
* java/io/File.java (deleteOnExit): New method.
|
* java/io/File.java (deleteOnExit): New method.
|
||||||
|
|
|
@ -233,14 +233,26 @@ public class File implements Serializable
|
||||||
File directory)
|
File directory)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
FileDescriptor desc = new FileDescriptor ();
|
// Grab the system temp directory if necessary
|
||||||
|
if (directory == null)
|
||||||
|
{
|
||||||
|
String dirname = tmpdir;
|
||||||
|
if (dirname == null)
|
||||||
|
throw
|
||||||
|
new IOException("Cannot determine system temporary directory");
|
||||||
|
|
||||||
SecurityManager s = System.getSecurityManager();
|
directory = new File(dirname);
|
||||||
if (s != null)
|
if (!directory.exists())
|
||||||
s.checkWrite (desc);
|
throw new IOException("System temporary directory "
|
||||||
|
+ directory.getName() + " does not exist.");
|
||||||
|
if (!directory.isDirectory())
|
||||||
|
throw new IOException("System temporary directory "
|
||||||
|
+ directory.getName()
|
||||||
|
+ " is not really a directory.");
|
||||||
|
}
|
||||||
|
|
||||||
if (prefix.length () < 3)
|
if (prefix.length () < 3)
|
||||||
throw new IllegalArgumentException ();
|
throw new IllegalArgumentException ("Prefix too short: " + prefix);
|
||||||
if (suffix == null)
|
if (suffix == null)
|
||||||
suffix = ".tmp";
|
suffix = ".tmp";
|
||||||
|
|
||||||
|
@ -259,8 +271,8 @@ public class File implements Serializable
|
||||||
prefix = prefix.substring(0, max_length - 6 - suf_len);
|
prefix = prefix.substring(0, max_length - 6 - suf_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't care about the name because we set it later.
|
File f;
|
||||||
File ret = new File ("");
|
|
||||||
// How many times should we try? We choose 100.
|
// How many times should we try? We choose 100.
|
||||||
for (int i = 0; i < 100; ++i)
|
for (int i = 0; i < 100; ++i)
|
||||||
{
|
{
|
||||||
|
@ -269,18 +281,33 @@ public class File implements Serializable
|
||||||
String l = prefix + t.substring(t.length() - 6) + suffix;
|
String l = prefix + t.substring(t.length() - 6) + suffix;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
desc = new FileDescriptor
|
f = new File(directory, l);
|
||||||
(l, FileDescriptor.WRITE | FileDescriptor.EXCL);
|
if (f.exists())
|
||||||
desc.close ();
|
continue;
|
||||||
ret.setPath(l);
|
else
|
||||||
return ret;
|
{
|
||||||
|
String af = f.getAbsolutePath ();
|
||||||
|
|
||||||
|
// Check to see if we're allowed to write to it.
|
||||||
|
SecurityManager s = System.getSecurityManager();
|
||||||
|
if (s != null)
|
||||||
|
s.checkWrite (af);
|
||||||
|
|
||||||
|
// Now create the file.
|
||||||
|
FileDescriptor fd =
|
||||||
|
new FileDescriptor (af,
|
||||||
|
FileDescriptor.WRITE
|
||||||
|
| FileDescriptor.EXCL);
|
||||||
|
fd.close ();
|
||||||
|
return f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (IOException _)
|
catch (IOException _)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IOException ("couldn't make temp file");
|
throw new IOException ("cannot create temporary file");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File createTempFile (String prefix, String suffix)
|
public static File createTempFile (String prefix, String suffix)
|
||||||
|
|
|
@ -244,8 +244,10 @@ java::lang::System::init_properties (void)
|
||||||
SET ("file.separator", "/");
|
SET ("file.separator", "/");
|
||||||
SET ("path.separator", ":");
|
SET ("path.separator", ":");
|
||||||
SET ("line.separator", "\n");
|
SET ("line.separator", "\n");
|
||||||
// FIXME: look at getenv("TMPDIR");
|
char *tmpdir = ::getenv("TMPDIR");
|
||||||
SET ("java.io.tmpdir", "/tmp");
|
if (! tmpdir)
|
||||||
|
tmpdir = "/tmp";
|
||||||
|
SET ("java.io.tmpdir", tmpdir);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_UNAME
|
#ifdef HAVE_UNAME
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue