[libbluray-devel] Create cache paths in native code

hpi1 git at videolan.org
Sun Mar 1 16:31:39 CET 2015


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Sun Mar  1 14:29:30 2015 +0200| [6fd56c1a486e32c392546f0ef27485f0b722e8db] | committer: hpi1

Create cache paths in native code

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

 src/file/dirs.h                                   |    1 -
 src/file/dirs_win32.c                             |    8 ----
 src/file/file.c                                   |   51 ++++++++++++++++++++-
 src/file/file.h                                   |    4 +-
 src/file/file_posix.c                             |   14 ++++++
 src/file/file_win32.c                             |   23 ++++++++++
 src/libbluray/bdj/java/org/videolan/CacheDir.java |    2 +
 src/libbluray/bdj/java/org/videolan/VFSCache.java |   31 +------------
 src/libbluray/disc/disc.c                         |    3 ++
 9 files changed, 97 insertions(+), 40 deletions(-)

diff --git a/src/file/dirs.h b/src/file/dirs.h
index 4ca71f3..820917f 100644
--- a/src/file/dirs.h
+++ b/src/file/dirs.h
@@ -23,7 +23,6 @@
 #include "util/attributes.h"
 
 #ifdef _WIN32
-BD_PRIVATE int         win32_mkdir(const char *dir);
 BD_PRIVATE char       *win32_get_font_dir(const char *font_file);
 #endif
 
diff --git a/src/file/dirs_win32.c b/src/file/dirs_win32.c
index 4d1e51d..3fe97f5 100644
--- a/src/file/dirs_win32.c
+++ b/src/file/dirs_win32.c
@@ -35,14 +35,6 @@
 #include <direct.h>
 
 
-int win32_mkdir(const char *dir)
-{
-    wchar_t wdir[MAX_PATH];
-
-    MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH);
-    return _wmkdir(wdir);
-}
-
 char *win32_get_font_dir(const char *font_file)
 {
     wchar_t wdir[MAX_PATH];
diff --git a/src/file/file.c b/src/file/file.c
index 7b0f2c4..15edfe0 100644
--- a/src/file/file.c
+++ b/src/file/file.c
@@ -23,7 +23,13 @@
 
 #include "file.h"
 
-#include <stdio.h>
+#include "util/logging.h"
+#include "util/macro.h"
+#include "util/strutl.h"
+
+#include <stdio.h>  // SEEK_*
+#include <string.h> // strchr
+
 
 int64_t file_size(BD_FILE_H *fp)
 {
@@ -38,3 +44,46 @@ int64_t file_size(BD_FILE_H *fp)
 
     return length;
 }
+
+int file_mkdirs(const char *path)
+{
+    int result = 0;
+    char *dir = str_dup(path);
+    char *end = dir;
+    char *p;
+
+    /* strip file name */
+    if (!(end = strrchr(end, DIR_SEP_CHAR))) {
+        X_FREE(dir);
+        return -1;
+    }
+    *end = 0;
+
+    /* tokenize, stop to first existing dir */
+    while ((p = strrchr(dir, DIR_SEP_CHAR))) {
+        if (!file_path_exists(dir)) {
+            break;
+        }
+        *p = 0;
+    }
+
+    /* create missing dirs */
+    p = dir;
+    while (p < end) {
+
+        /* concatenate next non-existing dir */
+        while (*p) p++;
+        if (p >= end) break;
+        *p = DIR_SEP_CHAR;
+
+        result = file_mkdir(dir);
+        if (result < 0) {
+            BD_DEBUG(DBG_FILE | DBG_CRIT, "Error creating directory %s\n", dir);
+            break;
+        }
+        BD_DEBUG(DBG_FILE, "  created directory %s\n", dir);
+    }
+
+    X_FREE(dir);
+    return result;
+}
diff --git a/src/file/file.h b/src/file/file.h
index 49a73de..2990d36 100644
--- a/src/file/file.h
+++ b/src/file/file.h
@@ -66,6 +66,8 @@ BD_PRIVATE BD_DIR_OPEN dir_open_default(void);
  */
 
 BD_PRIVATE int file_unlink(const char *file);
-
+BD_PRIVATE int file_path_exists(const char *path);
+BD_PRIVATE int file_mkdir(const char *dir);
+BD_PRIVATE int file_mkdirs(const char *path);
 
 #endif /* FILE_H_ */
diff --git a/src/file/file_posix.c b/src/file/file_posix.c
index 14b5b3e..739b718 100644
--- a/src/file/file_posix.c
+++ b/src/file/file_posix.c
@@ -115,3 +115,17 @@ int file_unlink(const char *file)
 {
     return remove(file);
 }
+
+#include <sys/stat.h>
+#include <sys/types.h>
+
+int file_path_exists(const char *path)
+{
+    struct stat s;
+    return stat(path, &s);
+}
+
+int file_mkdir(const char *dir)
+{
+    return mkdir(dir, S_IRWXU);
+}
diff --git a/src/file/file_win32.c b/src/file/file_win32.c
index 2e200ba..ca81ccd 100644
--- a/src/file/file_win32.c
+++ b/src/file/file_win32.c
@@ -133,3 +133,26 @@ int file_unlink(const char *file)
     MultiByteToWideChar(CP_UTF8, 0, file, -1, wfile, MAX_PATH);
     return _wremove(wfile);
 }
