|
From: <chr...@us...> - 2009-05-01 07:09:30
|
Revision: 5376
http://jnode.svn.sourceforge.net/jnode/?rev=5376&view=rev
Author: chrisboertien
Date: 2009-05-01 07:09:28 +0000 (Fri, 01 May 2009)
Log Message:
-----------
Improved df impl
Support for -h/-H/-B/-k switches
Signed-off-by: chrisboertien <chr...@gm...>
Modified Paths:
--------------
trunk/cli/descriptors/org.jnode.command.file.xml
trunk/cli/src/commands/org/jnode/command/file/DFCommand.java
Modified: trunk/cli/descriptors/org.jnode.command.file.xml
===================================================================
--- trunk/cli/descriptors/org.jnode.command.file.xml 2009-05-01 06:59:13 UTC (rev 5375)
+++ trunk/cli/descriptors/org.jnode.command.file.xml 2009-05-01 07:09:28 UTC (rev 5376)
@@ -100,8 +100,31 @@
</sequence>
</syntax>
<syntax alias="df">
- <empty description="display disk usage for all filesystems"/>
- <argument argLabel="device" description="display disk usage for the filesystem on a device"/>
+ <sequence description="display disk usage for file systems">
+ <optionSet>
+ <option argLabel="human-read-bin" shortName="h" longName="human-readable"/>
+ <option argLabel="human-read-dec" shortName="H" longName="si"/>
+ <option argLabel="show-all" shortName="a" longName="all"/>
+ <option argLabel="block-size" shortName="B" longName="block-size"/>
+ <option argLabel="block-size-1k" shortName="k"/>
+ <!--
+ <option argLabel="posix" shortName="P" longName="portability"/>
+ <option argLabel="inodes" shortName="i" longName="inodes"/>
+ <option argLabel="local" shortName="l" longName="local"/>
+ <option argLabel="sync" longName="sync"/>
+ <option argLabel="no-sync" longName="no-sync"/>
+ <option argLabel="fs-type" shortName="t" longName="type"/>
+ <option argLabel="print-type" shortName="T" longName="print-type"/>
+ <option argLabel="ex-type" shortName="x" longName="exclude-type"/>
+ -->
+ </optionSet>
+ <optional>
+ <alternatives>
+ <argument argLabel="path"/>
+ <argument argLabel="device"/>
+ </alternatives>
+ </optional>
+ </sequence>
</syntax>
<syntax alias="dir">
<empty description="list the current directory"/>
@@ -231,7 +254,9 @@
<optional eager="true">
<option argLabel="recursive" shortName="r" longName="recursive"/>
</optional>
- <repeat minCount="1"><argument argLabel="paths"/></repeat>
+ <repeat minCount="1">
+ <argument argLabel="paths"/>
+ </repeat>
</sequence>
</syntax>
<syntax alias="mkdir">
@@ -297,5 +322,14 @@
<permission class="java.io.RuntimePermission" name="writeFileDescriptor"/>
<permission class="java.util.PropertyPermission" name="user.dir" actions="read,write"/>
<permission class="java.util.PropertyPermission" name="user.home" actions="read,write"/>
+ <permission class="java.lang.RuntimePermission" name="getenv.*" actions="read"/>
+ <!--
+ <permission class="java.lang.RuntimePermission" name="getenv.POSIXLY_CORRECT" actions="read"/>
+ <permission class="java.lang.RuntimePermission" name="getenv.DF_BLOCK_SIZE" actions="read"/>
+ <permission class="java.lang.RuntimePermission" name="genenv.DU_BLOCK_SIZE" actions="read"/>
+ <permission class="java.lang.RuntimePermission" name="getenv.LS_BLOCK_SIZE" actions="read"/>
+ <permission class="java.lang.RuntimePermission" name="getenv.BLOCK_SIZE" actions="read"/>
+ <permission class="java.lang.RuntimePermission" name="getenv.BLOCKSIZE" actions="read"/>
+ -->
</extension>
</plugin>
Modified: trunk/cli/src/commands/org/jnode/command/file/DFCommand.java
===================================================================
--- trunk/cli/src/commands/org/jnode/command/file/DFCommand.java 2009-05-01 06:59:13 UTC (rev 5375)
+++ trunk/cli/src/commands/org/jnode/command/file/DFCommand.java 2009-05-01 07:09:28 UTC (rev 5376)
@@ -26,6 +26,8 @@
import javax.naming.NameNotFoundException;
+import java.io.File;
+
import org.jnode.driver.Device;
import org.jnode.driver.DeviceManager;
import org.jnode.fs.FileSystem;
@@ -34,6 +36,10 @@
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.syntax.Argument;
import org.jnode.shell.syntax.DeviceArgument;
+import org.jnode.shell.syntax.FileArgument;
+import org.jnode.shell.syntax.FlagArgument;
+import org.jnode.shell.syntax.IntegerArgument;
+import org.jnode.util.NumberUtils;
/**
* The DF command prints disk usage information for devices with filesystems.
@@ -41,13 +47,21 @@
* @author ga...@jn...
* @author cr...@jn...
* @author Levente S\u00e1ntha
+ * @author chris boertien
*/
public class DFCommand extends AbstractCommand {
private static final String help_device = "The device for which disk usage information should be displayed";
+ private static final String help_path = "Display disk usage info for the file system that contains this path";
+ private static final String help_read_dec = "Print output in human readable decimal form (1000)";
+ private static final String help_read_bin = "Print output in human readable binary form (1024)";
+ private static final String help_all = "Show all file systems, even pseudo file systems";
+ private static final String help_block_1k = "Same as -B 1024";
+ private static final String help_block = "Print output with a specified block size";
private static final String help_super = "Print file system usage information";
private static final String str_id = "ID";
private static final String str_size = "Size";
+ private static final String str_blocks = "blocks";
private static final String str_used = "Used";
private static final String str_free = "Free";
private static final String str_mount = "Mount";
@@ -55,44 +69,110 @@
private static final String str_unknown = "unknown";
private static final String err_get_info = "\tError getting disk usage information for %s on %s : %s%n";
+ private static final int OUT_BINARY = 1;
+ private static final int OUT_DECIMAL = 2;
+ private static final int OUT_BLOCKS = 3;
+
+ private static final int DEFAULT_BLOCK_SIZE = 1024;
+
private final DeviceArgument argDevice;
-
+ private final FileArgument argPath;
+ private final FlagArgument argReadDec;
+ private final FlagArgument argReadBin;
+ private final FlagArgument argAll;
+ private final FlagArgument argBlock1k;
+ private final IntegerArgument argBlock;
+
+ private StringBuilder line;
+ private FileSystemService fss;
+ private DeviceManager dm;
+ private Map<String, String> mountPoints;
+ private PrintWriter out;
+ private int outputType;
+ private int blockSize;
+ private boolean all;
+
public DFCommand() {
super(help_super);
- argDevice = new DeviceArgument("device", Argument.OPTIONAL | Argument.EXISTING, help_device);
- registerArguments(argDevice);
+ argDevice = new DeviceArgument("device", Argument.EXISTING, help_device);
+ argPath = new FileArgument("path", Argument.EXISTING, help_path);
+ argReadDec = new FlagArgument("human-read-dec", 0, help_read_dec);
+ argReadBin = new FlagArgument("human-read-bin", 0, help_read_bin);
+ argAll = new FlagArgument("show-all", 0, help_all);
+ argBlock1k = new FlagArgument("block-size-1k", 0, help_block_1k);
+ argBlock = new IntegerArgument("block-size", 0, help_block);
+ registerArguments(argDevice, argPath, argReadDec, argReadBin, argAll, argBlock1k, argBlock);
}
-
- public void execute() throws NameNotFoundException {
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
- final Map<String, String> mountPoints = fss.getDeviceMountPoints();
- PrintWriter out = getOutput().getPrintWriter(false);
- format(out, str_id, true);
- format(out, str_size, false);
- format(out, str_used, false);
- format(out, str_free, false);
- out.println(str_mount);
- out.println();
- if (argDevice.isSet()) {
- final Device dev = argDevice.getValue();
- FileSystem<?> fs = fss.getFileSystem(dev);
- if (fs == null) {
- out.println(str_no_fs);
- } else {
- displayInfo(out, dev, fs, mountPoints.get(fs.getDevice().getId()));
- }
+
+ public void execute() throws NameNotFoundException, IOException {
+ parseOptions();
+ fss = InitialNaming.lookup(FileSystemService.NAME);
+ dm = InitialNaming.lookup(DeviceManager.NAME);
+ mountPoints = fss.getDeviceMountPoints();
+ out = getOutput().getPrintWriter(true);
+ line = new StringBuilder();
+
+ Device device = null;
+
+ printHeader();
+
+ if (argPath.isSet()) {
+ device = getDeviceForPath(argPath.getValue());
+ } else if (argDevice.isSet()) {
+ device = argDevice.getValue();
} else {
- final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
for (Device dev : dm.getDevices()) {
FileSystem<?> fs = fss.getFileSystem(dev);
if (fs != null) {
displayInfo(out, dev, fs, mountPoints.get(fs.getDevice().getId()));
}
}
+ out.flush();
+ exit(0);
}
- out.flush();
+ if (device != null) {
+ FileSystem<?> fs = fss.getFileSystem(device);
+ if (fs == null) {
+ out.println(str_no_fs);
+ } else {
+ displayInfo(out, device, fs, mountPoints.get(fs.getDevice().getId()));
+ }
+ out.flush();
+ exit(0);
+ }
}
-
+
+ private Device getDeviceForPath(File file) throws IOException {
+ String path = file.getCanonicalPath();
+ String mp = null;
+ for (String mountPoint : fss.getMountPoints().keySet()) {
+ if (path.startsWith(mountPoint)) {
+ if (mp != null) {
+ if (!mp.startsWith(mountPoint)) {
+ continue;
+ }
+ }
+ mp = mountPoint;
+ }
+ }
+ if (mp == null) {
+ throw new AssertionError("No fs device for " + path);
+ }
+ return fss.getMountPoints().get(mp).getDevice();
+ }
+
+ private void printHeader() {
+ format(out, str_id, true);
+ if (outputType == OUT_BLOCKS) {
+ format(out, String.format("%s-%s", NumberUtils.toBinaryByte(blockSize), str_blocks), false);
+ } else {
+ format(out, str_size, false);
+ }
+ format(out, str_used, false);
+ format(out, str_free, false);
+ out.println(str_mount);
+ }
+
/**
* @param out
* @param dev
@@ -101,27 +181,27 @@
*/
private void displayInfo(PrintWriter out, Device dev, FileSystem<?> fs, String mountPoint) {
try {
-
String str = dev.getId();
+ long total = fs.getTotalSpace();
+ long free = fs.getFreeSpace();
+
format(out, str, true);
-
- final long total = fs.getTotalSpace();
- str = total < 0 ? str_unknown : String.valueOf(total);
+
+ str = total < 0 ? str_unknown : valueOf(total, true);
format(out, str, false);
-
- final long free = fs.getFreeSpace();
- str = total < 0 ? str_unknown : String.valueOf(total - free);
+
+ str = total < 0 ? str_unknown : valueOf(total - free, true);
format(out, str, false);
-
- str = free < 0 ? str_unknown : String.valueOf(free);
+
+ str = free < 0 ? str_unknown : valueOf(free, false);
format(out, str, false);
-
+
out.println(mountPoint);
} catch (IOException ex) {
out.format(err_get_info, mountPoint, dev.getId(), ex.getLocalizedMessage());
}
}
-
+
private void format(PrintWriter out, String str, boolean left) {
int ln;
ln = 15 - str.length();
@@ -138,4 +218,70 @@
}
out.print(' ');
}
+
+ private String valueOf(long size, boolean up) {
+ switch(outputType) {
+ case OUT_DECIMAL :
+ return NumberUtils.toDecimalByte(size, 0);
+ case OUT_BINARY :
+ return NumberUtils.toBinaryByte(size, 0);
+ case OUT_BLOCKS :
+ return toBlock(size, blockSize, up);
+ default :
+ return String.valueOf(size);
+ }
+ }
+
+ private String toBlock(long size, long blockSize, boolean up) {
+ return String.valueOf(size / blockSize + ((up && ((size % blockSize) > 0)) ? 1 : 0));
+ }
+
+ private void parseOptions() {
+ if (argReadDec.isSet()) {
+ outputType = OUT_DECIMAL;
+ } else if (argReadBin.isSet()) {
+ outputType = OUT_BINARY;
+ } else {
+ outputType = OUT_BLOCKS;
+ }
+
+ all = argAll.isSet();
+
+ if (argBlock1k.isSet()) {
+ blockSize = 1024;
+ } else if (argBlock.isSet()) {
+ blockSize = argBlock.getValue();
+ } else {
+ blockSize = getDefaultBlock();
+ }
+ }
+
+ private int getDefaultBlock() {
+ /* Env vars are broken
+ String DF_BLOCK_SIZE = System.getenv("DF_BLOCK_SIZE");
+ String BLOCK_SIZE = System.getenv("BLOCK_SIZE");
+ String BLOCKSIZE = System.getenv("BLOCKSIZE");
+ String POSIXLY_CORRECT = System.getenv("POSIXLY_CORRECT");
+
+ String size = null;
+ if (DF_BLOCK_SIZE != null) {
+ size = DF_BLOCK_SIZE;
+ } else if (BLOCK_SIZE != null) {
+ size = BLOCK_SIZE;
+ } else if (BLOCKSIZE != null) {
+ size = BLOCKSIZE;
+ } else if (POSIXLY_CORRECT != null) {
+ return 512;
+ } else {
+ return DEFAULT_BLOCK_SIZE;
+ }
+
+ try {
+ return Integer.parseInt(size);
+ } catch (NumberFormatException e) {
+ return DEFAULT_BLOCK_SIZE;
+ }
+ */
+ return DEFAULT_BLOCK_SIZE;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|