[libbluray-devel] CacheDir: remove old files from cache

hpi1 git at videolan.org
Tue Nov 4 13:30:23 CET 2014


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Tue Nov  4 14:28:12 2014 +0200| [1122e1f42c9dc824b8b44d90429199502efa11ea] | committer: hpi1

CacheDir: remove old files from cache

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=1122e1f42c9dc824b8b44d90429199502efa11ea
---

 .../bdj/java-j2me/org/videolan/LockFile.java       |   32 +++++++++
 .../bdj/java-j2se/org/videolan/LockFile.java       |   71 ++++++++++++++++++++
 src/libbluray/bdj/java/org/videolan/CacheDir.java  |   37 ++++++++--
 3 files changed, 136 insertions(+), 4 deletions(-)

diff --git a/src/libbluray/bdj/java-j2me/org/videolan/LockFile.java b/src/libbluray/bdj/java-j2me/org/videolan/LockFile.java
new file mode 100644
index 0000000..eebc7c9
--- /dev/null
+++ b/src/libbluray/bdj/java-j2me/org/videolan/LockFile.java
@@ -0,0 +1,32 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2014  Petri Hintukainen <phintuka at users.sourceforge.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+package org.videolan;
+
+class LockFile {
+
+    /* no support for file channels */
+
+    public static LockFile create(String path) {
+        return null;
+    }
+
+    public void release() {
+    }
+}
diff --git a/src/libbluray/bdj/java-j2se/org/videolan/LockFile.java b/src/libbluray/bdj/java-j2se/org/videolan/LockFile.java
new file mode 100644
index 0000000..7fc2758
--- /dev/null
+++ b/src/libbluray/bdj/java-j2se/org/videolan/LockFile.java
@@ -0,0 +1,71 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2014  Petri Hintukainen <phintuka at users.sourceforge.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+package org.videolan;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+class LockFile {
+
+    private LockFile(RandomAccessFile lockFile) {
+        this.lockFile = lockFile;
+    }
+
+    public static LockFile create(String path) {
+
+        try {
+            RandomAccessFile os = new RandomAccessFile(path, "rw");
+            if (os.getChannel().tryLock() != null) {
+                try {
+                    /* Test if locking works: second tryLock() should fail */
+                    if (os.getChannel().tryLock() != null) {
+                        try {
+                            os.close();
+                        } catch (Exception e) {
+                        }
+                        logger.error("File locking is unreliable !");
+                        return null;
+                    }
+                } catch (Exception e) {
+                }
+                return new LockFile(os);
+            } else {
+                logger.info("Failed locking " + path);
+            }
+        } catch (Exception e) {
+            logger.error("Failed creating lock file: " + e);
+        }
+        return null;
+    }
+
+    public void release() {
+        try {
+            if (lockFile != null) {
+                lockFile.close();
+            }
+        } catch (Exception e) {
+        }
+    }
+
+    private RandomAccessFile lockFile;
+
+    private static final Logger logger = Logger.getLogger(LockFile.class.getName());
+}
diff --git a/src/libbluray/bdj/java/org/videolan/CacheDir.java b/src/libbluray/bdj/java/org/videolan/CacheDir.java
index 8586bb0..11b8f9e 100644
--- a/src/libbluray/bdj/java/org/videolan/CacheDir.java
+++ b/src/libbluray/bdj/java/org/videolan/CacheDir.java
@@ -24,20 +24,40 @@ import java.io.IOException;
 
 class CacheDir {
 
+    private static LockFile lockCache(String path) {
+        return LockFile.create(path + File.separator + "lock");
+    }
+
+    private static void cleanupCache() {
+        File[] files = new File(baseDir).listFiles();
+        if (files != null) {
+            for (int i = 0; i < files.length; i++) {
+                File dir = files[i];
+                if (dir.isDirectory()) {
+                    LockFile lock = lockCache(dir.getPath());
+                    if (lock != null) {
+                        lock.release();
+                        removeImpl(dir);
+                    }
+                }
+            }
+        }
+    }
+
     private static synchronized File getCacheRoot() throws IOException {
 
         if (cacheRoot != null) {
             return cacheRoot;
         }
 
-        String base = System.getProperty("java.io.tmpdir") + File.separator +
-            "libbluray-bdj-cache" + File.separator;
+        cleanupCache();
 
         for (int i = 0; i < 100; i++) {
-            File tmpDir = new File(base + System.nanoTime());
+            File tmpDir = new File(baseDir + System.nanoTime());
             tmpDir = new File(tmpDir.getCanonicalPath());
             if (tmpDir.mkdirs()) {
                 cacheRoot = tmpDir;
+                lockFile  = lockCache(cacheRoot.getPath());
                 logger.info("Created cache in " + tmpDir.getPath());
                 return cacheRoot;
             }
@@ -98,6 +118,12 @@ class CacheDir {
     }
 
     public static synchronized void remove() {
+
+        if (lockFile != null) {
+            lockFile.release();
+            lockFile = null;
+        }
+
         if (cacheRoot != null) {
             remove(cacheRoot);
             cacheRoot = null;
@@ -105,5 +131,8 @@ class CacheDir {
     }
 
     private static File cacheRoot = null;
-    private static final Logger logger = Logger.getLogger(MountManager.class.getName());
+    private static LockFile lockFile = null;
+
+    private static final String baseDir = System.getProperty("java.io.tmpdir") + File.separator + "libbluray-bdj-cache" + File.separator;
+    private static final Logger logger = Logger.getLogger(CacheDir.class.getName());
 }



More information about the libbluray-devel mailing list