file.c 9.39 KB
Newer Older
1 2 3
/*****************************************************************************
 * file.c: file input (file: access plug-in)
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001-2006 VLC authors and VideoLAN
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
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
11 12 13
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
14
 * (at your option) any later version.
15
 *
16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
21 22 23
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25
 *****************************************************************************/

26 27 28 29
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

30
#include <assert.h>
31
#include <errno.h>
32
#include <sys/types.h>
33
#include <sys/stat.h>
34
#include <fcntl.h>
35 36 37 38 39 40 41
#ifdef HAVE_FSTATVFS
#   include <sys/statvfs.h>
#   if defined (HAVE_SYS_MOUNT_H)
#      include <sys/param.h>
#      include <sys/mount.h>
#   endif
#endif
42
#ifdef HAVE_LINUX_MAGIC_H
43
#   include <sys/vfs.h>
44
#   include <linux/magic.h>
45
#endif
46

47
#if defined( _WIN32 )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
48
#   include <io.h>
49
#   include <ctype.h>
50
#   include <shlwapi.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
51
#else
52 53
#   include <unistd.h>
#endif
54
#include <dirent.h>
55

56 57 58 59
#include <vlc_common.h>
#include "fs.h"
#include <vlc_input.h>
#include <vlc_access.h>
60
#ifdef _WIN32
61 62
# include <vlc_charset.h>
#endif
63
#include <vlc_fs.h>
64
#include <vlc_url.h>
65
#include <vlc_interrupt.h>
66

67
struct access_sys_t
68
{
69
    int fd;
Gildas Bazin's avatar
Gildas Bazin committed
70

71
    bool b_pace_control;
72
};
73

74
#if !defined (_WIN32) && !defined (__OS2__)
75 76
static bool IsRemote (int fd)
{
77 78 79 80 81 82
#if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
    struct statvfs stf;

    if (fstatvfs (fd, &stf))
        return false;
    /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
Joseph S. Atkinson's avatar
Joseph S. Atkinson committed
83
    return !(stf.f_flag & MNT_LOCAL);
84 85

#elif defined (HAVE_LINUX_MAGIC_H)
86 87 88 89 90
    struct statfs stf;

    if (fstatfs (fd, &stf))
        return false;

91
    switch ((unsigned long)stf.f_type)
92 93 94 95 96 97 98 99 100 101
    {
        case AFS_SUPER_MAGIC:
        case CODA_SUPER_MAGIC:
        case NCP_SUPER_MAGIC:
        case NFS_SUPER_MAGIC:
        case SMB_SUPER_MAGIC:
        case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
            return true;
    }
    return false;
102 103

#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
104
    (void)fd;
105 106 107 108
    return false;

#endif
}
109 110
# define IsRemote(fd,path) IsRemote(fd)

111
#else /* _WIN32 || __OS2__ */
112 113
static bool IsRemote (const char *path)
{
114
# if !defined(__OS2__) && !VLC_WINSTORE_APP
115 116 117 118 119 120 121 122 123 124
    wchar_t *wpath = ToWide (path);
    bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
    free (wpath);
    return is_remote;
# else
    return (! strncmp(path, "\\\\", 2));
# endif
}
# define IsRemote(fd,path) IsRemote(path)
#endif
125

126
#ifndef HAVE_POSIX_FADVISE
127
# define posix_fadvise(fd, off, len, adv)
128
#endif
129

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
130 131 132 133
static ssize_t Read (stream_t *, void *, size_t);
static int FileSeek (stream_t *, uint64_t);
static int NoSeek (stream_t *, uint64_t);
static int FileControl (stream_t *, int, va_list);
134

135
/*****************************************************************************
136
 * FileOpen: open the file
137
 *****************************************************************************/
138
int FileOpen( vlc_object_t *p_this )
139
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
140
    stream_t *p_access = (stream_t*)p_this;
141

142
    /* Open file */
143
    int fd = -1;
144

145
    if (!strcasecmp (p_access->psz_name, "fd"))
146 147
    {
        char *end;
148
        int oldfd = strtol (p_access->psz_location, &end, 10);
149 150

        if (*end == '\0')
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
151
            fd = vlc_dup (oldfd);
152
        else if (*end == '/' && end > p_access->psz_location)
153
        {
154
            char *name = vlc_uri_decode_duplicate (end - 1);
155
            if (name != NULL)
156 157
            {
                name[0] = '.';
158
                fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
159 160 161 162
                free (name);
            }
        }
    }
163
    else
164
    {
165
        if (unlikely(p_access->psz_filepath == NULL))
166
            return VLC_EGENERIC;
167
        fd = vlc_open (p_access->psz_filepath, O_RDONLY | O_NONBLOCK);
168
    }
169

170
    if (fd == -1)
171 172 173 174 175
    {
        msg_Err (p_access, "cannot open file %s (%s)",
                 p_access->psz_filepath ? p_access->psz_filepath
                                        : p_access->psz_location,
                 vlc_strerror_c(errno));
176
        return VLC_EGENERIC;
177
    }
178

179
    struct stat st;
180
    if (fstat (fd, &st))
Gildas Bazin's avatar
Gildas Bazin committed
181
    {
182
        msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
183
        goto error;
184
    }
185

186
#if O_NONBLOCK
187
    /* Force blocking mode back */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
188
    fcntl (fd, F_SETFL, fcntl (fd, F_GETFL) & ~O_NONBLOCK);
