From 440a6e7a2010b01170650dcf276d23c8aaad34fc Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Fri, 12 Jul 2024 14:17:35 +0100 Subject: [PATCH] JAL-3631 insert digest of full path to installer.appdir in path for user appdir and add tests to verify nouserdefaultappdir clobbers userdefaultappdir --- .../com/threerings/getdown/data/EnvConfig.java | 28 +++++++++++++--- .../com/threerings/getdown/data/EnvConfigTest.java | 35 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/EnvConfig.java b/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/EnvConfig.java index e5051de..8c73262 100644 --- a/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/EnvConfig.java +++ b/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/EnvConfig.java @@ -5,6 +5,8 @@ package com.threerings.getdown.data; +import static java.nio.charset.StandardCharsets.UTF_8; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -344,7 +346,15 @@ public final class EnvConfig { this.appArgs = appArgs; } - private static final String getUserAppdir() { + public static final String getFullPathToDirectoryHash(String install_app_dir) + { + MessageDigest md = Digest.getMessageDigest(Digest.VERSION); + byte[] contents = install_app_dir.getBytes(UTF_8); + String hash = StringUtil.hexlate(md.digest(contents)); + return hash; + } + + protected static final String getUserAppdir() { final String noUseDefaultAppDirProperty = "no" + USER_DEFAULT_APPDIR_PROPERTY; if (Boolean.valueOf(System.getProperty(noUseDefaultAppDirProperty))) { System.err.println("Not using default user appdir because property '" + noUseDefaultAppDirProperty + "' is '" + System.getProperty(noUseDefaultAppDirProperty) + "'"); @@ -358,6 +368,14 @@ public final class EnvConfig { String home = System.getProperty("user.home"); String appname = StringUtil.isBlank(appName) ? ChannelProperties.FALLBACK_APPNAME : appName; final String FS = File.separator; + String appDirHash; + try { + appDirHash = getFullPathToDirectoryHash(new File(installerAppdir).getCanonicalPath()); + } catch (IOException ioex) { + System.err.println("Unable to resolve '"+installerAppdir+"' as a proper path on this system.\nNot generating a user local appdir - getdown may fail to update!"); + return null; + } + String appDataPath; String append; if (LaunchUtil.isMacOS()) { @@ -373,7 +391,7 @@ public final class EnvConfig { appDataPath = osAppDataPathMap.get("other"); append = appdirname.toLowerCase(Locale.ROOT); } - String returnString = home + FS + appDataPath + FS + append + FS + "app"; + String returnString = home + FS + appDataPath + FS + append + FS+ appDirHash + FS + "app"; return returnString; } @@ -409,11 +427,11 @@ public final class EnvConfig { private static String appName = null; - private static final String USER_DEFAULT_APPDIR_PROPERTY = "userdefaultappdir"; + protected static final String USER_DEFAULT_APPDIR_PROPERTY = "userdefaultappdir"; - private static final String APPLICATION_APPDIR_PROPERTY = "installer.appdir"; + protected static final String APPLICATION_APPDIR_PROPERTY = "installer.appdir"; - private static final String POPULATE_DEFAULT_APPDIR_PROPERTY= "populatedefaultappdir"; + protected static final String POPULATE_DEFAULT_APPDIR_PROPERTY= "populatedefaultappdir"; private static final Map osAppDataPathMap; diff --git a/getdown/src/getdown/core/src/test/java/com/threerings/getdown/data/EnvConfigTest.java b/getdown/src/getdown/core/src/test/java/com/threerings/getdown/data/EnvConfigTest.java index 6178651..25b810e 100644 --- a/getdown/src/getdown/core/src/test/java/com/threerings/getdown/data/EnvConfigTest.java +++ b/getdown/src/getdown/core/src/test/java/com/threerings/getdown/data/EnvConfigTest.java @@ -139,4 +139,39 @@ public class EnvConfigTest { checkAppBase(cfg, TESTBASE); checkNoAppArgs(cfg); } + + /** + * check that the appdir hash distinguishes between different base-app paths + */ + @Test public void testDifferentUserappdirHash() { + String envhash1 = EnvConfig.getFullPathToDirectoryHash("/Applications/Getdown.app/"); + String envhash2 = EnvConfig.getFullPathToDirectoryHash("/Applications/Getdown 1.app/"); + assertTrue(!envhash1.equals(envhash2)); + } + + /** + * check whether userdefaultappdir can be overrridden by nouserdefaultappdir + */ + @Test public void checkUserAppDir() { + List notes = new ArrayList<>(); + System.getProperties().setProperty(EnvConfig.APPLICATION_APPDIR_PROPERTY, "/Applications/Getdown.app"); + System.getProperties().setProperty("userdefaultappdir","true"); + EnvConfig cfg = sysPropsConfig(notes, "appdir", CWD); + hasUserAppDir(); + + System.getProperties().setProperty("nouserdefaultappdir","true"); + cfg = sysPropsConfig(notes, "appdir", CWD); + noUserAppDir(); + + System.getProperties().remove("nouserdefaultappdir"); + cfg = sysPropsConfig(notes, "appdir", CWD); + hasUserAppDir(); + } + + public void noUserAppDir() { + assertTrue(EnvConfig.getUserAppdir()==null); + } + public void hasUserAppDir() { + assertTrue(EnvConfig.getUserAppdir()!=null && !"".equals(EnvConfig.getUserAppdir())); + } } -- 1.7.10.2