Default temporary directory to $HOME/.gerritcodereview/tmp

Some operating system distributions include a cron script which
cleans /tmp by unlinking any files not modified in the past 7 days.
This can kill a running Gerrit server by deleting its JAR files,
preventing it from lazily-loading any code after the unlink occurs.

Work around this by unpacking our JARs into a directory below the
user's home directory.  This matches the behavior of Hudson CI,
which also suffered from having their JARs disappear at runtime
due to these automatic /tmp cleaning scripts.

Change-Id: Ia83c65d61310658fc9afd1c663b4b3877c457a3c
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-12-07 13:58:32 -08:00
parent dc5d1ee12d
commit 79860d1ddb

View File

@@ -330,7 +330,7 @@ public final class GerritLauncher {
private static File createTempFile(String prefix, String suffix)
throws IOException {
if (!temporaryDirectoryFound) {
final File d = File.createTempFile("gerrit_", "_app");
final File d = File.createTempFile("gerrit_", "_app", tmproot());
if (d.delete() && d.mkdir()) {
// Try to lock the directory down to be accessible by us.
// We first have to remove all permissions, then add back
@@ -370,6 +370,45 @@ public final class GerritLauncher {
return tmp;
}
private static File tmproot() {
// Try to find the user's home directory. If we can't find it
// return null so the JVM's default temporary directory is used
// instead. This is probably /tmp or /var/tmp.
//
String userHome = System.getProperty("user.home");
if (userHome == null || "".equals(userHome)) {
userHome = System.getenv("HOME");
if (userHome == null || "".equals(userHome)) {
System.err.println("warning: cannot determine home directory");
System.err.println("warning: using system temporary directory instead");
return null;
}
}
// Ensure the home directory exists. If it doesn't, try to make it.
//
final File home = new File(userHome);
if (!home.exists()) {
if (home.mkdirs()) {
System.err.println("warning: created " + home.getAbsolutePath());
} else {
System.err.println("warning: " + home.getAbsolutePath() + " not found");
System.err.println("warning: using system temporary directory instead");
return null;
}
}
// Use $HOME/.gerritcodereview/tmp for our temporary file area.
//
final File tmp = new File(new File(home, ".gerritcodereview"), "tmp");
if (!tmp.exists() && !tmp.mkdirs()) {
System.err.println("warning: cannot create " + tmp.getAbsolutePath());
System.err.println("warning: using system temporary directory instead");
return null;
}
return tmp;
}
private GerritLauncher() {
}
}