mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Separate FileUtils implementations for Android and desktop builds.
The method used by Commons IO to get the available disk space fails on Android devices that lack a df binary - use the Android API instead.
This commit is contained in:
@@ -17,6 +17,7 @@ import net.sf.briar.api.db.DatabaseConfig;
|
||||
import net.sf.briar.api.db.DatabaseExecutor;
|
||||
import net.sf.briar.api.lifecycle.LifecycleManager;
|
||||
import net.sf.briar.api.lifecycle.ShutdownManager;
|
||||
import net.sf.briar.api.os.FileUtils;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
@@ -45,8 +46,9 @@ public class DatabaseModule extends AbstractModule {
|
||||
}
|
||||
|
||||
@Provides
|
||||
Database<Connection> getDatabase(DatabaseConfig config) {
|
||||
return new H2Database(config, new SystemClock());
|
||||
Database<Connection> getDatabase(DatabaseConfig config,
|
||||
FileUtils fileUtils) {
|
||||
return new H2Database(config, fileUtils, new SystemClock());
|
||||
}
|
||||
|
||||
@Provides @Singleton
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.util.Properties;
|
||||
import net.sf.briar.api.clock.Clock;
|
||||
import net.sf.briar.api.db.DatabaseConfig;
|
||||
import net.sf.briar.api.db.DbException;
|
||||
import net.sf.briar.util.FileUtils;
|
||||
import net.sf.briar.api.os.FileUtils;
|
||||
import net.sf.briar.util.StringUtils;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
@@ -25,12 +25,14 @@ class H2Database extends JdbcDatabase {
|
||||
private static final String SECRET_TYPE = "BINARY(32)";
|
||||
|
||||
private final DatabaseConfig config;
|
||||
private final FileUtils fileUtils;
|
||||
private final String url;
|
||||
|
||||
@Inject
|
||||
H2Database(DatabaseConfig config, Clock clock) {
|
||||
H2Database(DatabaseConfig config, FileUtils fileUtils, Clock clock) {
|
||||
super(HASH_TYPE, BINARY_TYPE, COUNTER_TYPE, SECRET_TYPE, clock);
|
||||
this.config = config;
|
||||
this.fileUtils = fileUtils;
|
||||
String path = new File(config.getDatabaseDirectory(), "db").getPath();
|
||||
url = "jdbc:h2:split:" + path + ";CIPHER=AES;MULTI_THREADED=1"
|
||||
+ ";WRITE_DELAY=0;DB_CLOSE_ON_EXIT=false";
|
||||
@@ -56,7 +58,7 @@ class H2Database extends JdbcDatabase {
|
||||
File dir = config.getDatabaseDirectory();
|
||||
long maxSize = config.getMaxSize();
|
||||
try {
|
||||
long free = FileUtils.getFreeSpace(dir);
|
||||
long free = fileUtils.getFreeSpace(dir);
|
||||
long used = getDiskSpace(dir);
|
||||
long quota = maxSize - used;
|
||||
long min = Math.min(free, quota);
|
||||
|
||||
@@ -13,19 +13,19 @@ import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sf.briar.api.ContactId;
|
||||
import net.sf.briar.api.os.FileUtils;
|
||||
import net.sf.briar.api.plugins.simplex.SimplexPlugin;
|
||||
import net.sf.briar.api.plugins.simplex.SimplexPluginCallback;
|
||||
import net.sf.briar.api.plugins.simplex.SimplexTransportReader;
|
||||
import net.sf.briar.api.plugins.simplex.SimplexTransportWriter;
|
||||
|
||||
import org.apache.commons.io.FileSystemUtils;
|
||||
|
||||
public abstract class FilePlugin implements SimplexPlugin {
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(FilePlugin.class.getName());
|
||||
|
||||
protected final Executor pluginExecutor;
|
||||
protected final FileUtils fileUtils;
|
||||
protected final SimplexPluginCallback callback;
|
||||
protected final int maxFrameLength;
|
||||
protected final long maxLatency;
|
||||
@@ -37,10 +37,11 @@ public abstract class FilePlugin implements SimplexPlugin {
|
||||
protected abstract void writerFinished(File f);
|
||||
protected abstract void readerFinished(File f);
|
||||
|
||||
protected FilePlugin(Executor pluginExecutor,
|
||||
protected FilePlugin(Executor pluginExecutor, FileUtils fileUtils,
|
||||
SimplexPluginCallback callback, int maxFrameLength,
|
||||
long maxLatency) {
|
||||
this.pluginExecutor = pluginExecutor;
|
||||
this.fileUtils = fileUtils;
|
||||
this.callback = callback;
|
||||
this.maxFrameLength = maxFrameLength;
|
||||
this.maxLatency = maxLatency;
|
||||
@@ -81,7 +82,7 @@ public abstract class FilePlugin implements SimplexPlugin {
|
||||
if(dir == null || !dir.exists() || !dir.isDirectory()) return null;
|
||||
File f = new File(dir, filename);
|
||||
try {
|
||||
long capacity = getCapacity(dir.getPath());
|
||||
long capacity = fileUtils.getFreeSpace(dir);
|
||||
if(capacity < MIN_CONNECTION_LENGTH) return null;
|
||||
OutputStream out = new FileOutputStream(f);
|
||||
return new FileTransportWriter(f, out, capacity, this);
|
||||
@@ -92,10 +93,6 @@ public abstract class FilePlugin implements SimplexPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private long getCapacity(String path) throws IOException {
|
||||
return FileSystemUtils.freeSpaceKb(path) * 1024;
|
||||
}
|
||||
|
||||
protected void createReaderFromFile(final File f) {
|
||||
if(!running) return;
|
||||
pluginExecutor.execute(new ReaderCreator(f));
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package net.sf.briar.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.CodeSource;
|
||||
|
||||
import org.apache.commons.io.FileSystemUtils;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
/**
|
||||
* Returns the directory where Briar is installed.
|
||||
*/
|
||||
public static File getBriarDirectory() {
|
||||
CodeSource c = FileUtils.class.getProtectionDomain().getCodeSource();
|
||||
File f = new File(c.getLocation().getPath());
|
||||
assert f.exists();
|
||||
if(f.isFile()) {
|
||||
// Running from a jar - return the jar's grandparent
|
||||
f = f.getParentFile().getParentFile();
|
||||
} else {
|
||||
// Running from Eclipse
|
||||
f = new File(f.getParentFile(), "Briar");
|
||||
}
|
||||
assert f.exists();
|
||||
assert f.isDirectory();
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a temporary file.
|
||||
*/
|
||||
public static File createTempFile() throws IOException {
|
||||
String rand = String.valueOf(1000 + (int) (Math.random() * 9000));
|
||||
return File.createTempFile(rand, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the contents of the source file to the destination file.
|
||||
*/
|
||||
public static void copy(File src, File dest) throws IOException {
|
||||
FileInputStream in = new FileInputStream(src);
|
||||
copy(in, dest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the contents of the input stream to the destination file.
|
||||
*/
|
||||
public static void copy(InputStream in, File dest) throws IOException {
|
||||
FileOutputStream out = new FileOutputStream(dest);
|
||||
byte[] buf = new byte[1024];
|
||||
int i;
|
||||
while((i = in.read(buf, 0, buf.length)) != -1) out.write(buf, 0, i);
|
||||
in.close();
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the source file or directory to the destination directory. If the
|
||||
* callback is not null it's called once for each file created.
|
||||
*/
|
||||
public static void copyRecursively(File src, File dest, Callback callback)
|
||||
throws IOException {
|
||||
assert dest.exists();
|
||||
assert dest.isDirectory();
|
||||
dest = new File(dest, src.getName());
|
||||
if(src.isDirectory()) {
|
||||
dest.mkdir();
|
||||
for(File f : src.listFiles()) copyRecursively(f, dest, callback);
|
||||
} else {
|
||||
if(callback != null) callback.processingFile(dest);
|
||||
copy(src, dest);
|
||||
}
|
||||
}
|
||||
|
||||
public static void delete(File f) throws IOException {
|
||||
if(f.isDirectory() && !org.apache.commons.io.FileUtils.isSymlink(f)) {
|
||||
for(File child : f.listFiles()) delete(child);
|
||||
}
|
||||
f.delete();
|
||||
}
|
||||
|
||||
public static long getFreeSpace(File f) throws IOException {
|
||||
return FileSystemUtils.freeSpaceKb(f.getAbsolutePath()) * 1024;
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
|
||||
void processingFile(File f);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user