]> nmode's Git Repositories - signal-cli/commitdiff
Change default data path to $XDG_DATA_HOME/signal-cli
authorAsamK <asamk@gmx.de>
Sat, 8 Dec 2018 17:26:54 +0000 (18:26 +0100)
committerAsamK <asamk@gmx.de>
Sat, 8 Dec 2018 17:26:54 +0000 (18:26 +0100)
Closes #152 and #125

README.md
man/signal-cli.1.adoc
src/main/java/org/asamk/signal/Main.java
src/main/java/org/asamk/signal/util/IOUtils.java

index e703a4ef15eb3f0de64fd16a905766c1bb36c246..b4bed3b85a0c3b2c235ab6bdabb74864bec543d0 100644 (file)
--- a/README.md
+++ b/README.md
@@ -53,9 +53,11 @@ For more information read the [man page](https://github.com/AsamK/signal-cli/blo
 
 The password and cryptographic keys are created when registering and stored in the current users home directory:
 
-        $HOME/.config/signal/data/
+`$XDG_DATA_HOME/signal-cli/data/` (`$HOME/.local/share/signal-cli/data/`)
+
+For legacy users, the old config directories are used as a fallback:
 
-For legacy users, the old config directory is used as a fallback:
+        $HOME/.config/signal/data/
 
         $HOME/.config/textsecure/data/
 
index 67e55bf520f54eb5bd1f99ec47e83648193c9505..d8e0cb9071d70110ccfe709e74d1441683bb3a21 100644 (file)
@@ -35,7 +35,7 @@ Options
 *--config* CONFIG::
        Set the path, where to store the config.
        Make sure you have full read/write access to the given directory.
-       (Default: $HOME/.config/signal)
+       (Default: `$XDG_DATA_HOME/signal-cli` (`$HOME/.local/share/signal-cli`))
 
 *-u* USERNAME, *--username* USERNAME::
        Specify your phone number, that will be your identifier.
@@ -259,9 +259,11 @@ Files
 The password and cryptographic keys are created when registering and stored in the
 current users home directory, the directory can be changed with *--config*:
 
-    $HOME/.config/signal/
+`$XDG_DATA_HOME/signal-cli/` (`$HOME/.local/share/signal-cli/`)
+
+For legacy users, the old config directories are used as a fallback:
 
-For legacy users, the old config directory is used as a fallback:
+    $HOME/.config/signal/
 
     $HOME/.config/textsecure/
 
index df22e63bddce027105444f9944db91e49e73ff15..e598d8e5c2cf143b2bd314b9ba3917d6cdd762c9 100644 (file)
@@ -24,6 +24,7 @@ import org.asamk.Signal;
 import org.asamk.signal.commands.*;
 import org.asamk.signal.manager.BaseConfig;
 import org.asamk.signal.manager.Manager;
+import org.asamk.signal.util.IOUtils;
 import org.asamk.signal.util.SecurityProvider;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.freedesktop.dbus.DBusConnection;
@@ -80,18 +81,12 @@ public class Main {
                     return 3;
                 }
             } else {
-                String settingsPath = ns.getString("config");
-                if (TextUtils.isEmpty(settingsPath)) {
-                    settingsPath = System.getProperty("user.home") + "/.config/signal";
-                    if (!new File(settingsPath).exists()) {
-                        String legacySettingsPath = System.getProperty("user.home") + "/.config/textsecure";
-                        if (new File(legacySettingsPath).exists()) {
-                            settingsPath = legacySettingsPath;
-                        }
-                    }
+                String dataPath = ns.getString("config");
+                if (TextUtils.isEmpty(dataPath)) {
+                    dataPath = getDefaultDataPath();
                 }
 
-                m = new Manager(username, settingsPath);
+                m = new Manager(username, dataPath);
                 ts = m;
                 try {
                     m.init();
@@ -134,6 +129,32 @@ public class Main {
         }
     }
 
+    /**
+     * Uses $XDG_DATA_HOME/signal-cli if it exists, or if none of the legacy directories exist:
+     * - $HOME/.config/signal
+     * - $HOME/.config/textsecure
+     *
+     * @return the data directory to be used by signal-cli.
+     */
+    private static String getDefaultDataPath() {
+        String dataPath = IOUtils.getDataHomeDir() + "/signal-cli";
+        if (new File(dataPath).exists()) {
+            return dataPath;
+        }
+
+        String legacySettingsPath = System.getProperty("user.home") + "/.config/signal";
+        if (new File(legacySettingsPath).exists()) {
+            return legacySettingsPath;
+        }
+
+        legacySettingsPath = System.getProperty("user.home") + "/.config/textsecure";
+        if (new File(legacySettingsPath).exists()) {
+            return legacySettingsPath;
+        }
+
+        return dataPath;
+    }
+
     private static Namespace parseArgs(String[] args) {
         ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
                 .build()
@@ -145,7 +166,7 @@ public class Main {
                 .help("Show package version.")
                 .action(Arguments.version());
         parser.addArgument("--config")
-                .help("Set the path, where to store the config (Default: $HOME/.config/signal).");
+                .help("Set the path, where to store the config (Default: $XDG_DATA_HOME/signal-cli , $HOME/.local/share/signal-cli).");
 
         MutuallyExclusiveGroup mut = parser.addMutuallyExclusiveGroup();
         mut.addArgument("-u", "--username")
index 9b8c3b5b3291b8f301782fa67744f7c0ce472efa..e1464b1c603997318cc21de696ad096ca64f50af 100644 (file)
@@ -57,4 +57,13 @@ public class IOUtils {
             Files.createFile(file);
         }
     }
+
+    public static String getDataHomeDir() {
+        String dataHome = System.getenv("XDG_DATA_HOME");
+        if (dataHome != null) {
+            return dataHome;
+        }
+
+        return System.getProperty("user.home") + "/.local/share";
+    }
 }