|
| 1 | +package org.cryptomator.cryptofs.health.type; |
| 2 | + |
| 3 | +import org.cryptomator.cryptofs.VaultConfig; |
| 4 | +import org.cryptomator.cryptofs.common.CiphertextFileType; |
| 5 | +import org.cryptomator.cryptofs.common.Constants; |
| 6 | +import org.cryptomator.cryptofs.health.api.CheckFailed; |
| 7 | +import org.cryptomator.cryptofs.health.api.DiagnosticResult; |
| 8 | +import org.cryptomator.cryptofs.health.api.HealthCheck; |
| 9 | +import org.cryptomator.cryptolib.api.Cryptor; |
| 10 | +import org.cryptomator.cryptolib.api.Masterkey; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.file.FileVisitResult; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.LinkOption; |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.nio.file.SimpleFileVisitor; |
| 20 | +import java.nio.file.attribute.BasicFileAttributes; |
| 21 | +import java.util.EnumSet; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +/** |
| 26 | + * Checks for each c9r or c9s dir in the vault structure if its {@link org.cryptomator.cryptofs.common.CiphertextFileType} can be determined. |
| 27 | + * <p> |
| 28 | + * The type is based on the presence of a valid type file. |
| 29 | + * Valid type files for a c9r dir are {@value org.cryptomator.cryptofs.common.Constants#DIR_FILE_NAME} and {@value org.cryptomator.cryptofs.common.Constants#SYMLINK_FILE_NAME}. |
| 30 | + * Valid type files for a c9s dir are the ones for c9r and {@value org.cryptomator.cryptofs.common.Constants#CONTENTS_FILE_NAME}. |
| 31 | + */ |
| 32 | +public class CiphertextFileTypeCheck implements HealthCheck { |
| 33 | + |
| 34 | + private static final Logger LOG = LoggerFactory.getLogger(CiphertextFileTypeCheck.class); |
| 35 | + private static final int MAX_TRAVERSAL_DEPTH = 3; |
| 36 | + |
| 37 | + @Override |
| 38 | + public String name() { |
| 39 | + return "Resource Type Check"; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void check(Path pathToVault, VaultConfig config, Masterkey masterkey, Cryptor cryptor, Consumer<DiagnosticResult> resultCollector) { |
| 44 | + |
| 45 | + // scan vault structure: |
| 46 | + var dataDirPath = pathToVault.resolve(Constants.DATA_DIR_NAME); |
| 47 | + var dirVisitor = new CiphertextFileTypeCheck.DirVisitor(resultCollector); |
| 48 | + try { |
| 49 | + Files.walkFileTree(dataDirPath, Set.of(), MAX_TRAVERSAL_DEPTH, dirVisitor); |
| 50 | + } catch (IOException e) { |
| 51 | + LOG.error("Traversal of data dir failed.", e); |
| 52 | + resultCollector.accept(new CheckFailed("Traversal of data dir failed. See log for details.")); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // visible for testing |
| 57 | + static class DirVisitor extends SimpleFileVisitor<Path> { |
| 58 | + |
| 59 | + private final Consumer<DiagnosticResult> resultCollector; |
| 60 | + |
| 61 | + public DirVisitor(Consumer<DiagnosticResult> resultCollector) { |
| 62 | + this.resultCollector = resultCollector; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public FileVisitResult visitFile(Path dir, BasicFileAttributes attrs) { |
| 67 | + var name = dir.getFileName().toString(); |
| 68 | + if (attrs.isDirectory() && name.endsWith(Constants.CRYPTOMATOR_FILE_SUFFIX)) { |
| 69 | + checkCiphertextType(dir, false); |
| 70 | + } else if (attrs.isDirectory() && name.endsWith(Constants.DEFLATED_FILE_SUFFIX)) { |
| 71 | + checkCiphertextType(dir, true); |
| 72 | + } |
| 73 | + return FileVisitResult.CONTINUE; |
| 74 | + } |
| 75 | + |
| 76 | + // visible for testing |
| 77 | + void checkCiphertextType(Path dir, boolean checkForContentsC9r) { |
| 78 | + var types = containedCiphertextFileTypes(dir, checkForContentsC9r); |
| 79 | + resultCollector.accept(switch (types.size()) { |
| 80 | + case 0 -> new UnknownType(dir); |
| 81 | + case 1 -> new KnownType(dir, types.iterator().next()); |
| 82 | + default -> new AmbiguousType(dir, types); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + private Set<CiphertextFileType> containedCiphertextFileTypes(Path dir, boolean checkForContentsC9r) { |
| 87 | + var result = EnumSet.noneOf(CiphertextFileType.class); |
| 88 | + if (containsDirFile(dir)) { |
| 89 | + result.add(CiphertextFileType.DIRECTORY); |
| 90 | + } |
| 91 | + if (containsSymlinkFile(dir)) { |
| 92 | + result.add(CiphertextFileType.SYMLINK); |
| 93 | + } |
| 94 | + if (checkForContentsC9r && containsContentsFile(dir)) { |
| 95 | + result.add(CiphertextFileType.FILE); |
| 96 | + } |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | + private boolean containsDirFile(Path path) { |
| 101 | + var dirc9r = path.resolve(Constants.DIR_FILE_NAME); |
| 102 | + return Files.isRegularFile(dirc9r, LinkOption.NOFOLLOW_LINKS); |
| 103 | + } |
| 104 | + |
| 105 | + private boolean containsSymlinkFile(Path path) { |
| 106 | + var symlinkc9r = path.resolve(Constants.SYMLINK_FILE_NAME); |
| 107 | + return Files.isRegularFile(symlinkc9r, LinkOption.NOFOLLOW_LINKS); |
| 108 | + } |
| 109 | + |
| 110 | + private boolean containsContentsFile(Path path) { |
| 111 | + var contentsc9r = path.resolve(Constants.CONTENTS_FILE_NAME); |
| 112 | + return Files.isRegularFile(contentsc9r, LinkOption.NOFOLLOW_LINKS); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments