jdk/test/java/nio/file/Files/FaultyFileSystem.java
author henryjen
Thu, 09 May 2013 14:44:56 -0700
changeset 17696 23a863fbb6c3
permissions -rw-r--r--
8006884: (fs) Add Files.list, lines and find Reviewed-by: briangoetz, mduigou Contributed-by: alan.bateman@oracle.com, henry.jen@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     1
/*
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     4
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     7
 * published by the Free Software Foundation.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     8
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    13
 * accompanied this code).
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    14
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    18
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    21
 * questions.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    22
 */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    23
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    24
import java.io.IOException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    25
import java.net.URI;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    26
import java.nio.channels.SeekableByteChannel;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    27
import java.nio.file.AccessMode;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    28
import java.nio.file.CopyOption;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    29
import java.nio.file.DirectoryIteratorException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    30
import java.nio.file.DirectoryStream;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    31
import java.nio.file.FileStore;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    32
import java.nio.file.FileSystem;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    33
import java.nio.file.FileSystemAlreadyExistsException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    34
import java.nio.file.FileSystemNotFoundException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    35
import java.nio.file.Files;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    36
import java.nio.file.LinkOption;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    37
import java.nio.file.NoSuchFileException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    38
import java.nio.file.OpenOption;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    39
import java.nio.file.Path;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    40
import java.nio.file.PathMatcher;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    41
import java.nio.file.WatchService;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    42
import java.nio.file.attribute.BasicFileAttributes;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    43
import java.nio.file.attribute.FileAttribute;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    44
import java.nio.file.attribute.FileAttributeView;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    45
import java.nio.file.attribute.UserPrincipalLookupService;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    46
import java.nio.file.spi.FileSystemProvider;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    47
import java.util.Iterator;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    48
import java.util.Map;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    49
import java.util.NoSuchElementException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    50
import java.util.Set;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    51
import java.util.function.Supplier;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    52
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    53
/**
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    54
 * A {@code FileSystem} that helps testing by trigger exception throwing based on filenames.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    55
 */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    56
class FaultyFileSystem extends FileSystem {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    57
    final Path root;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    58
    final boolean removeRootAfterClose;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    59
    final FileSystem delegate;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    60
    boolean isOpen;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    61
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    62
    FaultyFileSystem(Path root) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    63
        if (root == null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    64
            root = Files.createTempDirectory("faultyFS");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    65
            removeRootAfterClose = true;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    66
        } else {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    67
            if (! Files.isDirectory(root)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    68
                throw new IllegalArgumentException("must be a directory.");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    69
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    70
            removeRootAfterClose = false;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    71
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    72
        this.root = root;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    73
        delegate = root.getFileSystem();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    74
        isOpen = true;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    75
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    76
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    77
    private static Path unwrap(Path p) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    78
        return PassThroughFileSystem.unwrap(p);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    79
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    80
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    81
    Path getRoot() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    82
        return new PassThroughFileSystem.PassThroughPath(this, root);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    83
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    84
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    85
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    86
    public void close() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    87
        if (isOpen) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    88
            if (removeRootAfterClose) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    89
                TestUtil.removeAll(root);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    90
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    91
            isOpen = false;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    92
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    93
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    94
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    95
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    96
    public FileSystemProvider provider() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    97
        return FaultyFSProvider.getInstance();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    98
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    99
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   100
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   101
    public boolean isOpen() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   102
        return isOpen;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   103
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   104
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   105
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   106
    public boolean isReadOnly() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   107
        return delegate.isReadOnly();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   108
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   109
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   110
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   111
    public String getSeparator() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   112
        return delegate.getSeparator();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   113
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   114
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   115
    private <T> Iterable<T> SoleIterable(final T element) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   116
        return new Iterable<T>() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   117
            @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   118
            public Iterator<T> iterator() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   119
                return new Iterator<T>() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   120
                    private T soleElement = element;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   121
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   122
                    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   123
                    public boolean hasNext() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   124
                        return soleElement != null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   125
                    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   126
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   127
                    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   128
                    public T next() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   129
                        try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   130
                            return soleElement;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   131
                        } finally {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   132
                            soleElement = null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   133
                        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   134
                    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   135
                };
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   136
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   137
        };
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   138
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   139
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   140
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   141
    public Iterable<Path> getRootDirectories() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   142
        return SoleIterable(getRoot());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   143
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   144
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   145
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   146
    public Iterable<FileStore> getFileStores() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   147
        FileStore store;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   148
        try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   149
            store = Files.getFileStore(root);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   150
        } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   151
            store = null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   152
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   153
        return SoleIterable(store);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   154
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   155
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   156
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   157
    public Set<String> supportedFileAttributeViews() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   158
        // assume that unwrapped objects aren't exposed
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   159
        return delegate.supportedFileAttributeViews();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   160
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   161
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   162
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   163
    public Path getPath(String first, String... more) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   164
        return new PassThroughFileSystem.PassThroughPath(this, delegate.getPath(first, more));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   165
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   166
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   167
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   168
    public PathMatcher getPathMatcher(String syntaxAndPattern) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   169
        final PathMatcher matcher = delegate.getPathMatcher(syntaxAndPattern);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   170
        return new PathMatcher() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   171
            @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   172
            public boolean matches(Path path) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   173
                return matcher.matches(unwrap(path));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   174
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   175
        };
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   176
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   177
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   178
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   179
    public UserPrincipalLookupService getUserPrincipalLookupService() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   180
        // assume that unwrapped objects aren't exposed
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   181
        return delegate.getUserPrincipalLookupService();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   182
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   183
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   184
    @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   185
    public WatchService newWatchService() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   186
        // to keep it simple
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   187
        throw new UnsupportedOperationException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   188
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   189
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   190
    static class FaultyException extends IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   191
        FaultyException() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   192
            super("fault triggered.");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   193
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   194
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   195
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   196
    static class FaultyFSProvider extends FileSystemProvider {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   197
        private static final String SCHEME = "faulty";
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   198
        private static volatile FaultyFileSystem delegate;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   199
        private static FaultyFSProvider INSTANCE = new FaultyFSProvider();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   200
        private boolean enabled;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   201
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   202
        private FaultyFSProvider() {}
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   203
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   204
        public static FaultyFSProvider getInstance() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   205
            return INSTANCE;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   206
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   207
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   208
        public void setFaultyMode(boolean enable) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   209
            enabled = enable;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   210
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   211
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   212
        private void triggerEx(String filename, String... names) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   213
            if (! enabled) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   214
                return;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   215
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   216
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   217
            if (filename.equals("SecurityException")) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   218
                throw new SecurityException("FaultyFS", new FaultyException());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   219
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   220
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   221
            if (filename.equals("IOException")) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   222
                throw new FaultyException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   223
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   224
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   225
            for (String name: names) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   226
                if (name.equals(filename)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   227
                    throw new FaultyException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   228
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   229
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   230
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   231
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   232
        private void triggerEx(Path path, String... names) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   233
            triggerEx(path.getFileName().toString(), names);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   234
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   235
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   236
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   237
        public String getScheme() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   238
            return SCHEME;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   239
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   240
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   241
        private void checkScheme(URI uri) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   242
            if (!uri.getScheme().equalsIgnoreCase(SCHEME))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   243
                throw new IllegalArgumentException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   244
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   245
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   246
        private void checkUri(URI uri) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   247
            checkScheme(uri);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   248
            if (!uri.getSchemeSpecificPart().equals("///"))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   249
                throw new IllegalArgumentException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   250
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   251
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   252
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   253
        public FileSystem newFileSystem(Path fakeRoot, Map<String,?> env)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   254
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   255
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   256
            if (env != null && env.keySet().contains("IOException")) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   257
                triggerEx("IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   258
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   259
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   260
            synchronized (FaultyFSProvider.class) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   261
                if (delegate != null && delegate.isOpen())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   262
                    throw new FileSystemAlreadyExistsException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   263
                FaultyFileSystem result = new FaultyFileSystem(fakeRoot);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   264
                delegate = result;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   265
                return result;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   266
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   267
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   268
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   269
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   270
        public FileSystem newFileSystem(URI uri, Map<String,?> env)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   271
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   272
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   273
            if (env != null && env.keySet().contains("IOException")) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   274
                triggerEx("IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   275
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   276
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   277
            checkUri(uri);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   278
            synchronized (FaultyFSProvider.class) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   279
                if (delegate != null && delegate.isOpen())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   280
                    throw new FileSystemAlreadyExistsException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   281
                FaultyFileSystem result = new FaultyFileSystem(null);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   282
                delegate = result;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   283
                return result;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   284
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   285
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   286
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   287
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   288
        public FileSystem getFileSystem(URI uri) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   289
            checkUri(uri);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   290
            FileSystem result = delegate;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   291
            if (result == null)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   292
                throw new FileSystemNotFoundException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   293
            return result;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   294
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   295
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   296
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   297
        public Path getPath(URI uri) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   298
            checkScheme(uri);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   299
            if (delegate == null)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   300
                throw new FileSystemNotFoundException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   301
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   302
            // only allow absolute path
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   303
            String path = uri.getSchemeSpecificPart();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   304
            if (! path.startsWith("///")) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   305
                throw new IllegalArgumentException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   306
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   307
            return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   308
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   309
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   310
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   311
        public void setAttribute(Path file, String attribute, Object value, LinkOption... options)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   312
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   313
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   314
            triggerEx(file, "setAttribute");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   315
            Files.setAttribute(unwrap(file), attribute, value, options);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   316
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   317
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   318
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   319
        public Map<String,Object> readAttributes(Path file, String attributes, LinkOption... options)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   320
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   321
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   322
            triggerEx(file, "readAttributes");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   323
            return Files.readAttributes(unwrap(file), attributes, options);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   324
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   325
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   326
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   327
        public <V extends FileAttributeView> V getFileAttributeView(Path file,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   328
                                                                    Class<V> type,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   329
                                                                    LinkOption... options)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   330
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   331
            return Files.getFileAttributeView(unwrap(file), type, options);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   332
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   333
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   334
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   335
        public <A extends BasicFileAttributes> A readAttributes(Path file,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   336
                                                                Class<A> type,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   337
                                                                LinkOption... options)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   338
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   339
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   340
            triggerEx(file, "readAttributes");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   341
            return Files.readAttributes(unwrap(file), type, options);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   342
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   343
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   344
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   345
        public void delete(Path file) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   346
            triggerEx(file, "delete");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   347
            Files.delete(unwrap(file));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   348
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   349
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   350
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   351
        public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   352
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   353
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   354
            triggerEx(target, "createSymbolicLink");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   355
            Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   356
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   357
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   358
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   359
        public void createLink(Path link, Path existing) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   360
            triggerEx(existing, "createLink");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   361
            Files.createLink(unwrap(link), unwrap(existing));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   362
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   363
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   364
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   365
        public Path readSymbolicLink(Path link) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   366
            Path target = Files.readSymbolicLink(unwrap(link));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   367
            triggerEx(target, "readSymbolicLink");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   368
            return new PassThroughFileSystem.PassThroughPath(delegate, target);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   369
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   370
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   371
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   372
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   373
        public void copy(Path source, Path target, CopyOption... options) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   374
            triggerEx(source, "copy");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   375
            Files.copy(unwrap(source), unwrap(target), options);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   376
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   377
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   378
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   379
        public void move(Path source, Path target, CopyOption... options) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   380
            triggerEx(source, "move");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   381
            Files.move(unwrap(source), unwrap(target), options);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   382
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   383
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   384
        private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   385
            return new DirectoryStream<Path>() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   386
                @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   387
                public Iterator<Path> iterator() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   388
                    final Iterator<Path> itr = stream.iterator();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   389
                    return new Iterator<Path>() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   390
                        private Path next = null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   391
                        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   392
                        public boolean hasNext() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   393
                            if (next == null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   394
                                if (itr.hasNext()) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   395
                                    next = itr.next();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   396
                                } else {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   397
                                    return false;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   398
                                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   399
                            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   400
                            if (next != null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   401
                                try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   402
                                    triggerEx(next, "DirectoryIteratorException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   403
                                } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   404
                                    throw new DirectoryIteratorException(ioe);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   405
                                } catch (SecurityException se) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   406
                                    // ??? Does DS throw SecurityException during iteration?
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   407
                                    next = null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   408
                                    return hasNext();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   409
                                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   410
                            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   411
                            return (next != null);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   412
                        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   413
                        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   414
                        public Path next() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   415
                            try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   416
                                if (next != null || hasNext()) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   417
                                    return new PassThroughFileSystem.PassThroughPath(delegate, next);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   418
                                } else {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   419
                                    throw new NoSuchElementException();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   420
                                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   421
                            } finally {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   422
                                next = null;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   423
                            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   424
                        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   425
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   426
                        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   427
                        public void remove() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   428
                            itr.remove();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   429
                        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   430
                    };
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   431
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   432
                @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   433
                public void close() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   434
                    stream.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   435
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   436
            };
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   437
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   438
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   439
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   440
        public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   441
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   442
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   443
            triggerEx(dir, "newDirectoryStream");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   444
            return wrap(Files.newDirectoryStream(unwrap(dir), filter));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   445
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   446
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   447
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   448
        public void createDirectory(Path dir, FileAttribute<?>... attrs)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   449
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   450
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   451
            triggerEx(dir, "createDirectory");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   452
            Files.createDirectory(unwrap(dir), attrs);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   453
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   454
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   455
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   456
        public SeekableByteChannel newByteChannel(Path file,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   457
                                                  Set<? extends OpenOption> options,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   458
                                                  FileAttribute<?>... attrs)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   459
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   460
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   461
            triggerEx(file, "newByteChannel");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   462
            return Files.newByteChannel(unwrap(file), options, attrs);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   463
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   464
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   465
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   466
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   467
        public boolean isHidden(Path file) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   468
            triggerEx(file, "isHidden");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   469
            return Files.isHidden(unwrap(file));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   470
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   471
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   472
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   473
        public FileStore getFileStore(Path file) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   474
            triggerEx(file, "getFileStore");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   475
            return Files.getFileStore(unwrap(file));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   476
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   477
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   478
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   479
        public boolean isSameFile(Path file, Path other) throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   480
            triggerEx(file, "isSameFile");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   481
            return Files.isSameFile(unwrap(file), unwrap(other));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   482
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   483
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   484
        @Override
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   485
        public void checkAccess(Path file, AccessMode... modes)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   486
            throws IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   487
        {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   488
            triggerEx(file, "checkAccess");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   489
            // hack
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   490
            if (modes.length == 0) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   491
                if (Files.exists(unwrap(file)))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   492
                    return;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   493
                else
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   494
                    throw new NoSuchFileException(file.toString());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   495
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   496
            throw new RuntimeException("not implemented yet");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   497
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   498
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   499
}