add_savefile: remove callback parameter
[vlc/asuraparaju-public.git] / modules / access / file.c
blob59f6c6790ed81aa00b2e6a67ed5814f0c4251993
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$
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 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include "fs.h"
32 #include <vlc_input.h>
33 #include <vlc_access.h>
34 #include <vlc_dialog.h>
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
57 #if defined( WIN32 )
58 # include <io.h>
59 # include <ctype.h>
60 # include <shlwapi.h>
61 #else
62 # include <unistd.h>
63 #endif
64 #include <dirent.h>
66 #if defined( WIN32 ) && !defined( UNDER_CE )
67 # ifdef lseek
68 # undef lseek
69 # endif
70 # define lseek _lseeki64
71 #elif defined( UNDER_CE )
72 /* FIXME the commandline on wince is a mess */
73 # define PathIsNetworkPathW(wpath) (! wcsncmp(wpath, L"\\\\", 2))
74 #endif
76 #include <vlc_fs.h>
77 #include <vlc_url.h>
79 struct access_sys_t
81 unsigned int i_nb_reads;
83 int fd;
85 /* */
86 unsigned caching;
87 bool b_pace_control;
90 #ifndef WIN32
91 static bool IsRemote (int fd)
93 #if defined (HAVE_FSTATVFS) && defined (MNT_LOCAL)
94 struct statvfs stf;
96 if (fstatvfs (fd, &stf))
97 return false;
98 /* fstatvfs() is in POSIX, but MNT_LOCAL is not */
99 return !(stf.f_flag & MNT_LOCAL);
101 #elif defined (HAVE_LINUX_MAGIC_H)
102 struct statfs stf;
104 if (fstatfs (fd, &stf))
105 return false;
107 switch ((unsigned long)stf.f_type)
109 case AFS_SUPER_MAGIC:
110 case CODA_SUPER_MAGIC:
111 case NCP_SUPER_MAGIC:
112 case NFS_SUPER_MAGIC:
113 case SMB_SUPER_MAGIC:
114 case 0xFF534D42 /*CIFS_MAGIC_NUMBER*/:
115 return true;
117 return false;
119 #else
120 (void)fd;
121 return false;
123 #endif
125 #endif
127 #ifndef HAVE_POSIX_FADVISE
128 # define posix_fadvise(fd, off, len, adv)
129 #endif
131 /*****************************************************************************
132 * Open: open the file
133 *****************************************************************************/
134 int Open( vlc_object_t *p_this )
136 access_t *p_access = (access_t*)p_this;
137 #ifdef WIN32
138 bool is_remote = false;
139 #endif
141 /* Open file */
142 int fd = -1;
144 if (!strcasecmp (p_access->psz_access, "fd"))
146 char *end;
147 int oldfd = strtol (p_access->psz_location, &end, 10);
149 if (*end == '\0')
150 fd = vlc_dup (oldfd);
151 else if (*end == '/' && end > p_access->psz_location)
153 char *name = decode_URI_duplicate (end - 1);
154 if (name != NULL)
156 name[0] = '.';
157 fd = vlc_openat (oldfd, name, O_RDONLY | O_NONBLOCK);
158 free (name);
162 else
164 const char *path = p_access->psz_filepath;
166 msg_Dbg (p_access, "opening file `%s'", path);
167 fd = vlc_open (path, O_RDONLY | O_NONBLOCK);
168 if (fd == -1)
170 msg_Err (p_access, "cannot open file %s (%m)", path);
171 dialog_Fatal (p_access, _("File reading failed"),
172 _("VLC could not open the file \"%s\"."), path);
175 #ifdef WIN32
176 wchar_t wpath[MAX_PATH+1];
177 if (MultiByteToWideChar (CP_UTF8, 0, path, -1,
178 wpath, MAX_PATH)
179 && PathIsNetworkPathW (wpath))
180 is_remote = true;
181 # define IsRemote( fd ) ((void)fd, is_remote)
182 #endif
184 if (fd == -1)
185 return VLC_EGENERIC;
187 struct stat st;
188 if (fstat (fd, &st))
190 msg_Err (p_access, "failed to read (%m)");
191 goto error;
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))
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
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->caching = var_InheritInteger (p_access, "file-caching");
220 if (IsRemote(fd))
221 p_sys->caching += var_InheritInteger (p_access, "network-caching");
222 p_sys->b_pace_control = true;
224 if (S_ISREG (st.st_mode))
225 p_access->info.i_size = st.st_size;
226 else if (!S_ISBLK (st.st_mode))
228 p_access->pf_seek = NoSeek;
229 p_sys->b_pace_control = strcasecmp (p_access->psz_access, "stream");
232 if (p_access->pf_seek != NoSeek)
234 /* Demuxers will need the beginning of the file for probing. */
235 posix_fadvise (fd, 0, 4096, POSIX_FADV_WILLNEED);
236 /* In most cases, we only read the file once. */
237 posix_fadvise (fd, 0, 0, POSIX_FADV_NOREUSE);
238 #if defined(HAVE_FCNTL)
239 /* We'd rather use any available memory for reading ahead
240 * than for caching what we've already seen/heard */
241 # if defined(F_RDAHEAD)
242 fcntl (fd, F_RDAHEAD, 1);
243 # endif
244 # if defined(F_NOCACHE)
245 fcntl (fd, F_NOCACHE, 1);
246 # endif
247 #endif
249 return VLC_SUCCESS;
251 error:
252 close (fd);
253 return VLC_EGENERIC;
256 /*****************************************************************************
257 * Close: close the target
258 *****************************************************************************/
259 void Close (vlc_object_t * p_this)
261 access_t *p_access = (access_t*)p_this;
263 if (p_access->pf_read == NULL)
265 DirClose (p_this);
266 return;
269 access_sys_t *p_sys = p_access->p_sys;
271 close (p_sys->fd);
272 free (p_sys);
276 #include <vlc_network.h>
278 /*****************************************************************************
279 * Read: standard read on a file descriptor.
280 *****************************************************************************/
281 ssize_t FileRead( access_t *p_access, uint8_t *p_buffer, size_t i_len )
283 access_sys_t *p_sys = p_access->p_sys;
284 int fd = p_sys->fd;
285 ssize_t i_ret;
287 #ifndef WIN32
288 if (p_access->pf_seek == NoSeek)
289 i_ret = net_Read (p_access, fd, NULL, p_buffer, i_len, false);
290 else
291 #endif
292 i_ret = read (fd, p_buffer, i_len);
294 if( i_ret < 0 )
296 switch (errno)
298 case EINTR:
299 case EAGAIN:
300 break;
302 default:
303 msg_Err (p_access, "failed to read (%m)");
304 dialog_Fatal (p_access, _("File reading failed"), "%s",
305 _("VLC could not read the file."));
306 p_access->info.b_eof = true;
307 return 0;
310 else if( i_ret > 0 )
311 p_access->info.i_pos += i_ret;
312 else
313 p_access->info.b_eof = true;
315 p_sys->i_nb_reads++;
317 if ((p_access->info.i_size && !(p_sys->i_nb_reads % INPUT_FSTAT_NB_READS))
318 || (p_access->info.i_pos > p_access->info.i_size))
320 #ifdef HAVE_SYS_STAT_H
321 struct stat st;
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;
329 #endif
331 return i_ret;
335 /*****************************************************************************
336 * Seek: seek to a specific location in a file
337 *****************************************************************************/
338 int FileSeek (access_t *p_access, uint64_t i_pos)
340 p_access->info.i_pos = i_pos;
341 p_access->info.b_eof = false;
343 lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
344 return VLC_SUCCESS;
347 int NoSeek (access_t *p_access, uint64_t i_pos)
349 /* assert(0); ?? */
350 (void) p_access; (void) i_pos;
351 return VLC_EGENERIC;
354 /*****************************************************************************
355 * Control:
356 *****************************************************************************/
357 int FileControl( access_t *p_access, int i_query, va_list args )
359 access_sys_t *p_sys = p_access->p_sys;
360 bool *pb_bool;
361 int64_t *pi_64;
363 switch( i_query )
365 /* */
366 case ACCESS_CAN_SEEK:
367 case ACCESS_CAN_FASTSEEK:
368 pb_bool = (bool*)va_arg( args, bool* );
369 *pb_bool = (p_access->pf_seek != NoSeek);
370 break;
372 case ACCESS_CAN_PAUSE:
373 case ACCESS_CAN_CONTROL_PACE:
374 pb_bool = (bool*)va_arg( args, bool* );
375 *pb_bool = p_sys->b_pace_control;
376 break;
378 /* */
379 case ACCESS_GET_PTS_DELAY:
380 pi_64 = (int64_t*)va_arg( args, int64_t * );
381 *pi_64 = p_sys->caching * INT64_C(1000);
382 break;
384 /* */
385 case ACCESS_SET_PAUSE_STATE:
386 /* Nothing to do */
387 break;
389 case ACCESS_GET_TITLE_INFO:
390 case ACCESS_SET_TITLE:
391 case ACCESS_SET_SEEKPOINT:
392 case ACCESS_SET_PRIVATE_ID_STATE:
393 case ACCESS_GET_META:
394 case ACCESS_GET_PRIVATE_ID_STATE:
395 case ACCESS_GET_CONTENT_TYPE:
396 return VLC_EGENERIC;
398 default:
399 msg_Warn( p_access, "unimplemented query %d in control", i_query );
400 return VLC_EGENERIC;
403 return VLC_SUCCESS;