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