File.createNewFile should return false if the file already exists

This commit is contained in:
Joel Dice 2012-03-13 08:26:51 -06:00
parent 6cc0ddda7c
commit 4aefa211a3
4 changed files with 27 additions and 27 deletions

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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();
}
}
}