Skip to content

Commit

Permalink
change the default directory returned on windows to APPDATA/postgresq…
Browse files Browse the repository at this point in the history
…l since that is what we end up using anyway (#2402)

* change the default directory returned on windows to APPDATA/postgresql since that is what we end up using anyway

* change getConfigRootDirectory to not return with a trailing file separator to honour the name of the function
  • Loading branch information
davecramer committed Jan 12, 2022
1 parent db82575 commit c12a76f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/util/OSUtil.java
Expand Up @@ -26,9 +26,9 @@ public static boolean isWindows() {
*/
public static String getUserConfigRootDirectory() {
if (isWindows()) {
return System.getenv("APPDATA") + File.separator;
return System.getenv("APPDATA") + File.separator + "postgresql";
} else {
return System.getProperty("user.home") + File.separator;
return System.getProperty("user.home");
}
}

Expand Down
Expand Up @@ -126,19 +126,18 @@ private InputStream openInputStream(String resourceName) throws IOException {

// if file in user home is readable, use it, otherwise continue - 3rd priority
{
String resourceName = OSUtil.getUserConfigRootDirectory();
if (OSUtil.isWindows()) {
resourceName += "postgresql" + File.separator;
} else {
String resourceName = "";
if ( !OSUtil.isWindows() ) {
resourceName += ".";
}
resourceName += pgPassFileDefaultName;
if (OSUtil.isWindows()) {
resourceName += ".conf";
}
if (new File(resourceName).canRead()) {
LOGGER.log(Level.FINE, "Value [{0}] selected because file exist in user home directory", new Object[]{resourceName});
return resourceName;
File resourceFile = new File(OSUtil.getUserConfigRootDirectory(), resourceName);
if (resourceFile.canRead()) {
LOGGER.log(Level.FINE, "Value [{0}] selected because file exist in user home directory", new Object[]{resourceFile.getAbsolutePath()});
return resourceFile.getAbsolutePath();
}
}

Expand Down
Expand Up @@ -111,17 +111,16 @@ private InputStream openInputStream(String resourceName) throws IOException {
}
}

// if file in user home is readable, use it, otherwise continue - 3rd priority
/*
if file in user home is readable, use it, otherwise continue - 3rd priority
in the case that the file is in the user home directory it is prepended with '.'
*/
{
String resourceName = OSUtil.getUserConfigRootDirectory();
if (OSUtil.isWindows()) {
resourceName += "postgresql" + File.separator;
}
resourceName += ".";
resourceName += pgServceConfFileDefaultName;
if (new File(resourceName).canRead()) {
LOGGER.log(Level.FINE, "Value [{0}] selected because file exist in user home directory", new Object[]{resourceName});
return resourceName;
String resourceName = "." + pgServceConfFileDefaultName;
File resourceFile = new File(OSUtil.getUserConfigRootDirectory(), resourceName);
if (resourceFile.canRead()) {
LOGGER.log(Level.FINE, "Value [{0}] selected because file exist in user home directory", new Object[]{resourceFile.getAbsolutePath()});
return resourceFile.getAbsolutePath();
}
}

Expand Down
7 changes: 2 additions & 5 deletions pgjdbc/src/test/java/org/postgresql/util/OSUtilTest.java
Expand Up @@ -6,7 +6,6 @@
package org.postgresql.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -26,15 +25,13 @@ void getUserConfigRootDirectory() throws Exception {
Resources.with(new EnvironmentVariables("APPDATA", "C:\\Users\\realuser\\AppData\\Roaming"),
new SystemProperties("os.name", "Windows 10")).execute(() -> {
String result = OSUtil.getUserConfigRootDirectory();
assertNotNull(result);
assertEquals("C:\\Users\\realuser\\AppData\\Roaming" + File.separator, result);
assertEquals("C:\\Users\\realuser\\AppData\\Roaming" + File.separator + "postgresql", result);
}
);
// linux
Resources.with(new SystemProperties("os.name", "Linux", "user.home", "/home/realuser")).execute(() -> {
String result = OSUtil.getUserConfigRootDirectory();
assertNotNull(result);
assertEquals("/home/realuser" + File.separator, result);
assertEquals("/home/realuser", result);
}
);
}
Expand Down

0 comments on commit c12a76f

Please sign in to comment.