file.c 16.4 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 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
30
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/input.h>
31
#include <vlc_interaction.h>
32

33
34
#include <stdlib.h>
#include <string.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
52
#endif

gbazin's avatar
   
gbazin committed
53
#if defined( WIN32 ) && !defined( UNDER_CE )
54
/* fstat() support for large files on win32 */
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#   define fstat(a,b) _fstati64(a,b)
#   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
70
71
#endif

72
73
#include "charset.h"

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

Christophe Massiot's avatar
Christophe Massiot committed
80
#define CACHING_TEXT N_("Caching value in ms")
gbazin's avatar
   
gbazin committed
81
#define CACHING_LONGTEXT N_( \
82
    "Caching value for files. This " \
zorglub's avatar
zorglub committed
83
    "value should be set in milliseconds." )
gbazin's avatar
gbazin committed
84
85
#define CAT_TEXT N_("Concatenate with additional files")
#define CAT_LONGTEXT N_( \
zorglub's avatar
zorglub committed
86
87
    "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
88

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


104
/*****************************************************************************
105
 * Exported prototypes
106
 *****************************************************************************/
107
108
109
110
static int  Seek( access_t *, int64_t );
static int  Read( access_t *, uint8_t *, int );
static int  Control( access_t *, int, va_list );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
111
static int  open_file( access_t *, const char * );
gbazin's avatar
gbazin committed
112

113
struct access_sys_t
114
{
115
116
    unsigned int i_nb_reads;
    vlc_bool_t   b_kfir;
gbazin's avatar
gbazin committed
117
118

    /* Files list */
119
120
121
    unsigned filec;
    int     *filev;
    int64_t *sizev;
gbazin's avatar
gbazin committed
122
123

    /* Current file */
124
    unsigned filep;
gbazin's avatar
gbazin committed
125

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

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

140
    vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
141

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

145
    STANDARD_READ_ACCESS_INIT;
146
147
    p_sys->i_nb_reads = 0;
    p_sys->b_kfir = VLC_FALSE;
148
149
    p_sys->filec = 1;
    p_sys->filep = 0;
150

151
    if (!strcasecmp (p_access->psz_access, "stream"))
152
    {
153
154
        p_sys->b_seekable = VLC_FALSE;
        p_sys->b_pace_control = VLC_FALSE;
155
    }
156
    else if (!strcasecmp (p_access->psz_access, "kfir"))
157
    {
158
159
160
        p_sys->b_seekable = VLC_FALSE;
        p_sys->b_pace_control = VLC_FALSE;
        p_sys->b_kfir = VLC_TRUE;
161
    }
162
163
    else
    {
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* returns early if psz_path is a directory */
#ifdef HAVE_SYS_STAT_H
        struct stat stat_info;

        if( utf8_stat( p_access->psz_path, &stat_info ) )
        {
            msg_Warn( p_access, "%s: %s", p_access->psz_path,
                    strerror( errno ) );
            return VLC_EGENERIC;
        }
        if S_ISDIR(stat_info.st_mode)
        {
            msg_Warn( p_access, "%s is a directory", p_access->psz_path );
            return VLC_EGENERIC;
        }
#endif

181
        p_sys->b_seekable = VLC_TRUE;
182
        p_sys->b_pace_control = VLC_TRUE;
183

184
    }
185

186
187
188
189
190
191
192
193
194
195
196
    /* Count number of files */
    catlist = var_CreateGetString (p_access, "file-cat");
    if (catlist == NULL)
    {
        free (p_sys);
        return VLC_ENOMEM;
    }

    if (*catlist)
    {
        for (const char *ptr = catlist; ptr != NULL; p_sys->filec++)
197
        {
198
199
200
            ptr = strchr (ptr, ',');
            if (ptr != NULL)
                ptr++;
201
202
203
        }
    }

204
205
    p_sys->filev = calloc (p_sys->filec, sizeof (p_sys->filev[0]));
    if (p_sys->filev == NULL)
206
    {
207
208
209
        free (catlist);
        free (p_sys);
        return VLC_ENOMEM;
210
211
    }

212
213
214
215
216
217
218
219
    p_sys->sizev = calloc (p_sys->filec, sizeof (p_sys->sizev[0]));
    if (p_sys->sizev == NULL)
    {
        free (catlist);
        free (p_sys->filev);
        free (p_sys);
        return VLC_ENOMEM;
    }
gbazin's avatar
gbazin committed
220

221
222
223
    /* Open files */
    char *filename = catlist;
    for (unsigned i = 0; i < p_sys->filec; i++)
gbazin's avatar
gbazin committed
224
    {
225
        int fd = -1;
gbazin's avatar
gbazin committed
226

227
        if (i == 0)
gbazin's avatar
gbazin committed
228
        {
229
            msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
gbazin's avatar
gbazin committed
230

231
232
233
            if (b_stdin)
                fd = dup (0);
            else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
234
                fd = open_file (p_access, p_access->psz_path);
235
236
237
238
239
240
241
242
243
244
        }
        else
        {
            assert (filename != NULL);

            char *ptr = strchr (filename, ',');
            if (ptr != NULL)
                *ptr = 0;

            msg_Dbg (p_access, "opening additionnal file `%s'", filename);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
245
            fd = open_file (p_access, filename);
246
247
            filename = ptr + 1;
        }
gbazin's avatar
gbazin committed
248
249

#ifdef HAVE_SYS_STAT_H
250
        struct stat st;
gbazin's avatar
gbazin committed
251

252
253
254
255
256
257
258
        if ((fd != -1) && fstat (fd, &st))
        {
            msg_Err (p_access, "fstat(%d): %s", fd, strerror (errno));
            close (fd);
            fd = -1;
        }
#endif
gbazin's avatar
gbazin committed
259

260
261
262
263
264
265
        if (fd == -1)
        {
            free (catlist);
            p_sys->filec = i;
            Close (p_this);
            return VLC_EGENERIC;
gbazin's avatar
gbazin committed
266
        }
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        p_sys->filev[i] = fd;

#ifdef HAVE_SYS_STAT_H
        p_sys->sizev[i] = st.st_size;
        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)))
            // If one file is not seekable, the concatenation is not either
            p_sys->b_seekable = VLC_FALSE;
