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
50 changes: 3 additions & 47 deletions test/jdk/java/io/File/GetXSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public class GetXSpace {
System.loadLibrary("GetXSpace");
}

private static final Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");

private static int fail = 0;
private static int pass = 0;
private static Throwable first;
Expand Down Expand Up @@ -105,12 +103,8 @@ private static class Space {
Space(String name) throws IOException {
this.name = name;
long[] sizes = new long[4];
if (Platform.isWindows() && isCDDrive(name)) {
getCDDriveSpace(name, sizes);
} else {
if (getSpace(name, sizes))
System.err.println("WARNING: total space is estimated");
}
if (getSpace(name, sizes))
System.err.println("WARNING: total space is estimated");
this.size = sizes[0];
this.total = sizes[1];
this.free = sizes[2];
Expand Down Expand Up @@ -178,8 +172,7 @@ private static void compare(Space s) throws IOException {

out.format("%s (%d):%n", s.name(), s.size());
String fmt = " %-4s total = %12d free = %12d usable = %12d%n";
String method = Platform.isWindows() && isCDDrive(s.name()) ? "getCDDriveSpace" : "getSpace";
out.format(fmt, method, s.total(), s.free(), s.available());
out.format(fmt, "getSpace", s.total(), s.free(), s.available());
out.format(fmt, "getXSpace", ts, fs, us);

// If the file system can dynamically change size, this check will fail.
Expand Down Expand Up @@ -331,7 +324,6 @@ 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
ArrayList<String> l;
try {
l = paths();
Expand Down Expand Up @@ -417,8 +409,6 @@ public static void main(String[] args) throws Exception {
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)
throws IOException {
try {
Expand All @@ -430,38 +420,4 @@ private static boolean getSpace(String root, long[] space)
throw e;
}
}

private static void getCDDriveSpace(String root, long[] sizes)
throws IOException {
String[] cmd = new String[] {"df", "-k", "-P", root};
Process p = Runtime.getRuntime().exec(cmd);
StringBuilder sb = new StringBuilder();

try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String s;
int i = 0;
while ((s = in.readLine()) != null) {
// skip header
if (i++ == 0) continue;
sb.append(s).append("\n");
}
}
out.println(sb);

Matcher m = DF_PATTERN.matcher(sb);
int j = 0;
while (j < sb.length()) {
if (m.find(j)) {
sizes[0] = Long.parseLong(m.group(2)) * 1024;
sizes[1] = Long.parseLong(m.group(3)) * 1024;
sizes[2] = sizes[0] - sizes[1];
sizes[3] = Long.parseLong(m.group(4)) * 1024;
j = m.end();
} else {
throw new RuntimeException("unrecognized df output format: "
+ "charAt(" + j + ") = '"
+ sb.charAt(j) + "'");
}
}
}
}
62 changes: 18 additions & 44 deletions test/jdk/java/io/File/libGetXSpace.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern "C" {

#ifdef WINDOWS
jboolean initialized = JNI_FALSE;
BOOL(WINAPI * pfnGetDiskSpaceInformation)(LPCWSTR, LPVOID) = NULL;
HRESULT(WINAPI * pfnGetDiskSpaceInformation)(LPCWSTR, LPVOID) = NULL;
#endif

//
Expand Down Expand Up @@ -82,27 +82,27 @@ Java_GetXSpace_getSpace0
if (pfnGetDiskSpaceInformation != NULL) {
// use GetDiskSpaceInformationW
DISK_SPACE_INFORMATION diskSpaceInfo;
BOOL hres = pfnGetDiskSpaceInformation(path, &diskSpaceInfo);
(*env)->ReleaseStringChars(env, root, strchars);
HRESULT hres = pfnGetDiskSpaceInformation(path, &diskSpaceInfo);
if (FAILED(hres)) {
JNU_ThrowByNameWithLastError(env, "java/io/IOException",
"GetDiskSpaceInformationW");
return totalSpaceIsEstimated;
totalSpaceIsEstimated = JNI_TRUE;
} else {
(*env)->ReleaseStringChars(env, root, strchars);
ULONGLONG bytesPerAllocationUnit =
diskSpaceInfo.SectorsPerAllocationUnit*diskSpaceInfo.BytesPerSector;
array[0] = (jlong)(diskSpaceInfo.ActualTotalAllocationUnits*
bytesPerAllocationUnit);
array[1] = (jlong)(diskSpaceInfo.CallerTotalAllocationUnits*
bytesPerAllocationUnit);
array[2] = (jlong)(diskSpaceInfo.ActualAvailableAllocationUnits*
bytesPerAllocationUnit);
array[3] = (jlong)(diskSpaceInfo.CallerAvailableAllocationUnits*
bytesPerAllocationUnit);
}

ULONGLONG bytesPerAllocationUnit =
diskSpaceInfo.SectorsPerAllocationUnit*diskSpaceInfo.BytesPerSector;
array[0] = (jlong)(diskSpaceInfo.ActualTotalAllocationUnits*
bytesPerAllocationUnit);
array[1] = (jlong)(diskSpaceInfo.CallerTotalAllocationUnits*
bytesPerAllocationUnit);
array[2] = (jlong)(diskSpaceInfo.ActualAvailableAllocationUnits*
bytesPerAllocationUnit);
array[3] = (jlong)(diskSpaceInfo.CallerAvailableAllocationUnits*
bytesPerAllocationUnit);
} else {
totalSpaceIsEstimated = JNI_TRUE;
}

if (totalSpaceIsEstimated == JNI_TRUE) {
// if GetDiskSpaceInformationW is unavailable ("The specified
// procedure could not be found"), fall back to GetDiskFreeSpaceExW
ULARGE_INTEGER freeBytesAvailable;
Expand All @@ -112,7 +112,7 @@ Java_GetXSpace_getSpace0
BOOL hres = GetDiskFreeSpaceExW(path, &freeBytesAvailable,
&totalNumberOfBytes, &totalNumberOfFreeBytes);
(*env)->ReleaseStringChars(env, root, strchars);
if (FAILED(hres)) {
if (!hres) {
JNU_ThrowByNameWithLastError(env, "java/io/IOException",
"GetDiskFreeSpaceExW");
return totalSpaceIsEstimated;
Expand Down Expand Up @@ -159,32 +159,6 @@ Java_GetXSpace_getSpace0
return totalSpaceIsEstimated;
}

JNIEXPORT jboolean JNICALL
Java_GetXSpace_isCDDrive
(JNIEnv *env, jclass cls, jstring root)
{
#ifdef WINDOWS
const jchar* strchars = (*env)->GetStringChars(env, root, NULL);
if (strchars == NULL) {
JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
"GetStringChars");
return JNI_FALSE;
}

LPCWSTR path = (LPCWSTR)strchars;
UINT driveType = GetDriveTypeW(path);

(*env)->ReleaseStringChars(env, root, strchars);

if (driveType != DRIVE_CDROM) {
return JNI_FALSE;
}

return JNI_TRUE;
#else
return JNI_FALSE;
#endif
}
#ifdef __cplusplus
}
#endif