-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from cryptomator/feature/fix-missing-content-dir
Implement fix for missing content dir
- Loading branch information
Showing
4 changed files
with
132 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/org/cryptomator/cryptofs/health/dirid/MissingContentDir.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.cryptomator.cryptofs.health.dirid; | ||
|
||
import org.cryptomator.cryptofs.CryptoPathMapper; | ||
import org.cryptomator.cryptofs.DirectoryIdBackup; | ||
import org.cryptomator.cryptofs.VaultConfig; | ||
import org.cryptomator.cryptofs.common.Constants; | ||
import org.cryptomator.cryptofs.health.api.DiagnosticResult; | ||
import org.cryptomator.cryptolib.api.Cryptor; | ||
import org.cryptomator.cryptolib.api.Masterkey; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
import static org.cryptomator.cryptofs.health.api.CommonDetailKeys.DIR_ID; | ||
import static org.cryptomator.cryptofs.health.api.CommonDetailKeys.DIR_ID_FILE; | ||
|
||
/** | ||
* Valid dir.c9r file, nonexisting content dir | ||
*/ | ||
public class MissingContentDir implements DiagnosticResult { | ||
|
||
final String dirId; | ||
final Path dirIdFile; | ||
|
||
MissingContentDir(String dirId, Path dirIdFile) { | ||
this.dirId = dirId; | ||
this.dirIdFile = dirIdFile; | ||
} | ||
|
||
@Override | ||
public Severity getSeverity() { | ||
return Severity.WARN; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("dir.c9r file (%s) points to non-existing directory.", dirIdFile); | ||
} | ||
|
||
@Override | ||
public Map<String, String> details() { | ||
return Map.of(DIR_ID, dirId, // | ||
DIR_ID_FILE, dirIdFile.toString()); | ||
} | ||
|
||
@Override | ||
public void fix(Path pathToVault, VaultConfig config, Masterkey masterkey, Cryptor cryptor) throws IOException { | ||
var dirIdHash = cryptor.fileNameCryptor().hashDirectoryId(dirId); | ||
Path dirPath = pathToVault.resolve(Constants.DATA_DIR_NAME).resolve(dirIdHash.substring(0, 2)).resolve(dirIdHash.substring(2, 30)); | ||
Files.createDirectories(dirPath); | ||
createDirIdBackupFile(cryptor, new CryptoPathMapper.CiphertextDirectory(dirId, dirPath)); | ||
} | ||
|
||
//visible for testing | ||
void createDirIdBackupFile(Cryptor cryptor, CryptoPathMapper.CiphertextDirectory cipherDirObj) throws IOException { | ||
new DirectoryIdBackup(cryptor).execute(cipherDirObj); | ||
} | ||
} |
41 changes: 0 additions & 41 deletions
41
src/main/java/org/cryptomator/cryptofs/health/dirid/MissingDirectory.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
src/test/java/org/cryptomator/cryptofs/health/dirid/MissingContentDirTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.cryptomator.cryptofs.health.dirid; | ||
|
||
import org.cryptomator.cryptofs.CryptoPathMapper; | ||
import org.cryptomator.cryptofs.VaultConfig; | ||
import org.cryptomator.cryptolib.api.Cryptor; | ||
import org.cryptomator.cryptolib.api.FileNameCryptor; | ||
import org.cryptomator.cryptolib.api.Masterkey; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.mockito.ArgumentMatcher; | ||
import org.mockito.Mockito; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.LinkOption; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
|
||
public class MissingContentDirTest { | ||
|
||
@TempDir | ||
public Path pathToVault; | ||
|
||
private MissingContentDir result; | ||
private String dirId; | ||
private Cryptor cryptor; | ||
private FileNameCryptor fileNameCryptor; | ||
|
||
@BeforeEach | ||
public void init() { | ||
Path p = Mockito.mock(Path.class, "ignored"); | ||
dirId = "1234-456789-1234"; | ||
result = new MissingContentDir(dirId, p); | ||
|
||
cryptor = Mockito.mock(Cryptor.class); | ||
fileNameCryptor = Mockito.mock(FileNameCryptor.class); | ||
Mockito.doReturn(fileNameCryptor).when(cryptor).fileNameCryptor(); | ||
Mockito.doReturn(fileNameCryptor).when(cryptor).fileNameCryptor(); | ||
} | ||
|
||
@DisplayName("After fix the content dir including dirId file exists ") | ||
@Test | ||
public void testFix() throws IOException { | ||
var dirIdHash = "ridiculous-30-char-pseudo-hash"; | ||
Mockito.doReturn(dirIdHash).when(fileNameCryptor).hashDirectoryId(dirId); | ||
var resultSpy = Mockito.spy(result); | ||
Mockito.doNothing().when(resultSpy).createDirIdBackupFile(Mockito.any(), Mockito.any()); | ||
|
||
resultSpy.fix(pathToVault, Mockito.mock(VaultConfig.class), Mockito.mock(Masterkey.class), cryptor); | ||
|
||
var expectedPath = pathToVault.resolve("d/ri/diculous-30-char-pseudo-hash"); | ||
ArgumentMatcher<CryptoPathMapper.CiphertextDirectory> cipherDirMatcher = obj -> obj.dirId.equals(dirId) && obj.path.endsWith(expectedPath); | ||
Mockito.verify(resultSpy, Mockito.times(1)).createDirIdBackupFile(Mockito.eq(cryptor), Mockito.argThat(cipherDirMatcher)); | ||
var attr = Assertions.assertDoesNotThrow(() -> Files.readAttributes(expectedPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)); | ||
Assertions.assertTrue(attr.isDirectory()); | ||
} | ||
|
||
@DisplayName("If dirId file creation fails, fix fails ") | ||
@Test | ||
public void testFixFailsOnFailingDirIdFile() throws IOException { | ||
var dirIdHash = "ridiculous-30-char-pseudo-hash"; | ||
Mockito.doReturn(dirIdHash).when(fileNameCryptor).hashDirectoryId(dirId); | ||
var resultSpy = Mockito.spy(result); | ||
Mockito.doThrow(new IOException("Access denied")).when(resultSpy).createDirIdBackupFile(Mockito.any(), Mockito.any()); | ||
|
||
Assertions.assertThrows(IOException.class, () -> resultSpy.fix(pathToVault, Mockito.mock(VaultConfig.class), Mockito.mock(Masterkey.class), cryptor)); | ||
} | ||
} |