3
3
*****************************************************************************
4
4
* Copyright (C) 2001-2006 the VideoLAN team
5
5
* Copyright © 2006-2007 Rémi Denis-Courmont
6
* $Id: ad111cb458c35af33a8accb6c172dc4ae7ffad13 $
6
* $Id: cf3deb26aa0c2e39d286e3c417c563350510ae35 $
8
8
* Authors: Christophe Massiot <massiot@via.ecp.fr>
9
9
* Rémi Denis-Courmont <rem # videolan # org>
23
23
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
24
*****************************************************************************/
26
/*****************************************************************************
28
*****************************************************************************/
29
26
#ifdef HAVE_CONFIG_H
30
27
# include "config.h"
33
30
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
32
#include <vlc_input.h>
36
33
#include <vlc_access.h>
37
34
#include <vlc_dialog.h>
39
36
#include <assert.h>
41
#ifdef HAVE_SYS_TYPES_H
42
# include <sys/types.h>
38
#include <sys/types.h>
44
39
#ifdef HAVE_SYS_STAT_H
45
40
# include <sys/stat.h>
47
42
#ifdef HAVE_FCNTL_H
48
43
# include <fcntl.h>
50
#if defined (__linux__)
46
# include <sys/statvfs.h>
47
# if defined (HAVE_SYS_MOUNT_H)
48
# include <sys/param.h>
49
# include <sys/mount.h>
52
#ifdef HAVE_LINUX_MAGIC_H
51
53
# include <sys/vfs.h>
52
54
# include <linux/magic.h>
53
#elif defined (HAVE_SYS_MOUNT_H)
54
# include <sys/param.h>
55
# include <sys/mount.h>
58
57
#if defined( WIN32 )
71
70
# define lseek _lseeki64
72
71
#elif defined( UNDER_CE )
73
72
/* FIXME the commandline on wince is a mess */
75
73
# define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
78
#include <vlc_charset.h>
80
/*****************************************************************************
82
*****************************************************************************/
83
static int Open ( vlc_object_t * );
84
static void Close( vlc_object_t * );
86
#define CACHING_TEXT N_("Caching value in ms")
87
#define CACHING_LONGTEXT N_( \
88
"Caching value for files. This " \
89
"value should be set in milliseconds." )
92
set_description( N_("File input") )
93
set_shortname( N_("File") )
94
set_category( CAT_INPUT )
95
set_subcategory( SUBCAT_INPUT_ACCESS )
96
add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, true )
98
add_obsolete_string( "file-cat" )
99
set_capability( "access", 50 )
100
add_shortcut( "file" )
102
add_shortcut( "stream" )
103
set_callbacks( Open, Close )
107
/*****************************************************************************
108
* Exported prototypes
109
*****************************************************************************/
110
static int Seek( access_t *, int64_t );
111
static int NoSeek( access_t *, int64_t );
112
static ssize_t Read( access_t *, uint8_t *, size_t );
113
static int Control( access_t *, int, va_list );
115
static int open_file( access_t *, const char * );
117
79
struct access_sys_t
124
87
bool b_pace_control;
127
91
static bool IsRemote (int fd)
93
#if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
96
if (fstatvfs (fd, &stf))
98
/* fstatvfs() is in POSIX, but MNT_LOCAL is not */
99
return !(s.f_flag & MNT_LOCAL);
101
#elif defined (HAVE_LINUX_MAGIC_H)
130
102
struct statfs stf;
132
104
if (fstatfs (fd, &stf))
135
#if defined(MNT_LOCAL)
136
return !(stf.f_flags & MNT_LOCAL);
138
#elif defined (__linux__)
139
107
switch (stf.f_type)
141
109
case AFS_SUPER_MAGIC:
152
#else /* !HAVE_FSTATFS */
127
#ifndef HAVE_POSIX_FADVISE
128
# define posix_fadvise(fd, off, len, adv)
159
131
/*****************************************************************************
160
132
* Open: open the file
161
133
*****************************************************************************/
162
static int Open( vlc_object_t *p_this )
134
int Open( vlc_object_t *p_this )
164
136
access_t *p_access = (access_t*)p_this;
137
const char *path = p_access->psz_path;
167
wchar_t wpath[MAX_PATH+1];
168
139
bool is_remote = false;
171
/* Update default_pts to a suitable value for file access */
172
var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
174
STANDARD_READ_ACCESS_INIT;
175
p_sys->i_nb_reads = 0;
176
p_sys->b_pace_control = true;
181
145
if (!strcasecmp (p_access->psz_access, "fd"))
182
fd = dup (atoi (p_access->psz_path));
183
else if (!strcmp (p_access->psz_path, "-"))
148
int oldfd = strtol (path, &end, 10);
151
fd = vlc_dup (oldfd);
152
else if (*end == '/' && end > path)
154
char *name = decode_URI_duplicate (end - 1);
158
fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
187
msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
188
fd = open_file (p_access, p_access->psz_path);
165
msg_Dbg (p_access, "opening file `%s'", path);
166
fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
169
msg_Err (p_access, "cannot open file %s (%m)", path);
170
dialog_Fatal (p_access, _("File reading failed"),
171
_("VLC could not open the file \"%s\"."), path);
190
if (MultiByteToWideChar (CP_UTF8, 0, p_access->psz_path, -1,
175
wchar_t wpath[MAX_PATH+1];
176
if (MultiByteToWideChar (CP_UTF8, 0, path, -1,
192
178
&& PathIsNetworkPathW (wpath))
193
179
is_remote = true;
209
193
* how to parse the data. The directory plugin will do it. */
210
194
if (S_ISDIR (st.st_mode))
196
#ifdef HAVE_FDOPENDIR
197
DIR *handle = fdopendir (fd);
199
goto error; /* Uh? */
200
return DirInit (p_access, handle);
212
202
msg_Dbg (p_access, "ignoring directory");
207
access_sys_t *p_sys = malloc (sizeof (*p_sys));
208
if (unlikely(p_sys == NULL))
210
access_InitFields (p_access);
211
p_access->pf_read = FileRead;
212
p_access->pf_block = NULL;
213
p_access->pf_control = FileControl;
214
p_access->pf_seek = FileSeek;
215
p_access->p_sys = p_sys;
216
p_sys->i_nb_reads = 0;
218
p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
220
p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
221
p_sys->b_pace_control = true;
215
223
if (S_ISREG (st.st_mode))
216
224
p_access->info.i_size = st.st_size;
217
225
else if (!S_ISBLK (st.st_mode))
219
227
p_access->pf_seek = NoSeek;
220
228
p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
223
# warning File size not known!
231
if (p_access->pf_seek != NoSeek)
233
/* Demuxers will need the beginning of the file for probing. */
234
posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
235
/* In most cases, we only read the file once. */
236
posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
237
#if defined(HAVE_FCNTL)
238
/* We'd rather use any available memory for reading ahead
239
* than for caching what we've already seen/heard */
240
# if defined(F_RDAHEAD)
241
fcntl (fd, F_RDAHEAD, 1);
243
# if defined(F_NOCACHE)
244
fcntl (fd, F_NOCACHE, 1);
228
int i_cache = var_GetInteger (p_access, "file-caching") + 700;
229
var_SetInteger (p_access, "file-caching", i_cache);
230
msg_Warn (p_access, "Opening remote file, increasing cache: %d",
235
248
return VLC_SUCCESS;
241
252
return VLC_EGENERIC;
244
255
/*****************************************************************************
245
256
* Close: close the target
246
257
*****************************************************************************/
247
static void Close (vlc_object_t * p_this)
258
void Close (vlc_object_t * p_this)
249
260
access_t *p_access = (access_t*)p_this;
262
if (p_access->pf_read == NULL)
250
268
access_sys_t *p_sys = p_access->p_sys;
252
270
close (p_sys->fd);
259
277
/*****************************************************************************
260
278
* Read: standard read on a file descriptor.
261
279
*****************************************************************************/
262
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
280
ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
264
282
access_sys_t *p_sys = p_access->p_sys;
265
283
int fd = p_sys->fd;
296
314
p_sys->i_nb_reads++;
298
#ifdef HAVE_SYS_STAT_H
299
316
if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
300
317
|| (p_access->info.i_pos > p_access->info.i_size))
319
#ifdef HAVE_SYS_STAT_H
304
322
if ((fstat (fd, &st) == 0)
305
&& (p_access->info.i_size != st.st_size))
323
&& (p_access->info.i_size != (uint64_t)st.st_size))
307
325
p_access->info.i_size = st.st_size;
308
326
p_access->info.i_update |= INPUT_UPDATE_SIZE;
316
334
/*****************************************************************************
317
335
* Seek: seek to a specific location in a file
318
336
*****************************************************************************/
319
static int Seek (access_t *p_access, int64_t i_pos)
337
int FileSeek (access_t *p_access, uint64_t i_pos)
321
339
p_access->info.i_pos = i_pos;
322
340
p_access->info.b_eof = false;
335
353
/*****************************************************************************
337
355
*****************************************************************************/
338
static int Control( access_t *p_access, int i_query, va_list args )
356
int FileControl( access_t *p_access, int i_query, va_list args )
340
358
access_sys_t *p_sys = p_access->p_sys;
384
402
return VLC_SUCCESS;
387
/*****************************************************************************
388
* open_file: Opens a specific file
389
*****************************************************************************/
390
static int open_file (access_t *p_access, const char *path)
393
if (!strcasecmp (p_access->psz_access, "file")
394
&& ('/' == path[0]) && isalpha (path[1])
395
&& (':' == path[2]) && ('/' == path[3]))
396
/* Explorer can open path such as file:/C:/ or file:///C:/
397
* hence remove leading / if found */
401
int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
404
msg_Err (p_access, "cannot open file %s (%m)", path);
405
dialog_Fatal (p_access, _("File reading failed"),
406
_("VLC could not open the file \"%s\"."), path);
410
#if defined(HAVE_FCNTL)
411
/* We'd rather use any available memory for reading ahead
412
* than for caching what we've already seen/heard */
413
# if defined(F_RDAHEAD)
414
fcntl (fd, F_RDAHEAD, 1);
416
# if defined(F_NOCACHE)
417
fcntl (fd, F_NOCACHE, 1);