]> git.sesse.net Git - vlc/blob - modules/access/file.c
Add more debug
[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 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 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc_interaction.h>
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #ifdef HAVE_SYS_TYPES_H
37 #   include <sys/types.h>
38 #endif
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42 #ifdef HAVE_FCNTL_H
43 #   include <fcntl.h>
44 #endif
45
46 #if defined( WIN32 ) && !defined( UNDER_CE )
47 #   include <io.h>
48 #else
49 #   include <unistd.h>
50 #   include <poll.h>
51 #endif
52
53 #if defined( WIN32 ) && !defined( UNDER_CE )
54 /* fstat() support for large files on win32 */
55 #   define fstat(a,b) _fstati64(a,b)
56 #   ifdef lseek
57 #      undef lseek
58 #   endif
59 #   define lseek _lseeki64
60 #elif defined( UNDER_CE )
61 #   ifdef read
62 #      undef read
63 #   endif
64 #   define read(a,b,c) fread(b,1,c,a)
65 #   define close(a) fclose(a)
66 #   ifdef lseek
67 #      undef lseek
68 #   endif
69 #   define lseek fseek
70 #endif
71
72 #include "charset.h"
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 static int  Open ( vlc_object_t * );
78 static void Close( vlc_object_t * );
79
80 #define CACHING_TEXT N_("Caching value in ms")
81 #define CACHING_LONGTEXT N_( \
82     "Caching value for files. This " \
83     "value should be set in milliseconds." )
84 #define CAT_TEXT N_("Concatenate with additional files")
85 #define CAT_LONGTEXT N_( \
86     "Play split files as if they were part of a unique file. " \
87     "You need to specify a comma-separated list of files." )
88
89 vlc_module_begin();
90     set_description( _("File input") );
91     set_shortname( _("File") );
92     set_category( CAT_INPUT );
93     set_subcategory( SUBCAT_INPUT_ACCESS );
94     add_integer( "file-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
95     add_deprecated( "file-cat", VLC_TRUE );
96     set_capability( "access2", 50 );
97     add_shortcut( "file" );
98     add_shortcut( "stream" );
99     add_shortcut( "kfir" );
100     set_callbacks( Open, Close );
101 vlc_module_end();
102
103
104 /*****************************************************************************
105  * Exported prototypes
106  *****************************************************************************/
107 static int  Seek( access_t *, int64_t );
108 static int  Read( access_t *, uint8_t *, int );
109 static int  Control( access_t *, int, va_list );
110
111 static int  open_file( access_t *, const char * );
112
113 struct access_sys_t
114 {
115     unsigned int i_nb_reads;
116     vlc_bool_t   b_kfir;
117
118     int fd;
119
120     /* */
121     vlc_bool_t b_seekable;
122     vlc_bool_t b_pace_control;
123 };
124
125 /*****************************************************************************
126  * Open: open the file
127  *****************************************************************************/
128 static int Open( vlc_object_t *p_this )
129 {
130     access_t     *p_access = (access_t*)p_this;
131     access_sys_t *p_sys;
132
133     vlc_bool_t    b_stdin = !strcmp (p_access->psz_path, "-");
134
135     /* Update default_pts to a suitable value for file access */
136     var_Create( p_access, "file-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
137
138     STANDARD_READ_ACCESS_INIT;
139     p_sys->i_nb_reads = 0;
140     p_sys->b_kfir = VLC_FALSE;
141     int fd = p_sys->fd = -1;
142
143     if (!strcasecmp (p_access->psz_access, "stream"))
144     {
145         p_sys->b_seekable = VLC_FALSE;
146         p_sys->b_pace_control = VLC_FALSE;
147     }
148     else if (!strcasecmp (p_access->psz_access, "kfir"))
149     {
150         p_sys->b_seekable = VLC_FALSE;
151         p_sys->b_pace_control = VLC_FALSE;
152         p_sys->b_kfir = VLC_TRUE;
153     }
154     else
155     {
156         p_sys->b_seekable = VLC_TRUE;
157         p_sys->b_pace_control = VLC_TRUE;
158     }
159
160     /* Open file */
161     msg_Dbg (p_access, "opening file `%s'", p_access->psz_path);
162
163     if (b_stdin)
164         fd = dup (0);
165     else
166         fd = open_file (p_access, p_access->psz_path);
167
168 #ifdef HAVE_SYS_STAT_H
169     struct stat st;
170
171     while (fd != -1)
172     {
173         if (fstat (fd, &st))
174             msg_Err (p_access, "fstat(%d): %s", fd, strerror (errno));
175         else
176         if (S_ISDIR (st.st_mode))
177             /* The directory plugin takes care of that */
178             msg_Dbg (p_access, "file is a directory, aborting");
179         else
180             break; // success
181
182         close (fd);
183         fd = -1;
184     }
185 #endif
186
187     if (fd == -1)
188     {
189         free (p_sys);
190         return VLC_EGENERIC;
191     }
192     p_sys->fd = fd;
193
194 #ifdef HAVE_SYS_STAT_H
195     p_access->info.i_size = st.st_size;
196     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode)
197      && (!S_ISCHR (st.st_mode) || (st.st_size == 0)))
198         p_sys->b_seekable = VLC_FALSE;
199 #else
200     p_sys->b_seekable = !b_stdin;
201 # warning File size not known!
202 #endif
203
204     if (p_sys->b_seekable && (p_access->info.i_size == 0))
205     {
206         /* FIXME that's bad because all others access will be probed */
207         msg_Err (p_access, "file is empty, aborting");
208         Close (p_this);
209         return VLC_EGENERIC;
210     }
211     msg_Dbg (p_access, "opened file of size "I64Fd" (FIXME: remove this)",
212              p_access->info.i_size);
213
214     return VLC_SUCCESS;
215 }
216
217 /*****************************************************************************
218  * Close: close the target
219  *****************************************************************************/
220 static void Close (vlc_object_t * p_this)
221 {
222     access_t     *p_access = (access_t*)p_this;
223     access_sys_t *p_sys = p_access->p_sys;
224
225     close (p_sys->fd);
226     free (p_sys);
227 }
228
229 /*****************************************************************************
230  * Read: standard read on a file descriptor.
231  *****************************************************************************/
232 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
233 {
234     access_sys_t *p_sys = p_access->p_sys;
235     int i_ret;
236     int fd = p_sys->fd;
237
238 #if !defined(WIN32) && !defined(UNDER_CE)
239     if( !p_sys->b_pace_control )
240     {
241         if( !p_sys->b_kfir )
242         {
243             /* Find if some data is available. This won't work under Windows. */
244             do
245             {
246                 struct pollfd ufd;
247
248                 if( p_access->b_die )
249                     return 0;
250
251                 memset (&ufd, 0, sizeof (ufd));
252                 ufd.fd = fd;
253                 ufd.events = POLLIN;
254
255                 i_ret = poll (&ufd, 1, 500);
256             }
257             while (i_ret <= 0);
258
259             i_ret = read (fd, p_buffer, i_len);
260         }
261         else
262         {
263             /* b_kfir ; work around a buggy poll() driver implementation */
264             while (((i_ret = read (fd, p_buffer, i_len)) == 0)
265                 && !p_access->b_die)
266             {
267                 msleep( INPUT_ERROR_SLEEP );
268             }
269         }
270     }
271     else
272 #endif /* WIN32 || UNDER_CE */
273         /* b_pace_control || WIN32 */
274         i_ret = read( fd, p_buffer, i_len );
275
276     if( i_ret < 0 )
277     {
278         switch (errno)
279         {
280             case EINTR:
281             case EAGAIN:
282                 break;
283
284             default:
285                 msg_Err (p_access, "read failed (%s)", strerror (errno));
286                 intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"),
287                                 _("VLC could not read file \"%s\"."),
288                                 strerror (errno));
289         }
290
291         /* Delay a bit to avoid consuming all the CPU. This is particularly
292          * useful when reading from an unconnected FIFO. */
293         msleep( INPUT_ERROR_SLEEP );
294     }
295
296     p_sys->i_nb_reads++;
297
298 #ifdef HAVE_SYS_STAT_H
299     if( p_access->info.i_size != 0 &&
300         (p_sys->i_nb_reads % INPUT_FSTAT_NB_READS) == 0 )
301     {
302         struct stat st;
303
304         if ((fstat (fd, &st) == 0)
305          && (p_access->info.i_size != st.st_size))
306         {
307             p_access->info.i_size = st.st_size;
308             p_access->info.i_update |= INPUT_UPDATE_SIZE;
309         }
310     }
311 #endif
312
313     if( i_ret > 0 )
314         p_access->info.i_pos += i_ret;
315     else if( i_ret == 0 )
316         p_access->info.b_eof = VLC_TRUE;
317
318     return i_ret;
319 }
320
321 /*****************************************************************************
322  * Seek: seek to a specific location in a file
323  *****************************************************************************/
324 static int Seek (access_t *p_access, int64_t i_pos)
325 {
326     if (i_pos > p_access->info.i_size)
327     {
328         msg_Err (p_access, "seeking too far");
329         i_pos = p_access->info.i_size;
330     }
331     else if (i_pos < 0)
332     {
333         msg_Err (p_access, "seeking too early");
334         i_pos = 0;
335     }
336
337     p_access->info.i_pos = i_pos;
338     p_access->info.b_eof = VLC_FALSE;
339
340     /* Determine which file we need to access */
341     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
342     return VLC_SUCCESS;
343 }
344
345 /*****************************************************************************
346  * Control:
347  *****************************************************************************/
348 static int Control( access_t *p_access, int i_query, va_list args )
349 {
350     access_sys_t *p_sys = p_access->p_sys;
351     vlc_bool_t   *pb_bool;
352     int          *pi_int;
353     int64_t      *pi_64;
354
355     switch( i_query )
356     {
357         /* */
358         case ACCESS_CAN_SEEK:
359         case ACCESS_CAN_FASTSEEK:
360             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
361             *pb_bool = p_sys->b_seekable;
362             break;
363
364         case ACCESS_CAN_PAUSE:
365         case ACCESS_CAN_CONTROL_PACE:
366             pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
367             *pb_bool = p_sys->b_pace_control;
368             break;
369
370         /* */
371         case ACCESS_GET_MTU:
372             pi_int = (int*)va_arg( args, int * );
373             *pi_int = 0;
374             break;
375
376         case ACCESS_GET_PTS_DELAY:
377             pi_64 = (int64_t*)va_arg( args, int64_t * );
378             *pi_64 = var_GetInteger( p_access, "file-caching" ) * I64C(1000);
379             break;
380
381         /* */
382         case ACCESS_SET_PAUSE_STATE:
383             /* Nothing to do */
384             break;
385
386         case ACCESS_GET_TITLE_INFO:
387         case ACCESS_SET_TITLE:
388         case ACCESS_SET_SEEKPOINT:
389         case ACCESS_SET_PRIVATE_ID_STATE:
390         case ACCESS_GET_META:
391             return VLC_EGENERIC;
392
393         default:
394             msg_Warn( p_access, "unimplemented query in control" );
395             return VLC_EGENERIC;
396
397     }
398     return VLC_SUCCESS;
399 }
400
401
402 static char *expand_path (const access_t *p_access, const char *path)
403 {
404     if (strncmp (path, "~/", 2) == 0)
405     {
406         char *res;
407
408          // TODO: we should also support the ~cmassiot/ syntax
409          if (asprintf (&res, "%s/%s", p_access->p_libvlc->psz_homedir, path + 2) == -1)
410              return NULL;
411          return res;
412     }
413
414 #if defined(WIN32)
415     if (!strcasecmp (p_access->psz_access, "file")
416       && ('/' == path[0]) && path[1] && (':' == path[2]) && ('/' == path[3]))
417         // Explorer can open path such as file:/C:/ or file:///C:/
418         // hence remove leading / if found
419         return strdup (path + 1);
420 #endif
421
422     return strdup (path);
423 }
424
425
426 /*****************************************************************************
427  * open_file: Opens a specific file
428  *****************************************************************************/
429 static int open_file (access_t *p_access, const char *psz_name)
430 {
431     char *path = expand_path (p_access, psz_name);
432
433 #ifdef UNDER_CE
434     p_sys->fd = utf8_fopen( path, "rb" );
435     if ( !p_sys->fd )
436     {
437         msg_Err( p_access, "cannot open file %s", psz_name );
438         intf_UserFatal( p_access, VLC_FALSE, _("File reading failed"), 
439                         _("VLC could not open file \"%s\"."), psz_name );
440         free (path);
441         return VLC_EGENERIC;
442     }
443
444     fseek( p_sys->fd, 0, SEEK_END );
445     p_access->info.i_size = ftell( p_sys->fd );
446     p_access->info.i_update |= INPUT_UPDATE_SIZE;
447     fseek( p_sys->fd, 0, SEEK_SET );
448 #else
449     int fd = utf8_open (path, O_RDONLY | O_NONBLOCK /* O_LARGEFILE*/, 0666);
450     if (fd == -1)
451     {
452         msg_Err (p_access, "cannot open file %s (%s)", psz_name,
453                  strerror (errno));
454         intf_UserFatal (p_access, VLC_FALSE, _("File reading failed"), 
455                         _("VLC could not open file \"%s\" (%s)."),
456                         psz_name, strerror (errno));
457         return -1;
458     }
459
460 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
461     /* We'd rather use any available memory for reading ahead
462      * than for caching what we've already seen/heard */
463     fcntl (fd, F_RDAHEAD, 1);
464     fcntl (fd, F_NOCACHE, 1);
465 # endif
466 #endif
467
468     return fd;
469 }