in util/src/main/java/com/epam/deltix/util/io/BasicIOUtil.java [1285:1378]
public static long getFreeDiskSpaceOn(File filesys)
throws IOException, InterruptedException {
if (!filesys.exists())
throw new FileNotFoundException(filesys.getPath());
String fsep = System.getProperty("file.separator");
String[] cmd = null;
boolean dos = true;
if (fsep.equals("\\"))
cmd = new String[] { "cmd.exe", "/c", "dir", filesys.getPath()};
else {
cmd = new String[] { "/usr/bin/df", "-k", filesys.getPath()};
dos = false;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int ret =
ProcessHelper.execAndWait(
cmd,
null,
null,
false,
baos,
true,
null,
false);
} catch (IOException e) {
cmd = new String[] { "df", "-k", filesys.getPath()};
int ret =
ProcessHelper.execAndWait(
cmd,
null,
null,
false,
baos,
true,
null,
false);
if (ret != 0)
throw new IOException(
"Command\n" + getCmdAsString(cmd) + "\nexited with " + ret);
}
String s = new String(baos.toByteArray());
if (System.getProperty("debug.diskspace") != null)
System.out.println(
"Output from\n" + getCmdAsString(cmd) + ":\n" + s);
int pos = -1;
long value = 0;
if (dos) {
pos = s.lastIndexOf(" bytes free");
if (pos < 0)
throw new IOException(
"Unrecognized output from\n"
+ getCmdAsString(cmd)
+ ":\n"
+ s);
long exp = 1;
for (;;) {
pos--;
char ch = s.charAt(pos);
if (ch == ' ')
break;
if (ch == ',')
continue;
value += exp * (ch - '0');
exp *= 10;
}
} else {
StringTokenizer st = new StringTokenizer(s);
for (int i = 1; i < 11; i++) {
st.nextToken();
if (i == 10)
value = Long.parseLong(st.nextToken());
value *= 1024; // df -k gives us in kilobytes
}
}
return (value);
}