file.c 17.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * file.c: file input (file: access plug-in)
 *****************************************************************************
4
 * Copyright (C) 2001-2006 the VideoLAN team
5
 * Copyright © 2006-2007 Rémi Denis-Courmont
Gildas Bazin's avatar
Gildas Bazin committed
6
 * $Id$
7 8
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9
 *          Rémi Denis-Courmont <rem # videolan # org>
10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
15
 *
16 17 18 19 20 21 22
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
30 31 32
#include <vlc_input.h>
#include <vlc_access.h>
#include <vlc_interface.h>
33

34
#include <assert.h>
35
#include <errno.h>
36 37 38 39 40 41 42 43 44 45
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
46 47 48
#if defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
#else
49
#   include <unistd.h>
50
#   include <poll.h>
51
#endif
52 53 54
#ifdef HAVE_MMAP
#   include <sys/mman.h>
#endif
55

56
#if defined( WIN32 ) && !defined( UNDER_CE )
57 58 59 60 61 62 63 64 65 66 67 68 69 70
#   ifdef lseek
#      undef lseek
#   endif
#   define lseek _lseeki64
#elif defined( UNDER_CE )
#   ifdef read
#      undef read
#   endif
#   define read(a,b,c) fread(b,1,c,a)
#   define close(a) fclose(a)
#   ifdef lseek
#      undef lseek
#   endif
#   define lseek fseek
Gildas Bazin's avatar
Gildas Bazin committed
71 72
#endif

73
#include <vlc_charset.h>
74

75 76 77
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
78 79 80
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

Christophe Massiot's avatar
Christophe Massiot committed
81
#define CACHING_TEXT N_("Caching value in ms")
82
#define CACHING_LONGTEXT N_( \
83
    "Caching value for files. This " \
84
    "value should be set in milliseconds." )
Gildas Bazin's avatar
Gildas Bazin committed
85 86
#define CAT_TEXT N_("Concatenate with additional files")
#define CAT_LONGTEXT N_( \
87 88
    "Play split files as if they were part of a unique file. " \
    "You need to specify a comma-separated list of files." )
89

90
vlc_module_begin();
91
    set_description( _("File input") );
92
    set_shortname( _("File") );
93 94
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
95
    add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
96
    add_obsolete_string( "file-cat" );
97
    set_capability( "access2", 50 );
98 99
    add_shortcut( "file" );
    add_shortcut( "stream" );
100
    add_shortcut( "kfir" );
101 102
    set_callbacks( Open, Close );
vlc_module_end();
103 104


105
/*****************************************************************************
106
 * Exported prototypes
107
 *****************************************************************************/
108
static int  Seek( access_t *, int64_t );
109
static ssize_t Read( access_t *, uint8_t *, size_t );
110
static int  Control( access_t *, int, va_list );
111
#ifdef HAVE_MMAP
112
static block_t *mmapBlock( access_t * );
113
#endif
114

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
115
static int  open_file( access_t *, const char * );
Gildas Bazin's avatar
Gildas Bazin committed
116

117
struct access_sys_t
118
{
119
    uint64_t     pagemask;
120 121
    unsigned int i_nb_reads;
    vlc_bool_t   b_kfir;
Gildas Bazin's avatar
Gildas Bazin committed
122

123
    int fd;
Gildas Bazin's avatar
Gildas Bazin committed
124

125 126 127 128
    /* */
    vlc_bool_t b_seekable;
    vlc_bool_t b_pace_control;
};
129

130 131 132 133 134
/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
135 136 137
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;

138
    vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
139

140 141
    /* Update default_pts to a suitable value for file access */
    var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
142

143
    STANDARD_READ_ACCESS_INIT;
144 145
    p_sys->i_nb_reads = 0;
    p_sys->b_kfir = VLC_FALSE;
146
    int fd = p_sys->fd = -1;
147

148
    if (!strcasecmp (p_access->psz_access, "stream"))
149
    {
150 151
        p_sys->b_seekable = VLC_FALSE;
        p_sys->b_pace_control = VLC_FALSE;
152
    }
153
    else if (!strcasecmp (p_access->psz_access, "kfir"))
154
    {
155 156 157
        p_sys->b_seekable = VLC_FALSE;
        p_sys->b_pace_control = VLC_FALSE;
        p_sys->b_kfir = VLC_TRUE;
158
    }
159 160
    else
    {
161
        p_sys->b_seekable = VLC_TRUE;
162
        p_sys->b_pace_control = VLC_TRUE;
163
    }
