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