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;
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) + "'");
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()) {
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;
}
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<String,String> osAppDataPathMap;
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<EnvConfig.Note> 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()));
+ }
}