Revision: 5289 http://jnode.svn.sourceforge.net/jnode/?rev=5289&view=rev Author: chrisboertien Date: 2009-04-15 21:03:43 +0000 (Wed, 15 Apr 2009)
Log Message: ----------- Updates to ADW and FindCommand javadoc by Alexander Kerner Added a walk(List<File) method to ADW and changed the behavior of ADW to not rewrite pathnames into canonical form. If canonical form is wanted then the caller should do this before, otherwise it is likely unexpected. + checkstyle fixes Signed-off-by: chrisboertien <chris.boert...@gmail.com> Modified Paths: -------------- trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java trunk/shell/src/shell/org/jnode/shell/command/GrepCommand.java trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java Modified: trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java 2009-04-15 15:01:23 UTC (rev 5288) +++ trunk/fs/src/fs/org/jnode/fs/command/AbstractDirectoryWalker.java 2009-04-15 21:03:43 UTC (rev 5289) @@ -1,3 +1,23 @@ +/* + * $Id: CdCommand.java 4975 2009-02-02 08:30:52Z lsantha $ + * + * Copyright (C) 2003-2009 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.File; @@ -6,7 +26,23 @@ import java.util.HashSet; import java.util.Set; import java.util.Stack; +import java.util.List; +/** + * <p> + * <code>AbstractDirectoryWalker</code> - walk through a directory hierarchy + * recursively + * </p> + * <code>AbstractDirectoryWalker</code> will start at a given starting depth + * relatively to the given directory and walk recursively through the directory + * hierarchy until stopping depth is reached. <br> + * On its way, it will call "handleFile()" and "handleDir()" for every file and + * directory, that is not filtered out by any of the filteres set for this + * DirectoryWalker. + * + * @author Alexander Kerner + * + */ public abstract class AbstractDirectoryWalker { private class FileObject { @@ -25,24 +61,36 @@ private volatile Long maxDepth = null; private volatile Long minDepth = null; + /** + * Main method to walk through directory hierarchy. + * + * @param dirs Array of <code>File</code> to walk through. + * @throws IOException if any IO error occurs. + */ public synchronized void walk(final File... dirs) throws IOException { - if (dirs == null) { + if (dirs == null || dirs.length == 0) { throw new NullPointerException("Directory to walk from must not be null"); } for (File dir : dirs) { if (dir == null || !dir.isDirectory()) - throw new IOException("No such directroy " + dir); - dir = dir.getCanonicalFile(); // to be able to handle relative paths - // and . / .. + throw new IOException("No such directory " + dir); + + /* See note in handleChilds() + dir = dir.getCanonicalPath(); + */ handleStartingDir(dir); stack.push(new FileObject(dir, 0L)); while (!cancelled && !stack.isEmpty()) { - go1(stack.pop()); + handle(stack.pop()); } } } - - private void go1(final FileObject file) throws IOException { + + public synchronized void walk(final List<File> dirs) throws IOException { + walk(dirs.toArray(new File[0])); + } + + private void handle(final FileObject file) throws IOException { if ((minDepth != null && file.depth < minDepth) || (maxDepth != null && file.depth > maxDepth)) { // out of boundaries @@ -52,13 +100,14 @@ // filtered out } try { - go2(file); + handleChilds(file); } catch (SecurityException e) { + // Exception rises, when access to folder content was denied handleRestrictedFile(file.file); } } - private void go2(final FileObject file) throws IOException, SecurityException { + private void handleChilds(final FileObject file) throws IOException, SecurityException { final Stack<File> stack = new Stack<File>(); final File[] content = file.file.listFiles(); if (content == null) { @@ -67,11 +116,18 @@ // dir is empty } else { for (File f : content) { + /* I dont think is the right way to handle this. getCanonicalPath() + * does more than just trim symlinks, and symlinks aren't something + * we need to worry about. Even when we do we should have a lower + * level API to work with. + * - Chris if (f.toString().equals(f.getCanonicalPath())) { stack.push(f); } else { // dont follow symlinks } + */ + stack.push(f); } while (!stack.isEmpty()) { this.stack.push(new FileObject(stack.pop(), file.depth + 1)); @@ -80,7 +136,7 @@ } - private void handleFileOrDir(final FileObject file) { + private void handleFileOrDir(final FileObject file) throws IOException { if (file.file.isDirectory()) handleDir(file.file); else if (file.file.isFile()) @@ -98,32 +154,77 @@ return true; } - public void stoppWalking() { + /** + * Abort walking. + */ + public void stopWalking() { cancelled = true; } + /** + * + * @param min starting depth at which actual action performing is started. + */ public void setMinDepth(Long min) { minDepth = min; } + /** + * + * @param max ending depth at which actual action performing is stopped. + */ public void setMaxDepth(Long max) { maxDepth = max; } + /** + * + * @param filter <code>FileFilter</code> to be added to this + * DirectoryWalkers FilterSet. + */ public synchronized void addFilter(FileFilter filter) { filters.add(filter); } + /** + * This method is called, when access to a file was denied.<br> + * Default implementation will rise a <code>IOException</code> instead of + * <code>SecurityException</code>. Maybe overridden by extending classes to + * do something else. + * + * @param file <code>File</code>-object, to which access was restricted. + * @throws IOException in default implementation. + */ protected void handleRestrictedFile(final File file) throws IOException { throw new IOException("Permission denied for " + file); } - protected void handleStartingDir(final File file) { - // do nothing + /** + * This method is called, when walking is about to start.<br> + * By default, it does nothing. Maybe overridden by extending classes to do + * something else. + * + * @param file <code>File</code>-object, that represents starting dir. + * @throws IOException if IO error occurs. + */ + protected void handleStartingDir(final File file) throws IOException { + // do nothing by default } - public abstract void handleDir(final File file); + /** + * + * @param file <code>File</code>-object, that represents current directory. <br> + * Override this, to do some actions on this directory. + * @throws IOException if IO error occurs. + */ + public abstract void handleDir(final File file) throws IOException; - public abstract void handleFile(final File file); + /** + * + * @param file <code>File</code>-object, that represents current file. <br> + * Override this, to do some actions on this file. + * @throws IOException if IO error occurs. + */ + public abstract void handleFile(final File file) throws IOException; } Modified: trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java 2009-04-15 15:01:23 UTC (rev 5288) +++ trunk/fs/src/fs/org/jnode/fs/command/FindCommand.java 2009-04-15 21:03:43 UTC (rev 5289) @@ -1,3 +1,23 @@ +/* + * $Id: CdCommand.java 4975 2009-02-02 08:30:52Z lsantha $ + * + * Copyright (C) 2003-2009 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.File; @@ -13,6 +33,13 @@ import org.jnode.shell.syntax.LongArgument; import org.jnode.shell.syntax.StringArgument; +/** + * <code>FindCommand</code> - search for files in a directory hierarchy + * + * @author Alexander Kerner + * @see AbstractDirectoryWalker + * + */ public class FindCommand extends AbstractCommand { private class Walker extends AbstractDirectoryWalker { @@ -31,21 +58,27 @@ public void handleFile(File f) { out.println(f); } - } - 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 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; @@ -55,7 +88,6 @@ } public static void main(String[] args) throws IOException { - new FindCommand().execute(); } Modified: trunk/shell/src/shell/org/jnode/shell/command/GrepCommand.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/GrepCommand.java 2009-04-15 15:01:23 UTC (rev 5288) +++ trunk/shell/src/shell/org/jnode/shell/command/GrepCommand.java 2009-04-15 21:03:43 UTC (rev 5289) @@ -39,18 +39,14 @@ import java.util.Deque; import java.util.List; -import org.jnode.shell.CommandLine.Token; import org.jnode.shell.AbstractCommand; import org.jnode.shell.syntax.Argument; import org.jnode.shell.syntax.FileArgument; import org.jnode.shell.syntax.FlagArgument; import org.jnode.shell.syntax.IntegerArgument; import org.jnode.shell.syntax.StringArgument; -import org.jnode.shell.syntax.CommandSyntaxException; /** - * FIXME SHELL : shell expands globbers within quotes. - * * TODO check performance of prefixing, probably needs some buffering. * TODO implement outputting context lines (requires buffering output lines) * TODO implement Fixed/Basic/Ext matchers Modified: trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java 2009-04-15 15:01:23 UTC (rev 5288) +++ trunk/shell/src/shell/org/jnode/shell/syntax/FileArgument.java 2009-04-15 21:03:43 UTC (rev 5289) @@ -190,7 +190,7 @@ return ALLOW_DODGY_NAMES; } else if (name.equals("HYPHEN_IS_SPECIAL")) { return HYPHEN_IS_SPECIAL; - } else{ + } else { return super.nameToFlag(name); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ This SF.net email is sponsored by: High Quality Requirements in a Collaborative Environment. Download a free trial of Rational Requirements Composer Now! http://p.sf.net/sfu/www-ibm-com _______________________________________________ Jnode-svn-commits mailing list Jnode-svn-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jnode-svn-commits