|
25 | 25 |
|
26 | 26 | package java.nio.file;
|
27 | 27 |
|
28 |
| -import java.nio.file.attribute.*; |
29 | 28 | import java.io.InputStream;
|
30 | 29 | import java.io.IOException;
|
| 30 | +import java.nio.file.attribute.BasicFileAttributes; |
| 31 | +import java.nio.file.attribute.BasicFileAttributeView; |
| 32 | +import java.nio.file.attribute.PosixFileAttributes; |
| 33 | +import java.nio.file.attribute.PosixFileAttributeView; |
| 34 | +import java.nio.file.spi.FileSystemProvider; |
31 | 35 |
|
32 | 36 | /**
|
33 | 37 | * Helper class to support copying or moving files when the source and target
|
@@ -69,14 +73,6 @@ static CopyOptions parse(CopyOption... options) {
|
69 | 73 | }
|
70 | 74 | return result;
|
71 | 75 | }
|
72 |
| - |
73 |
| - CopyOption[] replaceExistingOrEmpty() { |
74 |
| - if (replaceExisting) { |
75 |
| - return new CopyOption[] { StandardCopyOption.REPLACE_EXISTING }; |
76 |
| - } else { |
77 |
| - return new CopyOption[0]; |
78 |
| - } |
79 |
| - } |
80 | 76 | }
|
81 | 77 |
|
82 | 78 | /**
|
@@ -137,14 +133,22 @@ static void copyToForeignTarget(Path source, Path target,
|
137 | 133 | if (sourceAttrs.isSymbolicLink())
|
138 | 134 | throw new IOException("Copying of symbolic links not supported");
|
139 | 135 |
|
| 136 | + // ensure source can be copied |
| 137 | + FileSystemProvider provider = source.getFileSystem().provider(); |
| 138 | + provider.checkAccess(source, AccessMode.READ); |
| 139 | + |
| 140 | + // delete target if it exists and REPLACE_EXISTING is specified |
| 141 | + if (opts.replaceExisting) |
| 142 | + Files.deleteIfExists(target); |
| 143 | + else if (Files.exists(target)) |
| 144 | + throw new FileAlreadyExistsException(target.toString()); |
| 145 | + |
140 | 146 | // create directory or copy file
|
141 | 147 | if (sourceAttrs.isDirectory()) {
|
142 |
| - if (opts.replaceExisting) |
143 |
| - Files.deleteIfExists(target); |
144 | 148 | Files.createDirectory(target);
|
145 | 149 | } else {
|
146 | 150 | try (InputStream in = Files.newInputStream(source)) {
|
147 |
| - Files.copy(in, target, opts.replaceExistingOrEmpty()); |
| 151 | + Files.copy(in, target); |
148 | 152 | }
|
149 | 153 | }
|
150 | 154 |
|
|
0 commit comments