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