From: <ls...@us...> - 2009-04-05 05:50:54
|
Revision: 5214 http://jnode.svn.sourceforge.net/jnode/?rev=5214&view=rev Author: lsantha Date: 2009-04-05 05:50:44 +0000 (Sun, 05 Apr 2009) Log Message: ----------- Progress with the find command - by Alexander Kerner Modified Paths: -------------- trunk/fs/descriptors/org.jnode.fs.command.xml trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java Modified: trunk/fs/descriptors/org.jnode.fs.command.xml =================================================================== --- trunk/fs/descriptors/org.jnode.fs.command.xml 2009-04-04 08:46:39 UTC (rev 5213) +++ trunk/fs/descriptors/org.jnode.fs.command.xml 2009-04-05 05:50:44 UTC (rev 5214) @@ -115,15 +115,15 @@ </syntax> <syntax alias="find"> <sequence description="find files or directories"> - <repeat minCount="1"> - <argument argLabel="directory"/> + <repeat minCount="0"> + <argument argLabel="directory" description="directory to start searching from"/> </repeat> <optionSet> - <option argLabel="type" longName="type"/> - <option argLabel="maxdepth" longName="maxdepth"/> - <option argLabel="mindepth" longName="mindepth"/> - <option argLabel="name" longName="name"/> - <option argLabel="iname" longName="iname"/> + <option argLabel="type" longName="type" shortName="t" description="filter results to show only files of given type. valid types are 'd' for directory and 'f' for file"/> + <option argLabel="maxdepth" longName="maxdepth" shortName="D" description="descent at most to given level of directories"/> + <option argLabel="mindepth" longName="mindepth" shortName="d" description="ignore files and directories at levels less than given level"/> + <option argLabel="name" longName="name" shortName="n" description="filter results to show only files that match given pattern"/> + <option argLabel="iname" longName="iname" shortName="N" description="same like 'n', but case insensitive"/> </optionSet> </sequence> </syntax> Modified: trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java 2009-04-04 08:46:39 UTC (rev 5213) +++ trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java 2009-04-05 05:50:44 UTC (rev 5214) @@ -26,13 +26,15 @@ private volatile Long minDepth = null; public synchronized void walk(final File... dirs) throws IOException { + if (dirs == null) { + throw new NullPointerException("Directory to walk from must not be null"); + } for (File dir : dirs) { - if (dir == null) + if (dir == null || !dir.isDirectory()) throw new IOException("No such directroy " + dir); - dir = dir.getAbsoluteFile(); // to be able to handle relative paths - if (!dir.canRead() || !dir.isDirectory()) { - throw new IOException("Cannot read directroy " + dir); - } + dir = dir.getCanonicalFile(); // to be able to handle relative paths + // and . / .. + handleStartingDir(dir); stack.push(new FileObject(dir, 0L)); while (!cancelled && !stack.isEmpty()) { go1(stack.pop()); @@ -41,22 +43,29 @@ } private void go1(final FileObject file) throws IOException { - if ((minDepth != null && file.depth < minDepth) || (maxDepth != null && file.depth > maxDepth)) { + if ((minDepth != null && file.depth < minDepth) || + (maxDepth != null && file.depth > maxDepth)) { // out of boundaries - } else if (!file.file.canRead()) { - // ignore for now - } else if (validFileOrDirectory(file)) { + } else if (notFiltered(file)) { handleFileOrDir(file); } else { // filtered out } - go2(file); + try { + go2(file); + } catch (SecurityException e) { + handleRestrictedFile(file.file); + } } - private void go2(final FileObject file) throws IOException { + private void go2(final FileObject file) throws IOException, SecurityException { final Stack<File> stack = new Stack<File>(); final File[] content = file.file.listFiles(); - if (content != null) { + if (content == null) { + // I/O Error or file + } else if (content.length == 0) { + // dir is empty + } else { for (File f : content) { if (f.toString().equals(f.getCanonicalPath())) { stack.push(f); @@ -65,11 +74,10 @@ } } while (!stack.isEmpty()) { - File tmp = stack.pop(); - // addToStack(stack.pop(), file.depth + 1); - this.stack.push(new FileObject(tmp, file.depth + 1)); + this.stack.push(new FileObject(stack.pop(), file.depth + 1)); } } + } private void handleFileOrDir(final FileObject file) { @@ -82,7 +90,7 @@ } } - private boolean validFileOrDirectory(final FileObject file) { + private boolean notFiltered(final FileObject file) { if (!filters.isEmpty()) for (FileFilter filter : filters) if (!filter.accept(file.file)) @@ -106,8 +114,16 @@ filters.add(filter); } - public abstract void handleDir(File f); + protected void handleRestrictedFile(final File file) throws IOException { + throw new IOException("Permission denied for " + file); + } - public abstract void handleFile(File f); + protected void handleStartingDir(final File file) { + // do nothing + } + public abstract void handleDir(final File file); + + public abstract void handleFile(final File file); + } Modified: trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java 2009-04-04 08:46:39 UTC (rev 5213) +++ trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java 2009-04-05 05:50:44 UTC (rev 5214) @@ -18,6 +18,11 @@ private class Walker extends AbstractDirectoryWalker { @Override + protected void handleRestrictedFile(File file) throws IOException { + err.println("Permission denied for \"" + file + "\""); + } + + @Override public void handleDir(File f) { out.println(f); } @@ -29,15 +34,20 @@ } - private final StringArgument nameArg = new StringArgument("name", Argument.OPTIONAL); - private final StringArgument inameArg = new StringArgument("iname", Argument.OPTIONAL); - private final LongArgument maxdepthArg = new LongArgument("maxdepth", Argument.OPTIONAL); - private final LongArgument mindepthArg = new LongArgument("mindepth", Argument.OPTIONAL); - private final StringArgument typeArg = new StringArgument("type", Argument.OPTIONAL); - private final FileArgument dirArg = - new FileArgument("directory", Argument.MANDATORY | Argument.EXISTING - | Argument.MULTIPLE); + private final StringArgument nameArg = new StringArgument("name", Argument.OPTIONAL, + "filter results to show only files that match given pattern"); + private final StringArgument inameArg = new StringArgument("iname", Argument.OPTIONAL, + "same like 'name', but case insensitive"); + private final LongArgument maxdepthArg = new LongArgument("maxdepth", Argument.OPTIONAL, + "descent at most to given level of directories"); + private final LongArgument mindepthArg = new LongArgument("mindepth", Argument.OPTIONAL, + "ignore files and directories at levels less than given level"); + private final StringArgument typeArg = new StringArgument( "type", Argument.OPTIONAL, + "filter results to show only files of given type. valid types are 'd' for directory and 'f' for file"); + private final FileArgument dirArg = new FileArgument("directory", Argument.OPTIONAL | Argument.MULTIPLE, + "directory to start searching from"); private PrintWriter out = null; + private PrintWriter err = null; public FindCommand() { super("Find files and directories"); @@ -51,6 +61,7 @@ public void execute() throws IOException { out = getOutput().getPrintWriter(); + err = getError().getPrintWriter(); final Walker walker = new Walker(); if (maxdepthArg.isSet()) { @@ -103,6 +114,10 @@ }); } } - walker.walk(dirArg.getValues()); + if (dirArg.isSet()) { + walker.walk(dirArg.getValues()); + } else { + walker.walk(new File(System.getProperty("user.dir"))); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |