file.c 10.2 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_common.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

51
#if defined( WIN32 )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
52
#   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

59
#if defined( WIN32 ) && !defined( UNDER_CE )
60 61 62 63 64
#   ifdef lseek
#      undef lseek
#   endif
#   define lseek _lseeki64
#elif defined( UNDER_CE )
65 66
/* FIXME the commandline on wince is a mess */
# define dup(a) -1
Gildas Bazin's avatar
Gildas Bazin committed
67 68
#endif

Clément Stenac's avatar
Clément Stenac committed
69
#include <vlc_charset.h>
70

71 72 73
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
74 75 76
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

Christophe Massiot's avatar
Christophe Massiot committed
77
#define CACHING_TEXT N_("Caching value in ms")
78
#define CACHING_LONGTEXT N_( \
79
    "Caching value for files. This " \
Clément Stenac's avatar
Clément Stenac committed
80
    "value should be set in milliseconds." )
81

82 83 84 85 86 87 88 89 90 91 92 93
vlc_module_begin ()
    set_description( N_("File input") )
    set_shortname( N_("File") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
    add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
    add_obsolete_string( "file-cat" )
    set_capability( "access", 50 )
    add_shortcut( "file" )
    add_shortcut( "stream" )
    set_callbacks( Open, Close )
vlc_module_end ()
94 95


96
/*****************************************************************************
97
 * Exported prototypes
98
 *****************************************************************************/
99
static int  Seek( access_t *, int64_t );
100
static ssize_t Read( access_t *, uint8_t *, size_t );
101 102
static int  Control( access_t *, int, va_list );

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

105
struct access_sys_t
106
{
107
    unsigned int i_nb_reads;
Gildas Bazin's avatar
Gildas Bazin committed
108

109
    int fd;
Gildas Bazin's avatar
Gildas Bazin committed
110

111
    /* */
112 113
    bool b_seekable;
    bool b_pace_control;
114
};
115

116 117 118 119 120
/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
121 122 123
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys;

124
    bool    b_stdin = !strcmp (p_access->psz_path, "-");
125

126 127
    /* Update default_pts to a suitable value for file access */
    var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
128

129
    STANDARD_READ_ACCESS_INIT;
130
    p_sys->i_nb_reads = 0;
131
    int fd = p_sys->fd = -1;
132

133
    if (!strcasecmp (p_access->psz_access, "stream"))
134
    {
135 136
        p_sys->b_seekable = false;
        p_sys->b_pace_control = false;
137 138 139
    }
    else
    {
140 141
        p_sys->b_seekable = true;
        p_sys->b_pace_control = true;
142
    }
143

144 145
    /* Open file */
    msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
146

147 148 149 150
    if (b_stdin)
        fd = dup (0);
    else
        fd = open_file (p_access, p_access->psz_path);
151

152
#ifdef HAVE_SYS_STAT_H
153
    struct stat st;
Gildas Bazin's avatar
Gildas Bazin committed
154

155
    while (fd != -1)
Gildas Bazin's avatar
Gildas Bazin committed
156
    {
157
        if (fstat (fd, &st))
158
            msg_Err (p_access, "fstat(%d): %m", fd);
159
        else
160 161 162 163 164
        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
165

166 167 168
        close (fd);
        fd = -1;
    }
169
#endif
Gildas Bazin's avatar
Gildas Bazin committed
170

171 172 173 174 175 176
    if (fd == -1)
    {
        free (p_sys);
        return VLC_EGENERIC;
    }
    p_sys->fd = fd;
177 178

#ifdef HAVE_SYS_STAT_H
179
    p_access->info.i_size = st.st_size;
180
    if (!S_ISREG (st.st_mode))
181
        p_sys->b_seekable = false;
182
#else
183
    p_sys->b_seekable = !b_stdin;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
184
# warning File size not known!
185 186
#endif

187
    return VLC_SUCCESS;
188 189 190
}

/*****************************************************************************
191
 * Close: close the target
192
 *****************************************************************************/
