Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid startup failure when appDataDir is a symbolic link #4002

Merged
merged 3 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions common/src/main/java/bisq/common/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,14 +797,15 @@ private static File tempUserDataDir() {
* nothing if the directory already exists.
* @return the given directory, now guaranteed to exist
*/
private static File mkAppDataDir(File appDataDir) {
Path path = appDataDir.toPath();
try {
Files.createDirectories(path);
} catch (IOException ex) {
throw new ConfigException(ex, "Application data directory '%s' could not be created", path);
private static File mkAppDataDir(File dir) {
if (!dir.exists()) {
try {
Files.createDirectories(dir.toPath());
} catch (IOException ex) {
throw new UncheckedIOException(format("Application data directory '%s' could not be created", dir), ex);
}
}
return appDataDir;
return dir;
}

/**
Expand All @@ -815,11 +816,10 @@ private static File mkAppDataDir(File appDataDir) {
private static File mkdir(File parent, String child) {
File dir = new File(parent, child);
if (!dir.exists()) {
Path path = dir.toPath();
try {
Files.createDirectory(path);
Files.createDirectory(dir.toPath());
} catch (IOException ex) {
throw new ConfigException(ex, "Directory '%s' could not be created", path);
throw new UncheckedIOException(format("Directory '%s' could not be created", dir), ex);
}
}
return dir;
Expand Down
4 changes: 0 additions & 4 deletions common/src/main/java/bisq/common/config/ConfigException.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ public class ConfigException extends BisqException {
public ConfigException(String format, Object... args) {
super(format, args);
}

public ConfigException(Throwable cause, String format, Object... args) {
super(cause, format, args);
}
}
22 changes: 20 additions & 2 deletions common/src/test/java/bisq/common/config/ConfigTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bisq.common.config;

import java.nio.file.Files;
import java.nio.file.Path;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -228,15 +230,31 @@ public void whenConfigIsConstructed_thenAppDataDirAndSubdirsAreCreated() {
}

@Test
public void whenAppDataDirCannotBeCreated_thenConfigExceptionIsThrown() throws IOException {
public void whenAppDataDirCannotBeCreated_thenUncheckedIoExceptionIsThrown() throws IOException {
// set a userDataDir that is actually a file so appDataDir cannot be created
File aFile = Files.createTempFile("A", "File").toFile();
exceptionRule.expect(ConfigException.class);
exceptionRule.expect(UncheckedIOException.class);
exceptionRule.expectMessage(containsString("Application data directory"));
exceptionRule.expectMessage(containsString("could not be created"));
configWithOpts(opt(USER_DATA_DIR, aFile));
}

@Test
public void whenAppDataDirIsSymbolicLink_thenAppDataDirCreationIsNoOp() throws IOException {
Path parentDir = Files.createTempDirectory("parent");
Path targetDir = parentDir.resolve("target");
Path symlink = parentDir.resolve("symlink");
Files.createDirectory(targetDir);
try {
Files.createSymbolicLink(symlink, targetDir);
} catch (Throwable ex) {
// An error occurred trying to create a symbolic link, likely because the
// operating system (e.g. Windows) does not support it, so we abort the test.
return;
}
configWithOpts(opt(APP_DATA_DIR, symlink));
}


// == TEST SUPPORT FACILITIES ========================================================

Expand Down