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