Clean up stale empty temporary directories from prior executions

Some NFS based filesystems seem to have a problem deleting the
temporary directory we create to hold our JARs.  Something on the
NFS server is pegging the directory open until the JVM exits,
which means we may wind up leaving an infinite number of empty
directories in ~/.gerritcodereview/tmp.

This appears to be isolated to certain NFS servers, because my local
/tmp doesn't suffer from the same problem, the temporary directory
is deleted on JVM exit.

We use a simple rule here, any empty directory with a modification
date older than 7 days is assumed to be garbage and removed.
Any directory that is non-empty can be assumed to be a running
daemon, even if it has a modification date of older than 7 days,
and is thus left alone.

Change-Id: I3d4e697863f347e66e5b142e4a7ad8e99a08956d
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2009-12-07 14:10:08 -08:00
parent 79860d1ddb
commit a68b89452f
1 changed files with 22 additions and 0 deletions

View File

@ -14,6 +14,9 @@ package com.google.gerrit.main;
// See the License for the specific language governing permissions and
// limitations under the License.
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@ -406,6 +409,25 @@ public final class GerritLauncher {
System.err.println("warning: using system temporary directory instead");
return null;
}
// Try to clean up any stale empty directories. Assume any empty
// directory that is older than 7 days is one of these dead ones
// that we can clean up.
//
final File[] tmpEntries = tmp.listFiles();
if (tmpEntries != null) {
final long now = System.currentTimeMillis();
final long expired = now - MILLISECONDS.convert(7, DAYS);
for (final File tmpEntry : tmpEntries) {
if (tmpEntry.isDirectory() && tmpEntry.lastModified() < expired) {
final String[] all = tmpEntry.list();
if (all == null || all.length == 0) {
tmpEntry.delete();
}
}
}
}
return tmp;
}