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>
45 #if defined (__linux__)
47 #ifdef HAVE_LINUX_MAGIC_H
48 # include <linux/magic.h>
50 #elif defined (HAVE_SYS_MOUNT_H)
51 # include <sys/param.h>
52 # include <sys/mount.h>
64 #if defined( WIN32 ) && !defined( UNDER_CE )
68 # define lseek _lseeki64
69 #elif defined( UNDER_CE )
70 /* FIXME the commandline on wince is a mess */
71 # define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
79 unsigned int i_nb_reads;
89 static bool IsRemote (int fd)
94 if (fstatfs (fd, &stf))
97 #if defined(MNT_LOCAL)
98 return !(stf.f_flags & MNT_LOCAL);
101 # ifdef HAVE_LINUX_MAGIC_H
104 case AFS_SUPER_MAGIC:
105 case CODA_SUPER_MAGIC:
106 case NCP_SUPER_MAGIC:
107 case NFS_SUPER_MAGIC:
108 case SMB_SUPER_MAGIC:
109 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
115 #else /* !HAVE_FSTATFS */
123 #ifndef HAVE_POSIX_FADVISE
124 # define posix_fadvise(fd, off, len, adv)
127 /*****************************************************************************
128 * Open: open the file
129 *****************************************************************************/
130 int Open( vlc_object_t *p_this )
132 access_t *p_access = (access_t*)p_this;
133 const char *path = p_access->psz_path;
135 bool is_remote = false;
141 if (!strcasecmp (p_access->psz_access, "fd"))
144 int oldfd = strtol (path, &end, 10);
147 fd = vlc_dup (oldfd);
148 #ifdef HAVE_FDOPENDIR
149 else if (*end == '/' && end > path)
151 char *name = decode_URI_duplicate (end - 1);
155 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
163 msg_Dbg (p_access, "opening file `%s'", path);
164 fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
167 msg_Err (p_access, "cannot open file %s (%m)", path);
168 dialog_Fatal (p_access, _("File reading failed"),
169 _("VLC could not open the file \"%s\"."), path);
173 wchar_t wpath[MAX_PATH+1];
174 if (MultiByteToWideChar (CP_UTF8, 0, path, -1,
176 && PathIsNetworkPathW (wpath))
178 # define IsRemote( fd ) ((void)fd, is_remote)
187 msg_Err (p_access, "failed to read (%m)");
190 /* Directories can be opened and read from, but only readdir() knows
191 * how to parse the data. The directory plugin will do it. */
192 if (S_ISDIR (st.st_mode))
194 #ifdef HAVE_FDOPENDIR
195 DIR *handle = fdopendir (fd);
197 goto error; /* Uh? */
198 return DirInit (p_access, handle);
200 msg_Dbg (p_access, "ignoring directory");
205 access_sys_t *p_sys = malloc (sizeof (*p_sys));
206 if (unlikely(p_sys == NULL))
208 access_InitFields (p_access);
209 p_access->pf_read = FileRead;
210 p_access->pf_block = NULL;
211 p_access->pf_control = FileControl;
212 p_access->pf_seek = FileSeek;
213 p_access->p_sys = p_sys;
214 p_sys->i_nb_reads = 0;
216 p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
218 p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
219 p_sys->b_pace_control = true;
221 if (S_ISREG (st.st_mode))
222 p_access->info.i_size = st.st_size;
223 else if (!S_ISBLK (st.st_mode))
225 p_access->pf_seek = NoSeek;
226 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
229 if (p_access->pf_seek != NoSeek)
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);
241 # if defined(F_NOCACHE)
242 fcntl (fd, F_NOCACHE, 1);
253 /*****************************************************************************
254 * Close: close the target
255 *****************************************************************************/
256 void Close (vlc_object_t * p_this)
258 access_t *p_access = (access_t*)p_this;
260 if (p_access->pf_read == NULL)
266 access_sys_t *p_sys = p_access->p_sys;
273 #include <vlc_network.h>
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 )
280 access_sys_t *p_sys = p_access->p_sys;
285 if (p_access->pf_seek == NoSeek)
286 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
289 i_ret = read (fd, p_buffer, i_len);
300 msg_Err (p_access, "failed to read (%m)");
301 dialog_Fatal (p_access, _("File reading failed"), "%s",
302 _("VLC could not read the file."));
303 p_access->info.b_eof = true;
308 p_access->info.i_pos += i_ret;
310 p_access->info.b_eof = true;
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))
317 #ifdef HAVE_SYS_STAT_H
320 if ((fstat (fd, &st) == 0)
321 && (p_access->info.i_size != (uint64_t)st.st_size))
323 p_access->info.i_size = st.st_size;
324 p_access->info.i_update |= INPUT_UPDATE_SIZE;
332 /*****************************************************************************
333 * Seek: seek to a specific location in a file
334 *****************************************************************************/
335 int FileSeek (access_t *p_access, uint64_t i_pos)
337 p_access->info.i_pos = i_pos;
338 p_access->info.b_eof = false;
340 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
344 int NoSeek (access_t *p_access, uint64_t i_pos)
347 (void) p_access; (void) i_pos;
351 /*****************************************************************************
353 *****************************************************************************/
354 int FileControl( access_t *p_access, int i_query, va_list args )
356 access_sys_t *p_sys = p_access->p_sys;
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);
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;
376 case ACCESS_GET_PTS_DELAY:
377 pi_64 = (int64_t*)va_arg( args, int64_t * );
378 *pi_64 = p_sys->caching * INT64_C(1000);
382 case ACCESS_SET_PAUSE_STATE:
386 case ACCESS_GET_TITLE_INFO:
387 case ACCESS_SET_TITLE:
388 case ACCESS_SET_SEEKPOINT:
389 case ACCESS_SET_PRIVATE_ID_STATE:
390 case ACCESS_GET_META:
391 case ACCESS_GET_PRIVATE_ID_STATE:
392 case ACCESS_GET_CONTENT_TYPE:
396 msg_Warn( p_access, "unimplemented query %d in control", i_query );