164

165 166
    /* Open file */
    msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
167

168 169 170 171
    if (b_stdin)
        fd = dup (0);
    else
        fd = open_file (p_access, p_access->psz_path);
172

173
#ifdef HAVE_SYS_STAT_H
174
    struct stat st;
Gildas Bazin's avatar
Gildas Bazin committed
175

176
    while (fd != -1)
Gildas Bazin's avatar
Gildas Bazin committed
177
    {
178
        if (fstat (fd, &st))
179
            msg_Err (p_access, "fstat(%d): %m", fd);
180
        else
181 182 183 184 185
        if (S_ISDIR (st.st_mode))
            /* The directory plugin takes care of that */
            msg_Dbg (p_access, "file is a directory, aborting");
        else
            break; // success
Gildas Bazin's avatar
Gildas Bazin committed
186

187 188 189
        close (fd);
        fd = -1;
    }
190
#endif
Gildas Bazin's avatar
Gildas Bazin committed
191

192 193 194 195 196 197
    if (fd == -1)
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
    p_sys->fd = fd;
198 199

#ifdef HAVE_SYS_STAT_H
200 201 202 203
    p_access->info.i_size = st.st_size;
    if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
     && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
        p_sys->b_seekable = VLC_FALSE;
204 205

# ifdef HAVE_MMAP
206 207
    p_sys->pagemask = sysconf (_SC_PAGE_SIZE) - 1;

208 209
    /* Autodetect mmap() support */
    if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
210
    {
211 212 213 214 215 216 217 218
        /* TODO: Do not allow PROT_WRITE, we should not need it.
         * However, this far, "block" ownership seems such that whoever
         * "receives" a block can freely modify its content. Hence we _may_
         * need PROT_WRITE not to default memory protection.
         * NOTE: With MAP_PRIVATE, changes are not committed to the underlying
         * file, write open permission is not required.
         */
        void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
219 220
        if (addr != MAP_FAILED)
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221
            /* Does the file system support mmap? */
222 223 224 225 226 227 228
            munmap (addr, 1);
            p_access->pf_read = NULL;
            p_access->pf_block = mmapBlock;
            msg_Dbg (p_this, "mmap enabled");
        }
        else
            msg_Dbg (p_this, "mmap disabled (%m)");
229
    }
230 231
    else
        msg_Dbg (p_this, "mmap disabled (non regular file)");
232
# endif
233
#else
234
    p_sys->b_seekable = !b_stdin;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
235
# warning File size not known!
236 237
#endif

238
    if (p_sys->b_seekable && (p_access->info.i_size == 0))
239 240
    {
        /* FIXME that's bad because all others access will be probed */
241
        msg_Err (p_access, "file is empty, aborting");
242 243
        Close (p_this);
        return VLC_EGENERIC;
Gildas Bazin's avatar
Gildas Bazin committed
244 245
    }

246
    return VLC_SUCCESS;
247 248 249
}

/*****************************************************************************
250
 * Close: close the target
251
 *****************************************************************************/