189
#endif
190

191 192 193
    /* Directories can be opened and read from, but only readdir() knows
     * how to parse the data. The directory plugin will do it. */
    if (S_ISDIR (st.st_mode))
194
    {
195
#ifdef HAVE_FDOPENDIR
196 197 198 199 200 201
        DIR *p_dir = fdopendir(fd);
        if (!p_dir) {
            msg_Err (p_access, "fdopendir error: %s", vlc_strerror_c(errno));
            goto error;
        }
        return DirInit (p_access, p_dir);
202
#else
203 204
        msg_Dbg (p_access, "ignoring directory");
        goto error;
205
#endif
206
    }
207

208
    access_sys_t *p_sys = vlc_obj_malloc(p_this, sizeof (*p_sys));
209 210
    if (unlikely(p_sys == NULL))
        goto error;
211
    p_access->pf_read = Read;
212 213 214 215 216
    p_access->pf_block = NULL;
    p_access->pf_control = FileControl;
    p_access->p_sys = p_sys;
    p_sys->fd = fd;

217
    if (S_ISREG (st.st_mode) || S_ISBLK (st.st_mode))
218
    {
219 220
        p_access->pf_seek = FileSeek;
        p_sys->b_pace_control = true;
221 222 223 224 225

        /* Demuxers will need the beginning of the file for probing. */
        posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
        /* In most cases, we only read the file once. */
        posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
226 227
#ifdef F_NOCACHE
        fcntl (fd, F_NOCACHE, 0);
228 229 230 231 232 233
#endif
#ifdef F_RDAHEAD
        if (IsRemote(fd, p_access->psz_filepath))
            fcntl (fd, F_RDAHEAD, 0);
        else
            fcntl (fd, F_RDAHEAD, 1);
234
#endif
235
    }
236 237 238
    else
    {
        p_access->pf_seek = NoSeek;
239
        p_sys->b_pace_control = strcasecmp (p_access->psz_name, "stream");
240 241
    }

242
    return VLC_SUCCESS;
243 244

error:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
245
    vlc_close (fd);
246
    return VLC_EGENERIC;
247 248 249
}

/*****************************************************************************
250
 * FileClose: close the target
251
 *****************************************************************************/
252
void FileClose (vlc_object_t * p_this)
253
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
254
    stream_t     *p_access = (stream_t*)p_this;
255 256 257 258 259 260 261

    if (p_access->pf_read == NULL)
    {
        DirClose (p_this);
        return;
    }

262
    access_sys_t *p_sys = p_access->p_sys;
263

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
264
    vlc_close (p_sys->fd);
265 266
}

267

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
268
static ssize_t Read (stream_t *p_access, void *p_buffer, size_t i_len)
269 270 271 272
{
    access_sys_t *p_sys = p_access->p_sys;
    int fd = p_sys->fd;

273
    ssize_t val = vlc_read_i11e (fd, p_buffer, i_len);
274 275 276 277 278 279 280 281
    if (val < 0)
    {
        switch (errno)
        {
            case EINTR:
            case EAGAIN:
                return -1;
        }
282

283
        msg_Err (p_access, "read error: %s", vlc_strerror_c(errno));
284 285 286 287 288 289
        val = 0;
    }

    return val;
}

290 291 292
/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
293
static int FileSeek (stream_t *p_access, uint64_t i_pos)
294
{
295 296 297
    access_sys_t *sys = p_access->p_sys;

    if (lseek(sys->fd, i_pos, SEEK_SET) == (off_t)-1)
298 299 300 301
        return VLC_EGENERIC;
    return VLC_SUCCESS;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
302
static int NoSeek (stream_t *p_access, uint64_t i_pos)
303
{
304
    /* vlc_assert_unreachable(); ?? */
305 306 307 308
    (void) p_access; (void) i_pos;
    return VLC_EGENERIC;
}

309 310 311
/*****************************************************************************
 * Control:
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
312
static int FileControl( stream_t *p_access, int i_query, va_list args )
313 314
{
    access_sys_t *p_sys = p_access->p_sys;
315 316
    bool    *pb_bool;
    int64_t *pi_64;
317 318

    switch( i_query )
319
    {
320 321
        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
322
            pb_bool = va_arg( args, bool * );
323
            *pb_bool = (p_access->pf_seek != NoSeek);
324 325
            break;

326 327
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
328
            pb_bool = va_arg( args, bool * );
329 330 331
            *pb_bool = p_sys->b_pace_control;
            break;

332
        case STREAM_GET_SIZE:
333 334 335
        {
            struct stat st;

336
            if (fstat (p_sys->fd, &st) || !S_ISREG(st.st_mode))
337
                return VLC_EGENERIC;
338
            *va_arg( args, uint64_t * ) = st.st_size;
339
            break;
340
        }
341

342
        case STREAM_GET_PTS_DELAY:
343
            pi_64 = va_arg( args, int64_t * );
344
            if (IsRemote (p_sys->fd, p_access->psz_filepath))
345 346 347 348
                *pi_64 = var_InheritInteger (p_access, "network-caching");
            else
                *pi_64 = var_InheritInteger (p_access, "file-caching");
            *pi_64 *= 1000;
349
            break;
350

351
        case STREAM_SET_PAUSE_STATE:
352 353 354 355 356 357
            /* Nothing to do */
            break;

        default:
            return VLC_EGENERIC;

358
    }
359
    return VLC_SUCCESS;
360
}