]> git.sesse.net Git - vlc/blob - modules/access/mmap.c
Remove most stray semi-colons in module descriptions
[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     add_bool ("file-mmap", false, NULL,
62               FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true);
63 vlc_module_end ()
64
65 static block_t *Block (access_t *);
66 static int Seek (access_t *, int64_t);
67 static int Control (access_t *, int, va_list);
68
69 struct access_sys_t
70 {
71     size_t page_size;
72     size_t mtu;
73     int    fd;
74 };
75
76 #define MMAP_SIZE (1 << 20)
77
78 static int Open (vlc_object_t *p_this)
79 {
80     access_t *p_access = (access_t *)p_this;
81     access_sys_t *p_sys;
82     const char *path = p_access->psz_path;
83     int fd;
84
85     assert ((INT64_C(1) << 63) == ((off_t)(INT64_C(1) << 63)));
86
87     if (!var_CreateGetBool (p_this, "file-mmap"))
88         return VLC_EGENERIC; /* disabled */
89
90     STANDARD_BLOCK_ACCESS_INIT;
91
92     if (!strcmp (p_access->psz_path, "-"))
93         fd = dup (0);
94     else
95     {
96         msg_Dbg (p_access, "opening file %s", path);
97         fd = utf8_open (path, O_RDONLY | O_NOCTTY, 0666);
98     }
99
100     if (fd == -1)
101     {
102         msg_Warn (p_access, "cannot open %s: %m", path);
103         goto error;
104     }
105     fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC);
106
107     /* mmap() is only safe for regular and block special files.
108      * For other types, it may be some idiosyncrasic interface (e.g. packet
109      * sockets are a good example), if it works at all. */
110     struct stat st;
111
112     if (fstat (fd, &st))
113     {
114         msg_Err (p_access, "cannot stat %s: %m", path);
115         goto error;
116     }
117
118     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode))
119     {
120         msg_Dbg (p_access, "skipping non regular file %s", path);
121         goto error;
122     }
123
124 #if defined(HAVE_FCNTL_H)
125     /* We'd rather use any available memory for reading ahead
126      * than for caching what we've already mmap'ed */
127 # if defined(F_RDAHEAD)
128     fcntl (fd, F_RDAHEAD, 1);
129 # endif
130 # if defined(F_NOCACHE)
131     fcntl (fd, F_NOCACHE, 1);
132 # endif
133 #endif
134
135     /* Autodetect mmap() support */
136     if (st.st_size > 0)
137     {
138         void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
139         if (addr != MAP_FAILED)
140             munmap (addr, 1);
141         else
142             goto error;
143     }
144
145     p_sys->page_size = sysconf (_SC_PAGE_SIZE);
146     p_sys->mtu = MMAP_SIZE;
147     if (p_sys->mtu < p_sys->page_size)
148         p_sys->mtu = p_sys->page_size;
149     p_sys->fd = fd;
150
151     p_access->info.i_size = st.st_size;
152 #ifdef HAVE_POSIX_FADVISE    
153     posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
154 #endif 
155
156     return VLC_SUCCESS;
157
158 error:
159     if (fd != -1)
160         close (fd);
161     free (p_sys);
162     return VLC_EGENERIC;
163 }
164
165
166 static void Close (vlc_object_t * p_this)
167 {
168     access_t *p_access = (access_t *)p_this;
169     access_sys_t *p_sys = p_access->p_sys;
170
171     close (p_sys->fd); /* don't care about error when only reading */
172     free (p_sys);
173 }
174
175 static block_t *Block (access_t *p_access)
176 {
177     access_sys_t *p_sys = p_access->p_sys;
178
179     /* Check if file size changed... */
180     struct stat st;
181
182     if ((fstat (p_sys->fd, &st) == 0)
183      && (st.st_size != p_access->info.i_size))
184     {
185         p_access->info.i_size = st.st_size;
186         p_access->info.i_update |= INPUT_UPDATE_SIZE;
187     }
188
189     if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
190     {
191         /* We are at end of file */
192         p_access->info.b_eof = true;
193         msg_Dbg (p_access, "at end of memory mapped file");
194         return NULL;
195     }
196
197 #ifdef MMAP_DEBUG
198     int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
199     if (dbgpos != p_access->info.i_pos)
200         msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
201                  p_access->info.i_pos, dbgpos);
202 #endif
203
204     const uintptr_t page_mask = p_sys->page_size - 1;
205     /* Start the mapping on a page boundary: */
206     off_t  outer_offset = p_access->info.i_pos & ~page_mask;
207     /* Skip useless bytes at the beginning of the first page: */
208     size_t inner_offset = p_access->info.i_pos & page_mask;
209     /* Map no more bytes than remain: */
210     size_t length = p_sys->mtu;
211     if (outer_offset + length > p_access->info.i_size)
212         length = p_access->info.i_size - outer_offset;
213
214     assert (outer_offset <= p_access->info.i_pos);          /* and */
215     assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
216     assert (outer_offset < p_access->info.i_size);         /* imply */
217     assert (length > 0);
218
219     /* NOTE: We use PROT_WRITE and MAP_PRIVATE so that the block can be
220      * modified down the chain, without messing up with the underlying
221      * original file. This does NOT need open write permission. */
222     void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE
223 #ifdef MAP_NO_CACHE
224                        | MAP_NOCACHE
225 #endif
226                        , p_sys->fd, outer_offset);
227     if (addr == MAP_FAILED)
228     {
229         msg_Err (p_access, "memory mapping failed (%m)");
230         intf_UserFatal (p_access, false, _("File reading failed"),
231                         _("VLC could not read the file."));
232         goto fatal;
233     }
234 #ifdef HAVE_POSIX_MADVISE    
235     posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
236 #endif
237
238     block_t *block = block_mmap_Alloc (addr, length);
239     if (block == NULL)
240         goto fatal;
241
242     block->p_buffer += inner_offset;
243     block->i_buffer -= inner_offset;
244
245 #ifdef MMAP_DEBUG
246     msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
247              (unsigned long)length, addr, (unsigned long)outer_offset);
248
249     /* Compare normal I/O with memory mapping */
250     char *buf = malloc (block->i_buffer);
251     ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
252
253     if (i_read != (ssize_t)block->i_buffer)
254         msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
255                  (unsigned)block->i_buffer);
256     if (memcmp (buf, block->p_buffer, block->i_buffer))
257         msg_Err (p_access, "inconsistent data buffer");
258     free (buf);
259 #endif
260
261     p_access->info.i_pos = outer_offset + length;
262     return block;
263
264 fatal:
265     p_access->info.b_eof = true;
266     return NULL;
267 }
268
269
270 static int Seek (access_t *p_access, int64_t i_pos)
271 {
272 #ifdef MMAP_DEBUG
273     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
274 #endif
275
276     p_access->info.i_pos = i_pos;
277     p_access->info.b_eof = false;
278     return VLC_SUCCESS;
279 }
280
281
282 static int Control (access_t *p_access, int query, va_list args)
283 {
284     access_sys_t *p_sys = p_access->p_sys;
285
286     switch (query)
287     {
288         case ACCESS_CAN_SEEK:
289         case ACCESS_CAN_FASTSEEK:
290         case ACCESS_CAN_PAUSE:
291         case ACCESS_CAN_CONTROL_PACE:
292             *((bool *)va_arg (args, bool *)) = true;
293             return VLC_SUCCESS;
294
295         case ACCESS_GET_MTU:
296             *((int *)va_arg (args, int *)) = p_sys->mtu;
297             return VLC_SUCCESS;
298
299         case ACCESS_GET_PTS_DELAY:
300         {
301             int delay_ms = var_CreateGetInteger (p_access, "file-caching");
302             *((int64_t *)va_arg (args, int64_t *)) = delay_ms * INT64_C (1000);
303             return VLC_SUCCESS;
304         }
305
306         case ACCESS_GET_TITLE_INFO:
307         case ACCESS_GET_META:
308             break;
309
310         case ACCESS_SET_PAUSE_STATE:
311             return VLC_SUCCESS;
312
313         case ACCESS_SET_TITLE:
314         case ACCESS_SET_SEEKPOINT:
315         case ACCESS_SET_PRIVATE_ID_STATE:
316         case ACCESS_SET_PRIVATE_ID_CA:
317         case ACCESS_GET_PRIVATE_ID_STATE:
318         case ACCESS_GET_CONTENT_TYPE:
319             break;
320
321         default:
322             msg_Warn (p_access, "unimplemented query %d in control", query);
323     }
324
325     return VLC_EGENERIC;
326 }