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
8 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * Rémi Denis-Courmont <rem # videolan # org>
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.
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.
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 *****************************************************************************/
32 #include <sys/types.h>
36 # include <sys/statvfs.h>
37 # if defined (HAVE_SYS_MOUNT_H)
38 # include <sys/param.h>
39 # include <sys/mount.h>
42 #ifdef HAVE_LINUX_MAGIC_H
44 # include <linux/magic.h>
56 #include <vlc_common.h>
58 #include <vlc_input.h>
59 #include <vlc_access.h>
60 #include <vlc_dialog.h>
62 # include <vlc_charset.h>
69 unsigned int i_nb_reads;
77 #if !defined (WIN32) && !defined (__OS2__)
78 static bool IsRemote (int fd)
80 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
83 if (fstatvfs (fd, &stf))
85 /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
86 return !(stf.f_flag & MNT_LOCAL);
88 #elif defined (HAVE_LINUX_MAGIC_H)
91 if (fstatfs (fd, &stf))
94 switch ((unsigned long)stf.f_type)
97 case CODA_SUPER_MAGIC:
100 case SMB_SUPER_MAGIC:
101 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
112 # define IsRemote(fd,path) IsRemote(fd)
114 #else /* WIN32 || __OS2__ */
115 static bool IsRemote (const char *path)
117 # if !defined(__OS2__)
118 wchar_t *wpath = ToWide (path);
119 bool is_remote = (wpath != NULL && PathIsNetworkPathW (wpath));
123 return (! strncmp(path, "\\\\", 2));
126 # define IsRemote(fd,path) IsRemote(path)
129 #ifndef HAVE_POSIX_FADVISE
130 # define posix_fadvise(fd, off, len, adv)
133 /*****************************************************************************
134 * FileOpen: open the file
135 *****************************************************************************/
136 int FileOpen( vlc_object_t *p_this )
138 access_t *p_access = (access_t*)p_this;
143 if (!strcasecmp (p_access->psz_access, "fd"))
146 int oldfd = strtol (p_access->psz_location, &end, 10);
149 fd = vlc_dup (oldfd);
150 else if (*end == '/' && end > p_access->psz_location)
152 char *name = decode_URI_duplicate (end - 1);
156 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
163 const char *path = p_access->psz_filepath;
165 if (unlikely(path == NULL))
167 msg_Dbg (p_access, "opening file `%s'", path);
168 fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
171 msg_Err (p_access, "cannot open file %s (%m)", path);
172 dialog_Fatal (p_access, _("File reading failed"),
173 _("VLC could not open the file \"%s\". (%m)"), path);
182 msg_Err (p_access, "failed to read (%m)");
187 int flags = fcntl (fd, F_GETFL);
188 if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode))
189 /* Force non-blocking mode where applicable (fd://) */
192 /* Force blocking mode when not useful or not specified */
193 flags &= ~O_NONBLOCK;
194 fcntl (fd, F_SETFL, flags);
197 /* Directories can be opened and read from, but only readdir() knows
198 * how to parse the data. The directory plugin will do it. */
199 if (S_ISDIR (st.st_mode))
201 #ifdef HAVE_FDOPENDIR
202 DIR *handle = fdopendir (fd);
204 goto error; /* Uh? */
205 return DirInit (p_access, handle);
207 msg_Dbg (p_access, "ignoring directory");
212 access_sys_t *p_sys = malloc (sizeof (*p_sys));
213 if (unlikely(p_sys == NULL))
215 access_InitFields (p_access);
216 p_access->pf_read = FileRead;
217 p_access->pf_block = NULL;
218 p_access->pf_control = FileControl;
219 p_access->pf_seek = FileSeek;
220 p_access->p_sys = p_sys;
221 p_sys->i_nb_reads = 0;
223 p_sys->b_pace_control = true;
225 if (S_ISREG (st.st_mode))
226 p_access->info.i_size = st.st_size;
227 else if (!S_ISBLK (st.st_mode))
229 p_access->pf_seek = NoSeek;
230 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
233 if (p_access->pf_seek != NoSeek)
235 /* Demuxers will need the beginning of the file for probing. */
236 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
237 /* In most cases, we only read the file once. */
238 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
239 #if defined(HAVE_FCNTL)
240 /* We'd rather use any available memory for reading ahead
241 * than for caching what we've already seen/heard */
242 # if defined(F_RDAHEAD)
243 fcntl (fd, F_RDAHEAD, 1);
245 # if defined(F_NOCACHE)
246 fcntl (fd, F_NOCACHE, 1);
257 /*****************************************************************************
258 * FileClose: close the target
259 *****************************************************************************/
260 void FileClose (vlc_object_t * p_this)
262 access_t *p_access = (access_t*)p_this;
264 if (p_access->pf_read == NULL)
270 access_sys_t *p_sys = p_access->p_sys;
277 #include <vlc_network.h>
279 /*****************************************************************************
280 * Read: standard read on a file descriptor.
281 *****************************************************************************/
282 ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
284 access_sys_t *p_sys = p_access->p_sys;
288 #if !defined (WIN32) && !defined (__OS2__)
289 if (p_access->pf_seek == NoSeek)
290 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
293 i_ret = read (fd, p_buffer, i_len);
304 msg_Err (p_access, "failed to read (%m)");
305 dialog_Fatal (p_access, _("File reading failed"),
306 _("VLC could not read the file (%m)."));
307 p_access->info.b_eof = true;
312 p_access->info.i_pos += i_ret;
314 p_access->info.b_eof = true;
318 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
319 || (p_access->info.i_pos > p_access->info.i_size))
323 if ((fstat (fd, &st) == 0)
324 && (p_access->info.i_size != (uint64_t)st.st_size))
326 p_access->info.i_size = st.st_size;
327 p_access->info.i_update |= INPUT_UPDATE_SIZE;
334 /*****************************************************************************
335 * Seek: seek to a specific location in a file
336 *****************************************************************************/
337 int FileSeek (access_t *p_access, uint64_t i_pos)
339 p_access->info.i_pos = i_pos;
340 p_access->info.b_eof = false;
342 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
346 int NoSeek (access_t *p_access, uint64_t i_pos)
349 (void) p_access; (void) i_pos;
353 /*****************************************************************************
355 *****************************************************************************/
356 int FileControl( access_t *p_access, int i_query, va_list args )
358 access_sys_t *p_sys = p_access->p_sys;
365 case ACCESS_CAN_SEEK:
366 case ACCESS_CAN_FASTSEEK:
367 pb_bool = (bool*)va_arg( args, bool* );
368 *pb_bool = (p_access->pf_seek != NoSeek);
371 case ACCESS_CAN_PAUSE:
372 case ACCESS_CAN_CONTROL_PACE:
373 pb_bool = (bool*)va_arg( args, bool* );
374 *pb_bool = p_sys->b_pace_control;
378 case ACCESS_GET_PTS_DELAY:
379 pi_64 = (int64_t*)va_arg( args, int64_t * );
380 if (IsRemote (p_sys->fd, p_access->psz_filepath))
381 *pi_64 = var_InheritInteger (p_access, "network-caching");
383 *pi_64 = var_InheritInteger (p_access, "file-caching");
388 case ACCESS_SET_PAUSE_STATE:
392 case ACCESS_GET_TITLE_INFO:
393 case ACCESS_SET_TITLE:
394 case ACCESS_SET_SEEKPOINT:
395 case ACCESS_SET_PRIVATE_ID_STATE:
396 case ACCESS_GET_META:
397 case ACCESS_GET_PRIVATE_ID_STATE:
398 case ACCESS_GET_CONTENT_TYPE:
402 msg_Warn( p_access, "unimplemented query %d in control", i_query );