file.c 16.7 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
gbazin's avatar
gbazin 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
dionoea's avatar
dionoea 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>
zorglub's avatar
zorglub committed
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

gbazin's avatar
   
gbazin committed
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
gbazin's avatar
gbazin committed
71
72
#endif

zorglub's avatar
zorglub committed
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")
gbazin's avatar
   
gbazin committed
82
#define CACHING_LONGTEXT N_( \
83
    "Caching value for files. This " \
zorglub's avatar
zorglub committed
84
    "value should be set in milliseconds." )
gbazin's avatar
gbazin committed
85
86
#define CAT_TEXT N_("Concatenate with additional files")
#define CAT_LONGTEXT N_( \
zorglub's avatar
zorglub committed
87
88
    "Play split files as if they were part of a unique file. " \
    "You need to specify a comma-separated list of files." )
gbazin's avatar
   
gbazin committed
89

90
vlc_module_begin();
zorglub's avatar
zorglub committed
91
    set_description( _("File input") );
zorglub's avatar
zorglub committed
92
    set_shortname( _("File") );
zorglub's avatar
zorglub committed
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 * );
gbazin's avatar
gbazin committed
116

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

123
    int fd;
gbazin's avatar
gbazin 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;
gbazin's avatar
gbazin committed
175

176
    while (fd != -1)
gbazin's avatar
gbazin 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
gbazin's avatar
gbazin committed
186

187
188
189
        close (fd);
        fd = -1;
    }
190
#endif
gbazin's avatar
gbazin 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;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
208
209
    /* Autodetect mmap() support */
    if (p_sys->b_pace_control && S_ISREG (st.st_mode) && (st.st_size > 0))
210
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
211
212
213
        void *addr = mmap (NULL, 1, PROT_READ, MAP_PRIVATE, fd, 0);
        if (addr != MAP_FAILED)
        {
Rémi Denis-Courmont's avatar
typo    
Rémi Denis-Courmont committed
214
            /* Does the file system support mmap? */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
215
216
217
218
219
220
221
            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)");
222
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
223
224
    else
        msg_Dbg (p_this, "mmap disabled (non regular file)");
225
# endif
226
#else
227
    p_sys->b_seekable = !b_stdin;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
228
# warning File size not known!
229
230
#endif

231
    if (p_sys->b_seekable && (p_access->info.i_size == 0))
232
233
    {
        /* FIXME that's bad because all others access will be probed */
234
        msg_Err (p_access, "file is empty, aborting");
235
236
        Close (p_this);
        return VLC_EGENERIC;
gbazin's avatar
gbazin committed
237
238
    }

239
    return VLC_SUCCESS;
240
241
242
}

/*****************************************************************************
243
 * Close: close the target
244
 *****************************************************************************/
245
static void Close (vlc_object_t * p_this)
246
{
247
248
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
249

250
    close (p_sys->fd);
251
    free (p_sys);
252
253
254
255
256
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
257
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
258
{
259
    access_sys_t *p_sys = p_access->p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
260
    ssize_t i_ret;
261
    int fd = p_sys->fd;
262

263
#if !defined(WIN32) && !defined(UNDER_CE)
264
    if( !p_sys->b_pace_control )
265
    {
266
        if( !p_sys->b_kfir )
267
        {
268
            /* Find if some data is available. This won't work under Windows. */
269
            do
270
            {
271
                struct pollfd ufd;
272

273
                if( p_access->b_die )
274
275
                    return 0;

276
                memset (&ufd, 0, sizeof (ufd));
277
                ufd.fd = fd;
278
279
                ufd.events = POLLIN;

280
                i_ret = poll (&ufd, 1, 500);
281
            }
282
            while (i_ret <= 0);
283

284
            i_ret = read (fd, p_buffer, i_len);
285
286
        }
        else
287
        {
288
            /* b_kfir ; work around a buggy poll() driver implementation */
289
290
            while (((i_ret = read (fd, p_buffer, i_len)) == 0)
                && !p_access->b_die)
291
            {
292
                msleep( INPUT_ERROR_SLEEP );
293
            }
294
295
        }
    }
296
    else
297
#endif /* WIN32 || UNDER_CE */
298
        /* b_pace_control || WIN32 */
299
        i_ret = read( fd, p_buffer, i_len );
300

301
302
    if( i_ret < 0 )
    {
303
        switch (errno)
304
        {
305
306
307
308
309
            case EINTR:
            case EAGAIN:
                break;

            default:
310
                msg_Err (p_access, "read failed (%m)");
311
                intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
312
                                _("VLC could not read the file."));
313
        }
314
315
316
317

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

    p_sys->i_nb_reads++;
321

322
#ifdef HAVE_SYS_STAT_H
323
324
    if( p_access->info.i_size != 0 &&
        (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
325
    {
326
        struct stat st;
gbazin's avatar
gbazin committed
327

328
        if ((fstat (fd, &st) == 0)
329
         && (p_access->info.i_size != st.st_size))
330
        {
331
            p_access->info.i_size = st.st_size;
332
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
333
334
335
336
        }
    }
#endif

337
338
339
340
341
    if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
    else if( i_ret == 0 )
        p_access->info.b_eof = VLC_TRUE;

342
343
344
    return i_ret;
}

345
#ifdef HAVE_MMAP
346
# define MMAP_SIZE (1 << 20)
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

struct block_sys_t
{
    block_t self;
    //vlc_object_t *owner;
    void *base_addr;
    size_t length;
};

static void mmapRelease (block_t *block)
{
    block_sys_t *p_sys = (block_sys_t *)block;

    munmap (p_sys->base_addr, p_sys->length);
    //vlc_object_release (p_sys->owner);
    free (p_sys);
}

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
369
    const int flags = MAP_SHARED;
370
371
372
    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);
373
374
    void *addr;

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

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

387
388
389
390
391
392
393
        /* 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;
        }
394
395
396
    }

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

400
401
402
    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 */
403
404
405
406
407
    assert (length > 0);

    addr = mmap (NULL, length, PROT_READ, flags, p_sys->fd, offset);
    if (addr == MAP_FAILED)
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
408
409
410
        msg_Err (p_access, "memory mapping failed (%m)");
        intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
                        _("VLC could not read the file."));
411
412
413
414
        msleep( INPUT_ERROR_SLEEP );
        return NULL;
    }

