diff options
author | Tom Tromey <tromey@cygnus.com> | 2000-02-11 17:32:52 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2000-02-11 17:32:52 +0000 |
commit | 304ccb107580ec81f3d44fbf0ecf5b0a66c291f0 (patch) | |
tree | 12601bfce83cfa5636689e3f8af63f4c45a4c273 /libjava | |
parent | a1cee8a3f3ce4487e9671739b4db2d1c5ec6e56c (diff) | |
download | gcc-304ccb107580ec81f3d44fbf0ecf5b0a66c291f0.zip gcc-304ccb107580ec81f3d44fbf0ecf5b0a66c291f0.tar.gz gcc-304ccb107580ec81f3d44fbf0ecf5b0a66c291f0.tar.bz2 |
natFileDescriptorPosix.cc (open): Recognize EXCL flag.
* java/io/natFileDescriptorPosix.cc (open): Recognize EXCL flag.
* java/io/FileDescriptor.java (EXCL): New static field.
* java/io/File.java (tmpdir): New static field.
(createTempFile): New method.
(nextValue): New method.
* java/lang/natSystem.cc (init_properties): Set java.io.tmpdir
property.
From-SVN: r31922
Diffstat (limited to 'libjava')
-rw-r--r-- | libjava/ChangeLog | 8 | ||||
-rw-r--r-- | libjava/java/io/File.java | 69 | ||||
-rw-r--r-- | libjava/java/io/FileDescriptor.java | 4 | ||||
-rw-r--r-- | libjava/java/io/natFileDescriptorPosix.cc | 12 | ||||
-rw-r--r-- | libjava/java/lang/natSystem.cc | 3 |
5 files changed, 92 insertions, 4 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 8bea0c7..35e132e 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,5 +1,13 @@ 2000-02-10 Tom Tromey <tromey@cygnus.com> + * java/io/natFileDescriptorPosix.cc (open): Recognize EXCL flag. + * java/io/FileDescriptor.java (EXCL): New static field. + * java/io/File.java (tmpdir): New static field. + (createTempFile): New method. + (nextValue): New method. + * java/lang/natSystem.cc (init_properties): Set java.io.tmpdir + property. + * include/jni.h (JNI_FALSE): Renamed from JNI_TRUE; oops. (jboolean): Declare as an attributed int, not a bool. (_Jv_func): Declare differently for C. diff --git a/libjava/java/io/File.java b/libjava/java/io/File.java index 454c15b..229125e 100644 --- a/libjava/java/io/File.java +++ b/libjava/java/io/File.java @@ -1,6 +1,6 @@ // File.java - File name -/* Copyright (C) 1998, 1999 Red Hat, Inc. +/* Copyright (C) 1998, 1999, 2000 Red Hat, Inc. This file is part of libgcj. @@ -215,6 +215,68 @@ public class File implements Serializable return mkdirs (new File (path)); } + private static String nextValue () + { + return Long.toString(counter++, Character.MAX_RADIX); + } + + public static File createTempFile (String prefix, String suffix, + File directory) + throws IOException + { + FileDescriptor desc = new FileDescriptor (); + + SecurityManager s = System.getSecurityManager(); + if (s != null) + s.checkWrite (desc); + + if (prefix.length () < 3) + throw new IllegalArgumentException (); + if (suffix == null) + suffix = ".tmp"; + + // FIXME: filename length varies by architecture and filesystem. + int max_length = 255; + + // Truncation rules. + // `6' is the number of characters we generate. + if (prefix.length () + 6 + suffix.length () > max_length) + { + int suf_len = 0; + if (suffix.charAt(0) == '.') + suf_len = 4; + suffix = suffix.substring(0, suf_len); + if (prefix.length () + 6 + suf_len > max_length) + prefix = prefix.substring(0, max_length - 6 - suf_len); + } + + // We don't care about the name because we set it later. + File ret = new File (""); + // How many times should we try? We choose 100. + for (int i = 0; i < 100; ++i) + { + // This is ugly. + String l = prefix + (nextValue () + "ZZZZZZ").substring(0,6) + suffix; + try + { + desc.open (l, FileDescriptor.WRITE | FileDescriptor.EXCL); + ret.setPath(l); + return ret; + } + catch (IOException _) + { + } + } + + throw new IOException ("couldn't make temp file"); + } + + public static File createTempFile (String prefix, String suffix) + throws IOException + { + return createTempFile (prefix, suffix, null); + } + private final native boolean performRenameTo (File dest); public boolean renameTo (File dest) { @@ -234,10 +296,15 @@ public class File implements Serializable public static final String separator = System.getProperty("file.separator"); public static final char separatorChar = separator.charAt(0); + private static final String tmpdir = System.getProperty("java.io.tmpdir"); // The path. private String path; + // We keep a counter for use by createTempFile. We choose the first + // value randomly to try to avoid clashes with other VMs. + private static long counter = Double.doubleToLongBits (Math.random ()); + // mkdirs() uses this to avoid repeated allocations. private final void setPath (String n) { diff --git a/libjava/java/io/FileDescriptor.java b/libjava/java/io/FileDescriptor.java index b23e0b6..541c07a 100644 --- a/libjava/java/io/FileDescriptor.java +++ b/libjava/java/io/FileDescriptor.java @@ -1,6 +1,6 @@ // FileDescriptor.java - Open file or device -/* Copyright (C) 1998, 1999 Red Hat, Inc. +/* Copyright (C) 1998, 1999, 2000 Red Hat, Inc. This file is part of libgcj. @@ -36,6 +36,8 @@ public final class FileDescriptor static final int READ = 1; static final int WRITE = 2; static final int APPEND = 4; + // EXCL is used only when making a temp file. + static final int EXCL = 8; // These are WHENCE values for seek. static final int SET = 0; diff --git a/libjava/java/io/natFileDescriptorPosix.cc b/libjava/java/io/natFileDescriptorPosix.cc index 9d1c8c9..6538960 100644 --- a/libjava/java/io/natFileDescriptorPosix.cc +++ b/libjava/java/io/natFileDescriptorPosix.cc @@ -1,6 +1,6 @@ // natFileDescriptor.cc - Native part of FileDescriptor class. -/* Copyright (C) 1998, 1999 Red Hat, Inc. +/* Copyright (C) 1998, 1999, 2000 Red Hat, Inc. This file is part of libgcj. @@ -83,6 +83,7 @@ java::io::FileDescriptor::open (jstring path, jint jflags) #endif JvAssert ((jflags & READ) || (jflags & WRITE)); + int mode = 0644; if ((jflags & READ) && (jflags & WRITE)) flags |= O_RDWR; else if ((jflags & READ)) @@ -94,9 +95,16 @@ java::io::FileDescriptor::open (jstring path, jint jflags) flags |= O_APPEND; else flags |= O_TRUNC; + + if ((jflags & EXCL)) + { + flags |= O_EXCL; + // In this case we are making a temp file. + mode = 0600; + } } - int fd = ::open (buf, flags, 0644); + int fd = ::open (buf, flags, mode); if (fd == -1) { char msg[MAXPATHLEN + 200]; diff --git a/libjava/java/lang/natSystem.cc b/libjava/java/lang/natSystem.cc index e423f92..35093df 100644 --- a/libjava/java/lang/natSystem.cc +++ b/libjava/java/lang/natSystem.cc @@ -270,11 +270,14 @@ java::lang::System::init_properties (void) SET ("file.separator", "\\"); SET ("path.separator", ";"); SET ("line.separator", "\r\n"); + SET ("java.io.tmpdir", "C:\\temp"); #else // Unix. SET ("file.separator", "/"); SET ("path.separator", ":"); SET ("line.separator", "\n"); + // FIXME: look at getenv("TMPDIR"); + SET ("java.io.tmpdir", "/tmp"); #endif #ifdef HAVE_UNAME |