+
+int file_path_exists(const char *path)
+{
+    wchar_t wpath[MAX_PATH];
+
+    MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH);
+
+    DWORD dwAttrib = GetFileAttributesW(wpath);
+    if (dwAttrib != INVALID_FILE_ATTRIBUTES) {
+        return 0;
+    }
+    return -1;
+}
+
+int file_mkdir(const char *dir)
+{
+    wchar_t wdir[MAX_PATH];
+
+    MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH);
+    if (!CreateDirectoryW(wdir, NULL))
+        return -1;
+    return 0;
+}
diff --git a/src/libbluray/bdj/java/org/videolan/CacheDir.java b/src/libbluray/bdj/java/org/videolan/CacheDir.java
index e2fdd68..30b4cd2 100644
--- a/src/libbluray/bdj/java/org/videolan/CacheDir.java
+++ b/src/libbluray/bdj/java/org/videolan/CacheDir.java
@@ -55,6 +55,8 @@ class CacheDir {
 
         SecurityManager sm = System.getSecurityManager();
         if (sm != null && sm instanceof BDJSecurityManager) {
+            ((BDJSecurityManager)sm).setCacheRoot(System.getProperty("java.io.tmpdir"));
+            new File(baseDir).mkdirs();
             ((BDJSecurityManager)sm).setCacheRoot(baseDir);
         }
 
diff --git a/src/libbluray/bdj/java/org/videolan/VFSCache.java b/src/libbluray/bdj/java/org/videolan/VFSCache.java
index f076abc..47b2184 100644
--- a/src/libbluray/bdj/java/org/videolan/VFSCache.java
+++ b/src/libbluray/bdj/java/org/videolan/VFSCache.java
@@ -68,8 +68,6 @@ class VFSCache {
             vfsRoot = vfsRoot + File.separator;
         }
         vfsRootLength = vfsRoot.length();
-
-        new File(cacheRoot + jarDir).mkdirs();
     }
 
     /*
@@ -164,8 +162,6 @@ class VFSCache {
     private void copyJarDir(String name) {
         /* copy directory from BDMV/JAR/ */
 
-        new File(cacheRoot + jarDir + name).mkdirs();
-
         File[] files = new File(vfsRoot + jarDir + name).listFiles();
         for (int i = 0; i < files.length; i++) {
             File   file    = files[i];
@@ -198,8 +194,6 @@ class VFSCache {
 
     protected synchronized File addFont(String fontFile) {
 
-        new File(fontRoot + fontDir).mkdirs();
-
         String relPath = fontDir + fontFile;
         String dstPath = fontRoot + relPath;
         File dstFile = new File(dstPath);
@@ -279,17 +273,6 @@ class VFSCache {
         inAccessFile = false;
     }
 
-    private void mkdirs_xletCode(String path) {
-        final File file = new File(path);
-        AccessController.doPrivileged(
-            new PrivilegedAction() {
-                public Object run() {
-                    file.mkdirs();
-                    return null;
-                }
-            });
-    }
-
     private void accessFileImp(String absPath) {
 
         if (BDFileSystem.nativeFileExists(absPath)) {
@@ -299,11 +282,8 @@ class VFSCache {
 
         String relPath = absPath.substring(vfsRootLength);
         String[] names = org.videolan.Libbluray.listBdFiles(relPath, true);
-        if (names == null) {
-            /* this is regular file */
-        } else {
-            /* this is directory, make sure it exists */
-            mkdirs_xletCode(absPath);
+        if (names != null) {
+            /* this is directory */
             return;
         }
 
@@ -312,13 +292,6 @@ class VFSCache {
             return;
         }
 
-        /* create the directory */
-        int sepPos = relPath.lastIndexOf(File.separatorChar);
-        if (sepPos > 0) {
-            String absDir = cacheRoot + relPath.substring(0, sepPos);
-            mkdirs_xletCode(absDir);
-        }
-
         /* finally, copy the file to cache */
         Libbluray.cacheBdRomFile(relPath, cacheRoot + relPath);
     }
diff --git a/src/libbluray/disc/disc.c b/src/libbluray/disc/disc.c
index c8aa934..7ac7e40 100644
--- a/src/libbluray/disc/disc.c
+++ b/src/libbluray/disc/disc.c
@@ -419,6 +419,9 @@ int disc_cache_bdrom_file(BD_DISC *p, const char *rel_path, const char *cache_pa
         return -1;
     }
 
+    /* make sure path exists */
+    file_mkdirs(cache_path);
+
     /* output file in local filesystem */
     fp_out = file_open_default()(cache_path, "wb");
     if (!fp_out) {



More information about the libbluray-devel mailing list