Added new filesystem layer for read/write alignment with boundary of encrypted chunks.

This commit is contained in:
Sebastian Stenzel
2015-12-29 13:41:47 +01:00
parent c21654eac1
commit 44d1250986
15 changed files with 283 additions and 117 deletions
@@ -0,0 +1,27 @@
package org.cryptomator.filesystem.blockaligned;
import java.io.UncheckedIOException;
import org.cryptomator.filesystem.File;
import org.cryptomator.filesystem.delegating.DelegatingFile;
class BlockAlignedFile extends DelegatingFile<BlockAlignedReadableFile, BlockAlignedWritableFile, BlockAlignedFolder> {
private final int blockSize;
public BlockAlignedFile(BlockAlignedFolder parent, File delegate, int blockSize) {
super(parent, delegate);
this.blockSize = blockSize;
}
@Override
public BlockAlignedReadableFile openReadable() throws UncheckedIOException {
return new BlockAlignedReadableFile(delegate.openReadable(), blockSize);
}
@Override
public BlockAlignedWritableFile openWritable() throws UncheckedIOException {
return new BlockAlignedWritableFile(delegate.openWritable(), blockSize);
}
}
@@ -0,0 +1,12 @@
package org.cryptomator.filesystem.blockaligned;
import org.cryptomator.filesystem.FileSystem;
import org.cryptomator.filesystem.Folder;
public class BlockAlignedFileSystem extends BlockAlignedFolder implements FileSystem {
public BlockAlignedFileSystem(Folder delegate, int blockSize) {
super(null, delegate, blockSize);
}
}
@@ -0,0 +1,26 @@
package org.cryptomator.filesystem.blockaligned;
import org.cryptomator.filesystem.File;
import org.cryptomator.filesystem.Folder;
import org.cryptomator.filesystem.delegating.DelegatingFolder;
class BlockAlignedFolder extends DelegatingFolder<BlockAlignedReadableFile, BlockAlignedWritableFile, BlockAlignedFolder, BlockAlignedFile> {
private final int blockSize;
public BlockAlignedFolder(BlockAlignedFolder parent, Folder delegate, int blockSize) {
super(parent, delegate);
this.blockSize = blockSize;
}
@Override
protected BlockAlignedFile file(File delegate) {
return new BlockAlignedFile(this, delegate, blockSize);
}
@Override
protected BlockAlignedFolder folder(Folder delegate) {
return new BlockAlignedFolder(this, delegate, blockSize);
}
}
@@ -0,0 +1,27 @@
package org.cryptomator.filesystem.blockaligned;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import org.cryptomator.filesystem.ReadableFile;
import org.cryptomator.filesystem.delegating.DelegatingReadableFile;
class BlockAlignedReadableFile extends DelegatingReadableFile {
public BlockAlignedReadableFile(ReadableFile delegate, int blockSize) {
super(delegate);
}
@Override
public void position(long position) throws UncheckedIOException {
// TODO Auto-generated method stub
super.position(position);
}
@Override
public int read(ByteBuffer target) throws UncheckedIOException {
// TODO Auto-generated method stub
return super.read(target);
}
}
@@ -0,0 +1,27 @@
package org.cryptomator.filesystem.blockaligned;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import org.cryptomator.filesystem.WritableFile;
import org.cryptomator.filesystem.delegating.DelegatingWritableFile;
class BlockAlignedWritableFile extends DelegatingWritableFile {
public BlockAlignedWritableFile(WritableFile delegate, int blockSize) {
super(delegate);
}
@Override
public void position(long position) throws UncheckedIOException {
// TODO Auto-generated method stub
super.position(position);
}
@Override
public int write(ByteBuffer source) throws UncheckedIOException {
// TODO Auto-generated method stub
return super.write(source);
}
}
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2015 Sebastian Stenzel and others.
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
*
* Contributors:
* Sebastian Stenzel - initial API and implementation
*******************************************************************************/
/**
* Provides a decoration layer for the {@link org.cryptomator.filesystem Filesystem API}, which guarantees, that all read/write attempts to underlying files always begin at a block start position.
* Block start positions are integer multiples of a block size + a fixed block shift.
* <p>
* In general the formula to align a requested read with a physical read is <code>floor(x / blockSize) * blockSize</code><br/>
* For example <code>blockSize=10</code> result in the following block-aligned read/write attempts:
*
* <table>
* <thead>
* <tr><th>Requested Read</th><th>Physical Read</th></tr>
* </thead>
* <tbody>
* <tr><td>0</td><td>0</td></td>
* <tr><td>5</td><td>0</td></td>
* <tr><td>9</td><td>0</td></td>
* <tr><td>10</td><td>10</td></td>
* <tr><td>11</td><td>10</td></td>
* <tr><td>35</td><td>30</td></td>
* </tbody>
* </table>
*/
package org.cryptomator.filesystem.blockaligned;