mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-10-01 12:15:32 +00:00
Removed SharedFileChannel transferTo and corresponding methods
* Removed from SharedFileChannel and Test * Refactored Copier#copy(File,File) to sequence of truncated, followed by looping read and write till EOF * Changed tests accordingly * Implemented CryptoWritableFile#truncate to make things work
This commit is contained in:
+5
@@ -27,6 +27,11 @@ public interface FileContentEncryptor extends Destroyable, Closeable {
|
||||
*/
|
||||
ByteBuffer getHeader();
|
||||
|
||||
/**
|
||||
* @return the size of headers created by this {@code FileContentCryptor}. The length of headers returned by {@link #getHeader()} equals this value.
|
||||
*/
|
||||
int getHeaderSize();
|
||||
|
||||
/**
|
||||
* Appends further cleartext to this encryptor. This method might block until space becomes available.
|
||||
*
|
||||
|
||||
+5
@@ -60,6 +60,11 @@ class FileContentEncryptorImpl implements FileContentEncryptor {
|
||||
return header.toByteBuffer(headerKey, hmacSha256);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeaderSize() {
|
||||
return FileHeader.HEADER_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(ByteBuffer cleartext) throws InterruptedException {
|
||||
cleartextBytesEncrypted.add(cleartext.remaining());
|
||||
|
||||
-11
@@ -18,7 +18,6 @@ import java.util.concurrent.Future;
|
||||
import org.cryptomator.crypto.engine.FileContentCryptor;
|
||||
import org.cryptomator.crypto.engine.FileContentDecryptor;
|
||||
import org.cryptomator.filesystem.ReadableFile;
|
||||
import org.cryptomator.filesystem.WritableFile;
|
||||
import org.cryptomator.io.ByteBuffers;
|
||||
|
||||
class CryptoReadableFile implements ReadableFile {
|
||||
@@ -90,16 +89,6 @@ class CryptoReadableFile implements ReadableFile {
|
||||
return ByteBuffers.copy(bufferedCleartext, target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copyTo(WritableFile other) {
|
||||
if (other instanceof CryptoWritableFile) {
|
||||
CryptoWritableFile dst = (CryptoWritableFile) other;
|
||||
file.copyTo(dst.file);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Can not move CryptoFile to conventional File.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return file.isOpen();
|
||||
|
||||
+25
-14
@@ -28,14 +28,21 @@ class CryptoWritableFile implements WritableFile {
|
||||
|
||||
final WritableFile file;
|
||||
private final ExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||
private final FileContentEncryptor encryptor;
|
||||
private final Future<Void> writeTask;
|
||||
private final FileContentCryptor cryptor;
|
||||
|
||||
private FileContentEncryptor encryptor;
|
||||
private Future<Void> writeTask;
|
||||
|
||||
public CryptoWritableFile(FileContentCryptor cryptor, WritableFile file) {
|
||||
this.file = file;
|
||||
this.encryptor = cryptor.createFileContentEncryptor(Optional.empty(), 0);
|
||||
this.cryptor = cryptor;
|
||||
initialize(0);
|
||||
}
|
||||
|
||||
private void initialize(long firstCleartextByte) {
|
||||
encryptor = cryptor.createFileContentEncryptor(Optional.empty(), firstCleartextByte);
|
||||
writeHeader();
|
||||
this.writeTask = executorService.submit(new CiphertextWriter(file, encryptor));
|
||||
writeTask = executorService.submit(new CiphertextWriter(file, encryptor));
|
||||
}
|
||||
|
||||
private void writeHeader() {
|
||||
@@ -87,11 +94,9 @@ class CryptoWritableFile implements WritableFile {
|
||||
|
||||
@Override
|
||||
public void truncate() {
|
||||
/*
|
||||
* TODO kill writer thread (EOF) and reinitialize CryptoWritableFile
|
||||
* after truncating the file
|
||||
*/
|
||||
throw new UnsupportedOperationException("Truncate not supported yet");
|
||||
terminateAndWaitForWriteTask();
|
||||
file.truncate();
|
||||
initialize(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -108,11 +113,20 @@ class CryptoWritableFile implements WritableFile {
|
||||
public void close() {
|
||||
try {
|
||||
if (file.isOpen()) {
|
||||
encryptor.append(FileContentCryptor.EOF);
|
||||
writeTask.get();
|
||||
terminateAndWaitForWriteTask();
|
||||
writeHeader();
|
||||
// TODO append padding
|
||||
}
|
||||
} finally {
|
||||
executorService.shutdownNow();
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void terminateAndWaitForWriteTask() {
|
||||
try {
|
||||
encryptor.append(FileContentCryptor.EOF);
|
||||
writeTask.get();
|
||||
} catch (ExecutionException e) {
|
||||
if (e.getCause() instanceof UncheckedIOException || e.getCause() instanceof IOException) {
|
||||
throw new UncheckedIOException(new IOException(e));
|
||||
@@ -121,9 +135,6 @@ class CryptoWritableFile implements WritableFile {
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new UncheckedIOException(new InterruptedIOException("Task interrupted while flushing encrypted content"));
|
||||
} finally {
|
||||
executorService.shutdownNow();
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
@@ -100,6 +100,11 @@ class NoFileContentCryptor implements FileContentCryptor {
|
||||
return buf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeaderSize() {
|
||||
return Long.BYTES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(ByteBuffer cleartext) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user