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 *****************************************************************************/
30 #include <vlc_common.h>
32 #include <vlc_input.h>
33 #include <vlc_access.h>
34 #include <vlc_dialog.h>
38 #include <sys/types.h>
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
46 # include <sys/statvfs.h>
47 # if defined (HAVE_SYS_MOUNT_H)
48 # include <sys/param.h>
49 # include <sys/mount.h>
52 #ifdef HAVE_LINUX_MAGIC_H
54 # include <linux/magic.h>
61 # include <vlc_charset.h>
62 #elif defined( __OS2__ )
69 #if defined( WIN32 ) && !defined( UNDER_CE )
73 # define lseek _lseeki64
74 #elif defined( UNDER_CE )
75 /* FIXME the commandline on wince is a mess */
76 # define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
84 unsigned int i_nb_reads;
92 #if !defined (WIN32) && !defined (__OS2__)
93 static bool IsRemote (int fd)
95 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
98 if (fstatvfs (fd, &stf))
100 /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
101 return !(stf.f_flag & MNT_LOCAL);
103 #elif defined (HAVE_LINUX_MAGIC_H)
106 if (fstatfs (fd, &stf))
109 switch ((unsigned long)stf.f_type)
111 case AFS_SUPER_MAGIC:
112 case CODA_SUPER_MAGIC:
113 case NCP_SUPER_MAGIC:
114 case NFS_SUPER_MAGIC:
115 case SMB_SUPER_MAGIC:
116 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
129 #ifndef HAVE_POSIX_FADVISE
130 # define posix_fadvise(fd, off, len, adv)
133 /*****************************************************************************
134 * Open: open the file
135 *****************************************************************************/
136 int Open( vlc_object_t *p_this )
138 access_t *p_access = (access_t*)p_this;
140 bool is_remote = false;
146 if (!strcasecmp (p_access->psz_access, "fd"))
149 int oldfd = strtol (p_access->psz_location, &end, 10);
152 fd = vlc_dup (oldfd);
153 else if (*end == '/' && end > p_access->psz_location)
155 char *name = decode_URI_duplicate (end - 1);
159 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
166 const char *path = p_access->psz_filepath;
168 msg_Dbg (p_access, "opening file `%s'", path);
169 fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
172 msg_Err (p_access, "cannot open file %s (%m)", path);
173 dialog_Fatal (p_access, _("File reading failed"),
174 _("VLC could not open the file \"%s\". (%m)"), path);
178 wchar_t *wpath = ToWide (path);
179 if (wpath != NULL && PathIsNetworkPathW (wpath))
182 # define IsRemote( fd ) ((void)fd, is_remote)
191 msg_Err (p_access, "failed to read (%m)");
194 /* Directories can be opened and read from, but only readdir() knows
195 * how to parse the data. The directory plugin will do it. */
196 if (S_ISDIR (st.st_mode))
198 #ifdef HAVE_FDOPENDIR
199 DIR *handle = fdopendir (fd);
201 goto error; /* Uh? */
202 return DirInit (p_access, handle);
204 msg_Dbg (p_access, "ignoring directory");
209 access_sys_t *p_sys = malloc (sizeof (*p_sys));
210 if (unlikely(p_sys == NULL))
212 access_InitFields (p_access);
213 p_access->pf_read = FileRead;
214 p_access->pf_block = NULL;
215 p_access->pf_control = FileControl;
216 p_access->pf_seek = FileSeek;
217 p_access->p_sys = p_sys;
218 p_sys->i_nb_reads = 0;
220 p_sys->b_pace_control = true;
222 if (S_ISREG (st.st_mode))
223 p_access->info.i_size = st.st_size;
224 else if (!S_ISBLK (st.st_mode))
226 p_access->pf_seek = NoSeek;
227 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
230 if (p_access->pf_seek != NoSeek)
232 /* Demuxers will need the beginning of the file for probing. */
233 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
234 /* In most cases, we only read the file once. */
235 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
236 #if defined(HAVE_FCNTL)
237 /* We'd rather use any available memory for reading ahead
238 * than for caching what we've already seen/heard */
239 # if defined(F_RDAHEAD)
240 fcntl (fd, F_RDAHEAD, 1);
242 # if defined(F_NOCACHE)
243 fcntl (fd, F_NOCACHE, 1);
254 /*****************************************************************************
255 * Close: close the target
256 *****************************************************************************/
257 void Close (vlc_object_t * p_this)
259 access_t *p_access = (access_t*)p_this;
261 if (p_access->pf_read == NULL)
267 access_sys_t *p_sys = p_access->p_sys;
274 #include <vlc_network.h>
276 /*****************************************************************************
277 * Read: standard read on a file descriptor.
278 *****************************************************************************/
279 ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
281 access_sys_t *p_sys = p_access->p_sys;
285 #if !defined (WIN32) && !defined (__OS2__)
286 if (p_access->pf_seek == NoSeek)
287 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
290 i_ret = read (fd, p_buffer, i_len);
301 msg_Err (p_access, "failed to read (%m)");
302 dialog_Fatal (p_access, _("File reading failed"), "%s (%m)",
303 _("VLC could not read the file."));
304 p_access->info.b_eof = true;
309 p_access->info.i_pos += i_ret;
311 p_access->info.b_eof = true;
315 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
316 || (p_access->info.i_pos > p_access->info.i_size))
318 #ifdef HAVE_SYS_STAT_H
321 if ((fstat (fd, &st) == 0)
322 && (p_access->info.i_size != (uint64_t)st.st_size))
324 p_access->info.i_size = st.st_size;
325 p_access->info.i_update |= INPUT_UPDATE_SIZE;
333 /*****************************************************************************
334 * Seek: seek to a specific location in a file
335 *****************************************************************************/
336 int FileSeek (access_t *p_access, uint64_t i_pos)
338 p_access->info.i_pos = i_pos;
339 p_access->info.b_eof = false;
341 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
345 int NoSeek (access_t *p_access, uint64_t i_pos)
348 (void) p_access; (void) i_pos;
352 /*****************************************************************************
354 *****************************************************************************/
355 int FileControl( access_t *p_access, int i_query, va_list args )
357 access_sys_t *p_sys = p_access->p_sys;
364 case ACCESS_CAN_SEEK:
365 case ACCESS_CAN_FASTSEEK:
366 pb_bool = (bool*)va_arg( args, bool* );
367 *pb_bool = (p_access->pf_seek != NoSeek);
370 case ACCESS_CAN_PAUSE:
371 case ACCESS_CAN_CONTROL_PACE:
372 pb_bool = (bool*)va_arg( args, bool* );
373 *pb_bool = p_sys->b_pace_control;
377 case ACCESS_GET_PTS_DELAY:
378 pi_64 = (int64_t*)va_arg( args, int64_t * );
379 if (IsRemote (p_sys->fd))
380 *pi_64 = var_InheritInteger (p_access, "network-caching");
382 *pi_64 = var_InheritInteger (p_access, "file-caching");
387 case ACCESS_SET_PAUSE_STATE:
391 case ACCESS_GET_TITLE_INFO:
392 case ACCESS_SET_TITLE:
393 case ACCESS_SET_SEEKPOINT:
394 case ACCESS_SET_PRIVATE_ID_STATE:
395 case ACCESS_GET_META:
396 case ACCESS_GET_PRIVATE_ID_STATE:
397 case ACCESS_GET_CONTENT_TYPE:
401 msg_Warn( p_access, "unimplemented query %d in control", i_query );