adjusted to updated API, restored Folder.copy and Folder.move

This commit is contained in:
Sebastian Stenzel
2015-12-15 02:27:41 +01:00
parent 3c7651a78a
commit 762f362784
10 changed files with 273 additions and 122 deletions
@@ -9,7 +9,6 @@
package org.cryptomator.filesystem.inmem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.time.Instant;
@@ -30,9 +29,9 @@ class InMemoryFile extends InMemoryNode implements ReadableFile, WritableFile {
}
@Override
public ReadableFile openReadable(long timeout, TimeUnit unit) throws IOException, TimeoutException {
public ReadableFile openReadable(long timeout, TimeUnit unit) throws TimeoutException {
if (!exists()) {
throw new FileNotFoundException(this.name() + " does not exist");
throw new UncheckedIOException(new FileNotFoundException(this.name() + " does not exist"));
}
try {
if (!lock.readLock().tryLock(timeout, unit)) {
@@ -45,7 +44,7 @@ class InMemoryFile extends InMemoryNode implements ReadableFile, WritableFile {
}
@Override
public WritableFile openWritable(long timeout, TimeUnit unit) throws IOException, TimeoutException {
public WritableFile openWritable(long timeout, TimeUnit unit) throws TimeoutException {
try {
if (!lock.writeLock().tryLock(timeout, unit)) {
throw new TimeoutException("Failed to open " + name() + " for writing within time limit.");
@@ -54,38 +53,34 @@ class InMemoryFile extends InMemoryNode implements ReadableFile, WritableFile {
Thread.currentThread().interrupt();
}
final InMemoryFolder parent = parent().get();
try {
parent.children.compute(this.name(), (k, v) -> {
if (v != null && v != this) {
throw new IllegalStateException("More than one representation of same file");
}
return this;
});
} catch (UncheckedIOException e) {
throw e.getCause();
}
parent.children.compute(this.name(), (k, v) -> {
if (v != null && v != this) {
throw new IllegalStateException("More than one representation of same file");
}
return this;
});
return this;
}
@Override
public void read(ByteBuffer target) throws IOException {
public void read(ByteBuffer target) {
this.read(target, 0);
}
@Override
public void read(ByteBuffer target, int position) throws IOException {
public void read(ByteBuffer target, int position) {
content.rewind();
content.position(position);
target.put(content);
}
@Override
public void write(ByteBuffer source) throws IOException {
public void write(ByteBuffer source) {
this.write(source, content.position());
}
@Override
public void write(ByteBuffer source, int position) throws IOException {
public void write(ByteBuffer source, int position) {
assert content != null;
if (position + source.remaining() > content.remaining()) {
// create bigger buffer
@@ -98,15 +93,26 @@ class InMemoryFile extends InMemoryNode implements ReadableFile, WritableFile {
}
@Override
public WritableFile moveTo(WritableFile other) throws IOException {
this.copyTo(other);
this.delete();
return other;
public void setLastModified(Instant instant) {
this.lastModified = instant;
}
@Override
public void setLastModified(Instant instant) {
this.lastModified = instant;
public void truncate() {
content = ByteBuffer.wrap(new byte[0]);
}
@Override
public void copyTo(WritableFile other) {
content.rewind();
other.truncate();
other.write(content);
}
@Override
public void moveTo(WritableFile other) {
this.copyTo(other);
this.delete();
}
@Override
@@ -117,23 +123,11 @@ class InMemoryFile extends InMemoryNode implements ReadableFile, WritableFile {
// returning null removes the entry.
return null;
});
assert!this.exists();
}
@Override
public void truncate() {
content = ByteBuffer.wrap(new byte[0]);
}
@Override
public WritableFile copyTo(WritableFile other) throws IOException {
content.rewind();
other.truncate();
other.write(content);
return other;
}
@Override
public void close() throws IOException {
public void close() {
if (lock.isWriteLockedByCurrentThread()) {
lock.writeLock().unlock();
} else if (lock.getReadHoldCount() > 0) {
@@ -9,7 +9,6 @@
package org.cryptomator.filesystem.inmem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileAlreadyExistsException;
import java.time.Instant;
@@ -67,36 +66,51 @@ class InMemoryFolder extends InMemoryNode implements Folder {
}
@Override
public void create(FolderCreateMode mode) throws IOException {
public void create(FolderCreateMode mode) {
if (exists()) {
return;
}
if (!parent.exists() && FolderCreateMode.FAIL_IF_PARENT_IS_MISSING.equals(mode)) {
throw new FileNotFoundException(parent.name);
throw new UncheckedIOException(new FileNotFoundException(parent.name));
} else if (!parent.exists() && FolderCreateMode.INCLUDING_PARENTS.equals(mode)) {
parent.create(mode);
}
assert parent.exists();
try {
parent.children.compute(this.name(), (k, v) -> {
if (v == null) {
this.lastModified = Instant.now();
return this;
} else {
throw new UncheckedIOException(new FileExistsException(k));
}
});
} catch (UncheckedIOException e) {
throw e.getCause();
parent.children.compute(this.name(), (k, v) -> {
if (v == null) {
this.lastModified = Instant.now();
return this;
} else {
throw new UncheckedIOException(new FileExistsException(k));
}
});
assert this.exists();
}
@Override
public void moveTo(Folder target) {
if (target.exists()) {
target.delete();
}
assert!target.exists();
target.create(FolderCreateMode.INCLUDING_PARENTS);
this.copyTo(target);
this.delete();
assert!this.exists();
}
@Override
public void delete() {
// delete subfolder recursively:
folders().forEach(Folder::delete);
// delete direct children (this deletes files):
this.children.clear();
// remove ourself from parent:
parent.children.computeIfPresent(name, (k, v) -> {
// returning null removes the entry.
return null;
});
assert!this.exists();
}
@Override
@@ -8,7 +8,6 @@
*******************************************************************************/
package org.cryptomator.filesystem.inmem;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -25,7 +24,7 @@ import org.junit.Test;
public class InMemoryFileSystemTest {
@Test
public void testFolderCreation() throws IOException {
public void testFolderCreation() {
final FileSystem fs = new InMemoryFileSystem();
Folder fooFolder = fs.folder("foo");
@@ -54,7 +53,7 @@ public class InMemoryFileSystemTest {
}
@Test
public void testFileReadCopyMoveWrite() throws IOException, TimeoutException {
public void testFileReadCopyMoveWrite() throws TimeoutException {
final FileSystem fs = new InMemoryFileSystem();
File fooFile = fs.file("foo.txt");
@@ -96,7 +95,36 @@ public class InMemoryFileSystemTest {
readable.read(readBuf, 6);
}
Assert.assertEquals("world", new String(readBuf.array()));
}
@Test
public void testFolderCopy() throws TimeoutException {
final FileSystem fs = new InMemoryFileSystem();
final Folder fooBarFolder = fs.folder("foo").folder("bar");
final Folder qweAsdFolder = fs.folder("qwe").folder("asd");
final Folder qweAsdBarFolder = qweAsdFolder.folder("bar");
final File test1File = fooBarFolder.file("test1.txt");
final File test2File = fooBarFolder.file("test2.txt");
fooBarFolder.create(FolderCreateMode.INCLUDING_PARENTS);
// create some files inside foo/bar/
try (WritableFile writable1 = test1File.openWritable(1, TimeUnit.SECONDS); //
WritableFile writable2 = test2File.openWritable(1, TimeUnit.SECONDS)) {
writable1.write(ByteBuffer.wrap("hello".getBytes()));
writable2.write(ByteBuffer.wrap("world".getBytes()));
}
Assert.assertTrue(test1File.exists());
Assert.assertTrue(test2File.exists());
// copy foo/bar/ to qwe/asd/ (result is qwe/asd/bar/file1.txt & qwe/asd/bar/file2.txt)
fooBarFolder.copyTo(qweAsdFolder);
Assert.assertTrue(qweAsdBarFolder.exists());
Assert.assertEquals(1, qweAsdFolder.folders().count());
Assert.assertEquals(2, qweAsdBarFolder.files().count());
// make sure original files still exist:
Assert.assertTrue(test1File.exists());
Assert.assertTrue(test2File.exists());
}
}