|
9 | 9 |
|
10 | 10 | package org.elasticsearch.test.cluster.util; |
11 | 11 |
|
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.Logger; |
| 14 | + |
12 | 15 | import java.io.File; |
13 | 16 | import java.io.IOException; |
14 | 17 | import java.io.UncheckedIOException; |
|
20 | 23 | import java.util.stream.Stream; |
21 | 24 |
|
22 | 25 | public final class IOUtils { |
| 26 | + private static final Logger LOGGER = LogManager.getLogger(IOUtils.class); |
23 | 27 | private static final int RETRY_DELETE_MILLIS = OS.current() == OS.WINDOWS ? 500 : 0; |
24 | 28 | private static final int MAX_RETRY_DELETE_TIMES = OS.current() == OS.WINDOWS ? 15 : 0; |
25 | 29 |
|
@@ -51,6 +55,30 @@ public static void uncheckedDeleteWithRetry(Path path) { |
51 | 55 | } |
52 | 56 | } |
53 | 57 |
|
| 58 | + /** |
| 59 | + * Attempts to do a copy via linking, falling back to a normal copy if an exception is encountered. |
| 60 | + * |
| 61 | + * @see #syncWithLinks(Path, Path) |
| 62 | + * @see #syncWithCopy(Path, Path) |
| 63 | + * @param sourceRoot where to copy from |
| 64 | + * @param destinationRoot destination to link to |
| 65 | + */ |
| 66 | + public static void syncMaybeWithLinks(Path sourceRoot, Path destinationRoot) { |
| 67 | + try { |
| 68 | + syncWithLinks(sourceRoot, destinationRoot); |
| 69 | + } catch (LinkCreationException e) { |
| 70 | + // Note does not work for network drives, e.g. Vagrant |
| 71 | + LOGGER.info("Failed to sync using hard links. Falling back to copy.", e); |
| 72 | + // ensure we get a clean copy |
| 73 | + try { |
| 74 | + deleteWithRetry(destinationRoot); |
| 75 | + } catch (IOException ex) { |
| 76 | + throw new UncheckedIOException(ex); |
| 77 | + } |
| 78 | + syncWithCopy(sourceRoot, destinationRoot); |
| 79 | + } |
| 80 | + } |
| 81 | + |
54 | 82 | /** |
55 | 83 | * Does the equivalent of `cp -lr` and `chmod -r a-w` to save space and improve speed. |
56 | 84 | * We remove write permissions to make sure files are note mistakenly edited ( e.x. the config file ) and changes |
|
0 commit comments