下面列出了java.io.File#getTotalSpace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static double getDiskPartitionSpaceUsedPercent(final String path) {
if (null == path || path.isEmpty())
return -1;
try {
File file = new File(path);
if (!file.exists())
return -1;
long totalSpace = file.getTotalSpace();
if (totalSpace > 0) {
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
return usedSpace / (double) totalSpace;
}
} catch (Exception e) {
return -1;
}
return -1;
}
/**
* 获取磁盘分区空间使用率
*/
public static double getDiskPartitionSpaceUsedPercent(final String path) {
if (null == path || path.isEmpty())
return -1;
try {
File file = new File(path);
if (!file.exists())
return -1;
long totalSpace = file.getTotalSpace();
if (totalSpace > 0) {
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
return usedSpace / (double) totalSpace;
}
} catch (Exception e) {
return -1;
}
return -1;
}
public static double getDiskPartitionSpaceUsedPercent(final String path) {
if (null == path || path.isEmpty())
return -1;
try {
File file = new File(path);
if (!file.exists())
return -1;
long totalSpace = file.getTotalSpace();
if (totalSpace > 0) {
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
return usedSpace / (double) totalSpace;
}
} catch (Exception e) {
return -1;
}
return -1;
}
private boolean isHealthy(Path spillDirPath) {
if (healthCheckEnabled) {
final File disk = new File(Path.getPathWithoutSchemeAndAuthority(spillDirPath).toString());
final double totalSpace = (double) disk.getTotalSpace();
minDiskSpace = options.minDiskSpace();
minDiskSpacePercentage = options.minDiskSpacePercentage();
logger.debug("Check isHealthy for {} minDiskSpace: {} minDiskSpacePercentage: {}",
spillDirPath.getName(), minDiskSpace, minDiskSpacePercentage);
final long threshold = Math.max((long) ((totalSpace / 100.0) * minDiskSpacePercentage), minDiskSpace);
final long available = disk.getFreeSpace();
if (available < threshold) {
return false;
}
}
return true;
}
public static double getDiskPartitionSpaceUsedPercent(final String path) {
if (null == path || path.isEmpty())
return -1;
try {
File file = new File(path);
if (!file.exists())
return -1;
long totalSpace = file.getTotalSpace();
if (totalSpace > 0) {
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
return usedSpace / (double) totalSpace;
}
} catch (Exception e) {
return -1;
}
return -1;
}
private static void compareZeroExist() {
try {
File f = File.createTempFile("tmp", null, new File("."));
long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
for (int i = 0; i < s.length; i++) {
if (s[i] == 0L)
fail(f.getName(), s[i], "==", 0L);
else
pass();
}
} catch (IOException x) {
fail("Couldn't create temp file for test");
}
}
private static void compareZeroExist() {
try {
File f = File.createTempFile("tmp", null, new File("."));
long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
for (int i = 0; i < s.length; i++) {
if (s[i] == 0L)
fail(f.getName(), s[i], "==", 0L);
else
pass();
}
} catch (IOException x) {
fail("Couldn't create temp file for test");
}
}
public static double getDiskPartitionSpaceUsedPercent(final String path) {
if (null == path || path.isEmpty())
return -1;
try {
File file = new File(path);
if (!file.exists())
return -1;
long totalSpace = file.getTotalSpace();
if (totalSpace > 0) {
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
return usedSpace / (double) totalSpace;
}
} catch (Exception e) {
return -1;
}
return -1;
}
@Override
public FsStatus getStatus(Path p) throws IOException {
File partition = pathToFile(p == null ? new Path("/") : p);
//File provides getUsableSpace() and getFreeSpace()
//File provides no API to obtain used space, assume used = total - free
return new FsStatus(partition.getTotalSpace(),
partition.getTotalSpace() - partition.getFreeSpace(),
partition.getFreeSpace());
}
protected boolean hasAvailableStorage(long expectedSize) {
File content = getContentFile();
double totalSpace = (double) content.getTotalSpace();
double freeSpace = (double) content.getFreeSpace();
double percentFull = 100 - ((freeSpace * 100) / totalSpace);
double afterSavedSize = (totalSpace - freeSpace) + expectedSize;
logger.debug("Reporting -> %[{}] Full (download size of [{}] bytes to be added)", percentFull, expectedSize);
// See: https://developer.android.com/training/basics/data-storage/files.html#GetFreeSpace
// If the number returned is a few MB more than the size of the data you want to save, or if the file system is
// less than 90% full, then it's probably safe to proceed. Otherwise, you probably shouldn't write to storage.
return afterSavedSize < totalSpace && percentFull <= 90D;
}
private static void compare(Space s) {
File f = new File(s.name());
long ts = f.getTotalSpace();
long fs = f.getFreeSpace();
long us = f.getUsableSpace();
out.format("%s:%n", s.name());
String fmt = " %-4s total= %12d free = %12d usable = %12d%n";
out.format(fmt, "df", s.total(), 0, s.free());
out.format(fmt, "getX", ts, fs, us);
// if the file system can dynamically change size, this check will fail
if (ts != s.total())
fail(s.name(), s.total(), "!=", ts);
else
pass();
// unix df returns statvfs.f_bavail
long tsp = (!name.startsWith("Windows") ? us : fs);
if (!s.woomFree(tsp))
fail(s.name(), s.free(), "??", tsp);
else
pass();
if (fs > s.total())
fail(s.name(), s.total(), ">", fs);
else
pass();
if (us > s.total())
fail(s.name(), s.total(), ">", us);
else
pass();
}
public String check(String folder) {
if (folder == null)
throw new IllegalArgumentException("folder");
File f = new File(folder);
folderSize = getFileSize(f);
usage = 1.0*(f.getTotalSpace() - f.getFreeSpace())/ f.getTotalSpace();
return String.format("used %d files %d disk in use %f", folderSize, fileCount, usage);
}
@PostConstruct
public void init() {
long totalSpace = 0;
try {
File file = new File(Constants.DOWNLOAD_HOME);
totalSpace = file.getTotalSpace();
} catch (Exception e) {
}
long youngGcThreshold = totalSpace > 0 && totalSpace / 4 < 100 * 1024 * 1024 * 1024L ? totalSpace / 4
: 100 * 1024 * 1024 * 1024L;
downSpaceCleaner.fillConf(DownSpaceCleaner.SPACE_TYPE_DISK, Constants.DOWNLOAD_HOME,
5 * 1024 * 1024 * 1024L,
youngGcThreshold, 1, 2 * 3600 * 1000L);
}
private static void compare(Space s) {
File f = new File(s.name());
long ts = f.getTotalSpace();
long fs = f.getFreeSpace();
long us = f.getUsableSpace();
out.format("%s:%n", s.name());
String fmt = " %-4s total= %12d free = %12d usable = %12d%n";
out.format(fmt, "df", s.total(), 0, s.free());
out.format(fmt, "getX", ts, fs, us);
// if the file system can dynamically change size, this check will fail
if (ts != s.total())
fail(s.name(), s.total(), "!=", ts);
else
pass();
// unix df returns statvfs.f_bavail
long tsp = (!name.startsWith("Windows") ? us : fs);
if (!s.woomFree(tsp))
fail(s.name(), s.free(), "??", tsp);
else
pass();
if (fs > s.total())
fail(s.name(), s.total(), ">", fs);
else
pass();
if (us > s.total())
fail(s.name(), s.total(), ">", us);
else
pass();
}
public String check(String folder) {
if (folder == null)
throw new IllegalArgumentException("folder");
File f = new File(folder);
folderSize = getFileSize(f);
usage = 1.0*(f.getTotalSpace() - f.getFreeSpace())/ f.getTotalSpace();
return String.format("used %d files %d disk in use %f", folderSize, fileCount, usage);
}
/**
* 磁盘占用
*
* @return 磁盘占用
*/
protected static String getHardDisk() {
File[] files = File.listRoots();
double totalSpace = 0;
double useAbleSpace = 0;
for (File file : files) {
double total = file.getTotalSpace();
totalSpace += total;
useAbleSpace += total - file.getUsableSpace();
}
return String.format("%.2f", useAbleSpace / totalSpace * 100);
}
private static void compare(Space s) {
File f = new File(s.name());
long ts = f.getTotalSpace();
long fs = f.getFreeSpace();
long us = f.getUsableSpace();
out.format("%s:%n", s.name());
String fmt = " %-4s total= %12d free = %12d usable = %12d%n";
out.format(fmt, "df", s.total(), 0, s.free());
out.format(fmt, "getX", ts, fs, us);
// if the file system can dynamically change size, this check will fail
if (ts != s.total())
fail(s.name(), s.total(), "!=", ts);
else
pass();
// unix df returns statvfs.f_bavail
long tsp = (!name.startsWith("Windows") ? us : fs);
if (!s.woomFree(tsp))
fail(s.name(), s.free(), "??", tsp);
else
pass();
if (fs > s.total())
fail(s.name(), s.total(), ">", fs);
else
pass();
if (us > s.total())
fail(s.name(), s.total(), ">", us);
else
pass();
}
/**
* Copy the systemvm.iso in if it doesn't exist or the size differs.
*
* @param storageUrl
* @param poolUuid
* @param host
*/
private void prepareSecondaryStorageStore(String storageUrl,
String poolUuid, String host) {
String mountPoint = storageUrl;
GlobalLock lock = GlobalLock.getInternLock("prepare.systemvm");
try {
/* double check */
if (config.getAgentHasMaster() && config.getAgentInOvm3Pool()) {
LOGGER.debug("Skip systemvm iso copy, leave it to the master");
return;
}
if (lock.lock(3600)) {
try {
/*
* save src iso real name for reuse, so we don't depend on
* other happy little accidents.
*/
File srcIso = getSystemVMPatchIsoFile();
String destPath = mountPoint + "/ISOs/";
try {
StoragePlugin sp = new StoragePlugin(c);
FileProperties fp = sp.storagePluginGetFileInfo(
poolUuid, host, destPath + "/"
+ srcIso.getName());
if (fp.getSize() != srcIso.getTotalSpace()) {
LOGGER.info(" System VM patch ISO file already exists: "
+ srcIso.getAbsolutePath().toString()
+ ", destination: " + destPath);
}
} catch (Exception e) {
LOGGER.info("Copy System VM patch ISO file to secondary storage. source ISO: "
+ srcIso.getAbsolutePath()
+ ", destination: "
+ destPath);
try {
/* Perhaps use a key instead ? */
SshHelper
.scpTo(c.getIp(), 22, config
.getAgentSshUserName(), null,
config.getAgentSshPassword(),
destPath, srcIso.getAbsolutePath()
.toString(), "0644");
} catch (Exception es) {
LOGGER.error("Unexpected exception ", es);
String msg = "Unable to copy systemvm ISO on secondary storage. src location: "
+ srcIso.toString()
+ ", dest location: "
+ destPath;
LOGGER.error(msg);
throw new CloudRuntimeException(msg, es);
}
}
} finally {
lock.unlock();
}
}
} finally {
lock.releaseRef();
}
}
public static boolean partitionHasUsableSpacePercentage(File file, int percentage) {
return ((double)file.getUsableSpace()/(double)file.getTotalSpace())*100 >= percentage;
}
@Override
public long getUsedSpace(final String path) {
final File file = new File(path);
return file.getTotalSpace() - file.getFreeSpace();
}