|
| 1 | +package org.cryptomator.cryptofs; |
| 2 | + |
| 3 | +import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; |
| 4 | +import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; |
| 5 | +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 6 | +import static java.nio.file.StandardOpenOption.CREATE_NEW; |
| 7 | +import static java.nio.file.StandardOpenOption.READ; |
| 8 | +import static java.nio.file.StandardOpenOption.WRITE; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.channels.FileChannel; |
| 12 | +import java.nio.file.AtomicMoveNotSupportedException; |
| 13 | +import java.nio.file.CopyOption; |
| 14 | +import java.nio.file.DirectoryStream; |
| 15 | +import java.nio.file.FileAlreadyExistsException; |
| 16 | +import java.nio.file.NoSuchFileException; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.nio.file.attribute.BasicFileAttributeView; |
| 19 | +import java.nio.file.attribute.BasicFileAttributes; |
| 20 | +import java.nio.file.spi.FileSystemProvider; |
| 21 | +import java.util.EnumSet; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +import javax.inject.Inject; |
| 25 | + |
| 26 | +import org.apache.commons.lang3.ArrayUtils; |
| 27 | + |
| 28 | +@PerProvider |
| 29 | +class CopyAndMoveOperations { |
| 30 | + |
| 31 | + @Inject |
| 32 | + public CopyAndMoveOperations() { |
| 33 | + } |
| 34 | + |
| 35 | + public void copy(CryptoPath source, CryptoPath target, CopyOption... options) throws IOException { |
| 36 | + if (source.equals(target)) { |
| 37 | + return; |
| 38 | + } |
| 39 | + if (pathsBelongToSameFileSystem(source, target)) { |
| 40 | + source.getFileSystem().copy(source, target, options); |
| 41 | + } else { |
| 42 | + Optional<BasicFileAttributes> sourceAttrs = attributes(source); |
| 43 | + Optional<BasicFileAttributes> targetAttrs = attributes(target); |
| 44 | + if (!sourceAttrs.isPresent()) { |
| 45 | + throw new NoSuchFileException(source.toUri().toString()); |
| 46 | + } |
| 47 | + if (targetAttrs.isPresent()) { |
| 48 | + if (ArrayUtils.contains(options, REPLACE_EXISTING)) { |
| 49 | + provider(target).delete(target); |
| 50 | + } else { |
| 51 | + throw new FileAlreadyExistsException(target.toUri().toString()); |
| 52 | + } |
| 53 | + } |
| 54 | + if (sourceAttrs.get().isDirectory()) { |
| 55 | + provider(target).createDirectory(target); |
| 56 | + } else { |
| 57 | + try (FileChannel sourceChannel = provider(source).newFileChannel(source, EnumSet.of(READ)); // |
| 58 | + FileChannel targetChannel = provider(target).newFileChannel(target, EnumSet.of(CREATE_NEW, WRITE))) { |
| 59 | + sourceChannel.transferTo(0, sourceChannel.size(), targetChannel); |
| 60 | + } |
| 61 | + } |
| 62 | + if (ArrayUtils.contains(options, COPY_ATTRIBUTES)) { |
| 63 | + BasicFileAttributeView targetAttrView = provider(target).getFileAttributeView(target, BasicFileAttributeView.class); |
| 64 | + targetAttrView.setTimes(sourceAttrs.get().lastModifiedTime(), sourceAttrs.get().lastAccessTime(), sourceAttrs.get().creationTime()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private FileSystemProvider provider(CryptoPath path) { |
| 70 | + return path.getFileSystem().provider(); |
| 71 | + } |
| 72 | + |
| 73 | + public void move(CryptoPath source, CryptoPath target, CopyOption... options) throws IOException { |
| 74 | + if (source.equals(target)) { |
| 75 | + return; |
| 76 | + } |
| 77 | + if (pathsBelongToSameFileSystem(source, target)) { |
| 78 | + source.getFileSystem().move(source, target, options); |
| 79 | + } else { |
| 80 | + if (ArrayUtils.contains(options, ATOMIC_MOVE)) { |
| 81 | + throw new AtomicMoveNotSupportedException(source.toUri().toString(), target.toUri().toString(), "Move of encrypted file to different FileSystem"); |
| 82 | + } |
| 83 | + if (isNonEmptyDirectory(source)) { |
| 84 | + throw new IOException("Can not move non empty directory to different FileSystem"); |
| 85 | + } |
| 86 | + boolean success = false; |
| 87 | + try { |
| 88 | + copy(source, target, addCopyAttributesTo(options)); |
| 89 | + success = true; |
| 90 | + } finally { |
| 91 | + if (!success) { |
| 92 | + // do a best effort to clean a partially copied file |
| 93 | + try { |
| 94 | + provider(target).deleteIfExists(target); |
| 95 | + } catch (IOException e) { |
| 96 | + // ignore |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + provider(source).deleteIfExists(source); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private boolean pathsBelongToSameFileSystem(CryptoPath source, CryptoPath target) { |
| 105 | + return source.getFileSystem() == target.getFileSystem(); |
| 106 | + } |
| 107 | + |
| 108 | + private boolean isNonEmptyDirectory(CryptoPath source) throws IOException { |
| 109 | + Optional<BasicFileAttributes> sourceAttrs = attributes(source); |
| 110 | + if (!sourceAttrs.map(BasicFileAttributes::isDirectory).orElse(false)) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + try (DirectoryStream<Path> contents = provider(source).newDirectoryStream(source, ignored -> true)) { |
| 114 | + return contents.iterator().hasNext(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private Optional<BasicFileAttributes> attributes(CryptoPath path) { |
| 119 | + try { |
| 120 | + return Optional.of(provider(path).readAttributes(path, BasicFileAttributes.class)); |
| 121 | + } catch (IOException e) { |
| 122 | + return Optional.empty(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private CopyOption[] addCopyAttributesTo(CopyOption[] options) { |
| 127 | + CopyOption[] result = new CopyOption[options.length + 1]; |
| 128 | + for (int i = 0; i < options.length; i++) { |
| 129 | + result[i] = options[i]; |
| 130 | + } |
| 131 | + result[options.length] = COPY_ATTRIBUTES; |
| 132 | + return result; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments