|
| 1 | +package rprocessing; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * - Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * |
| 13 | + * - Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in the |
| 15 | + * documentation and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * - Neither the name of Oracle nor the names of its |
| 18 | + * contributors may be used to endorse or promote products derived |
| 19 | + * from this software without specific prior written permission. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 22 | + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 23 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 25 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +import static java.nio.file.FileVisitResult.CONTINUE; |
| 35 | +import static java.nio.file.FileVisitResult.SKIP_SUBTREE; |
| 36 | +import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; |
| 37 | +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.nio.file.FileAlreadyExistsException; |
| 41 | +import java.nio.file.FileSystemLoopException; |
| 42 | +import java.nio.file.FileVisitResult; |
| 43 | +import java.nio.file.FileVisitor; |
| 44 | +import java.nio.file.Files; |
| 45 | +import java.nio.file.Path; |
| 46 | +import java.nio.file.attribute.BasicFileAttributes; |
| 47 | +import java.nio.file.attribute.FileTime; |
| 48 | + |
| 49 | +class TreeCopier implements FileVisitor<Path> { |
| 50 | + |
| 51 | + /** |
| 52 | + * Copy source file to target location. |
| 53 | + */ |
| 54 | + static void copyFile(final Path source, final Path target) { |
| 55 | + try { |
| 56 | + Files.copy(source, target, COPY_ATTRIBUTES, REPLACE_EXISTING); |
| 57 | + } catch (final IOException x) { |
| 58 | + System.err.format("Unable to copy: %s: %s%n", source, x); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private final Path source; |
| 63 | + private final Path target; |
| 64 | + |
| 65 | + TreeCopier(final Path source, final Path target) { |
| 66 | + this.source = source; |
| 67 | + this.target = target; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) { |
| 72 | + // before visiting entries in a directory we copy the directory |
| 73 | + // (okay if directory already exists). |
| 74 | + final Path newdir = target.resolve(source.relativize(dir)); |
| 75 | + try { |
| 76 | + Files.copy(dir, newdir, COPY_ATTRIBUTES); |
| 77 | + } catch (final FileAlreadyExistsException x) { |
| 78 | + // ignore |
| 79 | + } catch (final IOException x) { |
| 80 | + System.err.format("Unable to create: %s: %s%n", newdir, x); |
| 81 | + return SKIP_SUBTREE; |
| 82 | + } |
| 83 | + return CONTINUE; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) { |
| 88 | + copyFile(file, target.resolve(source.relativize(file))); |
| 89 | + return CONTINUE; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) { |
| 94 | + // fix up modification time of directory when done |
| 95 | + if (exc == null) { |
| 96 | + final Path newdir = target.resolve(source.relativize(dir)); |
| 97 | + try { |
| 98 | + final FileTime time = Files.getLastModifiedTime(dir); |
| 99 | + Files.setLastModifiedTime(newdir, time); |
| 100 | + } catch (final IOException x) { |
| 101 | + System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x); |
| 102 | + } |
| 103 | + } |
| 104 | + return CONTINUE; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public FileVisitResult visitFileFailed(final Path file, final IOException exc) { |
| 109 | + if (exc instanceof FileSystemLoopException) { |
| 110 | + System.err.println("cycle detected: " + file); |
| 111 | + } else { |
| 112 | + System.err.format("Unable to copy: %s: %s%n", file, exc); |
| 113 | + } |
| 114 | + return CONTINUE; |
| 115 | + } |
| 116 | +} |
0 commit comments