]> git.sesse.net Git - vlc/blob - modules/access/file.c
cdda4d139cc896d933c9302c7220d6d0e3cbdfa8
[vlc] / modules / access / file.c
1 /*****************************************************************************
2  * file.c: file input (file: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: file.c,v 1.1 2002/08/04 17:23:41 sam Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/input.h>
36
37 #ifdef HAVE_UNISTD_H
38 #   include <unistd.h>
39 #elif defined( _MSC_VER ) && defined( _WIN32 )
40 #   include <io.h>
41 #endif
42
43 /*****************************************************************************
44  * Open: open the file
45  *****************************************************************************/
46 static int Open( vlc_object_t *p_this )
47 {
48     input_thread_t *    p_input = (input_thread_t *)p_this;
49     char *              psz_name = p_input->psz_name;
50     int                 i_stat;
51     struct stat         stat_info;                                              
52     input_socket_t *    p_access_data;
53     vlc_bool_t          b_stdin;
54
55     p_input->i_mtu = 0;
56
57     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
58
59     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
60     {
61         msg_Err( p_input, "cannot stat() file `%s' (%s)",
62                           psz_name, strerror(errno));
63         return( -1 );
64     }
65
66     p_input->pf_read = input_FDRead;
67     p_input->pf_set_program = input_SetProgram;
68     p_input->pf_set_area = NULL;
69     p_input->pf_seek = input_FDSeek;
70
71     vlc_mutex_lock( &p_input->stream.stream_lock );
72
73     if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
74     {
75         /* stream:%s */
76         p_input->stream.b_pace_control = 0;
77         p_input->stream.b_seekable = 0;
78         p_input->stream.p_selected_area->i_size = 0;
79     }
80     else
81     {
82         /* file:%s or %s */
83         p_input->stream.b_pace_control = 1;
84
85         if( b_stdin )
86         {
87             p_input->stream.b_seekable = 0;
88             p_input->stream.p_selected_area->i_size = 0;
89         }
90         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
91                   || S_ISBLK(stat_info.st_mode) )
92         {
93             p_input->stream.b_seekable = 1;
94             p_input->stream.p_selected_area->i_size = stat_info.st_size;
95         }
96         else if( S_ISFIFO(stat_info.st_mode)
97 #if !defined( SYS_BEOS ) && !defined( WIN32 )
98                   || S_ISSOCK(stat_info.st_mode)
99 #endif
100                )
101         {
102             p_input->stream.b_seekable = 0;
103             p_input->stream.p_selected_area->i_size = 0;
104         }
105         else
106         {
107             vlc_mutex_unlock( &p_input->stream.stream_lock );
108             msg_Err( p_input, "unknown file type for `%s'", psz_name );
109             return( -1 );
110         }
111     }
112  
113     p_input->stream.p_selected_area->i_tell = 0;
114     p_input->stream.i_method = INPUT_METHOD_FILE;
115     vlc_mutex_unlock( &p_input->stream.stream_lock );
116  
117     msg_Dbg( p_input, "opening file `%s'", psz_name );
118     p_access_data = malloc( sizeof(input_socket_t) );
119     p_input->p_access_data = (void *)p_access_data;
120     if( p_access_data == NULL )
121     {
122         msg_Err( p_input, "out of memory" );
123         return( -1 );
124     }
125
126     if( b_stdin )
127     {
128         p_access_data->i_handle = 0;
129     }
130     else if( (p_access_data->i_handle = open( psz_name,
131                                    /*O_NONBLOCK | O_LARGEFILE*/ 0 )) == (-1) )
132     {
133         msg_Err( p_input, "cannot open file %s (%s)", psz_name,
134                           strerror(errno) );
135         free( p_access_data );
136         return( -1 );
137     }
138
139     return( 0 );
140 }
141
142 /*****************************************************************************
143  * Module descriptor
144  *****************************************************************************/
145 vlc_module_begin();
146     set_description( _("Standard filesystem file reading") );
147     set_capability( "access", 50 );
148     add_shortcut( "stream" );
149     set_callbacks( Open, __input_FDClose );
150 vlc_module_end();
151