193
static void Close (vlc_object_t * p_this)
194
{
195 196
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;
197

198
    close (p_sys->fd);
199
    free (p_sys);
200 201
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
202 203 204

#include <vlc_network.h>

205 206 207
/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
208
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
209
{
210
    access_sys_t *p_sys = p_access->p_sys;
211
    int fd = p_sys->fd;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
212
    ssize_t i_ret;
213

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
214 215 216
#ifndef WIN32
    i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
#else
217
    i_ret = read (fd, p_buffer, i_len);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
218
#endif
219

220 221
    if( i_ret < 0 )
    {
222
        switch (errno)
223
        {
224 225 226 227 228
            case EINTR:
            case EAGAIN:
                break;

            default:
229
                msg_Err (p_access, "read failed (%m)");
230
                intf_UserFatal (p_access, false, _("File reading failed"),
231
                                _("VLC could not read the file."));
232
                return 0;
233
        }
234
    }
235 236 237 238
    else if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
    else if( i_ret == 0 )
        p_access->info.b_eof = true;
239 240

    p_sys->i_nb_reads++;
241

242
#ifdef HAVE_SYS_STAT_H
243 244
    if( p_access->info.i_size != 0 &&
        (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
245
    {
246
        struct stat st;
Gildas Bazin's avatar
Gildas Bazin committed
247

248
        if ((fstat (fd, &st) == 0)
249
         && (p_access->info.i_size != st.st_size))
250
        {
251
            p_access->info.i_size = st.st_size;
252
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
253 254 255
        }
    }
#endif
256 257 258
    return i_ret;
}

259

260 261 262
/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
263
static int Seek (access_t *p_access, int64_t i_pos)
264
{
265
    p_access->info.i_pos = i_pos;
266
    p_access->info.b_eof = false;
267

268
    lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
269 270 271 272 273 274 275 276 277
    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;
278
    bool   *pb_bool;
279 280 281 282
    int          *pi_int;
    int64_t      *pi_64;

    switch( i_query )
283
    {
284 285 286
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
287
            pb_bool = (bool*)va_arg( args, bool* );
288 289 290 291 292
            *pb_bool = p_sys->b_seekable;
            break;

        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
293
            pb_bool = (bool*)va_arg( args, bool* );
294 295 296 297 298 299 300 301 302 303 304
            *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
305
            *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
306
            break;
307

308 309 310 311 312 313 314 315
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
316
        case ACCESS_SET_PRIVATE_ID_STATE:
317
        case ACCESS_GET_META:
318
        case ACCESS_GET_PRIVATE_ID_STATE:
319
        case ACCESS_GET_CONTENT_TYPE:
320 321 322
            return VLC_EGENERIC;

        default:
323
            msg_Warn( p_access, "unimplemented query %d in control", i_query );
324 325
            return VLC_EGENERIC;

326
    }
327
    return VLC_SUCCESS;
328 329
}

Gildas Bazin's avatar
Gildas Bazin committed
330
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
331
 * open_file: Opens a specific file
Gildas Bazin's avatar
Gildas Bazin committed
332
 *****************************************************************************/
333
static int open_file (access_t *p_access, const char *path)
Gildas Bazin's avatar
Gildas Bazin committed
334
{
335 336 337 338 339 340 341 342
#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
343

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
344
    int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
345
    if (fd == -1)
Gildas Bazin's avatar
Gildas Bazin committed
346
    {
347
        msg_Err (p_access, "cannot open file %s (%m)", path);
348
        intf_UserFatal (p_access, false, _("File reading failed"),
349
                        _("VLC could not open the file \"%s\"."), path);
350
        return -1;
Gildas Bazin's avatar
Gildas Bazin committed
351
    }
352

Geoffroy Couprie's avatar
Geoffroy Couprie committed
353
#if defined(HAVE_FCNTL)
354 355
    fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC);

356 357
    /* We'd rather use any available memory for reading ahead
     * than for caching what we've already seen/heard */
358
# if defined(F_RDAHEAD)
359
    fcntl (fd, F_RDAHEAD, 1);
360 361
# endif
# if defined(F_NOCACHE)
362 363
    fcntl (fd, F_NOCACHE, 1);
# endif
364
#endif
Gildas Bazin's avatar
Gildas Bazin committed
365

366
    return fd;
Gildas Bazin's avatar
Gildas Bazin committed
367
}