]> git.sesse.net Git - vlc/blob - modules/access/file.c
33d8c2dc1d6607924713ecffc47d991e6b0b65b1
[vlc] / modules / access / file.c
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
6  * $Id$
7  *
8  * Authors: Christophe Massiot <massiot@via.ecp.fr>
9  *          Rémi Denis-Courmont <rem # videolan # org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
37 #include <vlc_dialog.h>
38
39 #include <assert.h>
40 #include <errno.h>
41 #ifdef HAVE_SYS_TYPES_H
42 #   include <sys/types.h>
43 #endif
44 #ifdef HAVE_SYS_STAT_H
45 #   include <sys/stat.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #   include <fcntl.h>
49 #endif
50
51 #if defined( WIN32 )
52 #   include <io.h>
53 #   include <ctype.h>
54 #else
55 #   include <unistd.h>
56 #   include <poll.h>
57 #endif
58
59 #if defined( WIN32 ) && !defined( UNDER_CE )
60 #   ifdef lseek
61 #      undef lseek
62 #   endif
63 #   define lseek _lseeki64
64 #elif defined( UNDER_CE )
65 /* FIXME the commandline on wince is a mess */
66 # define dup(a) -1
67 #endif
68
69 #include <vlc_charset.h>
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 static int  Open ( vlc_object_t * );
75 static void Close( vlc_object_t * );
76
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." )
81
82 vlc_module_begin ()
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( "fd" )
92     add_shortcut( "stream" )
93     set_callbacks( Open, Close )
94 vlc_module_end ()
95
96
97 /*****************************************************************************
98  * Exported prototypes
99  *****************************************************************************/
100 static int  Seek( access_t *, int64_t );
101 static int  NoSeek( access_t *, int64_t );
102 static ssize_t Read( access_t *, uint8_t *, size_t );
103 static int  Control( access_t *, int, va_list );
104
105 static int  open_file( access_t *, const char * );
106
107 struct access_sys_t
108 {
109     unsigned int i_nb_reads;
110
111     int fd;
112
113     /* */
114     bool b_pace_control;
115 };
116
117 /*****************************************************************************
118  * Open: open the file
119  *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
121 {
122     access_t     *p_access = (access_t*)p_this;
123     access_sys_t *p_sys;
124
125     /* Update default_pts to a suitable value for file access */
126     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
127
128     STANDARD_READ_ACCESS_INIT;
129     p_sys->i_nb_reads = 0;
130
131     if (!strcasecmp (p_access->psz_access, "stream"))
132         p_sys->b_pace_control = false;
133     else
134         p_sys->b_pace_control = true;
135
136     /* Open file */
137     int fd = -1;
138
139     if (!strcasecmp (p_access->psz_access, "fd"))
140         fd = dup (atoi (p_access->psz_path));
141     else if (!strcmp (p_access->psz_path, "-"))
142         fd = dup (0);
143     else
144     {
145         msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
146         fd = open_file (p_access, p_access->psz_path);
147     }
148     if (fd == -1)
149         goto error;
150
151 #ifdef HAVE_SYS_STAT_H
152     struct stat st;
153
154     if (fstat (fd, &st))
155     {
156         msg_Err (p_access, "failed to read (%m)");
157         goto error;
158     }
159     /* Directories can be opened and read from, but only readdir() knows
160      * how to parse the data. The directory plugin will do it. */
161     if (S_ISDIR (st.st_mode))
162     {
163         msg_Dbg (p_access, "ignoring directory");
164         goto error;
165     }
166     if (S_ISREG (st.st_mode))
167         p_access->info.i_size = st.st_size;
168     else if (!S_ISBLK (st.st_mode))
169         p_access->pf_seek = NoSeek;
170 #else
171 # warning File size not known!
172 #endif
173
174     p_sys->fd = fd;
175     return VLC_SUCCESS;
176
177 error:
178     if (fd != -1)
179         close (fd);
180     free (p_sys);
181     return VLC_EGENERIC;
182 }
183
184 /*****************************************************************************
185  * Close: close the target
186  *****************************************************************************/
187 static void Close (vlc_object_t * p_this)
188 {
189     access_t     *p_access = (access_t*)p_this;
190     access_sys_t *p_sys = p_access->p_sys;
191
192     close (p_sys->fd);
193     free (p_sys);
194 }
195
196
197 #include <vlc_network.h>
198
199 /*****************************************************************************
200  * Read: standard read on a file descriptor.
201  *****************************************************************************/
202 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
203 {
204     access_sys_t *p_sys = p_access->p_sys;
205     int fd = p_sys->fd;
206     ssize_t i_ret;
207
208 #ifndef WIN32
209     if (p_access->pf_seek == NoSeek)
210         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
211     else
212 #endif
213         i_ret = read (fd, p_buffer, i_len);
214
215     if( i_ret < 0 )
216     {
217         switch (errno)
218         {
219             case EINTR:
220             case EAGAIN:
221                 break;
222
223             default:
224                 msg_Err (p_access, "failed to read (%m)");
225                 dialog_Fatal (p_access, _("File reading failed"),
226                               _("VLC could not read the file."));
227                 p_access->info.b_eof = true;
228                 return 0;
229         }
230     }
231     else if( i_ret > 0 )
232         p_access->info.i_pos += i_ret;
233     else
234         p_access->info.b_eof = true;
235
236     p_sys->i_nb_reads++;
237
238 #ifdef HAVE_SYS_STAT_H
239     if( p_access->info.i_size != 0 &&
240         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
241     {
242         struct stat st;
243
244         if ((fstat (fd, &st) == 0)
245          && (p_access->info.i_size != st.st_size))
246         {
247             p_access->info.i_size = st.st_size;
248             p_access->info.i_update |= INPUT_UPDATE_SIZE;
249         }
250     }
251 #endif
252     return i_ret;
253 }
254
255
256 /*****************************************************************************
257  * Seek: seek to a specific location in a file
258  *****************************************************************************/
259 static int Seek (access_t *p_access, int64_t i_pos)
260 {
261     p_access->info.i_pos = i_pos;
262     p_access->info.b_eof = false;
263
264     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
265     return VLC_SUCCESS;
266 }
267
268 static int NoSeek (access_t *p_access, int64_t i_pos)
269 {
270     /* assert(0); ?? */
271     (void) p_access; (void) i_pos;
272     return VLC_EGENERIC;
273 }
274
275 /*****************************************************************************
276  * Control:
277  *****************************************************************************/
278 static int Control( access_t *p_access, int i_query, va_list args )
279 {
280     access_sys_t *p_sys = p_access->p_sys;
281     bool    *pb_bool;
282     int64_t *pi_64;
283
284     switch( i_query )
285     {
286         /* */
287         case ACCESS_CAN_SEEK:
288         case ACCESS_CAN_FASTSEEK:
289             pb_bool = (bool*)va_arg( args, bool* );
290             *pb_bool = (p_access->pf_seek != NoSeek);
291             break;
292
293         case ACCESS_CAN_PAUSE:
294         case ACCESS_CAN_CONTROL_PACE:
295             pb_bool = (bool*)va_arg( args, bool* );
296             *pb_bool = p_sys->b_pace_control;
297             break;
298
299         /* */
300         case ACCESS_GET_PTS_DELAY:
301             pi_64 = (int64_t*)va_arg( args, int64_t * );
302             *pi_64 = var_GetInteger( p_access, "file-caching" ) * INT64_C(1000);
303             break;
304
305         /* */
306         case ACCESS_SET_PAUSE_STATE:
307             /* Nothing to do */
308             break;
309
310         case ACCESS_GET_TITLE_INFO:
311         case ACCESS_SET_TITLE:
312         case ACCESS_SET_SEEKPOINT:
313         case ACCESS_SET_PRIVATE_ID_STATE:
314         case ACCESS_GET_META:
315         case ACCESS_GET_PRIVATE_ID_STATE:
316         case ACCESS_GET_CONTENT_TYPE:
317             return VLC_EGENERIC;
318
319         default:
320             msg_Warn( p_access, "unimplemented query %d in control", i_query );
321             return VLC_EGENERIC;
322
323     }
324     return VLC_SUCCESS;
325 }
326
327 /*****************************************************************************
328  * open_file: Opens a specific file
329  *****************************************************************************/
330 static int open_file (access_t *p_access, const char *path)
331 {
332 #if defined(WIN32)
333     if (!strcasecmp (p_access->psz_access, "file")
334       && ('/' == path[0]) && isalpha (path[1])
335       && (':' == path[2]) && ('/' == path[3]))
336         /* Explorer can open path such as file:/C:/ or file:///C:/
337          * hence remove leading / if found */
338         path++;
339 #endif
340
341     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
342     if (fd == -1)
343     {
344         msg_Err (p_access, "cannot open file %s (%m)", path);
345         dialog_Fatal (p_access, _("File reading failed"),
346                       _("VLC could not open the file \"%s\"."), path);
347         return -1;
348     }
349
350 #if defined(HAVE_FCNTL)
351     /* We'd rather use any available memory for reading ahead
352      * than for caching what we've already seen/heard */
353 # if defined(F_RDAHEAD)
354     fcntl (fd, F_RDAHEAD, 1);
355 # endif
356 # if defined(F_NOCACHE)
357     fcntl (fd, F_NOCACHE, 1);
358 # endif
359 #endif
360
361     return fd;
362 }