Improve JShell client CLI
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / OS.java
index c63d7a1906e3be03a7d5917c01659152906e81be..8d7e693e9f85841d7dcb4bc9045c179cf0fb886f 100644 (file)
@@ -1,10 +1,14 @@
 package org.argeo.cms.util;
 
 import java.io.File;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
-/** When OS specific informations are needed. */
+/**
+ * Wrapper around system properties and portable Java APIS, for when OS specific
+ * informations are needed.
+ */
 public class OS {
        public final static OS LOCAL = new OS();
 
@@ -41,15 +45,16 @@ public class OS {
                        return new String[] { "cmd.exe", "/C" };
        }
 
-       public static long getJvmPid() {
-               return ProcessHandle.current().pid();
-//             String pidAndHost = ManagementFactory.getRuntimeMXBean().getName();
-//             return Integer.parseInt(pidAndHost.substring(0, pidAndHost.indexOf('@')));
-       }
+//     public static long getJvmPid() {
+//             return ProcessHandle.current().pid();
+////           String pidAndHost = ManagementFactory.getRuntimeMXBean().getName();
+////           return Integer.parseInt(pidAndHost.substring(0, pidAndHost.indexOf('@')));
+//     }
 
        /**
         * Get the runtime directory. It will be the environment variable
-        * XDG_RUNTIME_DIR if it is set, or ~/.cache/argeo if not.
+        * XDG_RUNTIME_DIR if it is set, or /run if the user is 'root', or
+        * ~/.cache/argeo if not.
         */
        public static Path getRunDir() {
                Path runDir;
@@ -58,7 +63,18 @@ public class OS {
                        // TODO support multiple names
                        runDir = Paths.get(xdgRunDir);
                } else {
-                       runDir = Paths.get(System.getProperty("user.home"), ".cache/argeo");
+                       String username = System.getProperty("user.name");
+                       if (username.equals("root")) {
+                               runDir = Paths.get("/run");
+                       } else {
+                               Path homeDir = Paths.get(System.getProperty("user.home"));
+                               if (!Files.isWritable(homeDir)) {
+                                       // typically, dameon's home (/usr/sbin) is not writable
+                                       runDir = Paths.get("/tmp/" + username + "/run");
+                               } else {
+                                       runDir = homeDir.resolve(".cache/argeo");
+                               }
+                       }
                }
                return runDir;
        }