File: libGetXSpace.c

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (191 lines) | stat: -rw-r--r-- 6,672 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (c) 2023, 2024, 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.
 */
#include <stdlib.h>
#include "jni.h"
#include "jni_util.h"
#ifdef WINDOWS
#include <windows.h>
#include <fileapi.h>
#include <winerror.h>
#else
#include <errno.h>
#include <string.h>
#if __APPLE__
#include <sys/param.h>
#include <sys/mount.h>
#else
#include <sys/statfs.h>
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

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

//
// root      the root of the volume
// sizes[0]  total size:   number of bytes in the volume
// sizes[1]  total space:  number of bytes visible to the caller
// sizes[2]  free space:   number of free bytes in the volume
// sizes[3]  usable space: number of bytes available to the caller
//
JNIEXPORT jboolean JNICALL
Java_GetXSpace_getSpace0
    (JNIEnv *env, jclass cls, jstring root, jlongArray sizes)
{
    jboolean totalSpaceIsEstimated = JNI_FALSE;
    jlong array[4];
    const jchar* strchars = (*env)->GetStringChars(env, root, NULL);
    if (strchars == NULL) {
        JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
                                     "GetStringChars");
        return JNI_FALSE;
    }

#ifdef WINDOWS
    if (initialized == JNI_FALSE) {
        initialized = JNI_TRUE;
        HMODULE hmod = GetModuleHandleW(L"kernel32");
        if (hmod != NULL) {
            *(FARPROC*)&pfnGetDiskSpaceInformation =
                GetProcAddress(hmod, "GetDiskSpaceInformationW");
        }
    }

    LPCWSTR path = (LPCWSTR)strchars;

    if (pfnGetDiskSpaceInformation != NULL) {
        // use GetDiskSpaceInformationW
        DISK_SPACE_INFORMATION diskSpaceInfo;
        BOOL hres = pfnGetDiskSpaceInformation(path, &diskSpaceInfo);
        (*env)->ReleaseStringChars(env, root, strchars);
        if (FAILED(hres)) {
            JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
                                         "GetDiskSpaceInformationW");
            return totalSpaceIsEstimated;
        }

        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 GetDiskSpaceInformationW is unavailable ("The specified
        // procedure could not be found"), fall back to GetDiskFreeSpaceExW
        ULARGE_INTEGER freeBytesAvailable;
        ULARGE_INTEGER totalNumberOfBytes;
        ULARGE_INTEGER totalNumberOfFreeBytes;

        BOOL hres = GetDiskFreeSpaceExW(path, &freeBytesAvailable,
            &totalNumberOfBytes, &totalNumberOfFreeBytes);
        (*env)->ReleaseStringChars(env, root, strchars);
        if (FAILED(hres)) {
            JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
                                         "GetDiskFreeSpaceExW");
            return totalSpaceIsEstimated;
        }

        // If quotas are in effect, it is impossible to obtain the volume size,
        // so estimate it as free + used = free + (visible - available)
        ULONGLONG used = totalNumberOfBytes.QuadPart - freeBytesAvailable.QuadPart;
        array[0] = (jlong)(totalNumberOfFreeBytes.QuadPart + used);
        array[1] = (jlong)totalNumberOfBytes.QuadPart;
        array[2] = (jlong)totalNumberOfFreeBytes.QuadPart;
        array[3] = (jlong)freeBytesAvailable.QuadPart;
    }
#else
    int len = (int)(*env)->GetStringLength(env, root);
    char* chars = (char*)malloc((len + 1)*sizeof(char));
    if (chars == NULL) {
        (*env)->ReleaseStringChars(env, root, strchars);
        JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
                                     "malloc");
        return JNI_FALSE;
    }

    for (int i = 0; i < len; i++) {
        chars[i] = (char)strchars[i];
    }
    chars[len] = '\0';
    (*env)->ReleaseStringChars(env, root, strchars);

    struct statfs buf;
    int result = statfs(chars, &buf);
    free(chars);
    if (result < 0) {
        JNU_ThrowByNameWithLastError(env, "java/lang/RuntimeException",
                                     strerror(errno));
        return totalSpaceIsEstimated;
    }

    array[0] = (jlong)(buf.f_blocks*buf.f_bsize);
    array[1] = array[0]; // number visible is the same as the total size
    array[2] = (jlong)(buf.f_bfree*buf.f_bsize);
    array[3] = (jlong)(buf.f_bavail*buf.f_bsize);
#endif
    (*env)->SetLongArrayRegion(env, sizes, 0, 4, array);
    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