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

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc/vlc.h>
34
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
35 36 37
#include <vlc_input.h>
#include <vlc_access.h>
#include <vlc_interface.h>
38

39
#include <assert.h>
40
#include <errno.h>
41 42 43 44 45 46 47 48 49 50
#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
51 52
#if defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
53
#   include <ctype.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54
#else
55
#   include <unistd.h>
56
#   include <poll.h>
57 58
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
59
#if defined( WIN32 ) && !defined( UNDER_CE )
60 61 62 63 64 65 66 67 68 69 70 71 72 73
#   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
74 75
#endif

Clément Stenac's avatar
Clément Stenac committed
76
#include <vlc_charset.h>
77

78 79 80
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
81 82 83
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

Christophe Massiot's avatar
Christophe Massiot committed
84
#define CACHING_TEXT N_("Caching value in ms")
Gildas Bazin's avatar
 
Gildas Bazin committed
85
#define CACHING_LONGTEXT N_( \
86
    "Caching value for files. This " \
Clément Stenac's avatar
Clément Stenac committed
87
    "value should be set in milliseconds." )
Gildas Bazin's avatar
 
Gildas Bazin committed
88

89
vlc_module_begin();
90 91
    set_description( N_("File input") );
    set_shortname( N_("File") );
Clément Stenac's avatar
Clément Stenac 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, true );
95
    add_obsolete_string( "file-cat" );
96
    set_capability( "access", 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
static int  Seek( access_t *, int64_t );
108
static ssize_t Read( access_t *, uint8_t *, size_t );
109 110
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 * );
Gildas Bazin's avatar
Gildas Bazin committed
112

113
struct access_sys_t
114
{
115
    unsigned int i_nb_reads;
116
    bool   b_kfir;
Gildas Bazin's avatar
Gildas Bazin committed
117

118
    int fd;
Gildas Bazin's avatar
Gildas Bazin committed
119

120
    /* */
121 122
    bool b_seekable;
    bool b_pace_control;
123
};
124

125 126 127 128 129
/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
130 131 132
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;

133
    bool    b_stdin = !strcmp (p_access->psz_path, "-");
134

135 136
    /* Update default_pts to a suitable value for file access */
    var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
137

138
    STANDARD_READ_ACCESS_INIT;
139
    p_sys->i_nb_reads = 0;
140
    p_sys->b_kfir = false;
141
    int fd = p_sys->fd = -1;
142

143
    if (!strcasecmp (p_access->psz_access, "stream"))
144
    {
145 146
        p_sys->b_seekable = false;
        p_sys->b_pace_control = false;
147
    }
148
    else if (!strcasecmp (p_access->psz_access, "kfir"))
149
    {
150 151 152
        p_sys->b_seekable = false;
        p_sys->b_pace_control = false;
        p_sys->b_kfir = true;
153
    }
154 155
    else
    {
156 157
        p_sys->b_seekable = true;
        p_sys->b_pace_control = true;
158
    }
159

160 161
    /* Open file */
    msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
162

163 164 165 166
    if (b_stdin)
        fd = dup (0);
    else
        fd = open_file (p_access, p_access->psz_path);
167

168
#ifdef HAVE_SYS_STAT_H
169
    struct stat st;
Gildas Bazin's avatar
Gildas Bazin committed
170

171
    while (fd != -1)
Gildas Bazin's avatar
Gildas Bazin committed
172
    {
173
        if (fstat (fd, &st))
174
            msg_Err (p_access, "fstat(%d): %m", fd);
175
        else
176 177 178 179 180
        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
181

182 183 184
        close (fd);
        fd = -1;
    }
185
#endif
Gildas Bazin's avatar
Gildas Bazin committed
186

187 188 189 190 191 192
    if (fd == -1)
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
    p_sys->fd = fd;
193 194

#ifdef HAVE_SYS_STAT_H
195
    p_access->info.i_size = st.st_size;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
196 197 198
    if (!S_ISREG (st.st_mode)
     && !S_ISBLK (st.st_mode)
     && !S_ISCHR (st.st_mode))
199
        p_sys->b_seekable = false;
200
#else
201
    p_sys->b_seekable = !b_stdin;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
202
# warning File size not known!
203 204
#endif

205
    return VLC_SUCCESS;
206 207 208
}

/*****************************************************************************
209
 * Close: close the target
210
 *****************************************************************************/
