Merge branch 'check-if-listfiles-returns-null' into 'master'

Check whether File#listFiles() returns null

The docs say this can happen if there's an I/O error. Also fixed a throw-in-finally-block warning and renamed an IoUtils method to make its contract clearer.

See merge request !328
This commit is contained in:
Torsten Grote
2016-09-27 16:17:09 +00:00
6 changed files with 32 additions and 10 deletions

View File

@@ -42,14 +42,17 @@ public class AppModule {
this.application = application; this.application = application;
uiCallback = new UiCallback() { uiCallback = new UiCallback() {
@Override
public int showChoice(String[] options, String... message) { public int showChoice(String[] options, String... message) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public boolean showConfirmationMessage(String... message) { public boolean showConfirmationMessage(String... message) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void showMessage(String... message) { public void showMessage(String... message) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@@ -75,24 +78,29 @@ public class AppModule {
private volatile SecretKey key = null; private volatile SecretKey key = null;
@Override
public boolean databaseExists() { public boolean databaseExists() {
if (!dir.isDirectory()) return false; if (!dir.isDirectory()) return false;
File[] files = dir.listFiles(); File[] files = dir.listFiles();
return files != null && files.length > 0; return files != null && files.length > 0;
} }
@Override
public File getDatabaseDirectory() { public File getDatabaseDirectory() {
return dir; return dir;
} }
@Override
public void setEncryptionKey(SecretKey key) { public void setEncryptionKey(SecretKey key) {
this.key = key; this.key = key;
} }
@Override
public SecretKey getEncryptionKey() { public SecretKey getEncryptionKey() {
return key; return key;
} }
@Override
public long getMaxSize() { public long getMaxSize() {
return Long.MAX_VALUE; return Long.MAX_VALUE;
} }

View File

@@ -248,17 +248,17 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
// Unzip the Tor binary to the filesystem // Unzip the Tor binary to the filesystem
in = getTorInputStream(); in = getTorInputStream();
out = new FileOutputStream(torFile); out = new FileOutputStream(torFile);
IoUtils.copy(in, out); IoUtils.copyAndClose(in, out);
// Make the Tor binary executable // Make the Tor binary executable
if (!torFile.setExecutable(true, true)) throw new IOException(); if (!torFile.setExecutable(true, true)) throw new IOException();
// Unzip the GeoIP database to the filesystem // Unzip the GeoIP database to the filesystem
in = getGeoIpInputStream(); in = getGeoIpInputStream();
out = new FileOutputStream(geoIpFile); out = new FileOutputStream(geoIpFile);
IoUtils.copy(in, out); IoUtils.copyAndClose(in, out);
// Copy the config file to the filesystem // Copy the config file to the filesystem
in = getConfigInputStream(); in = getConfigInputStream();
out = new FileOutputStream(configFile); out = new FileOutputStream(configFile);
IoUtils.copy(in, out); IoUtils.copyAndClose(in, out);
doneFile.createNewFile(); doneFile.createNewFile();
} catch (IOException e) { } catch (IOException e) {
tryToClose(in); tryToClose(in);
@@ -306,8 +306,12 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
} }
private void listFiles(File f) { private void listFiles(File f) {
if (f.isDirectory()) for (File child : f.listFiles()) listFiles(child); if (f.isDirectory()) {
else LOG.info(f.getAbsolutePath()); File[] children = f.listFiles();
if (children != null) for (File child : children) listFiles(child);
} else {
LOG.info(f.getAbsolutePath());
}
} }
private byte[] read(File f) throws IOException { private byte[] read(File f) throws IOException {
@@ -322,7 +326,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
} }
return b; return b;
} finally { } finally {
in.close(); tryToClose(in);
} }
} }

View File

@@ -35,6 +35,7 @@ class H2Database extends JdbcDatabase {
+ ";WRITE_DELAY=0;DB_CLOSE_ON_EXIT=false"; + ";WRITE_DELAY=0;DB_CLOSE_ON_EXIT=false";
} }
@Override
public boolean open() throws DbException { public boolean open() throws DbException {
boolean reopen = config.databaseExists(); boolean reopen = config.databaseExists();
if (!reopen) config.getDatabaseDirectory().mkdirs(); if (!reopen) config.getDatabaseDirectory().mkdirs();
@@ -42,6 +43,7 @@ class H2Database extends JdbcDatabase {
return reopen; return reopen;
} }
@Override
public void close() throws DbException { public void close() throws DbException {
// H2 will close the database when the last connection closes // H2 will close the database when the last connection closes
try { try {
@@ -51,6 +53,7 @@ class H2Database extends JdbcDatabase {
} }
} }
@Override
public long getFreeSpace() throws DbException { public long getFreeSpace() throws DbException {
File dir = config.getDatabaseDirectory(); File dir = config.getDatabaseDirectory();
long maxSize = config.getMaxSize(); long maxSize = config.getMaxSize();
@@ -63,7 +66,9 @@ class H2Database extends JdbcDatabase {
private long getDiskSpace(File f) { private long getDiskSpace(File f) {
if (f.isDirectory()) { if (f.isDirectory()) {
long total = 0; long total = 0;
for (File child : f.listFiles()) total += getDiskSpace(child); File[] children = f.listFiles();
if (children != null)
for (File child : children) total += getDiskSpace(child);
return total; return total;
} else if (f.isFile()) { } else if (f.isFile()) {
return f.length(); return f.length();

View File

@@ -35,7 +35,7 @@ class DevReporterImpl implements DevReporter {
private final DevConfig devConfig; private final DevConfig devConfig;
private final SocketFactory torSocketFactory; private final SocketFactory torSocketFactory;
public DevReporterImpl(CryptoComponent crypto, DevConfig devConfig, DevReporterImpl(CryptoComponent crypto, DevConfig devConfig,
SocketFactory torSocketFactory) { SocketFactory torSocketFactory) {
this.crypto = crypto; this.crypto = crypto;
this.devConfig = devConfig; this.devConfig = devConfig;
@@ -90,7 +90,7 @@ class DevReporterImpl implements DevReporter {
Socket s = connectToDevelopers(); Socket s = connectToDevelopers();
out = s.getOutputStream(); out = s.getOutputStream();
in = new FileInputStream(f); in = new FileInputStream(f);
IoUtils.copy(in, out); IoUtils.copyAndClose(in, out);
f.delete(); f.delete();
} catch (IOException e) { } catch (IOException e) {
LOG.log(WARNING, "Failed to send reports", e); LOG.log(WARNING, "Failed to send reports", e);

View File

@@ -20,7 +20,7 @@ public class IoUtils {
} }
} }
public static void copy(InputStream in, OutputStream out) public static void copyAndClose(InputStream in, OutputStream out)
throws IOException { throws IOException {
byte[] buf = new byte[4096]; byte[] buf = new byte[4096];
try { try {

View File

@@ -16,24 +16,29 @@ public class TestDatabaseConfig implements DatabaseConfig {
this.maxSize = maxSize; this.maxSize = maxSize;
} }
@Override
public boolean databaseExists() { public boolean databaseExists() {
if (!dir.isDirectory()) return false; if (!dir.isDirectory()) return false;
File[] files = dir.listFiles(); File[] files = dir.listFiles();
return files != null && files.length > 0; return files != null && files.length > 0;
} }
@Override
public File getDatabaseDirectory() { public File getDatabaseDirectory() {
return dir; return dir;
} }
@Override
public void setEncryptionKey(SecretKey key) { public void setEncryptionKey(SecretKey key) {
this.key = key; this.key = key;
} }
@Override
public SecretKey getEncryptionKey() { public SecretKey getEncryptionKey() {
return key; return key;
} }
@Override
public long getMaxSize() { public long getMaxSize() {
return maxSize; return maxSize;
} }