|
| 1 | +package org.aksw.maven.plugin.lsq; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.OutputStream; |
| 5 | +import java.nio.file.AtomicMoveNotSupportedException; |
| 6 | +import java.nio.file.FileAlreadyExistsException; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.StandardCopyOption; |
| 10 | +import java.nio.file.StandardOpenOption; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.function.Consumer; |
| 13 | +import java.util.function.Function; |
| 14 | + |
| 15 | +import org.aksw.commons.lambda.throwing.ThrowingConsumer; |
| 16 | + |
| 17 | +/** FIXME Delete this class as it has been moved to aksw-commons */ |
| 18 | +public class FileUtilsBackport { |
| 19 | + public static void moveAtomicIfSupported(Consumer<String> warnCallback, Path source, Path target) throws IOException { |
| 20 | + try { |
| 21 | + Files.move(source, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); |
| 22 | + } catch (AtomicMoveNotSupportedException e) { |
| 23 | + if (warnCallback != null) { |
| 24 | + warnCallback.accept(String.format("Atomic move from %s to %s failed, falling back to copy", source, target)); |
| 25 | + } |
| 26 | + Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /** Actions if the target already exists */ |
| 31 | + public static enum OverwriteMode { |
| 32 | + /** Raise an error */ |
| 33 | + ERROR, |
| 34 | + |
| 35 | + /** Overwrite the target */ |
| 36 | + OVERWRITE, |
| 37 | + |
| 38 | + /** Skip the write */ |
| 39 | + SKIP |
| 40 | + } |
| 41 | + |
| 42 | + public static void safeCreate(Path target, OverwriteMode overwriteAction, ThrowingConsumer<OutputStream> writer) throws Exception { |
| 43 | + safeCreate(target, null, overwriteAction, writer); |
| 44 | + } |
| 45 | + |
| 46 | + public static void safeCreate(Path target, Function<OutputStream, OutputStream> encoder, OverwriteMode overwriteAction, ThrowingConsumer<OutputStream> writer) throws Exception { |
| 47 | + Objects.requireNonNull(overwriteAction); |
| 48 | + |
| 49 | + String fileName = target.getFileName().toString(); |
| 50 | + String tmpFileName = "." + fileName + ".tmp"; // + new Random().nextInt(); |
| 51 | + Path tmpFile = target.resolveSibling(tmpFileName); |
| 52 | + |
| 53 | + Boolean fileExists = OverwriteMode.SKIP.equals(overwriteAction) || OverwriteMode.ERROR.equals(overwriteAction) |
| 54 | + ? Files.exists(target) |
| 55 | + : null; |
| 56 | + |
| 57 | + // Check whether the target already exists before we start writing the tmpFile |
| 58 | + if (Boolean.TRUE.equals(fileExists) && OverwriteMode.ERROR.equals(overwriteAction)) { |
| 59 | + throw new FileAlreadyExistsException(target.toAbsolutePath().toString()); |
| 60 | + } |
| 61 | + |
| 62 | + if (!(Boolean.TRUE.equals(fileExists) && OverwriteMode.SKIP.equals(overwriteAction))) { |
| 63 | + Path parent = target.getParent(); |
| 64 | + if (parent != null) { |
| 65 | + Files.createDirectories(parent); |
| 66 | + } |
| 67 | + |
| 68 | + boolean allowOverwrite = OverwriteMode.OVERWRITE.equals(overwriteAction); |
| 69 | + // What to do if the tmp file already exists? |
| 70 | + try (OutputStream raw = Files.newOutputStream(tmpFile, allowOverwrite ? StandardOpenOption.CREATE : StandardOpenOption.CREATE_NEW); |
| 71 | + OutputStream out = encoder != null ? encoder.apply(raw) : raw) { |
| 72 | + writer.accept(out); |
| 73 | + out.flush(); |
| 74 | + } |
| 75 | + moveAtomicIfSupported(null, tmpFile, target); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** Delete a path if it is an empty directory */ |
| 80 | + public void deleteDirectoryIfEmpty(Path path) throws IOException { |
| 81 | + boolean isDirectory = Files.isDirectory(path); |
| 82 | + if (isDirectory) { |
| 83 | + boolean isEmpty = Files.list(path).count() == 0; |
| 84 | + if (isEmpty) { |
| 85 | + Files.delete(path); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments