libbluray | branch: master | hpi1 <[email protected]> | Wed Oct 22 13:25:27 2014 +0300| [b55058f3d6dddc35f5d0872c9b501c3fcfec4e2a] | committer: hpi1
Added CacheDir class > http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=b55058f3d6dddc35f5d0872c9b501c3fcfec4e2a --- src/libbluray/bdj/java/org/videolan/CacheDir.java | 106 ++++++++++++++++++++ src/libbluray/bdj/java/org/videolan/Libbluray.java | 1 + .../bdj/java/org/videolan/MountManager.java | 28 +----- 3 files changed, 112 insertions(+), 23 deletions(-) diff --git a/src/libbluray/bdj/java/org/videolan/CacheDir.java b/src/libbluray/bdj/java/org/videolan/CacheDir.java new file mode 100644 index 0000000..a903ed8 --- /dev/null +++ b/src/libbluray/bdj/java/org/videolan/CacheDir.java @@ -0,0 +1,106 @@ +/* + * This file is part of libbluray + * Copyright (C) 2014 Petri Hintukainen <[email protected]> + * + * 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; + +class CacheDir { + + 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; + + for (int i = 0; i < 100; i++) { + File tmpDir = new File(base + System.nanoTime()); + tmpDir = new File(tmpDir.getCanonicalPath()); + if (tmpDir.mkdirs()) { + cacheRoot = tmpDir; + logger.info("Created cache in " + tmpDir.getPath()); + return cacheRoot; + } + logger.error("error creating " + tmpDir.getPath()); + } + + logger.error("failed to create temporary cache directory"); + throw new IOException(); + } + + public static synchronized File create(String domain) throws IOException { + + File tmpDir = new File(getCacheRoot().getPath() + File.separator + domain); + if (!tmpDir.exists() && !tmpDir.mkdirs()) { + logger.error("Error creating " + tmpDir.getPath()); + throw new IOException(); + } + + return tmpDir; + } + + public static File create(String domain, String name) throws IOException { + return create(domain + File.separator + name); + } + + private static void removeImpl(File dir) { + File[] files = dir.listFiles(); + for (int i = 0; i < files.length; i++) { + File file = files[i]; + if (file.isDirectory()) { + remove(file); + } else { + if (!file.delete()) { + logger.error("Error removing " + file.getPath()); + } + } + } + + if (!dir.delete()) { + logger.error("Error removing " + dir.getPath()); + } + } + + public static synchronized void remove(File dir) { + String path = dir.getPath(); + if (path.indexOf(cacheRoot.getPath()) != 0) { + logger.error("Error removing " + dir.getPath() + ": not inside cache !"); + return; + } + if (path.indexOf("..") >= 0) { + logger.error("Error removing " + dir.getPath() + ": not canonical path !"); + return; + } + + removeImpl(dir); + } + + public static synchronized void remove() { + if (cacheRoot != null) { + remove(cacheRoot); + } + } + + private static File cacheRoot = null; + private static final Logger logger = Logger.getLogger(MountManager.class.getName()); +} diff --git a/src/libbluray/bdj/java/org/videolan/Libbluray.java b/src/libbluray/bdj/java/org/videolan/Libbluray.java index 07088b7..a7c1cb7 100644 --- a/src/libbluray/bdj/java/org/videolan/Libbluray.java +++ b/src/libbluray/bdj/java/org/videolan/Libbluray.java @@ -145,6 +145,7 @@ public class Libbluray { EventManager.shutdown(); Status.shutdown(); ServiceContextFactoryImpl.shutdown(); + CacheDir.remove(); } catch (Throwable e) { e.printStackTrace(); } diff --git a/src/libbluray/bdj/java/org/videolan/MountManager.java b/src/libbluray/bdj/java/org/videolan/MountManager.java index ed91f6b..14efc21 100644 --- a/src/libbluray/bdj/java/org/videolan/MountManager.java +++ b/src/libbluray/bdj/java/org/videolan/MountManager.java @@ -1,6 +1,7 @@ /* * This file is part of libbluray * Copyright (C) 2010 William Hahne + * Copyright (C) 2014 Petri Hintukainen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -31,7 +32,6 @@ import java.util.Iterator; import java.util.Map; import java.util.jar.JarEntry; import java.util.jar.JarFile; -import org.videolan.Logger; /** * This class handle mounting jar files so that their contents can be accessed. @@ -61,16 +61,12 @@ public class MountManager { File tmpDir = null; try { jar = new JarFile(path); - tmpDir = File.createTempFile("bdj-", ""); + tmpDir = CacheDir.create("mount", jarStr); } catch (IOException e) { e.printStackTrace(); throw new MountException(); } - // create temporary directory - tmpDir.delete(); - tmpDir.mkdir(); - try { byte[] buffer = new byte[32*1024]; Enumeration entries = jar.entries(); @@ -100,7 +96,7 @@ public class MountManager { } } catch (IOException e) { e.printStackTrace(); - recursiveDelete(tmpDir); + CacheDir.remove(tmpDir); throw new MountException(); } @@ -121,7 +117,7 @@ public class MountManager { mountPoint = (File)mountPoints.remove(id); } if (mountPoint != null) { - recursiveDelete(mountPoint); + CacheDir.remove(mountPoint); } else { logger.info("JAR " + jarId + " not mounted"); } @@ -138,7 +134,7 @@ public class MountManager { } if (dirs != null) { for (int i = 0; i < dirs.length; i++) { - recursiveDelete((File)dirs[i]); + CacheDir.remove((File)dirs[i]); } } } @@ -164,20 +160,6 @@ public class MountManager { return BDJUtil.makeFiveDigitStr(jarId); } - private static void recursiveDelete(File dir) { - File[] files = dir.listFiles(); - for (int i = 0; i < files.length; i++) { - File file = files[i]; - if (file.isDirectory()) { - recursiveDelete(file); - } else { - file.delete(); - } - } - - dir.delete(); - } - private static Map mountPoints = new HashMap(); private static final Logger logger = Logger.getLogger(MountManager.class.getName()); } _______________________________________________ libbluray-devel mailing list [email protected] https://mailman.videolan.org/listinfo/libbluray-devel
