diff --git a/classpath/java-io.cpp b/classpath/java-io.cpp index 5b723114ae..c1373a3c58 100644 --- a/classpath/java-io.cpp +++ b/classpath/java-io.cpp @@ -48,7 +48,6 @@ # define R_OK 4 # else # define OPEN _wopen -# define CREAT _wcreat # endif # define GET_CHARS GetStringChars @@ -71,7 +70,6 @@ typedef wchar_t char_t; # define STRUCT_STAT struct stat # define MKDIR mkdir # define CHMOD chmod -# define CREAT creat # define UNLINK unlink # define RENAME rename # define OPEN_MASK 0 @@ -102,12 +100,6 @@ OPEN(string_t path, int mask, int mode) return -1; } } - -inline int -CREAT(string_t path, int mode) -{ - return OPEN(path, _O_CREAT, mode); -} #endif inline bool @@ -407,21 +399,28 @@ Java_java_io_File_mkdir(JNIEnv* e, jclass, jstring path) } } -extern "C" JNIEXPORT void JNICALL +extern "C" JNIEXPORT jboolean JNICALL Java_java_io_File_createNewFile(JNIEnv* e, jclass, jstring path) { + bool result = false; string_t chars = getChars(e, path); if (chars) { + fprintf(stderr, "create file \"%s\"\n", chars); if (not exists(chars)) { - int fd = CREAT(chars, 0600); + int fd = OPEN(chars, O_CREAT | O_WRONLY | O_EXCL, 0600); if (fd == -1) { - throwNewErrno(e, "java/io/IOException"); + fprintf(stderr, "errno %d\n", errno); + if (errno != EEXIST) { + throwNewErrno(e, "java/io/IOException"); + } } else { + result = true; doClose(e, fd); } } releaseChars(e, path, chars); } + return result; } extern "C" JNIEXPORT void JNICALL diff --git a/classpath/java/io/File.java b/classpath/java/io/File.java index a77502c041..59c7da8d16 100644 --- a/classpath/java/io/File.java +++ b/classpath/java/io/File.java @@ -204,15 +204,10 @@ public class File implements Serializable { } } - private static native void createNewFile(String path) throws IOException; + private static native boolean createNewFile(String path) throws IOException; - public boolean createNewFile() { - try { - createNewFile(path); - return true; - } catch (IOException e) { - return false; - } + public boolean createNewFile() throws IOException { + return createNewFile(path); } public static native void delete(String path) throws IOException; diff --git a/src/classpath-openjdk.cpp b/src/classpath-openjdk.cpp index 2866b6072b..bfa759d63d 100644 --- a/src/classpath-openjdk.cpp +++ b/src/classpath-openjdk.cpp @@ -42,7 +42,6 @@ # define S_IWUSR _S_IWRITE # else # define OPEN _open -# define CREAT _creat # endif # define O_RDONLY _O_RDONLY @@ -82,6 +81,8 @@ typedef int socklen_t; #endif // not PLATFORM_WINDOWS +#define JVM_EEXIST -100 + using namespace vm; namespace { @@ -97,12 +98,6 @@ OPEN(string_t path, int mask, int mode) return -1; } } - -inline int -CREAT(string_t path, int mode) -{ - return OPEN(path, _O_CREAT, mode); -} #endif namespace local { @@ -4819,7 +4814,12 @@ EXPORT(JVM_NativePath)(char* path) extern "C" JNIEXPORT jint JNICALL EXPORT(JVM_Open)(const char* path, jint flags, jint mode) { - return OPEN(path, flags, mode); + int r = OPEN(path, flags, mode); + if (r == -1) { + return errno == EEXIST ? JVM_EEXIST : -1; + } else { + return r; + } } extern "C" JNIEXPORT jint JNICALL diff --git a/test/Files.java b/test/Files.java index f3d1988f31..b5484462cf 100644 --- a/test/Files.java +++ b/test/Files.java @@ -40,6 +40,12 @@ public class Files { isAbsoluteTest(false); setExecutableTestWithPermissions(true); setExecutableTestWithPermissions(false); + + { File f = new File("test.txt"); + f.createNewFile(); + expect(! f.createNewFile()); + f.delete(); + } } }