#else
        p_sys->b_seekable = !b_stdin;
#endif

    }

    free (catlist);

    if (p_sys->b_seekable && !p_access->info.i_size)
    {
        /* FIXME that's bad because all others access will be probed */
        msg_Err (p_access, "file %s is empty, aborting", p_access->psz_path);
        Close (p_this);
        return VLC_EGENERIC;
gbazin's avatar
gbazin committed
291
292
    }

293
    return VLC_SUCCESS;
294
295
296
}

/*****************************************************************************
297
 * Close: close the target
298
 *****************************************************************************/
299
static void Close (vlc_object_t * p_this)
300
{
301
302
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
303

304
305
    for (unsigned i = 0; i < p_sys->filec; i++)
        close (p_sys->filev[i]);
306

307
308
309
    free (p_sys->filev);
    free (p_sys->sizev);
    free (p_sys);
310
311
312
313
314
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
315
static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
316
{
317
318
    access_sys_t *p_sys = p_access->p_sys;
    int i_ret;
319
    int fd = p_sys->filev[p_sys->filep];
320

321
#if !defined(WIN32) && !defined(UNDER_CE)
322
    if( !p_sys->b_pace_control )
323
    {
324
        if( !p_sys->b_kfir )
325
        {
326
            /* Find if some data is available. This won't work under Windows. */
327
            do
328
            {
329
                struct pollfd ufd;
330

331
                if( p_access->b_die )
332
333
                    return 0;

334
                memset (&ufd, 0, sizeof (ufd));
335
                ufd.fd = fd;
336
337
                ufd.events = POLLIN;

338
                i_ret = poll (&ufd, 1, 500);
339
            }
340
            while (i_ret <= 0);
341

342
            i_ret = read (fd, p_buffer, i_len);
343
344
        }
        else
345
        {
346
            /* b_kfir ; work around a buggy poll() driver implementation */
347
348
            while (((i_ret = read (fd, p_buffer, i_len)) == 0)
                && !p_access->b_die)
349
            {
350
                msleep( INPUT_ERROR_SLEEP );
351
            }
352
353
        }
    }
354
    else
355
#endif /* WIN32 || UNDER_CE */
356
        /* b_pace_control || WIN32 */
357
        i_ret = read( fd, p_buffer, i_len );
358

359
360
    if( i_ret < 0 )
    {
361
        switch (errno)
362
        {
363
364
365
366
367
368
369
370
371
            case EINTR:
            case EAGAIN:
                break;

            default:
                msg_Err (p_access, "read failed (%s)", strerror (errno));
                intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
                                _("VLC could not read file \"%s\"."),
                                strerror (errno));
372
        }
373
374
375
376

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

    p_sys->i_nb_reads++;
380

381
#ifdef HAVE_SYS_STAT_H
382
383
    if( p_access->info.i_size != 0 &&
        (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
384
    {
385
386
        struct stat st;
        int i = p_sys->filep;
gbazin's avatar
gbazin committed
387

388
389
        if ((fstat (fd, &st) == 0)
         && (p_sys->sizev[i] != st.st_size))
390
        {
391
392
            p_access->info.i_size += st.st_size - p_sys->sizev[i];
            p_sys->sizev[i] = st.st_size;
393
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
394
395
396
397
        }
    }
#endif

gbazin's avatar
gbazin committed
398
    /* If we reached an EOF then switch to the next file in the list */
399
    if (i_ret == 0)
gbazin's avatar
gbazin committed
400
    {
401
402
403
404
405
        if  (++p_sys->filep < p_sys->filec)
            /* We have to read some data */
            return Read (p_access, p_buffer, i_len);
        else
            p_sys->filep--;
gbazin's avatar
gbazin committed
406
407
    }

408
409
410
411
412
    if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
    else if( i_ret == 0 )
        p_access->info.b_eof = VLC_TRUE;

413
414
415
416
417
418
    return i_ret;
}

/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
419
static int Seek (access_t *p_access, int64_t i_pos)
420
{
421
    access_sys_t *p_sys = p_access->p_sys;
gbazin's avatar
gbazin committed
422

423
    if (p_access->info.i_size < p_access->info.i_pos)
gbazin's avatar
gbazin committed
424
    {
425
426
        msg_Err (p_access, "seeking too far");
        i_pos = p_access->info.i_pos = p_access->info.i_size;
gbazin's avatar
gbazin committed
427
    }
428
    else if (p_access->info.i_pos < 0)
429
    {
430
431
        msg_Err (p_access, "seeking too early");
        i_pos = p_access->info.i_pos = 0;
432
    }
433
434
435
436
437
438
439
440
441

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

    /* Determine which file we need to access */
    unsigned i = 0;
    assert (p_sys->filec > 0);

    while (i_pos > p_sys->sizev[i])
442
    {
443
444
        i_pos -= p_sys->sizev[i++];
        assert (i < p_sys->filec);
445
    }
446
    p_sys->filep = i;
447

448
    lseek (p_sys->filev[i], i_pos, SEEK_SET);
449
450
451
452
453
454
455
456
457
458
459
460
461
462
    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 )
463
    {
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
        /* */
        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 * );
485
            *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
486
            break;
487

488
489
490
491
492
493
494
495
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
496
        case ACCESS_SET_PRIVATE_ID_STATE:
497
        case ACCESS_GET_META:
498
499
500
            return VLC_EGENERIC;

        default:
501
            msg_Warn( p_access, "unimplemented query in control" );
502
503
            return VLC_EGENERIC;

504
    }
505
    return VLC_SUCCESS;
506
507
}

508

509
510
static char *expand_path (const access_t *p_access, const char *path)
{
Rafaël Carré's avatar
Rafaël Carré committed
511
    if (strncmp (path, "~/", 2) == 0)
512
513
514
515
516
517
518
519
520
521
    {
        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
522
    if (!strcasecmp (p_access->psz_access, "file")
523
524
525
526
527
528
529
530
531
532
      && ('/' == 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
533
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
534
 * open_file: Opens a specific file
gbazin's avatar
gbazin committed
535
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
536
static int open_file (access_t *p_access, const char *psz_name)
gbazin's avatar
gbazin committed
537
{
538
    char *path = expand_path (p_access, psz_name);
gbazin's avatar
gbazin committed
539
540

#ifdef UNDER_CE
541
    p_sys->fd = utf8_fopen( path, "rb" );
542
    if ( !p_sys->fd )
gbazin's avatar
gbazin committed
543
    {
544
        msg_Err( p_access, "cannot open file %s", psz_name );
545
546
        intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
                        _("VLC could not open file \"%s\"."), psz_name );
547
        free (path);
gbazin's avatar
gbazin committed
548
549
        return VLC_EGENERIC;
    }
550
551
552

    fseek( p_sys->fd, 0, SEEK_END );
    p_access->info.i_size = ftell( p_sys->fd );
553
    p_access->info.i_update |= INPUT_UPDATE_SIZE;
554
    fseek( p_sys->fd, 0, SEEK_SET );
gbazin's avatar
gbazin committed
555
#else
556
557
    const char *psz_localname = ToLocale (path);
    if (psz_localname == NULL)
558
    {
559
560
561
        msg_Err (p_access, "incorrect file name %s", psz_name);
        free (path);
        return -1;
562
    }
gbazin's avatar
gbazin committed
563

564
    // FIXME: support non-ANSI filenames on Win32
565
566
567
    int fd = open (path, O_NONBLOCK /*| O_LARGEFILE*/);
    LocaleFree (psz_localname);
    free (path);
568

569
    if (fd == -1)
gbazin's avatar
gbazin committed
570
    {
571
572
573
        msg_Err (p_access, "cannot open file %s (%s)", psz_name,
                 strerror (errno));
        intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"), 
574
                        _("VLC could not open file \"%s\" (%s)."),
575
576
                        psz_name, strerror (errno));
        return -1;
gbazin's avatar
gbazin committed
577
    }
578

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

587
    return fd;
gbazin's avatar
gbazin committed
588
}