file.c 10.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-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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

30
#include <vlc_common.h>
31
#include "fs.h"
zorglub's avatar
zorglub committed
32
33
#include <vlc_input.h>
#include <vlc_access.h>
34
#include <vlc_dialog.h>
35

36
#include <assert.h>
37
#include <errno.h>
38
#include <sys/types.h>
39
40
41
42
43
44
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif
45
46
47
48
49
50
51
#ifdef HAVE_FSTATVFS
#   include <sys/statvfs.h>
#   if defined (HAVE_SYS_MOUNT_H)
#      include <sys/param.h>
#      include <sys/mount.h>
#   endif
#endif
52
#ifdef HAVE_LINUX_MAGIC_H
53
#   include <sys/vfs.h>
54
#   include <linux/magic.h>
55
#endif
56

57
#if defined( WIN32 )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
58
#   include <io.h>
59
#   include <ctype.h>
60
#   include <shlwapi.h>
61
#   include <vlc_charset.h>
62
63
#elif defined( __OS2__ )
#   include <ctype.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
64
#else
65
66
#   include <unistd.h>
#endif
67
#include <dirent.h>
68

gbazin's avatar
   
gbazin committed
69
#if defined( WIN32 ) && !defined( UNDER_CE )
70
71
72
73
74
#   ifdef lseek
#      undef lseek
#   endif
#   define lseek _lseeki64
#elif defined( UNDER_CE )
75
# define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
gbazin's avatar
gbazin committed
76
77
#endif

78
#include <vlc_fs.h>
79
#include <vlc_url.h>
80

81
struct access_sys_t
82
{
83
    unsigned int i_nb_reads;
gbazin's avatar
gbazin committed
84

85
    int fd;
gbazin's avatar
gbazin committed
86

87
    /* */
88
    bool b_pace_control;
89
};
90

91
92
static bool IsRemote (int fd)
{
93
94
95
96
97
98
#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
99
    return !(stf.f_flag & MNT_LOCAL);
100
101

#elif defined (HAVE_LINUX_MAGIC_H)
102
103
104
105
106
    struct statfs stf;

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

107
    switch ((unsigned long)stf.f_type)
108
109
110
111
112
113
114
115
116
117
    {
        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;
118
119

#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
120
    (void)fd;
121
122
123
124
125
    return false;

#endif
}

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

130
/*****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
131
 * FileOpen: open the file
132
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
133
int FileOpen( vlc_object_t *p_this )
134
{
135
    access_t     *p_access = (access_t*)p_this;
136
137
138
#ifdef WIN32
    bool is_remote = false;
#endif
139

140
    /* Open file */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
141
    int fd = -1;
142
143

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

        if (*end == '\0')
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
149
            fd = vlc_dup (oldfd);
150
        else if (*end == '/' && end > p_access->psz_location)
151
152
        {
            char *name = decode_URI_duplicate (end - 1);
153
            if (name != NULL)
154
155
            {
                name[0] = '.';
156
                fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
157
158
159
160
                free (name);
            }
        }
    }
161
    else
162
    {
163
164
        const char *path = p_access->psz_filepath;

165
        msg_Dbg (p_access, "opening file `%s'", path);
166
        fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
167
168
169
170
        if (fd == -1)
        {
            msg_Err (p_access, "cannot open file %s (%m)", path);
            dialog_Fatal (p_access, _("File reading failed"),
171
                          _("VLC could not open the file \"%s\". (%m)"), path);
172
        }
173
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
174
    if (fd == -1)
175
        return VLC_EGENERIC;
176

177
    struct stat st;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
178
    if (fstat (fd, &st))
gbazin's avatar
gbazin committed
179
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
180
181
        msg_Err (p_access, "failed to read (%m)");
        goto error;
182
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
183
184
185
    /* 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))
186
    {
187
188
189
190
191
192
#ifdef HAVE_FDOPENDIR
        DIR *handle = fdopendir (fd);
        if (handle == NULL)
            goto error; /* Uh? */
        return DirInit (p_access, handle);
#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
193
194
        msg_Dbg (p_access, "ignoring directory");
        goto error;
195
#endif
196
    }
197
198
199
200
201
202
203
204
205
206
207
208
209
210

    access_sys_t *p_sys = malloc (sizeof (*p_sys));
    if (unlikely(p_sys == NULL))
        goto error;
    access_InitFields (p_access);
    p_access->pf_read = FileRead;
    p_access->pf_block = NULL;
    p_access->pf_control = FileControl;
    p_access->pf_seek = FileSeek;
    p_access->p_sys = p_sys;
    p_sys->i_nb_reads = 0;
    p_sys->fd = fd;
    p_sys->b_pace_control = true;

211
212
213
    if (S_ISREG (st.st_mode))
        p_access->info.i_size = st.st_size;
    else if (!S_ISBLK (st.st_mode))
