[vlc-devel] [PATCH 1/5] url: add vlc_uri2fd

Thomas Guillem thomas at gllm.fr
Thu Nov 5 12:09:08 CET 2015


---
 include/vlc_url.h     |  1 +
 modules/access/file.c | 27 +--------------------------
 src/libvlccore.sym    |  1 +
 src/text/url.c        | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 51 insertions(+), 26 deletions(-)

diff --git a/include/vlc_url.h b/include/vlc_url.h
index cbd67ab..72208fd 100644
--- a/include/vlc_url.h
+++ b/include/vlc_url.h
@@ -31,6 +31,7 @@
  */
 
 VLC_API char *vlc_path2uri (const char *path, const char *scheme) VLC_MALLOC;
+VLC_API int vlc_uri2fd (const char *uri);
 
 struct vlc_url_t
 {
diff --git a/modules/access/file.c b/modules/access/file.c
index f697ef3..fbd9aa0 100644
--- a/modules/access/file.c
+++ b/modules/access/file.c
@@ -141,32 +141,7 @@ int FileOpen( vlc_object_t *p_this )
     access_t *p_access = (access_t*)p_this;
 
     /* Open file */
-    int fd = -1;
-
-    if (!strcasecmp (p_access->psz_access, "fd"))
-    {
-        char *end;
-        int oldfd = strtol (p_access->psz_location, &end, 10);
-
-        if (*end == '\0')
-            fd = vlc_dup (oldfd);
-        else if (*end == '/' && end > p_access->psz_location)
-        {
-            char *name = decode_URI_duplicate (end - 1);
-            if (name != NULL)
-            {
-                name[0] = '.';
-                fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
-                free (name);
-            }
-        }
-    }
-    else
-    {
-        if (unlikely(p_access->psz_filepath == NULL))
-            return VLC_EGENERIC;
-        fd = vlc_open (p_access->psz_filepath, O_RDONLY | O_NONBLOCK);
-    }
+    int fd = vlc_uri2fd (p_access->psz_url);
 
     if (fd == -1)
     {
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index a9e2636..204b5d5 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -248,6 +248,7 @@ libvlc_ArtRequest
 vlc_UrlParse
 vlc_UrlClean
 vlc_path2uri
+vlc_uri2fd
 make_path
 mdate
 module_config_free
diff --git a/src/text/url.c b/src/text/url.c
index aeebfea..d7aa9d4 100644
--- a/src/text/url.c
+++ b/src/text/url.c
@@ -30,12 +30,15 @@
 #ifdef _WIN32
 # include <io.h>
 #endif
+#include <fcntl.h>
 
 #include <vlc_common.h>
 #include <vlc_url.h>
 #include <vlc_fs.h>
 #include <ctype.h>
 
+char *get_path(const char *location);
+
 /**
  * Decodes an encoded URI component. See also decode_URI().
  * \return decoded string allocated on the heap, or NULL on error.
@@ -253,6 +256,51 @@ char *vlc_path2uri (const char *path, const char *scheme)
 }
 
 /**
+ * Get a fd from an URI
+ * @param uri nul-terminated URI string
+ * @return a valid fd (use close() to release it), or -1 in case of error
+ */
+int vlc_uri2fd (const char *uri)
+{
+    const char *p = strstr(uri, "://");
+    if (p == NULL)
+        return -1;
+
+    const char *location = p + 3;
+    if (strncmp(uri, "fd", p - uri) == 0)
+    {
+        char *end;
+        int oldfd = strtol (location, &end, 10);
+
+        if (*end == '\0')
+            return vlc_dup (oldfd);
+        else if (*end == '/' && end > location)
+        {
+            char *name = decode_URI_duplicate (end - 1);
+            if (name != NULL)
+            {
+                name[0] = '.';
+                int fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
+                free (name);
+                return fd;
+            }
+        }
+    }
+    else
+    {
+        char *path = get_path (location);
+        if (path != NULL)
+        {
+            int fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
+            free (path);
+            return fd;
+        }
+
+    }
+    return -1;
+}
+
+/**
  * Tries to convert a URI to a local (UTF-8-encoded) file path.
  * @param url URI to convert
  * @return NULL on error, a nul-terminated string otherwise
-- 
2.1.4



More information about the vlc-devel mailing list