Commits

Sebastian Sdorra committed bde953b

fallback to temp directory if the home directory is not writable

Comments (0)

Files changed (1)

src/main/java/com/aragost/javahg/internals/Utils.java

     static {
         String osName = System.getProperty("os.name");
         IS_WINDOWS = osName.startsWith("Windows");
-        File baseDirectory = new File(System.getProperty("user.home"), ".javahg" + File.separator + "tmp");
-        RESOURCES_TEMP_DIR = createTempDirectory(baseDirectory);
+        RESOURCES_TEMP_DIR = createTempDirectory();
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
     }
     
     /**
+     * Creates a new directory for temporary files. We try to use the home 
+     * directory of the user which owns the process, but we fallback to temp 
+     * directory if the directory is not writable.
+     * 
+     * @return a new directory for temporary files
+     */
+    private static File createTempDirectory() {
+      File home = new File(System.getProperty("user.home"));
+      File base = new File(home, ".javahg" + File.separator + "tmp");
+      if ((base.exists() && ! base.canWrite()) || (!base.exists() && !home.canWrite())) {
+        // fall back to temp directory
+        base = new File(System.getProperty("java.io.tmpdir"));
+        LOG.warn("could not create temp directory at {}/.javahg, use {} as parent instead", home, base);
+      }
+      return createTempDirectory(base);
+    }
+    
+    /**
      * Creates a new temporary directory as child of the given parent.
      * 
      * @param parent parent directory