mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-09-13 11:34:33 +00:00
- moved method from WritableFile to File: moveTo, setLastModified, setCreationTime, delete
- moved method from File and Folder to Node: setLastModified, setCreationTime, delete
This commit is contained in:
+33
-17
@@ -37,6 +37,22 @@ class InMemoryFile extends InMemoryNode implements File {
|
||||
content.flip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveTo(File destination) throws UncheckedIOException {
|
||||
if (destination instanceof InMemoryFile) {
|
||||
internalMoveTo((InMemoryFile) destination);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Can only move an InMemoryFile to another InMemoryFile");
|
||||
}
|
||||
}
|
||||
|
||||
private void internalMoveTo(InMemoryFile destination) {
|
||||
this.content.rewind();
|
||||
destination.create();
|
||||
destination.content = this.content;
|
||||
this.delete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadableFile openReadable() {
|
||||
if (!exists()) {
|
||||
@@ -62,21 +78,8 @@ class InMemoryFile extends InMemoryNode implements File {
|
||||
final WriteLock writeLock = lock.writeLock();
|
||||
writeLock.lock();
|
||||
try {
|
||||
final InMemoryFolder parent = parent().get();
|
||||
parent.existingChildren.compute(this.name(), (k, v) -> {
|
||||
if (v != null && v != this) {
|
||||
// other file or folder with same name already exists.
|
||||
throw new UncheckedIOException(new FileAlreadyExistsException(k));
|
||||
} else {
|
||||
if (v == null) {
|
||||
assert!content.hasRemaining();
|
||||
this.creationTime = Instant.now();
|
||||
}
|
||||
this.lastModified = Instant.now();
|
||||
return this;
|
||||
}
|
||||
});
|
||||
final WritableFile result = new InMemoryWritableFile(this::setLastModified, this::setCreationTime, this::getContent, this::setContent, this::delete, writeLock);
|
||||
create();
|
||||
final WritableFile result = new InMemoryWritableFile(this::getContent, this::setContent, writeLock);
|
||||
success = true;
|
||||
return result;
|
||||
} finally {
|
||||
@@ -86,8 +89,21 @@ class InMemoryFile extends InMemoryNode implements File {
|
||||
}
|
||||
}
|
||||
|
||||
private void setLastModified(Instant lastModified) {
|
||||
this.lastModified = lastModified;
|
||||
private void create() {
|
||||
final InMemoryFolder parent = parent().get();
|
||||
parent.existingChildren.compute(this.name(), (k, v) -> {
|
||||
if (v != null && v != this) {
|
||||
// other file or folder with same name already exists.
|
||||
throw new UncheckedIOException(new FileAlreadyExistsException(k));
|
||||
} else {
|
||||
if (v == null) {
|
||||
assert!content.hasRemaining();
|
||||
this.creationTime = Instant.now();
|
||||
}
|
||||
this.lastModified = Instant.now();
|
||||
return this;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ByteBuffer getContent() {
|
||||
|
||||
+20
-15
@@ -16,7 +16,7 @@ import java.util.Optional;
|
||||
|
||||
import org.cryptomator.filesystem.Node;
|
||||
|
||||
class InMemoryNode implements Node {
|
||||
abstract class InMemoryNode implements Node {
|
||||
|
||||
protected final InMemoryFolder parent;
|
||||
protected final String name;
|
||||
@@ -53,6 +53,25 @@ class InMemoryNode implements Node {
|
||||
return lastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLastModified(Instant lastModified) throws UncheckedIOException {
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Instant> creationTime() throws UncheckedIOException {
|
||||
if (exists()) {
|
||||
return Optional.of(creationTime);
|
||||
} else {
|
||||
throw new UncheckedIOException(new IOException("Node does not exist"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCreationTime(Instant creationTime) throws UncheckedIOException {
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
@@ -74,18 +93,4 @@ class InMemoryNode implements Node {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Instant> creationTime() throws UncheckedIOException {
|
||||
if (exists()) {
|
||||
return Optional.of(creationTime);
|
||||
} else {
|
||||
throw new UncheckedIOException(new IOException("Node does not exist"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCreationTime(Instant creationTime) throws UncheckedIOException {
|
||||
this.creationTime = creationTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-45
@@ -10,7 +10,6 @@ package org.cryptomator.filesystem.inmem;
|
||||
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
@@ -20,23 +19,17 @@ import org.cryptomator.io.ByteBuffers;
|
||||
|
||||
public class InMemoryWritableFile implements WritableFile {
|
||||
|
||||
private final Consumer<Instant> lastModifiedSetter;
|
||||
private final Consumer<Instant> creationTimeSetter;
|
||||
private final Supplier<ByteBuffer> contentGetter;
|
||||
private final Consumer<ByteBuffer> contentSetter;
|
||||
private final Runnable deleter;
|
||||
private final WriteLock writeLock;
|
||||
|
||||
private boolean open = true;
|
||||
private volatile int position = 0;
|
||||
|
||||
public InMemoryWritableFile(Consumer<Instant> lastModifiedSetter, Consumer<Instant> creationTimeSetter, Supplier<ByteBuffer> contentGetter, Consumer<ByteBuffer> contentSetter, Runnable deleter, WriteLock writeLock) {
|
||||
this.lastModifiedSetter = lastModifiedSetter;
|
||||
public InMemoryWritableFile(Supplier<ByteBuffer> contentGetter, Consumer<ByteBuffer> contentSetter, WriteLock writeLock) {
|
||||
this.contentGetter = contentGetter;
|
||||
this.contentSetter = contentSetter;
|
||||
this.deleter = deleter;
|
||||
this.writeLock = writeLock;
|
||||
this.creationTimeSetter = creationTimeSetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,37 +37,6 @@ public class InMemoryWritableFile implements WritableFile {
|
||||
return open;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveTo(WritableFile other) throws UncheckedIOException {
|
||||
if (other instanceof InMemoryWritableFile) {
|
||||
internalMoveTo((InMemoryWritableFile) other);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Can only move an InMemoryWritableFile to another InMemoryWritableFile");
|
||||
}
|
||||
}
|
||||
|
||||
private void internalMoveTo(InMemoryWritableFile destination) {
|
||||
try {
|
||||
destination.contentSetter.accept(this.contentGetter.get());
|
||||
destination.contentGetter.get().rewind();
|
||||
deleter.run();
|
||||
} finally {
|
||||
open = false;
|
||||
destination.open = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLastModified(Instant instant) throws UncheckedIOException {
|
||||
lastModifiedSetter.accept(instant);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete() throws UncheckedIOException {
|
||||
deleter.run();
|
||||
open = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void truncate() throws UncheckedIOException {
|
||||
contentSetter.accept(ByteBuffer.allocate(0));
|
||||
@@ -111,12 +73,6 @@ public class InMemoryWritableFile implements WritableFile {
|
||||
public void close() throws UncheckedIOException {
|
||||
open = false;
|
||||
writeLock.unlock();
|
||||
lastModifiedSetter.accept(Instant.now());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCreationTime(Instant instant) throws UncheckedIOException {
|
||||
creationTimeSetter.accept(instant);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-5
@@ -116,11 +116,7 @@ public class InMemoryFileSystemTest {
|
||||
|
||||
// move bar to baz
|
||||
File bazFile = fs.file("baz.txt");
|
||||
try (WritableFile src = barFile.openWritable()) {
|
||||
try (WritableFile dst = bazFile.openWritable()) {
|
||||
src.moveTo(dst);
|
||||
}
|
||||
}
|
||||
barFile.moveTo(bazFile);
|
||||
Assert.assertFalse(barFile.exists());
|
||||
Assert.assertTrue(bazFile.exists());
|
||||
|
||||
|
||||
+13
-11
@@ -12,6 +12,7 @@ import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Instant;
|
||||
|
||||
import org.cryptomator.filesystem.WritableFile;
|
||||
@@ -27,38 +28,39 @@ public class InMemoryFileTest {
|
||||
@Test
|
||||
public void testCreationTimeOfNonExistingFileThrowsUncheckedIOException() {
|
||||
InMemoryFileSystem fileSystem = new InMemoryFileSystem();
|
||||
InMemoryFile inTest = fileSystem.file("foo");
|
||||
InMemoryFile file = fileSystem.file("foo");
|
||||
|
||||
thrown.expect(UncheckedIOException.class);
|
||||
|
||||
inTest.creationTime();
|
||||
file.creationTime();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreationTimeOfCreatedFileIsSetToInstantDuringCreation() {
|
||||
InMemoryFileSystem fileSystem = new InMemoryFileSystem();
|
||||
InMemoryFile inTest = fileSystem.file("foo");
|
||||
InMemoryFile file = fileSystem.file("foo");
|
||||
|
||||
Instant minCreationTime = Instant.now();
|
||||
Instant maxCreationTime;
|
||||
try (WritableFile writable = inTest.openWritable()) {
|
||||
try (WritableFile writable = file.openWritable()) {
|
||||
maxCreationTime = Instant.now();
|
||||
}
|
||||
|
||||
assertThat(inTest.creationTime().get().isBefore(minCreationTime), is(false));
|
||||
assertThat(inTest.creationTime().get().isAfter(maxCreationTime), is(false));
|
||||
assertThat(file.creationTime().get().isBefore(minCreationTime), is(false));
|
||||
assertThat(file.creationTime().get().isAfter(maxCreationTime), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreationTimeSetInWritableFileIsSaved() {
|
||||
public void testCreationTimeSetIsSaved() {
|
||||
Instant creationTime = Instant.parse("2015-03-23T21:11:32Z");
|
||||
InMemoryFileSystem fileSystem = new InMemoryFileSystem();
|
||||
InMemoryFile inTest = fileSystem.file("foo");
|
||||
try (WritableFile writable = inTest.openWritable()) {
|
||||
writable.setCreationTime(creationTime);
|
||||
InMemoryFile file = fileSystem.file("foo");
|
||||
try (WritableFile writable = file.openWritable()) {
|
||||
writable.write(ByteBuffer.allocate(0));
|
||||
}
|
||||
|
||||
assertThat(inTest.creationTime().get(), is(creationTime));
|
||||
file.setCreationTime(creationTime);
|
||||
assertThat(file.creationTime().get(), is(creationTime));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user