252
static void Close (vlc_object_t * p_this)
253
{
254 255
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
256

257
    close (p_sys->fd);
258
    free (p_sys);
259 260 261 262 263
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
264
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
265
{
266
    access_sys_t *p_sys = p_access->p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
267
    ssize_t i_ret;
268
    int fd = p_sys->fd;
269

270
#if !defined(WIN32) && !defined(UNDER_CE)
271
    if( !p_sys->b_pace_control )
272
    {
273
        if( !p_sys->b_kfir )
274
        {
275
            /* Find if some data is available. This won't work under Windows. */
276
            do
277
            {
278
                struct pollfd ufd;
279

280
                if( p_access->b_die )
281 282
                    return 0;

283
                memset (&ufd, 0, sizeof (ufd));
284
                ufd.fd = fd;
285 286
                ufd.events = POLLIN;

287
                i_ret = poll (&ufd, 1, 500);
288
            }
289
            while (i_ret <= 0);
290

291
            i_ret = read (fd, p_buffer, i_len);
292 293
        }
        else
294
        {
295
            /* b_kfir ; work around a buggy poll() driver implementation */
296 297
            while (((i_ret = read (fd, p_buffer, i_len)) == 0)
                && !p_access->b_die)
298
            {
299
                msleep( INPUT_ERROR_SLEEP );
300
            }
301 302
        }
    }
303
    else
304
#endif /* WIN32 || UNDER_CE */
305
        /* b_pace_control || WIN32 */
306
        i_ret = read( fd, p_buffer, i_len );
307

308 309
    if( i_ret < 0 )
    {
310
        switch (errno)
311
        {
312 313 314 315 316
            case EINTR:
            case EAGAIN:
                break;

            default:
317
                msg_Err (p_access, "read failed (%m)");
318
                intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
319
                                _("VLC could not read the file."));
320
        }
321 322 323 324

        /* Delay a bit to avoid consuming all the CPU. This is particularly
         * useful when reading from an unconnected FIFO. */
        msleep( INPUT_ERROR_SLEEP );
325
    }
326 327

    p_sys->i_nb_reads++;
328

329
#ifdef HAVE_SYS_STAT_H
330 331
    if( p_access->info.i_size != 0 &&
        (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
332
    {
333
        struct stat st;
Gildas Bazin's avatar
Gildas Bazin committed
334

335
        if ((fstat (fd, &st) == 0)
336
         && (p_access->info.i_size != st.st_size))
337
        {
338
            p_access->info.i_size = st.st_size;
339
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
340 341 342 343
        }
    }
#endif

344 345 346 347 348
    if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
    else if( i_ret == 0 )
        p_access->info.b_eof = VLC_TRUE;

349 350 351
    return i_ret;
}

352
#ifdef HAVE_MMAP
353
# define MMAP_SIZE (1 << 20)
354 355 356 357 358

static block_t *mmapBlock (access_t *p_access)
{
    access_sys_t *p_sys = p_access->p_sys;

359
    const int flags = MAP_SHARED;
360 361 362
    off_t offset = p_access->info.i_pos & ~p_sys->pagemask;
    size_t align = p_access->info.i_pos & p_sys->pagemask;
    size_t length = (MMAP_SIZE > p_sys->pagemask) ? MMAP_SIZE : (p_sys->pagemask + 1);
363 364
    void *addr;

365 366 367 368 369 370 371
#ifndef NDEBUG
    int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
    if (dbgpos != p_access->info.i_pos)
        msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
                 p_access->info.i_pos, dbgpos);
#endif

372
    if (p_access->info.i_pos >= p_access->info.i_size)
373
    {
374
        /* End of file - check if file size changed... */
375 376 377
        struct stat st;

        if ((fstat (p_sys->fd, &st) == 0)
378
         && (st.st_size != p_access->info.i_size))
379 380 381 382 383
        {
            p_access->info.i_size = st.st_size;
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
        }

384 385 386 387 388 389 390
        /* Really at end of file then */
        if (p_access->info.i_pos >= p_access->info.i_size)
        {
            p_access->info.b_eof = VLC_TRUE;
            msg_Dbg (p_access, "at end of memory mapped file");
            return NULL;
        }
391 392 393
    }

    if (offset + length > p_access->info.i_size)
394
        /* Don't mmap beyond end of file */
395 396
        length = p_access->info.i_size - offset;

397 398 399
    assert (offset <= p_access->info.i_pos);               /* and */
    assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
    assert (offset < p_access->info.i_size);               /* imply */
400 401 402 403 404
    assert (length > 0);

    addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
    if (addr == MAP_FAILED)
    {
405 406 407
        msg_Err (p_access, "memory mapping failed (%m)");
        intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
                        _("VLC could not read the file."));
408 409 410 411
        msleep( INPUT_ERROR_SLEEP );
        return NULL;
    }

412
    p_access->info.i_pos = offset + length;
413

414
    block_t *block = block_mmap_Alloc (addr, length);
415 416 417
    if (block == NULL)
        return NULL;

418 419
    block->p_buffer += align;
    block->i_buffer -= align;
420

421
#ifndef NDEBUG
422 423 424
    msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
             (unsigned long)length, addr, (unsigned long)offset);

425
    /* Compare normal I/O with memory mapping */
426 427
    char *buf = malloc (block->i_buffer);
    ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
428

429
    if (i_read != (ssize_t)block->i_buffer)
430
        msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
431 432
                 (unsigned)block->i_buffer);
    if (memcmp (buf, block->p_buffer, block->i_buffer))
433
        msg_Err (p_access, "inconsistent data buffer");
434
    free (buf);
435 436
#endif

437
    return block;
438 439 440
}
#endif

