|
From: <cr...@us...> - 2008-05-24 13:36:47
|
Revision: 4123
http://jnode.svn.sourceforge.net/jnode/?rev=4123&view=rev
Author: crawley
Date: 2008-05-24 06:36:45 -0700 (Sat, 24 May 2008)
Log Message:
-----------
Converted DFCommand and added support for a device argument
Modified Paths:
--------------
trunk/fs/descriptors/org.jnode.fs.command.xml
trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java
Modified: trunk/fs/descriptors/org.jnode.fs.command.xml
===================================================================
--- trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-24 12:47:57 UTC (rev 4122)
+++ trunk/fs/descriptors/org.jnode.fs.command.xml 2008-05-24 13:36:45 UTC (rev 4123)
@@ -69,6 +69,10 @@
<repeat minCount="1"><argument argLabel="paths"/></repeat>
</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"/>
+ </syntax>
</extension>
Modified: trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java 2008-05-24 12:47:57 UTC (rev 4122)
+++ trunk/fs/src/fs/org/jnode/fs/command/DFCommand.java 2008-05-24 13:36:45 UTC (rev 4123)
@@ -1,72 +1,95 @@
+/*
+ * $Id: Help.java 3962 2008-04-17 14:33:02Z crawley $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library 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 Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
package org.jnode.fs.command;
+import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
-import java.io.IOException;
import javax.naming.NameNotFoundException;
-import org.apache.log4j.Logger;
import org.jnode.driver.Device;
import org.jnode.driver.DeviceManager;
import org.jnode.fs.FileSystem;
-import org.jnode.fs.jfat.FatFileSystem;
import org.jnode.fs.service.FileSystemService;
import org.jnode.naming.InitialNaming;
import org.jnode.shell.AbstractCommand;
import org.jnode.shell.CommandLine;
-import org.jnode.shell.help.Help;
-import org.jnode.shell.help.Syntax;
+import org.jnode.shell.syntax.Argument;
+import org.jnode.shell.syntax.DeviceArgument;
+/**
+ * The DF command prints disk usage information for devices with filesystems.
+ *
+ * @author ga...@jn...
+ * @author cr...@jn...
+ *
+ */
public class DFCommand extends AbstractCommand {
+
+ private final DeviceArgument ARG_DEVICE = new DeviceArgument(
+ "device", Argument.OPTIONAL,
+ "The device for which disk usage inforrmation should be displayed");
- private static final Logger log = Logger.getLogger ( DFCommand.class );
-
- public static Help.Info HELP_INFO =
- new Help.Info(
- "device",
- new Syntax[] {
- new Syntax("Print disk usage about all devices")});
-
- public void execute(CommandLine commandLine, InputStream in,
- PrintStream out, PrintStream err) throws Exception {
- //ParsedArguments cmdLine = HELP_INFO.parse(commandLine);
- print(out);
- }
-
- private void print(PrintStream out) throws NameNotFoundException {
- final DeviceManager dm = InitialNaming.lookup(DeviceManager.NAME);
- final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
- StringBuffer b = new StringBuffer();
- b.append("ID")
- .append("\t").append("Total")
- .append("\t").append("Use")
- .append("\t").append("Free")
- .append("\n");
- FileSystem fs;
- for (Device dev : dm.getDevices()) {
- // Is device contains a filesystem ?
- fs = fss.getFileSystem(dev);
- long total, free, use;
- if (fs != null) {
- log.debug("Check device : " + dev.getId());
- try {
- total = fs.getTotalSpace();
- if(total > 0){
- free = fs.getFreeSpace();
- use = total - free;
- b.append(dev.getId())
- .append("\t").append(total)
- .append("\t").append(use)
- .append("\t").append(free)
- .append("\n");
- }
- } catch (IOException x){
- b.append("\t").append("Error getting disk usage information.").append("\n");
+ public DFCommand() {
+ super("Print file system usage information");
+ registerArguments(ARG_DEVICE);
+ }
+
+ public void execute(CommandLine commandLine, InputStream in,
+ PrintStream out, PrintStream err) throws NameNotFoundException {
+ final FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
+ out.println("ID\tTotal\tUse\tFree");
+ if (ARG_DEVICE.isSet()) {
+ final Device dev = ARG_DEVICE.getValue();
+ FileSystem fs = fss.getFileSystem(dev);
+ if (fs == null) {
+ out.println("No filesystem on device");
+ }
+ else {
+ displayInfo(out, dev, fs);
+ }
+ }
+ 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);
}
}
- }
- out.print(b.toString());
- }
+ }
+ }
+ private void displayInfo(PrintStream out, Device dev, FileSystem fs) {
+ try {
+ long total = fs.getTotalSpace();
+ if (total > 0) {
+ long free = fs.getFreeSpace();
+ long use = total - free;
+ out.println(dev.getId() + "\t" + total + "\t" + use + "\t" + free);
+ }
+ } catch (IOException ex) {
+ out.println("\tError getting disk usage information: " + ex.getLocalizedMessage());
+ }
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|