1 /*****************************************************************************
2 * file.c: file input (file: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: file.c,v 1.3 2002/11/12 13:57:12 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <sys/types.h>
35 #include <vlc/input.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
49 static void Seek ( input_thread_t *, off_t );
50 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
52 /*****************************************************************************
54 *****************************************************************************/
56 set_description( _("Standard filesystem file reading") );
57 set_capability( "access", 50 );
58 add_shortcut( "file" );
59 add_shortcut( "stream" );
60 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open( vlc_object_t *p_this )
68 input_thread_t * p_input = (input_thread_t *)p_this;
69 char * psz_name = p_input->psz_name;
71 struct stat stat_info;
72 input_socket_t * p_access_data;
77 b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
79 if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
81 msg_Err( p_input, "cannot stat() file `%s' (%s)",
82 psz_name, strerror(errno));
86 p_input->pf_read = Read;
87 p_input->pf_set_program = input_SetProgram;
88 p_input->pf_set_area = NULL;
89 p_input->pf_seek = Seek;
91 vlc_mutex_lock( &p_input->stream.stream_lock );
93 if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
96 p_input->stream.b_pace_control = 0;
97 p_input->stream.b_seekable = 0;
98 p_input->stream.p_selected_area->i_size = 0;
103 p_input->stream.b_pace_control = 1;
107 p_input->stream.b_seekable = 0;
108 p_input->stream.p_selected_area->i_size = 0;
110 else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
111 || S_ISBLK(stat_info.st_mode) )
113 p_input->stream.b_seekable = 1;
114 p_input->stream.p_selected_area->i_size = stat_info.st_size;
116 else if( S_ISFIFO(stat_info.st_mode)
117 #if !defined( SYS_BEOS ) && !defined( WIN32 )
118 || S_ISSOCK(stat_info.st_mode)
122 p_input->stream.b_seekable = 0;
123 p_input->stream.p_selected_area->i_size = 0;
127 vlc_mutex_unlock( &p_input->stream.stream_lock );
128 msg_Err( p_input, "unknown file type for `%s'", psz_name );
133 p_input->stream.p_selected_area->i_tell = 0;
134 p_input->stream.i_method = INPUT_METHOD_FILE;
135 vlc_mutex_unlock( &p_input->stream.stream_lock );
137 msg_Dbg( p_input, "opening file `%s'", psz_name );
138 p_access_data = malloc( sizeof(input_socket_t) );
139 p_input->p_access_data = (void *)p_access_data;
140 if( p_access_data == NULL )
142 msg_Err( p_input, "out of memory" );
148 p_access_data->i_handle = 0;
150 else if( (p_access_data->i_handle = open( psz_name,
151 /*O_NONBLOCK | O_LARGEFILE*/ 0 )) == (-1) )
153 msg_Err( p_input, "cannot open file %s (%s)", psz_name,
155 free( p_access_data );
162 /*****************************************************************************
163 * Close: close the target
164 *****************************************************************************/
165 static void Close( vlc_object_t * p_this )
167 input_thread_t * p_input = (input_thread_t *)p_this;
168 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
170 msg_Info( p_input, "closing `%s/%s://%s'",
171 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
174 CloseHandle( (HANDLE)p_access_data->i_handle );
176 close( p_access_data->i_handle );
179 free( p_access_data );
182 /*****************************************************************************
183 * Read: standard read on a file descriptor.
184 *****************************************************************************/
185 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
187 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
191 if( !ReadFile( (HANDLE)p_access_data->i_handle, p_buffer, i_len,
192 (LPWORD)&i_ret, NULL ) )
197 i_ret = read( p_access_data->i_handle, p_buffer, i_len );
203 msg_Err( p_input, "read failed (%s)", strerror(errno) );
205 msg_Err( p_input, "read failed" );
212 /*****************************************************************************
213 * Seek: seek to a specific location in a file
214 *****************************************************************************/
215 static void Seek( input_thread_t * p_input, off_t i_pos )
217 #define S p_input->stream
218 input_socket_t * p_access_data = (input_socket_t *)p_input->p_access_data;
220 lseek( p_access_data->i_handle, i_pos, SEEK_SET );
222 vlc_mutex_lock( &S.stream_lock );
223 S.p_selected_area->i_tell = i_pos;
224 if( S.p_selected_area->i_tell > S.p_selected_area->i_size )
226 msg_Err( p_input, "seeking too far" );
227 S.p_selected_area->i_tell = S.p_selected_area->i_size;
229 else if( S.p_selected_area->i_tell < 0 )
231 msg_Err( p_input, "seeking too early" );
232 S.p_selected_area->i_tell = 0;
234 vlc_mutex_unlock( &S.stream_lock );