[libbluray-devel] bd_open(): convert /dev/ nodes to mount point (Linux only)

Rafaël Carré git at videolan.org
Mon Jan 16 10:44:11 CET 2012


libbluray | branch: master | Rafaël Carré <funman at videolan.org> | Mon Jan  2 12:55:17 2012 -0500| [8b7a58e7c2f1f162524c27440ad80df394ef5bba] | committer: hpi1

bd_open(): convert /dev/ nodes to mount point (Linux only)

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

 src/libbluray/bluray.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/src/libbluray/bluray.c b/src/libbluray/bluray.c
index 9645553..32230e7 100644
--- a/src/libbluray/bluray.c
+++ b/src/libbluray/bluray.c
@@ -47,6 +47,13 @@
 #include <inttypes.h>
 #include <string.h>
 
+#ifdef __linux__
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#endif
+
 typedef int     (*fptr_int)();
 typedef int32_t (*fptr_int32)();
 typedef void*   (*fptr_p_void)();
@@ -907,6 +914,54 @@ static void _fill_disc_info(BLURAY *bd)
     }
 }
 
+#ifdef __linux__
+/*
+ * Replace device node (/dev/sr0) by mount point
+ * Implemented on Linux only at the moment, could be added for other OS
+ */
+static void get_mount_point(BLURAY *bd)
+{
+    struct stat st;
+    stat(bd->device_path, &st);
+
+    /* If it's a directory, all is good */
+    if (S_ISDIR(st.st_mode))
+        return;
+
+    char *line = NULL;
+    size_t len = 0;
+    size_t devlen = strlen(bd->device_path);
+
+    FILE *f = fopen("/proc/mounts", "r");
+    if (!f)
+        return;
+
+    while (getline(&line, &len, f) != -1) {
+        if (strncmp(line, bd->device_path, devlen))
+            continue;
+
+        /* if device is not followed immediately by a space it's not the one */
+        /* e.g. when looking for "/dev/sda1", "/dev/sda XXX" won't match */
+        char *space = strchr(line, ' ');
+        if (space != &line[devlen])
+            continue;
+
+        space = strchr(space + 1, ' '); /* space points just after the mount dir */
+        if (!space) /* corrupted line ? */
+            break;
+
+        *space = '\0';
+        /* FIXME : spaces (at least) are octal escaped */
+        free(bd->device_path);
+        bd->device_path = strdup(&line[devlen+1]);
+        break;
+    }
+
+    free(line);
+    fclose(f);
+}
+#endif
+
 /*
  * open / close
  */
@@ -929,6 +984,10 @@ BLURAY *bd_open(const char* device_path, const char* keyfile_path)
 
     bd->device_path = strdup(device_path);
 
+#ifdef __linux__
+    get_mount_point(bd);
+#endif
+
     _libaacs_open(bd, keyfile_path);
 
     _libbdplus_open(bd, keyfile_path);



More information about the libbluray-devel mailing list