add -i (inode format) support to df command

This commit is contained in:
Doug MacEachern 2010-02-09 18:01:34 -08:00
parent a210aa7e23
commit e20e2eaa54
1 changed files with 58 additions and 22 deletions

View File

@ -48,8 +48,19 @@ public class Df extends SigarCommandBase {
"Mounted on", "Mounted on",
"Type" "Type"
}; };
//df -i
private static final String[] IHEADER = new String[] {
"Filesystem",
"Inodes",
"IUsed",
"IFree",
"IUse%",
"Mounted on",
"Type"
};
private GetlineCompleter completer; private GetlineCompleter completer;
private boolean opt_i;
public Df(Shell shell) { public Df(Shell shell) {
super(shell); super(shell);
@ -67,7 +78,7 @@ public class Df extends SigarCommandBase {
} }
protected boolean validateArgs(String[] args) { protected boolean validateArgs(String[] args) {
return args.length <= 1; return true;
} }
public String getSyntaxArgs() { public String getSyntaxArgs() {
@ -79,31 +90,42 @@ public class Df extends SigarCommandBase {
} }
public void printHeader() { public void printHeader() {
printf(HEADER); printf(this.opt_i ? IHEADER : HEADER);
} }
public void output(String[] args) throws SigarException { public void output(String[] args) throws SigarException {
if (args.length == 1) { this.opt_i = false;
ArrayList sys = new ArrayList();
if (args.length > 0) {
FileSystemMap mounts = this.proxy.getFileSystemMap(); FileSystemMap mounts = this.proxy.getFileSystemMap();
String name = FileCompleter.expand(args[0]); for (int i=0; i<args.length; i++) {
FileSystem fs = mounts.getMountPoint(name); String arg = args[i];
if (arg.equals("-i")) {
this.opt_i = true;
continue;
}
String name = FileCompleter.expand(arg);
FileSystem fs = mounts.getMountPoint(name);
if (fs != null) { if (fs == null) {
printHeader(); throw new SigarException(arg +
output(fs); " No such file or directory");
return; }
sys.add(fs);
} }
throw new SigarException(args[0] +
" No such file or directory");
} }
else { if (sys.size() == 0) {
FileSystem[] fslist = this.proxy.getFileSystemList(); FileSystem[] fslist = this.proxy.getFileSystemList();
printHeader();
for (int i=0; i<fslist.length; i++) { for (int i=0; i<fslist.length; i++) {
output(fslist[i]); sys.add(fslist[i]);
} }
} }
printHeader();
for (int i=0; i<sys.size(); i++) {
output((FileSystem)sys.get(i));
}
} }
public void output(FileSystem fs) throws SigarException { public void output(FileSystem fs) throws SigarException {
@ -119,12 +141,26 @@ public class Df extends SigarCommandBase {
} }
} }
usage = this.sigar.getFileSystemUsage(fs.getDirName()); usage = this.sigar.getFileSystemUsage(fs.getDirName());
if (this.opt_i) {
used = usage.getFiles() - usage.getFreeFiles();
avail = usage.getFreeFiles();
total = usage.getFiles();
if (total == 0) {
pct = 0;
}
else {
long u100 = used * 100;
pct = u100 / total +
((u100 % total != 0) ? 1 : 0);
}
}
else {
used = usage.getTotal() - usage.getFree();
avail = usage.getAvail();
total = usage.getTotal();
used = usage.getTotal() - usage.getFree(); pct = (long)(usage.getUsePercent() * 100);
avail = usage.getAvail(); }
total = usage.getTotal();
pct = (long)(usage.getUsePercent() * 100);
} catch (SigarException e) { } catch (SigarException e) {
//e.g. on win32 D:\ fails with "Device not ready" //e.g. on win32 D:\ fails with "Device not ready"
//if there is no cd in the drive. //if there is no cd in the drive.
@ -152,8 +188,8 @@ public class Df extends SigarCommandBase {
printf(items); printf(items);
} }
private static String formatSize(long size) { private String formatSize(long size) {
return Sigar.formatSize(size * 1024); return this.opt_i ? String.valueOf(size) : Sigar.formatSize(size * 1024);
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {