]> git.sesse.net Git - vlc/blobdiff - modules/access/file.c
Used uint64_t for access_t::info.i_size/i_pos and access_t::pf_seek().
[vlc] / modules / access / file.c
index 8d34cab4f81c8f756c3e369172ec63be22f0a43f..42a1213b53c1b6a20988694a73e16cfedaa76775 100644 (file)
@@ -35,9 +35,7 @@
 
 #include <assert.h>
 #include <errno.h>
-#ifdef HAVE_SYS_TYPES_H
-#   include <sys/types.h>
-#endif
+#include <sys/types.h>
 #ifdef HAVE_SYS_STAT_H
 #   include <sys/stat.h>
 #endif
@@ -75,6 +73,7 @@
 #endif
 
 #include <vlc_charset.h>
+#include <vlc_url.h>
 
 struct access_sys_t
 {
@@ -132,31 +131,38 @@ static bool IsRemote (int fd)
 int Open( vlc_object_t *p_this )
 {
     access_t     *p_access = (access_t*)p_this;
-    access_sys_t *p_sys = malloc (sizeof (*p_sys));
     const char   *path = p_access->psz_path;
 #ifdef WIN32
     bool is_remote = false;
 #endif
 
-    if (unlikely(p_sys == NULL))
-        return VLC_ENOMEM;
-
-    access_InitFields (p_access);
-    p_access->pf_read = FileRead;
-    p_access->pf_block = NULL;
-    p_access->pf_control = FileControl;
-    p_access->pf_seek = FileSeek;
-    p_access->p_sys = p_sys;
-    p_sys->i_nb_reads = 0;
-    p_sys->b_pace_control = true;
-
     /* Open file */
     int fd = -1;
 
     if (!strcasecmp (p_access->psz_access, "fd"))
-        fd = dup (atoi (path));
-    else if (!strcmp (path, "-"))
-        fd = dup (0);
+    {
+        char *end;
+        int oldfd = strtol (path, &end, 10);
+
+        if (*end == '\0')
+        {
+            fd = dup (oldfd);
+            if (fd != -1)
+                lseek (fd, 0, SEEK_SET);
+        }
+#ifdef HAVE_FDOPENDIR
+        else if (*end == '/' && end > path)
+        {
+            char *name = decode_URI_duplicate (end - 1);
+            if (name != NULL) /* TODO: ToLocale(), FD_CLOEXEC */
+            {
+                name[0] = '.';
+                fd = openat (oldfd, name, O_RDONLY | O_NONBLOCK);
+                free (name);
+            }
+        }
+#endif
+    }
     else
     {
         msg_Dbg (p_access, "opening file `%s'", path);
@@ -178,11 +184,9 @@ int Open( vlc_object_t *p_this )
 #endif
     }
     if (fd == -1)
-        goto error;
+        return VLC_EGENERIC;
 
-#ifdef HAVE_SYS_STAT_H
     struct stat st;
-
     if (fstat (fd, &st))
     {
         msg_Err (p_access, "failed to read (%m)");
@@ -192,9 +196,35 @@ int Open( vlc_object_t *p_this )
      * how to parse the data. The directory plugin will do it. */
     if (S_ISDIR (st.st_mode))
     {
+        if (!strcasecmp (p_access->psz_access, "fd"))
+            goto error; /* fd:// not supported for directories yet */
+#ifdef HAVE_FDOPENDIR
+        DIR *handle = fdopendir (fd);
+        if (handle == NULL)
+            goto error; /* Uh? */
+        return DirInit (p_access, handle);
+#else
         msg_Dbg (p_access, "ignoring directory");
         goto error;
+#endif
     }
+
+    access_sys_t *p_sys = malloc (sizeof (*p_sys));
+    if (unlikely(p_sys == NULL))
+        goto error;
+    access_InitFields (p_access);
+    p_access->pf_read = FileRead;
+    p_access->pf_block = NULL;
+    p_access->pf_control = FileControl;
+    p_access->pf_seek = FileSeek;
+    p_access->p_sys = p_sys;
+    p_sys->i_nb_reads = 0;
+    p_sys->fd = fd;
+    p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
+    if (IsRemote(fd))
+        p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
+    p_sys->b_pace_control = true;
+
     if (S_ISREG (st.st_mode))
         p_access->info.i_size = st.st_size;
     else if (!S_ISBLK (st.st_mode))
@@ -202,15 +232,6 @@ int Open( vlc_object_t *p_this )
         p_access->pf_seek = NoSeek;
         p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
     }
-#else
-# warning File size not known!
-#endif
-
-    p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
-    if (IsRemote(fd))
-        p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
-
-    p_sys->fd = fd;
 
     if (p_access->pf_seek != NoSeek)
     {
@@ -232,9 +253,7 @@ int Open( vlc_object_t *p_this )
     return VLC_SUCCESS;
 
 error:
-    if (fd != -1)
-        close (fd);
-    free (p_sys);
+    close (fd);
     return VLC_EGENERIC;
 }
 
@@ -244,6 +263,13 @@ error:
 void Close (vlc_object_t * p_this)
 {
     access_t     *p_access = (access_t*)p_this;
+
+    if (p_access->pf_read == NULL)
+    {
+        DirClose (p_this);
+        return;
+    }
+
     access_sys_t *p_sys = p_access->p_sys;
 
     close (p_sys->fd);
@@ -313,7 +339,7 @@ ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
 /*****************************************************************************
  * Seek: seek to a specific location in a file
  *****************************************************************************/
-int FileSeek (access_t *p_access, int64_t i_pos)
+int FileSeek (access_t *p_access, uint64_t i_pos)
 {
     p_access->info.i_pos = i_pos;
     p_access->info.b_eof = false;
@@ -322,7 +348,7 @@ int FileSeek (access_t *p_access, int64_t i_pos)
     return VLC_SUCCESS;
 }
 
-int NoSeek (access_t *p_access, int64_t i_pos)
+int NoSeek (access_t *p_access, uint64_t i_pos)
 {
     /* assert(0); ?? */
     (void) p_access; (void) i_pos;