file.c 10.9 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 <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
60
61
62
63
#include <vlc_common.h>
#include "fs.h"
#include <vlc_input.h>
#include <vlc_access.h>
#include <vlc_dialog.h>
#ifdef WIN32
# include <vlc_charset.h>
#endif
64
#include <vlc_fs.h>
65
#include <vlc_url.h>
66

67
struct access_sys_t
68
{
69
    unsigned int i_nb_reads;
gbazin's avatar
gbazin committed
70

71
    int fd;
gbazin's avatar
gbazin committed
72

73
    /* */
74
    bool b_pace_control;
75
};
76

77
#if !defined (WIN32) && !defined (__OS2__)
78
79
static bool IsRemote (int fd)
{
80
81
82
83
84
85
#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
86
    return !(stf.f_flag & MNT_LOCAL);
87
88

#elif defined (HAVE_LINUX_MAGIC_H)
89
90
91
92
93
    struct statfs stf;

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

94
    switch ((unsigned long)stf.f_type)
95
96
97
98
99
100
101
102
103
104
    {
        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;
105
106

#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
107
    (void)fd;
108
109
110
111
    return false;

#endif
}
112
113
# define IsRemote(fd,path) IsRemote(fd)

KO Myung-Hun's avatar
KO Myung-Hun committed
114
#else /* WIN32 || __OS2__ */
115
116
static bool IsRemote (const char *path)
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
117
# if !defined(__OS2__)
118
119
120
121
122
123
124
125
126
127
    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
128

129
#ifndef HAVE_POSIX_FADVISE
130
# define posix_fadvise(fd, off, len, adv)
131
#endif
132

133
/*****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
134
 * FileOpen: open the file
135
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
136
int FileOpen( vlc_object_t *p_this )
137
{
138
139
    access_t     *p_access = (access_t*)p_this;

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

179
    struct stat st;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
180
    if (fstat (fd, &st))
gbazin's avatar
gbazin committed
181
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182
183
        msg_Err (p_access, "failed to read (%m)");
        goto error;
184
    }
185

186
187
188
189
190
191
192
193
194
#if O_NONBLOCK
    int flags = fcntl (fd, F_GETFL);
    if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode))
        /* Force non-blocking mode where applicable (fd://) */
        flags |= O_NONBLOCK;
    else
        /* Force blocking mode when not useful or not specified */
        flags &= ~O_NONBLOCK;
    fcntl (fd, F_SETFL, flags);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
195
#endif
196

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
197
198
199
    /* 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))
200
    {
201
202
203
204
205
206
#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
207
208
        msg_Dbg (p_access, "ignoring directory");
        goto error;
209
#endif
210
    }
211
212
213
214
215
216
217
218
219
220
221
222
223
224

    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;

225
226
227
    if (S_ISREG (st.st_mode))
        p_access->info.i_size = st.st_size;
    else if (!S_ISBLK (st.st_mode))
228
    {
229
        p_access->pf_seek = NoSeek;
230
231
        p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
    }
232
233
234
235
236
237
238

    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);
239
240
241
242
243
244
245
246
247
248
#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
249
    }
250
    return VLC_SUCCESS;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
251
252

error:
253
    close (fd);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
254
    return VLC_EGENERIC;
255
256
257
}

/*****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
258
 * FileClose: close the target
259
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
260
void FileClose (vlc_object_t * p_this)
261
{
262
    access_t     *p_access = (access_t*)p_this;
263
264
265
266
267
268
269

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

270
    access_sys_t *p_sys = p_access->p_sys;
271

272
    close (p_sys->fd);
273
    free (p_sys);
274
275
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
276
277
278

#include <vlc_network.h>

279
280
281
/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
282
ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
283
{
284
    access_sys_t *p_sys = p_access->p_sys;
285
    int fd = p_sys->fd;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
286
    ssize_t i_ret;
287

288
#if !defined (WIN32) && !defined (__OS2__)
289
    if (p_access->pf_seek == NoSeek)
290
291
        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
292
#endif
293
        i_ret = read (fd, p_buffer, i_len);
294

295
296
    if( i_ret < 0 )
    {
297
        switch (errno)
298
        {
299
300
301
302
303
            case EINTR:
            case EAGAIN:
                break;

            default:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
304
                msg_Err (p_access, "failed to read (%m)");
305
306
                dialog_Fatal (p_access, _("File reading failed"),
                              _("VLC could not read the file (%m)."));
307
                p_access->info.b_eof = true;
308
                return 0;
309
        }
310
    }
311
312
    else if( i_ret > 0 )
        p_access->info.i_pos += i_ret;
ivoire's avatar
ivoire committed
313
    else
314
        p_access->info.b_eof = true;
315
316

    p_sys->i_nb_reads++;
317

318
319
    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))
320
    {
321
        struct stat st;
gbazin's avatar
gbazin committed
322

323
        if ((fstat (fd, &st) == 0)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
324
         && (p_access->info.i_size != (uint64_t)st.st_size))
325
        {
326
            p_access->info.i_size = st.st_size;
327
            p_access->info.i_update |= INPUT_UPDATE_SIZE;
328
        }
329
    }
330
331
332
    return i_ret;
}

333

334
335
336
/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
337
int FileSeek (access_t *p_access, uint64_t i_pos)
338
{
339
    p_access->info.i_pos = i_pos;
340
    p_access->info.b_eof = false;
341

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
342
    lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
343
344
345
    return VLC_SUCCESS;
}

346
int NoSeek (access_t *p_access, uint64_t i_pos)
347
348
349
350
351
352
{
    /* assert(0); ?? */
    (void) p_access; (void) i_pos;
    return VLC_EGENERIC;
}

353
354
355
/*****************************************************************************
 * Control:
 *****************************************************************************/
356
int FileControl( access_t *p_access, int i_query, va_list args )
357
358
{
    access_sys_t *p_sys = p_access->p_sys;
359
360
    bool    *pb_bool;
    int64_t *pi_64;
361
362

    switch( i_query )
363
    {
364
365
366
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
367
            pb_bool = (bool*)va_arg( args, bool* );
368
            *pb_bool = (p_access->pf_seek != NoSeek);
369
370
371
372
            break;

        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
373
            pb_bool = (bool*)va_arg( args, bool* );
374
375
376
377
378
379
            *pb_bool = p_sys->b_pace_control;
            break;

        /* */
        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
380
            if (IsRemote (p_sys->fd, p_access->psz_filepath))
381
382
383
384
                *pi_64 = var_InheritInteger (p_access, "network-caching");
            else
                *pi_64 = var_InheritInteger (p_access, "file-caching");
            *pi_64 *= 1000;
385
            break;
386

387
388
389
390
391
392
393
394
        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
395
        case ACCESS_SET_PRIVATE_ID_STATE:
396
        case ACCESS_GET_META:
397
        case ACCESS_GET_PRIVATE_ID_STATE:
398
        case ACCESS_GET_CONTENT_TYPE:
399
400
401
            return VLC_EGENERIC;

        default:
402
            msg_Warn( p_access, "unimplemented query %d in control", i_query );
403
404
            return VLC_EGENERIC;

405
    }
406
    return VLC_SUCCESS;
407
}