/* * Copyright (c) 2005, 2022, 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.
*/
privatestatic SecurityManager [] sma = { null, new Allow(), new DenyFSA(), new DenyRead() };
// FileSystem Total Used Available Use% MountedOn privatestaticfinal Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");
privatestaticvoid compare(Space s) {
File f = new File(s.name()); long ts = f.getTotalSpace(); long fs = f.getFreeSpace(); long us = f.getUsableSpace();
// If the file system can dynamically change size, this check will fail. // This can happen on macOS for the /dev files system. if (ts != s.total() && (!Platform.isOSX() || !s.name().equals("/dev"))) { long blockSize = 1; long numBlocks = 0; try {
FileStore fileStore = Files.getFileStore(f.toPath());
blockSize = fileStore.getBlockSize();
numBlocks = fileStore.getTotalSpace()/blockSize;
} catch (NoSuchFileException nsfe) { // On Linux, ignore the NSFE if the path is one of the // /run/user/$UID mounts created by pam_systemd(8) as it // might be deleted during the test if (!Platform.isLinux() || !s.name().contains("/run/user")) thrownew RuntimeException(nsfe);
} catch (IOException e) { thrownew RuntimeException(e);
}
// On macOS, the number of 1024 byte blocks might be incorrectly // calculated by 'df' using integer division by 2 of the number of // 512 byte blocks, resulting in a size smaller than the actual // value when the number of blocks is odd. if (!Platform.isOSX() || blockSize != 512 || numBlocks % 2 == 0 ||
ts - s.total() != 512) { if (Platform.isWindows()) { // // In Cygwin, 'df' has been observed to account for quotas // when reporting the total disk size, but the total size // reported by GetDiskFreeSpaceExW() has been observed not // to account for the quota in which case the latter value // should be larger. // if (s.total() > ts) {
fail(s.name() + " total space", s.total(), ">", ts);
}
} else {
fail(s.name() + " total space", s.total(), "!=", ts);
}
}
} else {
pass();
}
// unix df returns statvfs.f_bavail long tsp = (!Platform.isWindows() ? us : fs); if (!s.woomFree(tsp)) {
fail(s.name(), s.available(), "??", tsp);
} else {
pass();
}
privatestaticint testDF() {
out.println("--- Testing df"); // Find all of the partitions on the machine and verify that the size // returned by "df" is equivalent to File.getXSpace() values.
ArrayList<Space> l; try {
l = space(null); if (Platform.isWindows()) {
diskFree();
}
} catch (IOException x) { thrownew RuntimeException("can't get file system information", x);
} if (l.size() == 0) thrownew RuntimeException("no partitions?");
for (int i = 0; i < sma.length; i++) {
System.setSecurityManager(sma[i]);
SecurityManager sm = System.getSecurityManager(); if (sma[i] != null && sm == null) thrownew RuntimeException("Test configuration error "
+ " - can't set security manager");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.