From cbe6f192e00d56a1469c0570d585a15d48779552 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 25 Feb 2020 11:32:38 +0100 Subject: [PATCH] Do not attempt to create already existing appDataDir This change fixes #3998 by avoiding the attempt to create a user's appDataDir if it already exists. Normally this attempt is not a problem, as Files#createDirectories does nothing if the target is a directory that already exists, but in the case that the target is a symbolic link, Files#createDirectories throws a FileAlreadyExistsException. This change prevents the call to Files#createDirectories if the target already exists, and if in fact the target is a symbolic link pointing to an existing directory, this is not a problem and everything proceeds as per usual. This mimics the behavior in Bisq v1.2.5 and prior, where File#mkdirs was used instead of Files#createDirectories. File#mkdirs does not throw an exception when the target directory is a symlink, it just returns false. --- .../main/java/bisq/common/config/Config.java | 10 ++++++---- .../java/bisq/common/config/ConfigTests.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/bisq/common/config/Config.java b/common/src/main/java/bisq/common/config/Config.java index 2222ce03313..fc34b80b98f 100644 --- a/common/src/main/java/bisq/common/config/Config.java +++ b/common/src/main/java/bisq/common/config/Config.java @@ -798,10 +798,12 @@ private static File tempUserDataDir() { * @return the given directory, now guaranteed to exist */ private static File mkAppDataDir(File dir) { - try { - Files.createDirectories(dir.toPath()); - } catch (IOException ex) { - throw new UncheckedIOException(format("Application data directory '%s' could not be created", dir), ex); + 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 dir; } diff --git a/common/src/test/java/bisq/common/config/ConfigTests.java b/common/src/test/java/bisq/common/config/ConfigTests.java index 6109342b339..fbc40151939 100644 --- a/common/src/test/java/bisq/common/config/ConfigTests.java +++ b/common/src/test/java/bisq/common/config/ConfigTests.java @@ -1,6 +1,7 @@ package bisq.common.config; import java.nio.file.Files; +import java.nio.file.Path; import java.io.ByteArrayOutputStream; import java.io.File; @@ -238,6 +239,22 @@ public void whenAppDataDirCannotBeCreated_thenUncheckedIoExceptionIsThrown() thr 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 ========================================================