50 return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")); |
50 return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes")); |
51 } |
51 } |
52 |
52 |
53 /** |
53 /** |
54 * Copy source file to target location. If {@code prompt} is true then |
54 * Copy source file to target location. If {@code prompt} is true then |
55 * prompted user to overwrite target if it exists. The {@code preserve} |
55 * prompt user to overwrite target if it exists. The {@code preserve} |
56 * parameter determines if file attributes should be copied/preserved. |
56 * parameter determines if file attributes should be copied/preserved. |
57 */ |
57 */ |
58 static void copyFile(Path source, Path target, boolean prompt, boolean preserve) { |
58 static void copyFile(Path source, Path target, boolean prompt, boolean preserve) { |
59 CopyOption[] options = (preserve) ? |
59 CopyOption[] options = (preserve) ? |
60 new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } : |
60 new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } : |
61 new CopyOption[] { REPLACE_EXISTING }; |
61 new CopyOption[] { REPLACE_EXISTING }; |
62 if (!prompt || target.notExists() || okayToOverwrite(target)) { |
62 if (!prompt || target.notExists() || okayToOverwrite(target)) { |
63 try { |
63 try { |
64 source.copyTo(target, options); |
64 source.copyTo(target, options); |
65 } catch (IOException x) { |
65 } catch (IOException x) { |
66 System.err.format("Unable to create: %s: %s%n", target, x); |
66 System.err.format("Unable to copy: %s: %s%n", source, x); |
67 } |
67 } |
68 } |
68 } |
69 } |
69 } |
70 |
70 |
71 /** |
71 /** |
122 |
122 |
123 @Override |
123 @Override |
124 public FileVisitResult postVisitDirectory(Path dir, IOException exc) { |
124 public FileVisitResult postVisitDirectory(Path dir, IOException exc) { |
125 // fix up modification time of directory when done |
125 // fix up modification time of directory when done |
126 if (exc == null && preserve) { |
126 if (exc == null && preserve) { |
|
127 Path newdir = target.resolve(source.relativize(dir)); |
127 try { |
128 try { |
128 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir); |
129 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir); |
129 Path newdir = target.resolve(source.relativize(dir)); |
|
130 Attributes.setLastModifiedTime(newdir, |
130 Attributes.setLastModifiedTime(newdir, |
131 attrs.lastModifiedTime(), attrs.resolution()); |
131 attrs.lastModifiedTime(), attrs.resolution()); |
132 } catch (IOException x) { |
132 } catch (IOException x) { |
133 // ignore |
133 System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x); |
134 } |
134 } |
135 } |
135 } |
136 return CONTINUE; |
136 return CONTINUE; |
137 } |
137 } |
138 |
138 |
189 // check if target is a directory |
189 // check if target is a directory |
190 boolean isDir = false; |
190 boolean isDir = false; |
191 try { |
191 try { |
192 isDir = Attributes.readBasicFileAttributes(target).isDirectory(); |
192 isDir = Attributes.readBasicFileAttributes(target).isDirectory(); |
193 } catch (IOException x) { |
193 } catch (IOException x) { |
|
194 // ignore (probably target does not exist) |
194 } |
195 } |
195 |
196 |
196 // copy each source file/directory to target |
197 // copy each source file/directory to target |
197 for (i=0; i<source.length; i++) { |
198 for (i=0; i<source.length; i++) { |
198 Path dest = (isDir) ? target.resolve(source[i].getName()) : target; |
199 Path dest = (isDir) ? target.resolve(source[i].getName()) : target; |
199 |
200 |
200 if (recursive) { |
201 if (recursive) { |
201 // follow links when copying files |
202 // follow links when copying files |
202 EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); |
203 EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); |
203 TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve); |
204 TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve); |
204 Files.walkFileTree(source[i], opts, -1, tc); |
205 Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc); |
205 } else { |
206 } else { |
206 // not recursive so source must not be a directory |
207 // not recursive so source must not be a directory |
207 try { |
208 try { |
208 if (Attributes.readBasicFileAttributes(source[i]).isDirectory()) { |
209 if (Attributes.readBasicFileAttributes(source[i]).isDirectory()) { |
209 System.err.format("%s: is a directory%n", source[i]); |
210 System.err.format("%s: is a directory%n", source[i]); |
210 continue; |
211 continue; |
211 } |
212 } |
212 } catch (IOException x) { } |
213 } catch (IOException x) { |
|
214 // assume not directory |
|
215 } |
213 copyFile(source[i], dest, prompt, preserve); |
216 copyFile(source[i], dest, prompt, preserve); |
214 } |
217 } |
215 } |
218 } |
216 } |
219 } |
217 } |
220 } |