]> git.sesse.net Git - vlc/blobdiff - modules/access/file.c
Fix Windows case which implements fd_set differently:
[vlc] / modules / access / file.c
index 577deec1903d79a471c4acca23484b1bbaf152c5..9f5750d317f29589b0f74841bf3b04a87b7cfd00 100644 (file)
@@ -116,6 +116,7 @@ static int  open_file( access_t *, const char * );
 
 struct access_sys_t
 {
+    uint64_t     pagemask;
     unsigned int i_nb_reads;
     vlc_bool_t   b_kfir;
 
@@ -202,10 +203,19 @@ static int Open( vlc_object_t *p_this )
         p_sys->b_seekable = VLC_FALSE;
 
 # ifdef HAVE_MMAP
+    p_sys->pagemask = sysconf (_SC_PAGE_SIZE) - 1;
+
     /* Autodetect mmap() support */
     if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
     {
-        void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
+        /* TODO: Do not allow PROT_WRITE, we should not need it.
+         * However, this far, "block" ownership seems such that whoever
+         * "receives" a block can freely modify its content. Hence we _may_
+         * need PROT_WRITE not to default memory protection.
+         * NOTE: With MAP_PRIVATE, changes are not committed to the underlying
+         * file, write open permission is not required.
+         */
+        void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
         if (addr != MAP_FAILED)
         {
             /* Does the file system support mmap? */
@@ -340,39 +350,28 @@ static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
 }
 
 #ifdef HAVE_MMAP
-# define MMAP_SIZE (1 << 18)
-
-struct block_sys_t
-{
-    block_t self;
-    //vlc_object_t *owner;
-    void *base_addr;
-    size_t length;
-};
-
-static void mmapRelease (block_t *block)
-{
-    block_sys_t *p_sys = (block_sys_t *)block;
-
-    munmap (p_sys->base_addr, p_sys->length);
-    //vlc_object_release (p_sys->owner);
-    free (p_sys);
-}
+# define MMAP_SIZE (1 << 20)
 
 static block_t *mmapBlock (access_t *p_access)
 {
     access_sys_t *p_sys = p_access->p_sys;
 
     const int flags = MAP_SHARED;
-    const size_t pagesize = sysconf (_SC_PAGE_SIZE);
-    off_t offset = p_access->info.i_pos & ~(pagesize - 1);
-    size_t align = p_access->info.i_pos & (pagesize - 1);
-    size_t length = (MMAP_SIZE > pagesize) ? MMAP_SIZE : pagesize;
+    off_t offset = p_access->info.i_pos & ~p_sys->pagemask;
+    size_t align = p_access->info.i_pos & p_sys->pagemask;
+    size_t length = (MMAP_SIZE > p_sys->pagemask) ? MMAP_SIZE : (p_sys->pagemask + 1);
     void *addr;
 
+#ifndef NDEBUG
+    int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
+    if (dbgpos != p_access->info.i_pos)
+        msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
+                 p_access->info.i_pos, dbgpos);
+#endif
+
     if (p_access->info.i_pos >= p_access->info.i_size)
     {
-        /* End of file - check that file size hasn't change... */
+        /* End of file - check if file size changed... */
         struct stat st;
 
         if ((fstat (p_sys->fd, &st) == 0)
@@ -392,7 +391,7 @@ static block_t *mmapBlock (access_t *p_access)
     }
 
     if (offset + length > p_access->info.i_size)
-        /* Don't mmap paste end of file */
+        /* Don't mmap beyond end of file */
         length = p_access->info.i_size - offset;
 
     assert (offset <= p_access->info.i_pos);               /* and */
@@ -412,22 +411,30 @@ static block_t *mmapBlock (access_t *p_access)
 
     p_access->info.i_pos = offset + length;
 
-    msg_Dbg (p_access, "mapped %lu bytes at %p from offset %lu",
-             (unsigned long)length, addr, (unsigned long)offset);
-    block_sys_t *block = malloc (sizeof (*block));
+    block_t *block = block_mmap_Alloc (addr, length);
     if (block == NULL)
-    {
-        munmap (addr, length);
         return NULL;
-    }
 
-    block_Init (&block->self, ((uint8_t *)addr) + align, length - align);
-    block->self.pf_release = mmapRelease;
-    block->base_addr = addr;
-    block->length = length;
-    //vlc_object_yield (block->owner = VLC_OBJECT (p_access));
+    block->p_buffer += align;
+    block->i_buffer -= align;
+
+#ifndef NDEBUG
+    msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
+             (unsigned long)length, addr, (unsigned long)offset);
+
+    /* Compare normal I/O with memory mapping */
+    char *buf = malloc (block->i_buffer);
+    ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
 
-    return &block->self;
+    if (i_read != (ssize_t)block->i_buffer)
+        msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
+                 (unsigned)block->i_buffer);
+    if (memcmp (buf, block->p_buffer, block->i_buffer))
+        msg_Err (p_access, "inconsistent data buffer");
+    free (buf);
+#endif
+
+    return block;
 }
 #endif
 
@@ -436,21 +443,20 @@ static block_t *mmapBlock (access_t *p_access)
  *****************************************************************************/
 static int Seek (access_t *p_access, int64_t i_pos)
 {
-    if (i_pos > p_access->info.i_size)
+    /* FIXME: i_size should really be unsigned */
+    if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
     {
-        msg_Err (p_access, "seeking too far");
+        /* This should only happen with corrupted files.
+         * But it also seems to happen with buggy demuxes (ASF) */
+        msg_Err (p_access, "seeking too far (0x"I64Fx" / 0x"I64Fx")",
+                 i_pos, p_access->info.i_size);
         i_pos = p_access->info.i_size;
     }
-    else if (i_pos < 0)
-    {
-        msg_Err (p_access, "seeking too early");
-        i_pos = 0;
-    }
 
     p_access->info.i_pos = i_pos;
     p_access->info.b_eof = VLC_FALSE;
 
-#ifdef HAVE_MMAP
+#if defined (HAVE_MMAP) && defined (NDEBUG)
     if (p_access->pf_block == NULL)
 #endif
         lseek (p_access->p_sys->fd, i_pos, SEEK_SET);