415
    p_access->info.i_pos = offset + length;
416

417
    msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
             (unsigned long)length, addr, (unsigned long)offset);
    block_sys_t *block = malloc (sizeof (*block));
    if (block == NULL)
    {
        munmap (addr, length);
        return NULL;
    }

    block_Init (&block->self, ((uint8_t *)addr) + align, length - align);
    block->self.pf_release = mmapRelease;
    block->base_addr = addr;
    block->length = length;
    //vlc_object_yield (block->owner = VLC_OBJECT (p_access));

    return &block->self;
}
#endif

436
437
438
/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
439
static int Seek (access_t *p_access, int64_t i_pos)
440
{
441
442
    /* FIXME: i_size should really be unsigned */
    if ((uint64_t)i_pos > (uint64_t)p_access->info.i_size)
gbazin's avatar
gbazin committed
443
    {
444
445
446
447
        /* 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
448
        i_pos = p_access->info.i_size;
gbazin's avatar
gbazin committed
449
    }
450
451
452
453

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

454
455
456
#ifdef HAVE_MMAP
    if (p_access->pf_block == NULL)
#endif
Rafaël Carré's avatar
Rafaël Carré committed
457
        lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    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 )
472
    {
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
        /* */
        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 * );
494
            *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
495
            break;
496

497
498
499
500
501
502
503
504
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
505
        case ACCESS_SET_PRIVATE_ID_STATE:
506
        case ACCESS_GET_META:
507
        case ACCESS_GET_CONTENT_TYPE:
508
509
510
            return VLC_EGENERIC;

        default:
511
            msg_Warn( p_access, "unimplemented query in control" );
512
513
            return VLC_EGENERIC;

514
    }
515
    return VLC_SUCCESS;
516
517
}

518

519
520
static char *expand_path (const access_t *p_access, const char *path)
{
Rafaël Carré's avatar
Rafaël Carré committed
521
    if (strncmp (path, "~/", 2) == 0)
522
523
524
525
526
527
528
529
530
531
    {
        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)
Rafaël Carré's avatar
Rafaël Carré committed
532
    if (!strcasecmp (p_access->psz_access, "file")
533
534
535
536
537
538
539
540
541
542
      && ('/' == 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);
}


gbazin's avatar
gbazin committed
543
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
544
 * open_file: Opens a specific file
gbazin's avatar
gbazin committed
545
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
546
static int open_file (access_t *p_access, const char *psz_name)
gbazin's avatar
gbazin committed
547
{
548
    char *path = expand_path (p_access, psz_name);
gbazin's avatar
gbazin committed
549
550

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

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

576
# if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
577
578
    /* We'd rather use any available memory for reading ahead
     * than for caching what we've already seen/heard */
579
580
581
    fcntl (fd, F_RDAHEAD, 1);
    fcntl (fd, F_NOCACHE, 1);
# endif
gbazin's avatar
gbazin committed
582
583
#endif

584
    return fd;
gbazin's avatar
gbazin committed
585
}