214
    {
215
        p_access->pf_seek = NoSeek;
216
217
        p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
    }
218
219
220
221
222
223
224

    if (p_access->pf_seek != NoSeek)
    {
        /* 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);
225
226
227
228
229
230
231
232
233
234
#if defined(HAVE_FCNTL)
        /* We'd rather use any available memory for reading ahead
         * than for caching what we've already seen/heard */
# if defined(F_RDAHEAD)
        fcntl (fd, F_RDAHEAD, 1);
# endif
# if defined(F_NOCACHE)
        fcntl (fd, F_NOCACHE, 1);
# endif
#endif
235
    }
236
    return VLC_SUCCESS;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
237
238

error:
239
    close (fd);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
240
    return VLC_EGENERIC;
241
242
243
}

/*****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
244
 * FileClose: close the target
245
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
246
void FileClose (vlc_object_t * p_this)
247
{
248
    access_t     *p_access = (access_t*)p_this;
249
250
251
252
253
254
255

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

256
    access_sys_t *p_sys = p_access->p_sys;
257

258
    close (p_sys->fd);
259
    free (p_sys);
260
261
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
262
263
264

#include <vlc_network.h>

265
266
267
/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
268
ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
269
{
270
    access_sys_t *p_sys = p_access->p_sys;
271
    int fd = p_sys->fd;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
272
    ssize_t i_ret;
273

274
#if !defined (WIN32) && !defined (__OS2__)
275
    if (p_access->pf_seek == NoSeek)
276
277
        i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
    else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
278
#endif
279
        i_ret = read (fd, p_buffer, i_len);
280

281
282
    if( i_ret < 0 )
    {
283
        switch (errno)
284
        {
285
286
287
288
289
            case EINTR:
            case EAGAIN:
                break;

            default:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
290
                msg_Err (p_access, "failed to read (%m)");
291
                dialog_Fatal (p_access, _("File reading failed"), "%s (%m)",
292
                              _("VLC could not read the file."));
293
                p_access->info.b_eof = true;
294
                return 0;
295
        }
296
    }
297
298
    else if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
ivoire's avatar
ivoire committed
299
    else
300
        p_access->info.b_eof = true;
301
302

    p_sys->i_nb_reads++;
303

304
305
    if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
     || (p_access->info.i_pos > p_access->info.i_size))
306
    {
307
#ifdef HAVE_SYS_STAT_H
308
        struct stat st;
gbazin's avatar
gbazin committed
309

310
        if ((fstat (fd, &st) == 0)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
311
         && (p_access->info.i_size != (uint64_t)st.st_size))
312
        {
313
            p_access->info.i_size = st.st_size;
314
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
315
316
        }
#endif
317
    }
318
319
320
    return i_ret;
}

321

322
323
324
/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
325
int FileSeek (access_t *p_access, uint64_t i_pos)
326
{
327
    p_access->info.i_pos = i_pos;
328
    p_access->info.b_eof = false;
329

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
330
    lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
331
332
333
    return VLC_SUCCESS;
}

334
int NoSeek (access_t *p_access, uint64_t i_pos)
335
336
337
338
339
340
{
    /* assert(0); ?? */
    (void) p_access; (void) i_pos;
    return VLC_EGENERIC;
}

341
342
343
/*****************************************************************************
 * Control:
 *****************************************************************************/
344
int FileControl( access_t *p_access, int i_query, va_list args )
345
346
{
    access_sys_t *p_sys = p_access->p_sys;
347
348
    bool    *pb_bool;
    int64_t *pi_64;
349
350

    switch( i_query )
351
    {
352
353
354
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
355
            pb_bool = (bool*)va_arg( args, bool* );
356
            *pb_bool = (p_access->pf_seek != NoSeek);
357
358
359
360
            break;

        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
361
            pb_bool = (bool*)va_arg( args, bool* );
362
363
364
365
366
367
            *pb_bool = p_sys->b_pace_control;
            break;

        /* */
        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
368
369
370
371
372
            if (IsRemote (p_sys->fd))
                *pi_64 = var_InheritInteger (p_access, "network-caching");
            else
                *pi_64 = var_InheritInteger (p_access, "file-caching");
            *pi_64 *= 1000;
373
            break;
374

375
376
377
378
379
380
381
382
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
383
        case ACCESS_SET_PRIVATE_ID_STATE:
384
        case ACCESS_GET_META:
385
        case ACCESS_GET_PRIVATE_ID_STATE:
386
        case ACCESS_GET_CONTENT_TYPE:
387
388
389
            return VLC_EGENERIC;

        default:
390
            msg_Warn( p_access, "unimplemented query %d in control", i_query );
391
392
            return VLC_EGENERIC;

393
    }
394
    return VLC_SUCCESS;
395
}