Added first draft of file system API

This commit is contained in:
Markus Kreusch
2015-12-13 21:43:37 +01:00
parent 3adfe6871b
commit 3971d3afd5
13 changed files with 265 additions and 0 deletions

1
main/filesystem-api/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target/

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2015 Markus Kreusch
This file is licensed under the terms of the MIT license.
See the LICENSE.txt file for more info.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.cryptomator</groupId>
<artifactId>main</artifactId>
<version>0.11.0-SNAPSHOT</version>
</parent>
<artifactId>filesystem-api</artifactId>
<name>Cryptomator filesystem API</name>
<dependencies>
<!-- commons -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public interface File extends Node {
ReadableFile openReadable(long timeout, TimeUnit unit) throws IOException;
WritableFile openWritable(long timeout, TimeUnit unit) throws IOException;
}

View File

@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.util.Optional;
/**
* The root folder of a file system.
*
* @author Markus Kreusch
*/
public interface FileSystem extends Folder {
@Override
default Optional<Folder> parent() {
return Optional.empty();
}
}

View File

@@ -0,0 +1,65 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.stream.Stream;
/**
* A {@link Folder} in a {@link FileSystem}.
*
* @author Markus Kreusch
*/
public interface Folder extends Node {
/**
* <p>
* Creates a {@link Stream} over all child nodes of this {@code Folder}.
* <p>
* <b>Note:</b> The {@link Stream} may be lazily populated and thus
* {@link IOException IOExceptions} may occurs after this method returned.
* In this case implementors should throw a {@link UncheckedIOException}
* from any method that produces an {@link IOException}. Thus users should
* expect {@link UncheckedIOException UncheckedIOExceptions} when invoking
* methods on the returned {@code Stream}.
*
* @return the created {@code Stream}
* @throws IOException
* if an {@link IOException} occurs while initializing the
* stream
*/
Stream<Node> children() throws IOException;
File file(String name) throws IOException;
Folder folder(String name) throws IOException;
void create(FolderCreateMode mode) throws IOException;
void delete() throws IOException;
/**
* @return the result of {@link #children()} filtered to contain only
* {@link File Files}
*/
default Stream<File> files() throws IOException {
return children() //
.filter(File.class::isInstance) //
.map(File.class::cast);
}
/**
* @return the result of {@link #children()} filtered to contain only
* {@link Folder Folders}
*/
default Stream<Folder> folders() throws IOException {
return children() //
.filter(Folder.class::isInstance) //
.map(Folder.class::cast);
}
}

View File

@@ -0,0 +1,12 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
public enum FolderCreateMode {
FAIL_IF_PARENT_IS_MISSING, INCLUDING_PARENTS
}

View File

@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.time.Instant;
import java.util.Optional;
/**
* Represents a node, namely a {@link File} or {@link Folder}, in a
* {@link FileSystem}.
*
* @author Markus Kreusch
* @see Folder
* @see File
*/
public interface Node {
String name() throws IOException;
Optional<Folder> parent() throws IOException;
boolean exists() throws IOException;
Instant lastModified() throws IOException;
}

View File

@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.nio.ByteBuffer;
public interface ReadableBytes {
void read(ByteBuffer target) throws IOException;
void read(ByteBuffer target, int position) throws IOException;
}

View File

@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
public interface ReadableFile extends File, ReadableBytes, AutoCloseable {
WritableFile copyTo(WritableFile other) throws IOException;
@Override
void close() throws IOException;
}

View File

@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.nio.ByteBuffer;
public interface WritableBytes {
void write(ByteBuffer source) throws IOException;
void write(ByteBuffer source, int position) throws IOException;
}

View File

@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2015 Markus Kreusch
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
******************************************************************************/
package org.cryptomator.filesystem;
import java.io.IOException;
import java.time.Instant;
public interface WritableFile extends File, WritableBytes, AutoCloseable {
WritableFile moveTo(WritableFile other) throws IOException;
void setLastModified(Instant instant) throws IOException;
void delete() throws IOException;
void truncate() throws IOException;
@Override
void close() throws IOException;
}

View File

@@ -0,0 +1,5 @@
/**
* Defines a file system abstraction to allow access to real and virtual file
* systems through a common API.
*/
package org.cryptomator.filesystem;

View File

@@ -59,6 +59,11 @@
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.cryptomator</groupId>
<artifactId>filesystem-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.cryptomator</groupId>
<artifactId>crypto-api</artifactId>
@@ -192,6 +197,7 @@
</dependencies>
<modules>
<module>filesystem-api</module>
<module>crypto-api</module>
<module>crypto-aes</module>
<module>core</module>