Skip to content
Closed
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
46 changes: 26 additions & 20 deletions test/jdk/java/io/File/GetXSpace.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2026, 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 @@ -44,7 +44,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jdk.test.lib.Platform;
import jdk.test.lib.Platform;

import static java.lang.System.err;
import static java.lang.System.out;
Expand Down Expand Up @@ -103,16 +102,11 @@ private static class Space {
private final long free;
private final long available;

Space(String name) {
Space(String name) throws IOException {
this.name = name;
long[] sizes = new long[4];
if (Platform.isWindows() && isCDDrive(name)) {
try {
getCDDriveSpace(name, sizes);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("can't get CDDrive sizes");
}
getCDDriveSpace(name, sizes);
} else {
if (getSpace(name, sizes))
System.err.println("WARNING: total space is estimated");
Expand Down Expand Up @@ -170,7 +164,7 @@ private static ArrayList<String> paths() throws IOException {
return al;
}

private static void compare(Space s) {
private static void compare(Space s) throws IOException {
File f = new File(s.name());
long ts = f.getTotalSpace();
long fs = f.getFreeSpace();
Expand Down Expand Up @@ -318,7 +312,7 @@ private static void compareZeroExist() {
}
}

private static int testFile(Path dir) {
private static int testFile(Path dir) throws IOException {
String dirName = dir.toString();
out.format("--- Testing %s%n", dirName);
compare(new Space(dir.getRoot().toString()));
Expand All @@ -333,10 +327,11 @@ private static int testFile(Path dir) {
return fail != 0 ? 1 : 0;
}

private static int testVolumes() {
private static int testVolumes() throws IOException {
out.println("--- Testing volumes");
// Find all of the partitions on the machine and verify that the sizes
// returned by File::getXSpace are equivalent to those from getSpace or getCDDriveSpace
// returned by File::getXSpace are equivalent to those from getSpace
// or getCDDriveSpace
ArrayList<String> l;
try {
l = paths();
Expand All @@ -350,7 +345,18 @@ private static int testVolumes() {
throw new RuntimeException("no partitions?");

for (var p : l) {
Space s = new Space(p);
Space s;
try {
s = new Space(p);
} catch (IOException x) {
// Avoid failing for transient file systems on Windows
if (Platform.isWindows()) {
File f = new File(p);
if (!f.exists())
continue;
}
throw new IOException("Failure for volume " + p, x);
}
compare(s);
compareZeroNonExist();
compareZeroExist();
Expand Down Expand Up @@ -408,19 +414,19 @@ public static void main(String[] args) throws Exception {
// size[2] free space: number of free bytes in the volume
// size[3] usable space: number of bytes available to the caller
//
private static native boolean getSpace0(String root, long[] space);
private static native boolean getSpace0(String root, long[] space)
throws IOException;

private static native boolean isCDDrive(String root);

private static boolean getSpace(String root, long[] space) {
private static boolean getSpace(String root, long[] space)
throws IOException {
try {
return getSpace0(root, space);
} catch (RuntimeException e) {
} catch (IOException e) {
File f = new File(root);
boolean exists = f.exists();
boolean readable = f.canRead();
System.err.printf("getSpace0 failed for %s (%s, %s)%n",
root, exists, readable);
root, f.exists(), f.canRead());
throw e;
}
}
Expand Down
11 changes: 5 additions & 6 deletions test/jdk/java/io/File/libGetXSpace.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2026, 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 @@ -85,7 +85,7 @@ Java_GetXSpace_getSpace0
BOOL hres = pfnGetDiskSpaceInformation(path, &diskSpaceInfo);
(*env)->ReleaseStringChars(env, root, strchars);
if (FAILED(hres)) {
JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
JNU_ThrowByNameWithLastError(env, "java/io/IOException",
"GetDiskSpaceInformationW");
return totalSpaceIsEstimated;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ Java_GetXSpace_getSpace0
&totalNumberOfBytes, &totalNumberOfFreeBytes);
(*env)->ReleaseStringChars(env, root, strchars);
if (FAILED(hres)) {
JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
JNU_ThrowByNameWithLastError(env, "java/io/IOException",
"GetDiskFreeSpaceExW");
return totalSpaceIsEstimated;
}
Expand All @@ -131,8 +131,7 @@ Java_GetXSpace_getSpace0
char* chars = (char*)malloc((len + 1)*sizeof(char));
if (chars == NULL) {
(*env)->ReleaseStringChars(env, root, strchars);
JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
"malloc");
JNU_ThrowOutOfMemoryError(env, "malloc");
return JNI_FALSE;
}

Expand All @@ -146,7 +145,7 @@ Java_GetXSpace_getSpace0
int result = statfs(chars, &buf);
free(chars);
if (result < 0) {
JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
JNU_ThrowByNameWithLastError(env, "java/io/IOException",
strerror(errno));
return totalSpaceIsEstimated;
}
Expand Down