jdk/src/share/classes/java/nio/file/FileTreeWalker.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8166 13423c0952ad
child 11506 905dbb3e77eb
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8166
diff changeset
     2
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    23
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    25
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    26
package java.nio.file;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    28
import java.nio.file.attribute.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    29
import java.io.IOException;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    30
import java.util.*;
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    31
import sun.nio.fs.BasicFileAttributesHolder;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    32
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
/**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    34
 * Simple file tree walker that works in a similar manner to nftw(3C).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    35
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
 * @see Files#walkFileTree
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    37
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    38
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    39
class FileTreeWalker {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    40
    private final boolean followLinks;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    41
    private final LinkOption[] linkOptions;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    42
    private final FileVisitor<? super Path> visitor;
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    43
    private final int maxDepth;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    44
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    45
    FileTreeWalker(Set<FileVisitOption> options,
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    46
                   FileVisitor<? super Path> visitor,
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    47
                   int maxDepth)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    48
    {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    49
        boolean fl = false;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    50
        for (FileVisitOption option: options) {
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
    51
            // will throw NPE if options contains null
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    52
            switch (option) {
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
    53
                case FOLLOW_LINKS : fl = true; break;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    54
                default:
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    55
                    throw new AssertionError("Should not get here");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    56
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    57
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    58
        this.followLinks = fl;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    59
        this.linkOptions = (fl) ? new LinkOption[0] :
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    60
            new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    61
        this.visitor = visitor;
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    62
        this.maxDepth = maxDepth;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    63
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    64
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    65
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    66
     * Walk file tree starting at the given file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    67
     */
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
    68
    void walk(Path start) throws IOException {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    69
        FileVisitResult result = walk(start,
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    70
                                      0,
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    71
                                      new ArrayList<AncestorDirectory>());
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
    72
        Objects.requireNonNull(result, "FileVisitor returned null");
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    73
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    74
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    75
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    76
     * @param   file
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    77
     *          the directory to visit
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    78
     * @param   depth
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    79
     *          depth remaining
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    80
     * @param   ancestors
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    81
     *          use when cycle detection is enabled
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    82
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    83
    private FileVisitResult walk(Path file,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    84
                                 int depth,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    85
                                 List<AncestorDirectory> ancestors)
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
    86
        throws IOException
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    87
    {
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    88
        // if attributes are cached then use them if possible
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    89
        BasicFileAttributes attrs = null;
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    90
        if ((depth > 0) &&
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    91
            (file instanceof BasicFileAttributesHolder) &&
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    92
            (System.getSecurityManager() == null))
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
    93
        {
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    94
            BasicFileAttributes cached = ((BasicFileAttributesHolder)file).get();
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    95
            if (!followLinks || !cached.isSymbolicLink())
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    96
                attrs = cached;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
    97
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    98
        IOException exc = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    99
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   100
        // attempt to get attributes of file. If fails and we are following
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   101
        // links then a link target might not exist so get attributes of link
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   102
        if (attrs == null) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   103
            try {
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   104
                try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   105
                    attrs = Files.readAttributes(file, BasicFileAttributes.class, linkOptions);
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   106
                } catch (IOException x1) {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   107
                    if (followLinks) {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   108
                        try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   109
                            attrs = Files.readAttributes(file,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   110
                                                         BasicFileAttributes.class,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   111
                                                         LinkOption.NOFOLLOW_LINKS);
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   112
                        } catch (IOException x2) {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   113
                            exc = x2;
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   114
                        }
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   115
                    } else {
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   116
                        exc = x1;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   117
                    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   118
                }
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   119
            } catch (SecurityException x) {
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
   120
                // If access to starting file is denied then SecurityException
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
   121
                // is thrown, otherwise the file is ignored.
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
   122
                if (depth == 0)
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
   123
                    throw x;
2071
5e6af6d106cb 6808647: (file) Paths.get("C:").newDirectoryStream() iterates over Path elements with additional slash [win]
alanb
parents: 2057
diff changeset
   124
                return FileVisitResult.CONTINUE;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   125
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   126
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   127
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   128
        // unable to get attributes of file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   129
        if (exc != null) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   130
            return visitor.visitFileFailed(file, exc);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   131
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   132
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   133
        // at maximum depth or file is not a directory
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   134
        if (depth >= maxDepth || !attrs.isDirectory()) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   135
            return visitor.visitFile(file, attrs);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   136
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   137
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   138
        // check for cycles when following links
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   139
        if (followLinks) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   140
            Object key = attrs.fileKey();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   141
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   142
            // if this directory and ancestor has a file key then we compare
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   143
            // them; otherwise we use less efficient isSameFile test.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   144
            for (AncestorDirectory ancestor: ancestors) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   145
                Object ancestorKey = ancestor.fileKey();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   146
                if (key != null && ancestorKey != null) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   147
                    if (key.equals(ancestorKey)) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   148
                        // cycle detected
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   149
                        return visitor.visitFileFailed(file,
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   150
                            new FileSystemLoopException(file.toString()));
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   151
                    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   152
                } else {
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   153
                    boolean isSameFile = false;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   154
                    try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   155
                        isSameFile = Files.isSameFile(file, ancestor.file());
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   156
                    } catch (IOException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   157
                        // ignore
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   158
                    } catch (SecurityException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   159
                        // ignore
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   160
                    }
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   161
                    if (isSameFile) {
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   162
                        // cycle detected
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   163
                        return visitor.visitFileFailed(file,
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   164
                            new FileSystemLoopException(file.toString()));
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   165
                    }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   166
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   167
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   168
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   169
            ancestors.add(new AncestorDirectory(file, key));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   170
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   171
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   172
        // visit directory
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   173
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   174
            DirectoryStream<Path> stream = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   175
            FileVisitResult result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   176
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   177
            // open the directory
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   178
            try {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   179
                stream = Files.newDirectoryStream(file);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   180
            } catch (IOException x) {
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   181
                return visitor.visitFileFailed(file, x);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   182
            } catch (SecurityException x) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   183
                // ignore, as per spec
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   184
                return FileVisitResult.CONTINUE;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   185
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   186
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   187
            // the exception notified to the postVisitDirectory method
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   188
            IOException ioe = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   189
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   190
            // invoke preVisitDirectory and then visit each entry
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   191
            try {
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   192
                result = visitor.preVisitDirectory(file, attrs);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   193
                if (result != FileVisitResult.CONTINUE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   194
                    return result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   195
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   196
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   197
                try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   198
                    for (Path entry: stream) {
3847
821abab95d38 6876541: (file) Files.walkFileTree(...): no SecurityException if read access to the starting file is denied
alanb
parents: 3065
diff changeset
   199
                        result = walk(entry, depth+1, ancestors);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   200
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   201
                        // returning null will cause NPE to be thrown
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   202
                        if (result == null || result == FileVisitResult.TERMINATE)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   203
                            return result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   204
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   205
                        // skip remaining siblings in this directory
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   206
                        if (result == FileVisitResult.SKIP_SIBLINGS)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   207
                            break;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   208
                    }
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   209
                } catch (DirectoryIteratorException e) {
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   210
                    // IOException will be notified to postVisitDirectory
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   211
                    ioe = e.getCause();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   212
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   213
            } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   214
                try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   215
                    stream.close();
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   216
                } catch (IOException e) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   217
                    // IOException will be notified to postVisitDirectory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   218
                    if (ioe == null)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   219
                        ioe = e;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   220
                }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   221
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   222
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   223
            // invoke postVisitDirectory last
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   224
            return visitor.postVisitDirectory(file, ioe);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   225
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   226
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   227
            // remove key from trail if doing cycle detection
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
   228
            if (followLinks) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   229
                ancestors.remove(ancestors.size()-1);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   230
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   231
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   232
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   233
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   234
    private static class AncestorDirectory {
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2071
diff changeset
   235
        private final Path dir;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   236
        private final Object key;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2071
diff changeset
   237
        AncestorDirectory(Path dir, Object key) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   238
            this.dir = dir;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   239
            this.key = key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   240
        }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2071
diff changeset
   241
        Path file() {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   242
            return dir;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   243
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   244
        Object fileKey() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   245
            return key;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   246
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   247
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   248
}