mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-08-20 14:16:01 +00:00
more fault-tolerant behaviour when mapping of long filenames couldn't be found.
This commit is contained in:
+5
-3
@@ -8,8 +8,6 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.cryptomator.filesystem.shortening;
|
package org.cryptomator.filesystem.shortening;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.UncheckedIOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
@@ -19,9 +17,12 @@ import org.apache.commons.codec.binary.BaseNCodec;
|
|||||||
import org.cryptomator.filesystem.File;
|
import org.cryptomator.filesystem.File;
|
||||||
import org.cryptomator.filesystem.Folder;
|
import org.cryptomator.filesystem.Folder;
|
||||||
import org.cryptomator.io.FileContents;
|
import org.cryptomator.io.FileContents;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
class FilenameShortener {
|
class FilenameShortener {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(FilenameShortener.class);
|
||||||
private static final String LONG_NAME_FILE_EXT = ".lng";
|
private static final String LONG_NAME_FILE_EXT = ".lng";
|
||||||
private static final ThreadLocal<MessageDigest> SHA1 = new ThreadLocalSha1();
|
private static final ThreadLocal<MessageDigest> SHA1 = new ThreadLocalSha1();
|
||||||
private static final BaseNCodec BASE32 = new Base32();
|
private static final BaseNCodec BASE32 = new Base32();
|
||||||
@@ -71,7 +72,8 @@ class FilenameShortener {
|
|||||||
private String loadMapping(String shortName) {
|
private String loadMapping(String shortName) {
|
||||||
final File mappingFile = mappingFile(shortName);
|
final File mappingFile = mappingFile(shortName);
|
||||||
if (!mappingFile.exists()) {
|
if (!mappingFile.exists()) {
|
||||||
throw new UncheckedIOException(new FileNotFoundException("Mapping file not found " + mappingFile));
|
LOG.warn("Mapping file not found: " + mappingFile);
|
||||||
|
return shortName;
|
||||||
} else {
|
} else {
|
||||||
return FileContents.UTF_8.readContents(mappingFile);
|
return FileContents.UTF_8.readContents(mappingFile);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -8,8 +8,6 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.cryptomator.filesystem.shortening;
|
package org.cryptomator.filesystem.shortening;
|
||||||
|
|
||||||
import java.io.UncheckedIOException;
|
|
||||||
|
|
||||||
import org.cryptomator.filesystem.FileSystem;
|
import org.cryptomator.filesystem.FileSystem;
|
||||||
import org.cryptomator.filesystem.inmem.InMemoryFileSystem;
|
import org.cryptomator.filesystem.inmem.InMemoryFileSystem;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@@ -45,12 +43,12 @@ public class FilenameShortenerTest {
|
|||||||
Assert.assertEquals("short", shortener.inflate("short"));
|
Assert.assertEquals("short", shortener.inflate("short"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = UncheckedIOException.class)
|
@Test
|
||||||
public void testInflateWithoutMappingFile() {
|
public void testInflateWithoutMappingFile() {
|
||||||
FileSystem fs = new InMemoryFileSystem();
|
FileSystem fs = new InMemoryFileSystem();
|
||||||
FilenameShortener shortener = new FilenameShortener(fs, 10);
|
FilenameShortener shortener = new FilenameShortener(fs, 10);
|
||||||
|
|
||||||
shortener.inflate("iJustMadeThisNameUp.lng");
|
Assert.assertEquals("iJustMadeThisNameUp.lng", shortener.inflate("iJustMadeThisNameUp.lng"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+26
@@ -16,6 +16,7 @@ import static org.junit.Assert.assertThat;
|
|||||||
|
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
@@ -102,6 +103,31 @@ public class ShorteningFileSystemTest {
|
|||||||
Assert.assertTrue(correspondingMetadataFile.exists());
|
Assert.assertTrue(correspondingMetadataFile.exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInflate() {
|
||||||
|
final FileSystem underlyingFs = new InMemoryFileSystem();
|
||||||
|
final Folder metadataRoot = underlyingFs.folder(METADATA_DIR_NAME);
|
||||||
|
final FileSystem fs = new ShorteningFileSystem(underlyingFs, METADATA_DIR_NAME, 10);
|
||||||
|
final File correspondingMetadataFile = metadataRoot.folder("QM").folder("JL").file("QMJL5GQUETRX2YRV6XDTJQ6NNM7IEUHP.lng");
|
||||||
|
final Folder shortenedFolder = underlyingFs.folder("QMJL5GQUETRX2YRV6XDTJQ6NNM7IEUHP.lng");
|
||||||
|
shortenedFolder.create();
|
||||||
|
correspondingMetadataFile.parent().get().create();
|
||||||
|
try (WritableFile w = correspondingMetadataFile.openWritable()) {
|
||||||
|
w.write(ByteBuffer.wrap("morethantenchars".getBytes(StandardCharsets.UTF_8)));
|
||||||
|
}
|
||||||
|
Assert.assertTrue(correspondingMetadataFile.exists());
|
||||||
|
Assert.assertTrue(fs.folders().map(Folder::name).anyMatch(n -> n.equals("morethantenchars")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInflateFailedDueToMissingMapping() {
|
||||||
|
final FileSystem underlyingFs = new InMemoryFileSystem();
|
||||||
|
final FileSystem fs = new ShorteningFileSystem(underlyingFs, METADATA_DIR_NAME, 10);
|
||||||
|
final Folder shortenedFolder = underlyingFs.folder("QMJL5GQUETRX2YRV6XDTJQ6NNM7IEUHP.lng");
|
||||||
|
shortenedFolder.create();
|
||||||
|
Assert.assertTrue(fs.folders().map(Folder::name).anyMatch(n -> n.equals("QMJL5GQUETRX2YRV6XDTJQ6NNM7IEUHP.lng")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMoveLongFolders() {
|
public void testMoveLongFolders() {
|
||||||
final FileSystem underlyingFs = new InMemoryFileSystem();
|
final FileSystem underlyingFs = new InMemoryFileSystem();
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<Configuration status="WARN">
|
||||||
|
|
||||||
|
<Appenders>
|
||||||
|
<Console name="Console" target="SYSTEM_OUT">
|
||||||
|
<PatternLayout pattern="%16d %-5p [%c{1}:%L] %m%n" />
|
||||||
|
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="ACCEPT" />
|
||||||
|
</Console>
|
||||||
|
<Console name="StdErr" target="SYSTEM_ERR">
|
||||||
|
<PatternLayout pattern="%16d %-5p [%c{1}:%L] %m%n" />
|
||||||
|
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY" />
|
||||||
|
</Console>
|
||||||
|
</Appenders>
|
||||||
|
|
||||||
|
<Loggers>
|
||||||
|
<Root level="DEBUG">
|
||||||
|
<AppenderRef ref="Console" />
|
||||||
|
<AppenderRef ref="StdErr" />
|
||||||
|
</Root>
|
||||||
|
</Loggers>
|
||||||
|
|
||||||
|
</Configuration>
|
||||||
Reference in New Issue
Block a user