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 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
47 #if defined (__linux__)
49 #ifdef HAVE_LINUX_MAGIC_H
50 # include <linux/magic.h>
52 #elif defined (HAVE_SYS_MOUNT_H)
53 # include <sys/param.h>
54 # include <sys/mount.h>
66 #if defined( WIN32 ) && !defined( UNDER_CE )
70 # define lseek _lseeki64
71 #elif defined( UNDER_CE )
72 /* FIXME the commandline on wince is a mess */
74 # define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
77 #include <vlc_charset.h>
81 unsigned int i_nb_reads;
91 static bool IsRemote (int fd)
96 if (fstatfs (fd, &stf))
99 #if defined(MNT_LOCAL)
100 return !(stf.f_flags & MNT_LOCAL);
103 # ifdef HAVE_LINUX_MAGIC_H
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*/:
117 #else /* !HAVE_FSTATFS */
125 #ifndef HAVE_POSIX_FADVISE
126 # define posix_fadvise(fd, off, len, adv)
129 /*****************************************************************************
130 * Open: open the file
131 *****************************************************************************/
132 int Open( vlc_object_t *p_this )
134 access_t *p_access = (access_t*)p_this;
135 const char *path = p_access->psz_path;
137 bool is_remote = false;
143 if (!strcasecmp (p_access->psz_access, "fd"))
144 fd = dup (atoi (path));
145 else if (!strcmp (path, "-"))
149 msg_Dbg (p_access, "opening file `%s'", path);
150 fd = utf8_open (path, O_RDONLY | O_NONBLOCK);
153 msg_Err (p_access, "cannot open file %s (%m)", path);
154 dialog_Fatal (p_access, _("File reading failed"),
155 _("VLC could not open the file \"%s\"."), path);
159 wchar_t wpath[MAX_PATH+1];
160 if (MultiByteToWideChar (CP_UTF8, 0, path, -1,
162 && PathIsNetworkPathW (wpath))
164 # define IsRemote( fd ) ((void)fd, is_remote)
173 msg_Err (p_access, "failed to read (%m)");
176 /* Directories can be opened and read from, but only readdir() knows
177 * how to parse the data. The directory plugin will do it. */
178 if (S_ISDIR (st.st_mode))
180 msg_Dbg (p_access, "ignoring directory");
184 access_sys_t *p_sys = malloc (sizeof (*p_sys));
185 if (unlikely(p_sys == NULL))
187 access_InitFields (p_access);
188 p_access->pf_read = FileRead;
189 p_access->pf_block = NULL;
190 p_access->pf_control = FileControl;
191 p_access->pf_seek = FileSeek;
192 p_access->p_sys = p_sys;
193 p_sys->i_nb_reads = 0;
195 p_sys->caching = var_CreateGetInteger (p_access, "file-caching");
197 p_sys->caching += var_CreateGetInteger (p_access, "network-caching");
198 p_sys->b_pace_control = true;
200 if (S_ISREG (st.st_mode))
201 p_access->info.i_size = st.st_size;
202 else if (!S_ISBLK (st.st_mode))
204 p_access->pf_seek = NoSeek;
205 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
208 if (p_access->pf_seek != NoSeek)
210 /* Demuxers will need the beginning of the file for probing. */
211 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
212 /* In most cases, we only read the file once. */
213 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
214 #if defined(HAVE_FCNTL)
215 /* We'd rather use any available memory for reading ahead
216 * than for caching what we've already seen/heard */
217 # if defined(F_RDAHEAD)
218 fcntl (fd, F_RDAHEAD, 1);
220 # if defined(F_NOCACHE)
221 fcntl (fd, F_NOCACHE, 1);
232 /*****************************************************************************
233 * Close: close the target
234 *****************************************************************************/
235 void Close (vlc_object_t * p_this)
237 access_t *p_access = (access_t*)p_this;
238 access_sys_t *p_sys = p_access->p_sys;
245 #include <vlc_network.h>
247 /*****************************************************************************
248 * Read: standard read on a file descriptor.
249 *****************************************************************************/
250 ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
252 access_sys_t *p_sys = p_access->p_sys;
257 if (p_access->pf_seek == NoSeek)
258 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
261 i_ret = read (fd, p_buffer, i_len);
272 msg_Err (p_access, "failed to read (%m)");
273 dialog_Fatal (p_access, _("File reading failed"), "%s",
274 _("VLC could not read the file."));
275 p_access->info.b_eof = true;
280 p_access->info.i_pos += i_ret;
282 p_access->info.b_eof = true;
286 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
287 || (p_access->info.i_pos > p_access->info.i_size))
289 #ifdef HAVE_SYS_STAT_H
292 if ((fstat (fd, &st) == 0)
293 && (p_access->info.i_size != st.st_size))
295 p_access->info.i_size = st.st_size;
296 p_access->info.i_update |= INPUT_UPDATE_SIZE;
304 /*****************************************************************************
305 * Seek: seek to a specific location in a file
306 *****************************************************************************/
307 int FileSeek (access_t *p_access, int64_t i_pos)
309 p_access->info.i_pos = i_pos;
310 p_access->info.b_eof = false;
312 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
316 int NoSeek (access_t *p_access, int64_t i_pos)
319 (void) p_access; (void) i_pos;
323 /*****************************************************************************
325 *****************************************************************************/
326 int FileControl( access_t *p_access, int i_query, va_list args )
328 access_sys_t *p_sys = p_access->p_sys;
335 case ACCESS_CAN_SEEK:
336 case ACCESS_CAN_FASTSEEK:
337 pb_bool = (bool*)va_arg( args, bool* );
338 *pb_bool = (p_access->pf_seek != NoSeek);
341 case ACCESS_CAN_PAUSE:
342 case ACCESS_CAN_CONTROL_PACE:
343 pb_bool = (bool*)va_arg( args, bool* );
344 *pb_bool = p_sys->b_pace_control;
348 case ACCESS_GET_PTS_DELAY:
349 pi_64 = (int64_t*)va_arg( args, int64_t * );
350 *pi_64 = p_sys->caching * INT64_C(1000);
354 case ACCESS_SET_PAUSE_STATE:
358 case ACCESS_GET_TITLE_INFO:
359 case ACCESS_SET_TITLE:
360 case ACCESS_SET_SEEKPOINT:
361 case ACCESS_SET_PRIVATE_ID_STATE:
362 case ACCESS_GET_META:
363 case ACCESS_GET_PRIVATE_ID_STATE:
364 case ACCESS_GET_CONTENT_TYPE:
368 msg_Warn( p_access, "unimplemented query %d in control", i_query );