[libbluray-devel] Add native method for listing files in BD-ROM directory

hpi1 git at videolan.org
Thu Feb 19 12:20:27 CET 2015


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Thu Feb 19 11:54:41 2015 +0200| [4b7c1b81e9708ff579b52c213186a21e1fcd0db8] | committer: hpi1

Add native method for listing files in BD-ROM directory

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

 src/libbluray/bdj/java/java/io/BDFileSystem.java   |   16 ++++-
 src/libbluray/bdj/java/org/videolan/Libbluray.java |    5 ++
 src/libbluray/bdj/native/org_videolan_Libbluray.c  |   75 ++++++++++++++++++++
 src/libbluray/bdj/native/org_videolan_Libbluray.h  |    9 +++
 src/libbluray/disc/disc.c                          |    5 ++
 src/libbluray/disc/disc.h                          |    3 +
 6 files changed, 112 insertions(+), 1 deletion(-)

diff --git a/src/libbluray/bdj/java/java/io/BDFileSystem.java b/src/libbluray/bdj/java/java/io/BDFileSystem.java
index 6bb7987..3107330 100644
--- a/src/libbluray/bdj/java/java/io/BDFileSystem.java
+++ b/src/libbluray/bdj/java/java/io/BDFileSystem.java
@@ -244,7 +244,21 @@ public abstract class BDFileSystem extends FileSystem {
     }
 
     public String[] list(File f) {
-        return fs.list(f);
+
+        String path = f.getPath();
+        String root = System.getProperty("bluray.vfs.root");
+        if (root == null || !path.startsWith(root)) {
+            /* not inside VFS */
+            return fs.list(f);
+        }
+
+        /* path is inside VFS */
+        /* EX. HOSTEL_2 lists files in BD-ROM */
+        int rootLength = root.length();
+        path = path.substring(rootLength);
+
+        String[] names = org.videolan.Libbluray.listBdFiles(path, false);
+        return names;
     }
 
     public boolean createDirectory(File f) {
diff --git a/src/libbluray/bdj/java/org/videolan/Libbluray.java b/src/libbluray/bdj/java/org/videolan/Libbluray.java
index 119a02b..a46d192 100644
--- a/src/libbluray/bdj/java/org/videolan/Libbluray.java
+++ b/src/libbluray/bdj/java/org/videolan/Libbluray.java
@@ -327,6 +327,10 @@ public class Libbluray {
         return cacheBdRomFileN(nativePointer, path, cachePath) == 0;
     }
 
+    public static String[] listBdFiles(String path, boolean onlyBdRom) {
+        return listBdFilesN(nativePointer, path, onlyBdRom);
+    }
+
     public static void updateGraphic(int width, int height, int[] rgbArray) {
         updateGraphicN(nativePointer, width, height, rgbArray,
                        0, 0, width - 1, height - 1);
@@ -544,6 +548,7 @@ public class Libbluray {
     private static native int setVirtualPackageN(long np, String vpPath, boolean psrBackup);
     private static native int readPSRN(long np, int num);
     private static native int cacheBdRomFileN(long np, String path, String cachePath);
+    private static native String[] listBdFilesN(long np, String path, boolean onlyBdRom);
     private static native Bdjo getBdjoN(long np, String name);
     private static native void updateGraphicN(long np, int width, int height, int[] rgbArray,
                                               int x0, int y0, int x1, int y1);
diff --git a/src/libbluray/bdj/native/org_videolan_Libbluray.c b/src/libbluray/bdj/native/org_videolan_Libbluray.c
index b5c62df..dcfdd8e 100644
--- a/src/libbluray/bdj/native/org_videolan_Libbluray.c
+++ b/src/libbluray/bdj/native/org_videolan_Libbluray.c
@@ -34,7 +34,9 @@
 
 #include "file/file.h"
 #include "util/logging.h"
+#include "util/macro.h"
 
+#include <stdlib.h>
 #include <string.h>
 #include <inttypes.h>
 
@@ -358,6 +360,74 @@ JNIEXPORT jint JNICALL Java_org_videolan_Libbluray_cacheBdRomFileN(JNIEnv * env,
     return result;
 }
 
+JNIEXPORT jobjectArray JNICALL Java_org_videolan_Libbluray_listBdFilesN(JNIEnv * env,
+                                                                        jclass cls, jlong np, jstring jpath,
+                                                                        jboolean onlyBdRom) {
+
+    BLURAY *bd = (BLURAY*)(intptr_t)np;
+    BD_DISC *disc = bd_get_disc(bd);
+
+    const char *path = (*env)->GetStringUTFChars(env, jpath, NULL);
+    if (!path) {
+        BD_DEBUG(DBG_JNI | DBG_CRIT, "listBdFilesN() failed: no path\n");
+        return NULL;
+    }
+    BD_DEBUG(DBG_JNI, "listBdFilesN(%s)\n", path);
+
+    /* open directory stream */
+    BD_DIR_H *dp;
+    if (onlyBdRom) {
+        dp = disc_open_bdrom_dir(disc, path);
+    } else {
+        dp = disc_open_dir(disc, path);
+    }
+    if (!dp) {
+        BD_DEBUG(DBG_JNI | DBG_CRIT, "failed opening directory %s\n", path);
+        (*env)->ReleaseStringUTFChars(env, jpath, path);
+        return NULL;
+    }
+    (*env)->ReleaseStringUTFChars(env, jpath, path);
+
+    /* count files and create java strings (java array size must be known when it is created) */
+    jstring  *files = NULL;
+    unsigned  count = 0;
+    unsigned  allocated = 0;
+    BD_DIRENT ent;
+    while (!dir_read(dp, &ent)) {
+        if (strcmp(ent.d_name, ".") && strcmp(ent.d_name, "..")) {
+            if (allocated <= count) {
+                allocated += 512;
+                jstring *tmp = realloc(files, sizeof(*files) * allocated);
+                if (!tmp) {
+                    BD_DEBUG(DBG_JNI | DBG_CRIT, "failed allocating memory for %u directory entries\n", allocated);
+                    break;
+                }
+                files = tmp;
+            }
+            files[count] = (*env)->NewStringUTF(env, ent.d_name);
+            count++;
+        }
+    }
+    dir_close(dp);
+
+    /* allocate java array */
+    jobjectArray arr = bdj_make_array(env, "java/lang/String", count);
+    if (!arr) {
+        BD_DEBUG(DBG_JNI | DBG_CRIT, "failed creating array [%d]\n", count);
+    } else {
+        /* populate files to array */
+        unsigned ii;
+        for (ii = 0; ii < count; ii++) {
+            (*env)->SetObjectArrayElement(env, arr, ii, files[ii]);
+        }
+    }
+
+    X_FREE(files);
+
+    return arr;
+}
+
+
 JNIEXPORT jobject JNICALL Java_org_videolan_Libbluray_getBdjoN(JNIEnv * env,
                                                                jclass cls, jlong np, jstring jfile) {
 
@@ -609,6 +679,11 @@ Java_org_videolan_Libbluray_methods[] =
         VC(Java_org_videolan_Libbluray_cacheBdRomFileN),
     },
     {
+        CC("listBdFilesN"),
+        CC("(JLjava/lang/String;Z)[Ljava/lang/String;"),
+        VC(Java_org_videolan_Libbluray_listBdFilesN),
+    },
+    {
         CC("getBdjoN"),
         CC("(JLjava/lang/String;)Lorg/videolan/bdjo/Bdjo;"),
         VC(Java_org_videolan_Libbluray_getBdjoN),
diff --git a/src/libbluray/bdj/native/org_videolan_Libbluray.h b/src/libbluray/bdj/native/org_videolan_Libbluray.h
index 1c5f234..7c7d3d0 100644
--- a/src/libbluray/bdj/native/org_videolan_Libbluray.h
+++ b/src/libbluray/bdj/native/org_videolan_Libbluray.h
@@ -230,6 +230,15 @@ JNIEXPORT jint JNICALL Java_org_videolan_Libbluray_cacheBdRomFileN
 
 /*
  * Class:     org_videolan_Libbluray
+ * Method:    listBdFilesN
+ * Signature: (JLjava/lang/String;Z)[Ljava/lang/String;
+ */
+JNIEXPORT jobjectArray JNICALL Java_org_videolan_Libbluray_listBdFilesN(JNIEnv * env,
+                                                                        jclass cls, jlong np, jstring jpath,
+                                                                        jboolean onlyBdRom);
+
+/*
+ * Class:     org_videolan_Libbluray
  * Method:    getBdjoN
  * Signature: (JLjava/lang/String;)Lorg/videolan/bdjo/Bdjo;
  */
diff --git a/src/libbluray/disc/disc.c b/src/libbluray/disc/disc.c
index 1de00b1..c8aa934 100644
--- a/src/libbluray/disc/disc.c
+++ b/src/libbluray/disc/disc.c
@@ -294,6 +294,11 @@ const char *disc_volume_id(BD_DISC *p)
     return p ? p->udf_volid : NULL;
 }
 
+BD_DIR_H *disc_open_bdrom_dir(BD_DISC *p, const char *rel_path)
+{
+    return p->pf_dir_open_bdrom(p->fs_handle, rel_path);
+}
+
 /*
  * VFS
  */
diff --git a/src/libbluray/disc/disc.h b/src/libbluray/disc/disc.h
index 408e9af..d1a0162 100644
--- a/src/libbluray/disc/disc.h
+++ b/src/libbluray/disc/disc.h
@@ -66,6 +66,9 @@ BD_PRIVATE void disc_update(BD_DISC *disc, const char *overlay_root);
 
 BD_PRIVATE int  disc_cache_bdrom_file(BD_DISC *p, const char *rel_path, const char *cache_path);
 
+/* open BD-ROM directory (relative to disc root) */
+BD_PRIVATE struct bd_dir_s  *disc_open_bdrom_dir(BD_DISC *disc, const char *path);
+
 /*
  * m2ts stream interface
  */



More information about the libbluray-devel mailing list