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