]> git.sesse.net Git - vlc/blob - modules/access/mmap.c
9f358546f541299b319d806a255db47b14eefb75
[vlc] / modules / access / mmap.c
1 /*****************************************************************************
2  * mmap.c: memory-mapped file input
3  *****************************************************************************
4  * Copyright © 2007-2008 Rémi Denis-Courmont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_access.h>
29 #include <vlc_input.h>
30 #include <vlc_charset.h>
31 #include <vlc_interface.h>
32
33 #include <assert.h>
34
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #include <sys/mman.h>
41
42 #define FILE_MMAP_TEXT N_("Use file memory mapping")
43 #define FILE_MMAP_LONGTEXT N_( \
44     "Try to use memory mapping to read files and block devices." )
45
46 #ifndef NDEBUG
47 /*# define MMAP_DEBUG 1*/
48 #endif
49
50 static int Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
52
53 vlc_module_begin();
54     set_shortname (N_("MMap"));
55     set_description (N_("Memory-mapped file input"));
56     set_category (CAT_INPUT);
57     set_subcategory (SUBCAT_INPUT_ACCESS);
58     set_capability ("access", 52);
59     add_shortcut ("file");
60     set_callbacks (Open, Close);
61
62     add_bool ("file-mmap", true, NULL,
63               FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true);
64 vlc_module_end();
65
66 static block_t *Block (access_t *);
67 static int Seek (access_t *, int64_t);
68 static int Control (access_t *, int, va_list);
69
70 struct access_sys_t
71 {
72     size_t page_size;
73     size_t mtu;
74     int    fd;
75 };
76
77 #define MMAP_SIZE (1 << 20)
78
79 static int Open (vlc_object_t *p_this)
80 {
81     access_t *p_access = (access_t *)p_this;
82     access_sys_t *p_sys;
83     const char *path = p_access->psz_path;
84     int fd;
85
86     if (!var_CreateGetBool (p_this, "file-mmap"))
87         return VLC_EGENERIC; /* disabled */
88
89     STANDARD_BLOCK_ACCESS_INIT;
90
91     if (!strcmp (p_access->psz_path, "-"))
92         fd = dup (0);
93     else
94     {
95         msg_Dbg (p_access, "opening file %s", path);
96         fd = utf8_open (path, O_RDONLY | O_NOCTTY, 0666);
97     }
98
99     if (fd == -1)
100     {
101         msg_Warn (p_access, "cannot open %s: %m", path);
102         goto error;
103     }
104
105     /* mmap() is only safe for regular and block special files.
106      * For other types, it may be some idiosyncrasic interface (e.g. packet
107      * sockets are a good example), if it works at all. */
108     struct stat st;
109
110     if (fstat (fd, &st))
111     {
112         msg_Err (p_access, "cannot stat %s: %m", path);
113         goto error;
114     }
115
116     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode))
117     {
118         msg_Dbg (p_access, "skipping non regular file %s", path);
119         goto error;
120     }
121
122     /* Autodetect mmap() support */
123     if (st.st_size > 0)
124     {
125         void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
126         if (addr != MAP_FAILED)
127             munmap (addr, 1);
128         else
129             goto error;
130     }
131
132     p_sys->page_size = sysconf (_SC_PAGE_SIZE);
133     p_sys->mtu = MMAP_SIZE;
134     if (p_sys->mtu < p_sys->page_size)
135         p_sys->mtu = p_sys->page_size;
136     p_sys->fd = fd;
137
138     p_access->info.i_size = st.st_size;
139 #ifdef HAVE_POSIX_FADVISE    
140     posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
141 #endif 
142
143     return VLC_SUCCESS;
144
145 error:
146     if (fd != -1)
147         close (fd);
148     free (p_sys);
149     return VLC_EGENERIC;
150 }
151
152
153 static void Close (vlc_object_t * p_this)
154 {
155     access_t *p_access = (access_t *)p_this;
156     access_sys_t *p_sys = p_access->p_sys;
157
158     close (p_sys->fd); /* don't care about error when only reading */
159     free (p_sys);
160 }
161
162 static block_t *Block (access_t *p_access)
163 {
164     access_sys_t *p_sys = p_access->p_sys;
165
166     if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
167     {
168         /* End of file - check if file size changed... */
169         struct stat st;
170
171         if ((fstat (p_sys->fd, &st) == 0)
172          && (st.st_size != p_access->info.i_size))
173         {
174             p_access->info.i_size = st.st_size;
175             p_access->info.i_update |= INPUT_UPDATE_SIZE;
176         }
177
178         /* Really at end of file then */
179         if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
180         {
181             p_access->info.b_eof = true;
182             msg_Dbg (p_access, "at end of memory mapped file");
183             return NULL;
184         }
185     }
186
187 #ifdef MMAP_DEBUG
188     int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
189     if (dbgpos != p_access->info.i_pos)
190         msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
191                  p_access->info.i_pos, dbgpos);
192 #endif
193
194     const uintptr_t page_mask = p_sys->page_size - 1;
195     /* Start the mapping on a page boundary: */
196     off_t  outer_offset = p_access->info.i_pos & ~page_mask;
197     /* Skip useless bytes at the beginning of the first page: */
198     size_t inner_offset = p_access->info.i_pos & page_mask;
199     /* Map no more bytes than remain: */
200     size_t length = p_sys->mtu;
201     if (outer_offset + length > p_access->info.i_size)
202         length = p_access->info.i_size - outer_offset;
203
204     assert (outer_offset <= p_access->info.i_pos);          /* and */
205     assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
206     assert (outer_offset < p_access->info.i_size);         /* imply */
207     assert (length > 0);
208
209     /* NOTE: We use PROT_WRITE and MAP_PRIVATE so that the block can be
210      * modified down the chain, without messing up with the underlying
211      * original file. This does NOT need open write permission. */
212     void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE,
213                        p_sys->fd, outer_offset);
214     if (addr == MAP_FAILED)
215     {
216         msg_Err (p_access, "memory mapping failed (%m)");
217         intf_UserFatal (p_access, false, _("File reading failed"),
218                         _("VLC could not read the file."));
219         msleep (INPUT_ERROR_SLEEP);
220         return NULL;
221     }
222 #ifdef HAVE_POSIX_MADVISE    
223     posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
224 #endif
225
226     block_t *block = block_mmap_Alloc (addr, length);
227     if (block == NULL)
228         return NULL;
229
230     block->p_buffer += inner_offset;
231     block->i_buffer -= inner_offset;
232
233 #ifdef MMAP_DEBUG
234     msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
235              (unsigned long)length, addr, (unsigned long)outer_offset);
236
237     /* Compare normal I/O with memory mapping */
238     char *buf = malloc (block->i_buffer);
239     ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
240
241     if (i_read != (ssize_t)block->i_buffer)
242         msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
243                  (unsigned)block->i_buffer);
244     if (memcmp (buf, block->p_buffer, block->i_buffer))
245         msg_Err (p_access, "inconsistent data buffer");
246     free (buf);
247 #endif
248
249     p_access->info.i_pos = outer_offset + length;
250     return block;
251 }
252
253
254 static int Seek (access_t *p_access, int64_t i_pos)
255 {
256 #ifdef MMAP_DEBUG
257     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
258 #endif
259
260     p_access->info.i_pos = i_pos;
261     p_access->info.b_eof = false;
262     return VLC_SUCCESS;
263 }
264
265
266 static int Control (access_t *p_access, int query, va_list args)
267 {
268     access_sys_t *p_sys = p_access->p_sys;
269
270     switch (query)
271     {
272         case ACCESS_CAN_SEEK:
273         case ACCESS_CAN_FASTSEEK:
274         case ACCESS_CAN_PAUSE:
275         case ACCESS_CAN_CONTROL_PACE:
276             *((bool *)va_arg (args, bool *)) = true;
277             return VLC_SUCCESS;
278
279         case ACCESS_GET_MTU:
280             *((int *)va_arg (args, int *)) = p_sys->mtu;
281             return VLC_SUCCESS;
282
283         case ACCESS_GET_PTS_DELAY:
284             *((int64_t *)va_arg (args, int64_t *)) = DEFAULT_PTS_DELAY;
285             return VLC_SUCCESS;
286
287         case ACCESS_GET_TITLE_INFO:
288         case ACCESS_GET_META:
289             break;
290
291         case ACCESS_SET_PAUSE_STATE:
292             return VLC_SUCCESS;
293
294         case ACCESS_SET_TITLE:
295         case ACCESS_SET_SEEKPOINT:
296         case ACCESS_SET_PRIVATE_ID_STATE:
297         case ACCESS_SET_PRIVATE_ID_CA:
298         case ACCESS_GET_PRIVATE_ID_STATE:
299         case ACCESS_GET_CONTENT_TYPE:
300             break;
301
302         default:
303             msg_Warn (p_access, "unimplemented query %d in control", query);
304     }
305
306     return VLC_EGENERIC;
307 }