211
static void Close (vlc_object_t * p_this)
212
{
213 214
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
215

216
    close (p_sys->fd);
217
    free (p_sys);
218 219 220 221 222
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
223
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
224
{
225
    access_sys_t *p_sys = p_access->p_sys;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
226
    ssize_t i_ret;
227
    int fd = p_sys->fd;
228

229
#if !defined(WIN32) && !defined(UNDER_CE)
230
    if( !p_sys->b_pace_control )
231
    {
232
        if( !p_sys->b_kfir )
233
        {
234
            /* Find if some data is available. This won't work under Windows. */
235
            do
236
            {
237
                struct pollfd ufd;
238

239
                if( p_access->b_die )
240 241
                    return 0;

242
                memset (&ufd, 0, sizeof (ufd));
243
                ufd.fd = fd;
244 245
                ufd.events = POLLIN;

246
                i_ret = poll (&ufd, 1, 500);
247
            }
248
            while (i_ret <= 0);
249

250
            i_ret = read (fd, p_buffer, i_len);
251 252
        }
        else
253
        {
254
            /* b_kfir ; work around a buggy poll() driver implementation */
255 256
            while (((i_ret = read (fd, p_buffer, i_len)) == 0)
                && !p_access->b_die)
257
            {
258
                msleep( INPUT_ERROR_SLEEP );
259
            }
260 261
        }
    }
262
    else
263
#endif /* WIN32 || UNDER_CE */
264
        /* b_pace_control || WIN32 */
265
        i_ret = read( fd, p_buffer, i_len );
266

267 268
    if( i_ret < 0 )
    {
269
        switch (errno)
270
        {
271 272 273 274 275
            case EINTR:
            case EAGAIN:
                break;

            default:
276
                msg_Err (p_access, "read failed (%m)");
277
                intf_UserFatal (p_access, false, _("File reading failed"),
278
                                _("VLC could not read the file."));
279
        }
280 281 282 283

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

    p_sys->i_nb_reads++;
287

288
#ifdef HAVE_SYS_STAT_H
289 290
    if( p_access->info.i_size != 0 &&
        (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
291
    {
292
        struct stat st;
Gildas Bazin's avatar
Gildas Bazin committed
293

294
        if ((fstat (fd, &st) == 0)
295
         && (p_access->info.i_size != st.st_size))
296
        {
297
            p_access->info.i_size = st.st_size;
298
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
299 300 301 302
        }
    }
#endif

303 304 305
    if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
    else if( i_ret == 0 )
306
        p_access->info.b_eof = true;
307

308 309 310
    return i_ret;
}

311

312 313 314
/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
315
static int Seek (access_t *p_access, int64_t i_pos)
316
{
317
    p_access->info.i_pos = i_pos;
318
    p_access->info.b_eof = false;
319

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
320
    lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
321 322 323 324 325 326 327 328 329
    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;
330
    bool   *pb_bool;
331 332 333 334
    int          *pi_int;
    int64_t      *pi_64;

    switch( i_query )
335
    {
336 337 338
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
339
            pb_bool = (bool*)va_arg( args, bool* );
340 341 342 343 344
            *pb_bool = p_sys->b_seekable;
            break;

        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
345
            pb_bool = (bool*)va_arg( args, bool* );
346 347 348 349 350 351 352 353 354 355 356
            *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 * );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
357
            *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
358
            break;
359

360 361 362 363 364 365 366 367
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
368
        case ACCESS_SET_PRIVATE_ID_STATE:
369
        case ACCESS_GET_META:
370
        case ACCESS_GET_PRIVATE_ID_STATE:
371
        case ACCESS_GET_CONTENT_TYPE:
372 373 374
            return VLC_EGENERIC;

        default:
375
            msg_Warn( p_access, "unimplemented query %d in control", i_query );
376 377
            return VLC_EGENERIC;

378
    }
379
    return VLC_SUCCESS;
380 381
}

Gildas Bazin's avatar
Gildas Bazin committed
382
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
383
 * open_file: Opens a specific file
Gildas Bazin's avatar
Gildas Bazin committed
384
 *****************************************************************************/
385
static int open_file (access_t *p_access, const char *path)
Gildas Bazin's avatar
Gildas Bazin committed
386
{
387 388 389 390 391 392 393 394
#if defined(WIN32)
    if (!strcasecmp (p_access->psz_access, "file")
      && ('/' == path[0]) && isalpha (path[1])
      && (':' == path[2]) && ('/' == path[3]))
        /* Explorer can open path such as file:/C:/ or file:///C:/
         * hence remove leading / if found */
        path++;
#endif
Gildas Bazin's avatar
Gildas Bazin committed
395 396

#ifdef UNDER_CE
397
    p_sys->fd = utf8_fopen( path, "rb" );
398
    if ( !p_sys->fd )
Gildas Bazin's avatar
Gildas Bazin committed
399
    {
400
        msg_Err( p_access, "cannot open file %s", path );
401
        intf_UserFatal( p_access, false, _("File reading failed"),
402
                        _("VLC could not open the file \"%s\"."), path );
Gildas Bazin's avatar
Gildas Bazin committed
403 404
        return VLC_EGENERIC;
    }
405 406 407

    fseek( p_sys->fd, 0, SEEK_END );
    p_access->info.i_size = ftell( p_sys->fd );
408
    p_access->info.i_update |= INPUT_UPDATE_SIZE;
409
    fseek( p_sys->fd, 0, SEEK_SET );
Gildas Bazin's avatar
Gildas Bazin committed
410
#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
411
    int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
412
    if (fd == -1)
Gildas Bazin's avatar
Gildas Bazin committed
413
    {
414
        msg_Err (p_access, "cannot open file %s (%m)", path);
415
        intf_UserFatal (p_access, false, _("File reading failed"),
416
                        _("VLC could not open the file \"%s\"."), path);
417
        return -1;
Gildas Bazin's avatar
Gildas Bazin committed
418
    }
419

420
# if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
421 422
    /* We'd rather use any available memory for reading ahead
     * than for caching what we've already seen/heard */
423 424 425
    fcntl (fd, F_RDAHEAD, 1);
    fcntl (fd, F_NOCACHE, 1);
# endif
Gildas Bazin's avatar
Gildas Bazin committed
426 427
#endif

428
    return fd;
Gildas Bazin's avatar
Gildas Bazin committed
429
}