Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/java.base/unix/native/libjava/io_util_md.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,6 +27,7 @@
#include "jvm.h"
#include "io_util.h"
#include "io_util_md.h"
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

Expand Down Expand Up @@ -75,20 +76,27 @@ FD
handleOpen(const char *path, int oflag, int mode) {
FD fd;
RESTARTABLE(open(path, oflag, mode), fd);
if (fd != -1) {
struct stat buf;
int result;
RESTARTABLE(fstat(fd, &buf), result);
if (result != -1) {
if (S_ISDIR(buf.st_mode)) {
close(fd);
errno = EISDIR;
fd = -1;
}
} else {
// Fast-path, either it is an error or if it's
// not a read access mode then the Unix standard
// guarantees to have failed with EISDIR
if (fd == -1 || ((oflag & O_ACCMODE) != O_RDONLY) != 0) {
return fd;
}

// Slow-path, while Unix allow for directories
// to be read, Java don't
struct stat buf;
int result;
RESTARTABLE(fstat(fd, &buf), result);
if (result != -1) {
if (S_ISDIR(buf.st_mode)) {
close(fd);
errno = EISDIR;
fd = -1;
}
} else {
close(fd);
fd = -1;
}
return fd;
}
Expand Down
55 changes: 55 additions & 0 deletions test/micro/org/openjdk/bench/java/io/FileWriteStress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.*;

/**
* Tests the overheads of I/O syscalls
*/
@State(Scope.Benchmark)
@Warmup(iterations = 3, time = 2)
@Measurement(iterations = 5, time = 5)
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Threads(1)

@Fork(value = 10)
public class FileWriteStress {
final byte[] payload = "something".getBytes();
final String path = System.getProperty("os.name", "unknown").toLowerCase().contains("win") ? "NUL" : "/dev/null";

@Benchmark
public void test() throws IOException {
try (FileOutputStream f = new FileOutputStream(path)) {
f.write(payload);
}
}

}