| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 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 |
|
|---|
| 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; |
|---|
| 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 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 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 |
|
|---|
| 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); |
|---|
| 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 |
|
|---|
| 167 |
struct stat st; |
|---|
| 168 |
|
|---|
| 169 |
if ((fstat (p_sys->fd, &st) == 0) |
|---|
| 170 |
&& (st.st_size != p_access->info.i_size)) |
|---|
| 171 |
{ |
|---|
| 172 |
p_access->info.i_size = st.st_size; |
|---|
| 173 |
p_access->info.i_update |= INPUT_UPDATE_SIZE; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size) |
|---|
| 177 |
{ |
|---|
| 178 |
|
|---|
| 179 |
p_access->info.b_eof = true; |
|---|
| 180 |
msg_Dbg (p_access, "at end of memory mapped file"); |
|---|
| 181 |
return NULL; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
#ifdef MMAP_DEBUG |
|---|
| 185 |
int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR); |
|---|
| 186 |
if (dbgpos != p_access->info.i_pos) |
|---|
| 187 |
msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx", |
|---|
| 188 |
p_access->info.i_pos, dbgpos); |
|---|
| 189 |
#endif |
|---|
| 190 |
|
|---|
| 191 |
const uintptr_t page_mask = p_sys->page_size - 1; |
|---|
| 192 |
|
|---|
| 193 |
off_t outer_offset = p_access->info.i_pos & ~page_mask; |
|---|
| 194 |
|
|---|
| 195 |
size_t inner_offset = p_access->info.i_pos & page_mask; |
|---|
| 196 |
|
|---|
| 197 |
size_t length = p_sys->mtu; |
|---|
| 198 |
if (outer_offset + length > p_access->info.i_size) |
|---|
| 199 |
length = p_access->info.i_size - outer_offset; |
|---|
| 200 |
|
|---|
| 201 |
assert (outer_offset <= p_access->info.i_pos); |
|---|
| 202 |
assert (p_access->info.i_pos < p_access->info.i_size); |
|---|
| 203 |
assert (outer_offset < p_access->info.i_size); |
|---|
| 204 |
assert (length > 0); |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, |
|---|
| 210 |
p_sys->fd, outer_offset); |
|---|
| 211 |
if (addr == MAP_FAILED) |
|---|
| 212 |
{ |
|---|
| 213 |
msg_Err (p_access, "memory mapping failed (%m)"); |
|---|
| 214 |
intf_UserFatal (p_access, false, _("File reading failed"), |
|---|
| 215 |
_("VLC could not read the file.")); |
|---|
| 216 |
msleep (INPUT_ERROR_SLEEP); |
|---|
| 217 |
return NULL; |
|---|
| 218 |
} |
|---|
| 219 |
#ifdef HAVE_POSIX_MADVISE |
|---|
| 220 |
posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL); |
|---|
| 221 |
#endif |
|---|
| 222 |
|
|---|
| 223 |
block_t *block = block_mmap_Alloc (addr, length); |
|---|
| 224 |
if (block == NULL) |
|---|
| 225 |
return NULL; |
|---|
| 226 |
|
|---|
| 227 |
block->p_buffer += inner_offset; |
|---|
| 228 |
block->i_buffer -= inner_offset; |
|---|
| 229 |
|
|---|
| 230 |
#ifdef MMAP_DEBUG |
|---|
| 231 |
msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx", |
|---|
| 232 |
(unsigned long)length, addr, (unsigned long)outer_offset); |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
char *buf = malloc (block->i_buffer); |
|---|
| 236 |
ssize_t i_read = read (p_sys->fd, buf, block->i_buffer); |
|---|
| 237 |
|
|---|
| 238 |
if (i_read != (ssize_t)block->i_buffer) |
|---|
| 239 |
msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read, |
|---|
| 240 |
(unsigned)block->i_buffer); |
|---|
| 241 |
if (memcmp (buf, block->p_buffer, block->i_buffer)) |
|---|
| 242 |
msg_Err (p_access, "inconsistent data buffer"); |
|---|
| 243 |
free (buf); |
|---|
| 244 |
#endif |
|---|
| 245 |
|
|---|
| 246 |
p_access->info.i_pos = outer_offset + length; |
|---|
| 247 |
return block; |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
static int Seek (access_t *p_access, int64_t i_pos) |
|---|
| 252 |
{ |
|---|
| 253 |
#ifdef MMAP_DEBUG |
|---|
| 254 |
lseek (p_access->p_sys->fd, i_pos, SEEK_SET); |
|---|
| 255 |
#endif |
|---|
| 256 |
|
|---|
| 257 |
p_access->info.i_pos = i_pos; |
|---|
| 258 |
p_access->info.b_eof = false; |
|---|
| 259 |
return VLC_SUCCESS; |
|---|
| 260 |
} |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
static int Control (access_t *p_access, int query, va_list args) |
|---|
| 264 |
{ |
|---|
| 265 |
access_sys_t *p_sys = p_access->p_sys; |
|---|
| 266 |
|
|---|
| 267 |
switch (query) |
|---|
| 268 |
{ |
|---|
| 269 |
case ACCESS_CAN_SEEK: |
|---|
| 270 |
case ACCESS_CAN_FASTSEEK: |
|---|
| 271 |
case ACCESS_CAN_PAUSE: |
|---|
| 272 |
case ACCESS_CAN_CONTROL_PACE: |
|---|
| 273 |
*((bool *)va_arg (args, bool *)) = true; |
|---|
| 274 |
return VLC_SUCCESS; |
|---|
| 275 |
|
|---|
| 276 |
case ACCESS_GET_MTU: |
|---|
| 277 |
*((int *)va_arg (args, int *)) = p_sys->mtu; |
|---|
| 278 |
return VLC_SUCCESS; |
|---|
| 279 |
|
|---|
| 280 |
case ACCESS_GET_PTS_DELAY: |
|---|
| 281 |
*((int64_t *)va_arg (args, int64_t *)) = DEFAULT_PTS_DELAY; |
|---|
| 282 |
return VLC_SUCCESS; |
|---|
| 283 |
|
|---|
| 284 |
case ACCESS_GET_TITLE_INFO: |
|---|
| 285 |
case ACCESS_GET_META: |
|---|
| 286 |
break; |
|---|
| 287 |
|
|---|
| 288 |
case ACCESS_SET_PAUSE_STATE: |
|---|
| 289 |
return VLC_SUCCESS; |
|---|
| 290 |
|
|---|
| 291 |
case ACCESS_SET_TITLE: |
|---|
| 292 |
case ACCESS_SET_SEEKPOINT: |
|---|
| 293 |
case ACCESS_SET_PRIVATE_ID_STATE: |
|---|
| 294 |
case ACCESS_SET_PRIVATE_ID_CA: |
|---|
| 295 |
case ACCESS_GET_PRIVATE_ID_STATE: |
|---|
| 296 |
case ACCESS_GET_CONTENT_TYPE: |
|---|
| 297 |
break; |
|---|
| 298 |
|
|---|
| 299 |
default: |
|---|
| 300 |
msg_Warn (p_access, "unimplemented query %d in control", query); |
|---|
| 301 |
} |
|---|
| 302 |
|
|---|
| 303 |
return VLC_EGENERIC; |
|---|
| 304 |
} |
|---|