441 442 443
/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
444
static int Seek (access_t *p_access, int64_t i_pos)
445
{
446 447
    /* FIXME: i_size should really be unsigned */
    if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
Gildas Bazin's avatar
Gildas Bazin committed
448
    {
449 450 451 452
        /* This should only happen with corrupted files.
         * But it also seems to happen with buggy demuxes (ASF) */
        msg_Err (p_access, "seeking too far (0x"I64Fx" / 0x"I64Fx")",
                 i_pos, p_access->info.i_size);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
453
        i_pos = p_access->info.i_size;
Gildas Bazin's avatar
Gildas Bazin committed
454
    }
455 456 457 458

    p_access->info.i_pos = i_pos;
    p_access->info.b_eof = VLC_FALSE;

459
#if defined (HAVE_MMAP) && defined (NDEBUG)
460 461
    if (p_access->pf_block == NULL)
#endif
Rafaël Carré's avatar
Rafaël Carré committed
462
        lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
463 464 465 466 467 468 469 470 471 472 473 474 475 476
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    access_sys_t *p_sys = p_access->p_sys;
    vlc_bool_t   *pb_bool;
    int          *pi_int;
    int64_t      *pi_64;

    switch( i_query )
477
    {
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = p_sys->b_seekable;
            break;

        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = p_sys->b_pace_control;
            break;

        /* */
        case ACCESS_GET_MTU:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = 0;
            break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
499
            *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
500
            break;
501

502 503 504 505 506 507 508 509
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
510
        case ACCESS_SET_PRIVATE_ID_STATE:
511
        case ACCESS_GET_META:
512
        case ACCESS_GET_CONTENT_TYPE:
513 514 515
            return VLC_EGENERIC;

        default:
516
            msg_Warn( p_access, "unimplemented query in control" );
517 518
            return VLC_EGENERIC;

519
    }
520
    return VLC_SUCCESS;
521 522
}

523

524 525
static char *expand_path (const access_t *p_access, const char *path)
{
526
    if (strncmp (path, "~/", 2) == 0)
527 528 529 530 531 532 533 534 535 536
    {
        char *res;

         // TODO: we should also support the ~cmassiot/ syntax
         if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
             return NULL;
         return res;
    }

#if defined(WIN32)
537
    if (!strcasecmp (p_access->psz_access, "file")
538 539 540 541 542 543 544 545 546 547
      && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
        // Explorer can open path such as file:/C:/ or file:///C:/
        // hence remove leading / if found
        return strdup (path + 1);
#endif

    return strdup (path);
}


Gildas Bazin's avatar
Gildas Bazin committed
548
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
549
 * open_file: Opens a specific file
Gildas Bazin's avatar
Gildas Bazin committed
550
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
551
static int open_file (access_t *p_access, const char *psz_name)
Gildas Bazin's avatar
Gildas Bazin committed
552
{
553
    char *path = expand_path (p_access, psz_name);
Gildas Bazin's avatar
Gildas Bazin committed
554 555

#ifdef UNDER_CE
556
    p_sys->fd = utf8_fopen( path, "rb" );
557
    if ( !p_sys->fd )
Gildas Bazin's avatar
Gildas Bazin committed
558
    {
559
        msg_Err( p_access, "cannot open file %s", psz_name );
560
        intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"),
561
                        _("VLC could not open the file \"%s\"."), psz_name );
562
        free (path);
Gildas Bazin's avatar
Gildas Bazin committed
563 564
        return VLC_EGENERIC;
    }
565 566 567

    fseek( p_sys->fd, 0, SEEK_END );
    p_access->info.i_size = ftell( p_sys->fd );
568
    p_access->info.i_update |= INPUT_UPDATE_SIZE;
569
    fseek( p_sys->fd, 0, SEEK_SET );
Gildas Bazin's avatar
Gildas Bazin committed
570
#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
571
    int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
572
    free (path);
573
    if (fd == -1)
Gildas Bazin's avatar
Gildas Bazin committed
574
    {
575
        msg_Err (p_access, "cannot open file %s (%m)", psz_name);
576
        intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
577
                        _("VLC could not open the file \"%s\"."), psz_name);
578
        return -1;
Gildas Bazin's avatar
Gildas Bazin committed
579
    }
580

581
# if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
582 583
    /* We'd rather use any available memory for reading ahead
     * than for caching what we've already seen/heard */
584 585 586
    fcntl (fd, F_RDAHEAD, 1);
    fcntl (fd, F_NOCACHE, 1);
# endif
Gildas Bazin's avatar
Gildas Bazin committed
587 588
#endif

589
    return fd;
Gildas Bazin's avatar
Gildas Bazin committed
590
}