1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001-2006 the VideoLAN team
5 * Copyright © 2006-2007 Rémi Denis-Courmont
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan # org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_interface.h>
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
59 #if defined( WIN32 ) && !defined( UNDER_CE )
63 # define lseek _lseeki64
64 #elif defined( UNDER_CE )
65 /* FIXME the commandline on wince is a mess */
69 #include <vlc_charset.h>
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t
* );
75 static void Close( vlc_object_t
* );
77 #define CACHING_TEXT N_("Caching value in ms")
78 #define CACHING_LONGTEXT N_( \
79 "Caching value for files. This " \
80 "value should be set in milliseconds." )
83 set_description( N_("File input") );
84 set_shortname( N_("File") );
85 set_category( CAT_INPUT
);
86 set_subcategory( SUBCAT_INPUT_ACCESS
);
87 add_integer( "file-caching", DEFAULT_PTS_DELAY
/ 1000, NULL
, CACHING_TEXT
, CACHING_LONGTEXT
, true );
88 add_obsolete_string( "file-cat" );
89 set_capability( "access", 50 );
90 add_shortcut( "file" );
91 add_shortcut( "stream" );
92 set_callbacks( Open
, Close
);
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Seek( access_t
*, int64_t );
100 static ssize_t
Read( access_t
*, uint8_t *, size_t );
101 static int Control( access_t
*, int, va_list );
103 static int open_file( access_t
*, const char * );
107 unsigned int i_nb_reads
;
116 /*****************************************************************************
117 * Open: open the file
118 *****************************************************************************/
119 static int Open( vlc_object_t
*p_this
)
121 access_t
*p_access
= (access_t
*)p_this
;
124 bool b_stdin
= !strcmp (p_access
->psz_path
, "-");
126 /* Update default_pts to a suitable value for file access */
127 var_Create( p_access
, "file-caching", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
129 STANDARD_READ_ACCESS_INIT
;
130 p_sys
->i_nb_reads
= 0;
131 int fd
= p_sys
->fd
= -1;
133 if (!strcasecmp (p_access
->psz_access
, "stream"))
135 p_sys
->b_seekable
= false;
136 p_sys
->b_pace_control
= false;
140 p_sys
->b_seekable
= true;
141 p_sys
->b_pace_control
= true;
145 msg_Dbg (p_access
, "opening file `%s'", p_access
->psz_path
);
150 fd
= open_file (p_access
, p_access
->psz_path
);
152 #ifdef HAVE_SYS_STAT_H
158 msg_Err (p_access
, "fstat(%d): %m", fd
);
160 if (S_ISDIR (st
.st_mode
))
161 /* The directory plugin takes care of that */
162 msg_Dbg (p_access
, "file is a directory, aborting");
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;
183 p_sys
->b_seekable
= !b_stdin
;
184 # warning File size not known!
190 /*****************************************************************************
191 * Close: close the target
192 *****************************************************************************/
193 static void Close (vlc_object_t
* p_this
)
195 access_t
*p_access
= (access_t
*)p_this
;
196 access_sys_t
*p_sys
= p_access
->p_sys
;
203 #include <vlc_network.h>
205 /*****************************************************************************
206 * Read: standard read on a file descriptor.
207 *****************************************************************************/
208 static ssize_t
Read( access_t
*p_access
, uint8_t *p_buffer
, size_t i_len
)
210 access_sys_t
*p_sys
= p_access
->p_sys
;
215 i_ret
= net_Read (p_access
, fd
, NULL
, p_buffer
, i_len
, false);
217 i_ret
= read (fd
, p_buffer
, i_len
);
229 msg_Err (p_access
, "read failed (%m)");
230 intf_UserFatal (p_access
, false, _("File reading failed"),
231 _("VLC could not read the file."));
236 p_access
->info
.i_pos
+= i_ret
;
237 else if( i_ret
== 0 )
238 p_access
->info
.b_eof
= true;
242 #ifdef HAVE_SYS_STAT_H
243 if( p_access
->info
.i_size
!= 0 &&
244 (p_sys
->i_nb_reads
% INPUT_FSTAT_NB_READS
) == 0 )
248 if ((fstat (fd
, &st
) == 0)
249 && (p_access
->info
.i_size
!= st
.st_size
))
251 p_access
->info
.i_size
= st
.st_size
;
252 p_access
->info
.i_update
|= INPUT_UPDATE_SIZE
;
260 /*****************************************************************************
261 * Seek: seek to a specific location in a file
262 *****************************************************************************/
263 static int Seek (access_t
*p_access
, int64_t i_pos
)
265 p_access
->info
.i_pos
= i_pos
;
266 p_access
->info
.b_eof
= false;
268 lseek (p_access
->p_sys
->fd
, i_pos
, SEEK_SET
);
272 /*****************************************************************************
274 *****************************************************************************/
275 static int Control( access_t
*p_access
, int i_query
, va_list args
)
277 access_sys_t
*p_sys
= p_access
->p_sys
;
285 case ACCESS_CAN_SEEK
:
286 case ACCESS_CAN_FASTSEEK
:
287 pb_bool
= (bool*)va_arg( args
, bool* );
288 *pb_bool
= p_sys
->b_seekable
;
291 case ACCESS_CAN_PAUSE
:
292 case ACCESS_CAN_CONTROL_PACE
:
293 pb_bool
= (bool*)va_arg( args
, bool* );
294 *pb_bool
= p_sys
->b_pace_control
;
299 pi_int
= (int*)va_arg( args
, int * );
303 case ACCESS_GET_PTS_DELAY
:
304 pi_64
= (int64_t*)va_arg( args
, int64_t * );
305 *pi_64
= var_GetInteger( p_access
, "file-caching" ) * INT64_C(1000);
309 case ACCESS_SET_PAUSE_STATE
:
313 case ACCESS_GET_TITLE_INFO
:
314 case ACCESS_SET_TITLE
:
315 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
:
323 msg_Warn( p_access
, "unimplemented query %d in control", i_query
);
330 /*****************************************************************************
331 * open_file: Opens a specific file
332 *****************************************************************************/
333 static int open_file (access_t
*p_access
, const char *path
)
336 if (!strcasecmp (p_access
->psz_access
, "file")
337 && ('/' == path
[0]) && isalpha (path
[1])
338 && (':' == path
[2]) && ('/' == path
[3]))
339 /* Explorer can open path such as file:/C:/ or file:///C:/
340 * hence remove leading / if found */
344 int fd
= utf8_open (path
, O_RDONLY
| O_NONBLOCK
/* O_LARGEFILE*/, 0666);
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
);
353 #if defined(HAVE_FCNTL)
354 fcntl (fd
, F_SETFD
, fcntl (fd
, F_GETFD
) | FD_CLOEXEC
);
356 /* We'd rather use any available memory for reading ahead
357 * than for caching what we've already seen/heard */
358 # if defined(F_RDAHEAD)
359 fcntl (fd
, F_RDAHEAD
, 1);
361 # if defined(F_NOCACHE)
362 fcntl (fd
, F_NOCACHE
, 1);