]> git.sesse.net Git - vlc/blob - modules/access/file.c
dhsow: missing white space (breaks PO files)
[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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include "fs.h"
32 #include <vlc_input.h>
33 #include <vlc_access.h>
34 #include <vlc_dialog.h>
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <sys/types.h>
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 #ifdef HAVE_FSTATVFS
46 #   include <sys/statvfs.h>
47 #   if defined (HAVE_SYS_MOUNT_H)
48 #      include <sys/param.h>
49 #      include <sys/mount.h>
50 #   endif
51 #endif
52 #ifdef HAVE_LINUX_MAGIC_H
53 #   include <sys/vfs.h>
54 #   include <linux/magic.h>
55 #endif
56
57 #if defined( WIN32 )
58 #   include <io.h>
59 #   include <ctype.h>
60 #   include <shlwapi.h>
61 #   include <vlc_charset.h>
62 #else
63 #   include <unistd.h>
64 #endif
65 #include <dirent.h>
66
67 #if defined( WIN32 ) && !defined( UNDER_CE )
68 #   ifdef lseek
69 #      undef lseek
70 #   endif
71 #   define lseek _lseeki64
72 #endif
73
74 #include <vlc_fs.h>
75 #include <vlc_url.h>
76
77 struct access_sys_t
78 {
79     unsigned int i_nb_reads;
80
81     int fd;
82
83     /* */
84     bool b_pace_control;
85 };
86
87 #if !defined (WIN32) && !defined (__OS2__)
88 static bool IsRemote (int fd)
89 {
90 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
91     struct statvfs stf;
92
93     if (fstatvfs (fd, &stf))
94         return false;
95     /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
96     return !(stf.f_flag & MNT_LOCAL);
97
98 #elif defined (HAVE_LINUX_MAGIC_H)
99     struct statfs stf;
100
101     if (fstatfs (fd, &stf))
102         return false;
103
104     switch ((unsigned long)stf.f_type)
105     {
106         case AFS_SUPER_MAGIC:
107         case CODA_SUPER_MAGIC:
108         case NCP_SUPER_MAGIC:
109         case NFS_SUPER_MAGIC:
110         case SMB_SUPER_MAGIC:
111         case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
112             return true;
113     }
114     return false;
115
116 #else
117     (void)fd;
118     return false;
119
120 #endif
121 }
122 # define IsRemote(fd,path) IsRemote(fd)
123
124 #else /* WIN32 || __OS2__ */
125 static bool IsRemote (const char *path)
126 {
127 # if !defined(UNDER_CE) && !defined(__OS2__)
128     wchar_t *wpath = ToWide (path);
129     bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
130     free (wpath);
131     return is_remote;
132 # else
133     return (! strncmp(path, "\\\\", 2));
134 # endif
135 }
136 # define IsRemote(fd,path) IsRemote(path)
137 #endif
138
139 #ifndef HAVE_POSIX_FADVISE
140 # define posix_fadvise(fd, off, len, adv)
141 #endif
142
143 /*****************************************************************************
144  * FileOpen: open the file
145  *****************************************************************************/
146 int FileOpen( vlc_object_t *p_this )
147 {
148     access_t     *p_access = (access_t*)p_this;
149
150     /* Open file */
151     int fd = -1;
152
153     if (!strcasecmp (p_access->psz_access, "fd"))
154     {
155         char *end;
156         int oldfd = strtol (p_access->psz_location, &end, 10);
157
158         if (*end == '\0')
159             fd = vlc_dup (oldfd);
160         else if (*end == '/' && end > p_access->psz_location)
161         {
162             char *name = decode_URI_duplicate (end - 1);
163             if (name != NULL)
164             {
165                 name[0] = '.';
166                 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
167                 free (name);
168             }
169         }
170     }
171     else
172     {
173         const char *path = p_access->psz_filepath;
174
175         msg_Dbg (p_access, "opening file `%s'", path);
176         fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
177         if (fd == -1)
178         {
179             msg_Err (p_access, "cannot open file %s (%m)", path);
180             dialog_Fatal (p_access, _("File reading failed"),
181                           _("VLC could not open the file \"%s\". (%m)"), path);
182         }
183     }
184     if (fd == -1)
185         return VLC_EGENERIC;
186
187     struct stat st;
188     if (fstat (fd, &st))
189     {
190         msg_Err (p_access, "failed to read (%m)");
191         goto error;
192     }
193     /* Directories can be opened and read from, but only readdir() knows
194      * how to parse the data. The directory plugin will do it. */
195     if (S_ISDIR (st.st_mode))
196     {
197 #ifdef HAVE_FDOPENDIR
198         DIR *handle = fdopendir (fd);
199         if (handle == NULL)
200             goto error; /* Uh? */
201         return DirInit (p_access, handle);
202 #else
203         msg_Dbg (p_access, "ignoring directory");
204         goto error;
205 #endif
206     }
207
208     access_sys_t *p_sys = malloc (sizeof (*p_sys));
209     if (unlikely(p_sys == NULL))
210         goto error;
211     access_InitFields (p_access);
212     p_access->pf_read = FileRead;
213     p_access->pf_block = NULL;
214     p_access->pf_control = FileControl;
215     p_access->pf_seek = FileSeek;
216     p_access->p_sys = p_sys;
217     p_sys->i_nb_reads = 0;
218     p_sys->fd = fd;
219     p_sys->b_pace_control = true;
220
221     if (S_ISREG (st.st_mode))
222         p_access->info.i_size = st.st_size;
223     else if (!S_ISBLK (st.st_mode))
224     {
225         p_access->pf_seek = NoSeek;
226         p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
227     }
228
229     if (p_access->pf_seek != NoSeek)
230     {
231         /* Demuxers will need the beginning of the file for probing. */
232         posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
233         /* In most cases, we only read the file once. */
234         posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
235 #if defined(HAVE_FCNTL)
236         /* We'd rather use any available memory for reading ahead
237          * than for caching what we've already seen/heard */
238 # if defined(F_RDAHEAD)
239         fcntl (fd, F_RDAHEAD, 1);
240 # endif
241 # if defined(F_NOCACHE)
242         fcntl (fd, F_NOCACHE, 1);
243 # endif
244 #endif
245     }
246     return VLC_SUCCESS;
247
248 error:
249     close (fd);
250     return VLC_EGENERIC;
251 }
252
253 /*****************************************************************************
254  * FileClose: close the target
255  *****************************************************************************/
256 void FileClose (vlc_object_t * p_this)
257 {
258     access_t     *p_access = (access_t*)p_this;
259
260     if (p_access->pf_read == NULL)
261     {
262         DirClose (p_this);
263         return;
264     }
265
266     access_sys_t *p_sys = p_access->p_sys;
267
268     close (p_sys->fd);
269     free (p_sys);
270 }
271
272
273 #include <vlc_network.h>
274
275 /*****************************************************************************
276  * Read: standard read on a file descriptor.
277  *****************************************************************************/
278 ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
279 {
280     access_sys_t *p_sys = p_access->p_sys;
281     int fd = p_sys->fd;
282     ssize_t i_ret;
283
284 #if !defined (WIN32) && !defined (__OS2__)
285     if (p_access->pf_seek == NoSeek)
286         i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
287     else
288 #endif
289         i_ret = read (fd, p_buffer, i_len);
290
291     if( i_ret < 0 )
292     {
293         switch (errno)
294         {
295             case EINTR:
296             case EAGAIN:
297                 break;
298
299             default:
300                 msg_Err (p_access, "failed to read (%m)");
301                 dialog_Fatal (p_access, _("File reading failed"),
302                               _("VLC could not read the file (%m)."));
303                 p_access->info.b_eof = true;
304                 return 0;
305         }
306     }
307     else if( i_ret > 0 )
308         p_access->info.i_pos += i_ret;
309     else
310         p_access->info.b_eof = true;
311
312     p_sys->i_nb_reads++;
313
314     if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
315      || (p_access->info.i_pos > p_access->info.i_size))
316     {
317 #ifdef HAVE_SYS_STAT_H
318         struct stat st;
319
320         if ((fstat (fd, &st) == 0)
321          && (p_access->info.i_size != (uint64_t)st.st_size))
322         {
323             p_access->info.i_size = st.st_size;
324             p_access->info.i_update |= INPUT_UPDATE_SIZE;
325         }
326 #endif
327     }
328     return i_ret;
329 }
330
331
332 /*****************************************************************************
333  * Seek: seek to a specific location in a file
334  *****************************************************************************/
335 int FileSeek (access_t *p_access, uint64_t i_pos)
336 {
337     p_access->info.i_pos = i_pos;
338     p_access->info.b_eof = false;
339
340     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
341     return VLC_SUCCESS;
342 }
343
344 int NoSeek (access_t *p_access, uint64_t i_pos)
345 {
346     /* assert(0); ?? */
347     (void) p_access; (void) i_pos;
348     return VLC_EGENERIC;
349 }
350
351 /*****************************************************************************
352  * Control:
353  *****************************************************************************/
354 int FileControl( access_t *p_access, int i_query, va_list args )
355 {
356     access_sys_t *p_sys = p_access->p_sys;
357     bool    *pb_bool;
358     int64_t *pi_64;
359
360     switch( i_query )
361     {
362         /* */
363         case ACCESS_CAN_SEEK:
364         case ACCESS_CAN_FASTSEEK:
365             pb_bool = (bool*)va_arg( args, bool* );
366             *pb_bool = (p_access->pf_seek != NoSeek);
367             break;
368
369         case ACCESS_CAN_PAUSE:
370         case ACCESS_CAN_CONTROL_PACE:
371             pb_bool = (bool*)va_arg( args, bool* );
372             *pb_bool = p_sys->b_pace_control;
373             break;
374
375         /* */
376         case ACCESS_GET_PTS_DELAY:
377             pi_64 = (int64_t*)va_arg( args, int64_t * );
378             if (IsRemote (p_sys->fd, p_access->psz_filepath))
379                 *pi_64 = var_InheritInteger (p_access, "network-caching");
380             else
381                 *pi_64 = var_InheritInteger (p_access, "file-caching");
382             *pi_64 *= 1000;
383             break;
384
385         /* */
386         case ACCESS_SET_PAUSE_STATE:
387             /* Nothing to do */
388             break;
389
390         case ACCESS_GET_TITLE_INFO:
391         case ACCESS_SET_TITLE:
392         case ACCESS_SET_SEEKPOINT:
393         case ACCESS_SET_PRIVATE_ID_STATE:
394         case ACCESS_GET_META:
395         case ACCESS_GET_PRIVATE_ID_STATE:
396         case ACCESS_GET_CONTENT_TYPE:
397             return VLC_EGENERIC;
398
399         default:
400             msg_Warn( p_access, "unimplemented query %d in control", i_query );
401             return VLC_EGENERIC;
402
403     }
404     return VLC_SUCCESS;
405 }