/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Simple file tree walker that works in a similar manner to nftw(3C).
*
* @see Files#walkFileTree
*/
class FileTreeWalker {
private final boolean followLinks;
private final int maxDepth;
int maxDepth)
{
boolean fl = false;
// will throw NPE if options contains null
switch (option) {
case FOLLOW_LINKS : fl = true; break;
default:
throw new AssertionError("Should not get here");
}
}
this.followLinks = fl;
}
/**
* Walk file tree starting at the given file
*/
0,
new ArrayList<AncestorDirectory>());
}
/**
* @param file
* the directory to visit
* @param depth
* depth remaining
* @param ancestors
* use when cycle detection is enabled
*/
int depth,
throws IOException
{
// if attributes are cached then use them if possible
if ((depth > 0) &&
(file instanceof BasicFileAttributesHolder) &&
{
}
// attempt to get attributes of file. If fails and we are following
// links then a link target might not exist so get attributes of link
try {
try {
} catch (IOException x1) {
if (followLinks) {
try {
BasicFileAttributes.class,
} catch (IOException x2) {
}
} else {
}
}
} catch (SecurityException x) {
// If access to starting file is denied then SecurityException
// is thrown, otherwise the file is ignored.
if (depth == 0)
throw x;
return FileVisitResult.CONTINUE;
}
}
// unable to get attributes of file
}
// at maximum depth or file is not a directory
}
// check for cycles when following links
if (followLinks) {
// if this directory and ancestor has a file key then we compare
// them; otherwise we use less efficient isSameFile test.
// cycle detected
}
} else {
boolean isSameFile = false;
try {
} catch (IOException x) {
// ignore
} catch (SecurityException x) {
// ignore
}
if (isSameFile) {
// cycle detected
}
}
}
}
// visit directory
try {
// open the directory
try {
} catch (IOException x) {
} catch (SecurityException x) {
// ignore, as per spec
return FileVisitResult.CONTINUE;
}
// the exception notified to the postVisitDirectory method
// invoke preVisitDirectory and then visit each entry
try {
return result;
}
try {
// returning null will cause NPE to be thrown
return result;
// skip remaining siblings in this directory
break;
}
} catch (DirectoryIteratorException e) {
// IOException will be notified to postVisitDirectory
}
} finally {
try {
} catch (IOException e) {
// IOException will be notified to postVisitDirectory
ioe = e;
}
}
// invoke postVisitDirectory last
} finally {
// remove key from trail if doing cycle detection
if (followLinks) {
}
}
}
private static class AncestorDirectory {
}
return dir;
}
return key;
}
}
}