### Eclipse Workspace Patch 1.0 #P jnode Index: fs/src/fs/org/jnode/fs/command/DuCommand.java =================================================================== --- fs/src/fs/org/jnode/fs/command/DuCommand.java (revision 0) +++ fs/src/fs/org/jnode/fs/command/DuCommand.java (revision 0) @@ -0,0 +1,219 @@ +package org.jnode.fs.command; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.Map.Entry; + +import org.jnode.shell.AbstractCommand; +import org.jnode.shell.syntax.Argument; +import org.jnode.shell.syntax.FileArgument; +import org.jnode.shell.syntax.FlagArgument; + +public class DuCommand extends AbstractCommand { + + private class FileSizePrinter { + private static final String POST_FIX_B = "B"; + private static final String POST_FIX_K = "K"; + private static final String POST_FIX_M = "M"; + private static final String POST_FIX_G = "G"; + private final long oneK; + private final long oneM; + private final long oneG; + private final long size; + + FileSizePrinter(long size, boolean usePowerOf1000) { + if (usePowerOf1000) + oneK = 1000L; + else + oneK = 1024L; + + oneM = oneK * oneK; + oneG = oneK * oneM; + this.size = size; + } + + FileSizePrinter(File file) { + oneK = 1024L; + oneM = oneK * oneK; + oneG = oneK * oneM; + this.size = file.length(); + } + + public String toString() { + String postFix = POST_FIX_B; + long sizeCp = size; + if (sizeCp / oneG > 0) { + postFix = POST_FIX_G; + sizeCp = sizeCp / oneG; + } else if (sizeCp / oneM > 0) { + postFix = POST_FIX_M; + sizeCp = sizeCp / oneM; + } else if (sizeCp / oneK > 0) { + postFix = POST_FIX_K; + sizeCp = sizeCp / oneK; + } else { + // nothing + } + return sizeCp + postFix; + } + } + + private abstract class Walker extends AbstractDirectoryWalker { + + protected final TreeMap map = new TreeMap(); + protected final boolean powerOf1000; + protected final boolean humanReadable; + + Walker(boolean humanReadable, boolean powerOf1000) { + this.powerOf1000 = powerOf1000; + this.humanReadable = humanReadable; + } + + @Override + public void handleDir(File file) { + handleAll(file); + } + + @Override + public void handleFile(File file) { + handleAll(file); + } + + @Override + protected void handleRestrictedFile(File file) throws IOException { + err.println("Permission denied for \"" + file + "\""); + } + + private void handleAll(File file) { + map.put(file, file.length()); + } + + protected TreeMap summariseIt(TreeMap map) { + TreeMap result = new TreeMap(); + NavigableMap navMap = map.descendingMap(); + Long tmpSize = 0L; + while (navMap.size() != 0) { + Entry e = navMap.pollFirstEntry(); + File key = e.getKey(); + Long value = e.getValue(); + tmpSize += key.length(); + + if (key.isFile()) { + result.put(key, value); + } else if (key.isDirectory()) { + result.put(key, tmpSize); + } else { + // ignore unknown file type + } + } + return result; + } + } + + private class AllWalker extends Walker { + + AllWalker(boolean humanReadable, boolean powerOf1000) { + super(humanReadable, powerOf1000); + } + + @Override + protected void lastAction(boolean wasCancelled) { + Map summarisedMap = summariseIt(map); + for (Entry e : summarisedMap.entrySet()) { + if(humanReadable) + out.println(new FileSizePrinter(e.getValue(), powerOf1000) + "\t" + e.getKey()); + else + out.println(e.getValue() + "\t" + e.getKey()); + } + } + } + + private class OnlyDirsWalker extends Walker { + + OnlyDirsWalker(boolean humanReadable, boolean powerOf1000) { + super(humanReadable, powerOf1000); + } + + @Override + protected void lastAction(boolean wasCancelled) { + Map summarisedMap = summariseIt(map); + for (Entry e : summarisedMap.entrySet()) { + if (e.getKey().isDirectory()) + if(humanReadable) + out.println(new FileSizePrinter(e.getValue(), powerOf1000) + "\t" + e.getKey()); + else + out.println(e.getValue() + "\t" + e.getKey()); + } + } + } + + private class TotalWalker extends Walker { + + TotalWalker(boolean humanReadable, boolean powerOf1000) { + super(humanReadable, powerOf1000); + } + + @Override + protected void lastAction(boolean wasCancelled) { + TreeMap summarisedMap = summariseIt(map); + Entry e = summarisedMap.firstEntry(); + if(humanReadable) + out.println(new FileSizePrinter(e.getValue(), powerOf1000) + "\t" + e.getKey()); + else + out.println(e.getValue() + "\t" + e.getKey()); + + } + } + + private PrintWriter out; + private PrintWriter err; + + private static final String HELP_TOTAL = "display only a total for each argument"; + private static final String HELP_ALL = "write counts for all files, not just directories"; + private static final String HELP_SUPER = "print file sizes"; + private static final String HELP_POWER_OF_1000 = ""; + private static final String HELP_DIR = "directory to start printing sizes"; + private static final String HELP_HUMAN_READABLE = "print sizes in human readable format (e.g., 1K 234M 2G)"; + + private final FlagArgument totalArg = + new FlagArgument("total", Argument.OPTIONAL, HELP_TOTAL); + private final FlagArgument allArg = + new FlagArgument("all", Argument.OPTIONAL, HELP_ALL); + private final FlagArgument powerOf1000Arg = + new FlagArgument("power-of-1000", Argument.OPTIONAL, HELP_POWER_OF_1000); + private final FileArgument dirArg + = new FileArgument("directory", Argument.OPTIONAL | Argument.MULTIPLE, HELP_DIR); + private final FlagArgument humanReadableArg = new FlagArgument("human-readable", Argument.OPTIONAL, HELP_HUMAN_READABLE); + + public DuCommand() { + super(HELP_SUPER); + registerArguments(totalArg, allArg, powerOf1000Arg, humanReadableArg, dirArg ); + } + + public static void main(String[] args) throws IOException { + new FindCommand().execute(); + } + + public void execute() throws IOException { + out = getOutput().getPrintWriter(); + err = getError().getPrintWriter(); + Walker walker = null; + if (totalArg.isSet()) { + walker = new TotalWalker(humanReadableArg.isSet(), powerOf1000Arg.isSet()); + } else if (allArg.isSet()) { + walker = new AllWalker(humanReadableArg.isSet(), powerOf1000Arg.isSet()); + } else { + walker = new OnlyDirsWalker(humanReadableArg.isSet(), powerOf1000Arg.isSet()); + } + + if (dirArg.isSet()) { + walker.walk(dirArg.getValues()); + } else { + walker.walk(new File(System.getProperty("user.dir"))); + } + } +} Index: fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java =================================================================== --- fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java (revision 5350) +++ fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java (working copy) @@ -147,6 +147,7 @@ while (!cancelled && !stack.isEmpty()) { handle(stack.pop()); } + lastAction(cancelled); } } @@ -253,7 +254,7 @@ /** * This method is called, when access to a file was denied.
* Default implementation will rise a IOException instead of - * SecurityException. Maybe overridden by extending classes to + * SecurityException. May be overridden by extending classes to * do something else. * * @param file File-object, to which access was restricted. @@ -265,7 +266,7 @@ /** * This method is called, when walking is about to start.
- * By default, it does nothing. Maybe overridden by extending classes to do + * By default, it does nothing. May be overridden by extending classes to do * something else. * * @param file File-object, that represents starting dir. @@ -274,6 +275,15 @@ protected void handleStartingDir(final File file) throws IOException { // do nothing by default } + + /** + * This method is called, when walking has finished.
+ * By default, it does nothing. May be overridden by extending classes to do something else. + * @param wasCancelled true, if directory walking was aborted. + */ + protected void lastAction(boolean wasCancelled){ + // do nothing by default + } /** * Index: fs/descriptors/org.jnode.fs.command.xml =================================================================== --- fs/descriptors/org.jnode.fs.command.xml (revision 5350) +++ fs/descriptors/org.jnode.fs.command.xml (working copy) @@ -26,6 +26,7 @@ + @@ -76,6 +77,19 @@ + + + + + + + + + Index: fs/src/fs/org/jnode/fs/command/FindCommand.java =================================================================== --- fs/src/fs/org/jnode/fs/command/FindCommand.java (revision 5350) +++ fs/src/fs/org/jnode/fs/command/FindCommand.java (working copy) @@ -89,6 +89,7 @@ new FindCommand().execute(); } + @Override public void execute() throws IOException { out = getOutput().getPrintWriter(); err = getError().getPrintWriter();