]> git.sesse.net Git - vlc/blob - plugins/access/file.c
* ALL: the first libvlc commit.
[vlc] / plugins / 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.7 2002/06/01 12:31:58 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  * Local prototypes
45  *****************************************************************************/
46 static void input_getfunctions( function_list_t * );
47 static int  FileOpen       ( input_thread_t * );
48
49 /*****************************************************************************
50  * Build configuration tree.
51  *****************************************************************************/
52 MODULE_CONFIG_START
53 MODULE_CONFIG_STOP
54  
55 MODULE_INIT_START
56     SET_DESCRIPTION( _("Standard filesystem file reading") )
57     ADD_CAPABILITY( ACCESS, 50 )
58     ADD_SHORTCUT( "stream" )
59 MODULE_INIT_STOP
60  
61 MODULE_ACTIVATE_START
62     input_getfunctions( &p_module->p_functions->access );
63 MODULE_ACTIVATE_STOP
64  
65 MODULE_DEACTIVATE_START
66 MODULE_DEACTIVATE_STOP
67
68 /*****************************************************************************
69  * Functions exported as capabilities. They are declared as static so that
70  * we don't pollute the namespace too much.
71  *****************************************************************************/
72 static void input_getfunctions( function_list_t * p_function_list )
73 {
74 #define input p_function_list->functions.access
75     input.pf_open             = FileOpen;
76     input.pf_read             = input_FDRead;
77     input.pf_close            = input_FDClose;
78     input.pf_set_program      = input_SetProgram;
79     input.pf_set_area         = NULL;
80     input.pf_seek             = input_FDSeek;
81 #undef input
82 }
83
84 /*****************************************************************************
85  * FileOpen: open the file
86  *****************************************************************************/
87 static int FileOpen( input_thread_t * p_input )
88 {
89     char *              psz_name = p_input->psz_name;
90     int                 i_stat;
91     struct stat         stat_info;                                              
92     input_socket_t *    p_access_data;
93     vlc_bool_t          b_stdin;
94
95     p_input->i_mtu = 0;
96
97     b_stdin = psz_name[0] == '-' && psz_name[1] == '\0';
98
99     if( !b_stdin && (i_stat = stat( psz_name, &stat_info )) == (-1) )
100     {
101         msg_Err( p_input, "cannot stat() file `%s' (%s)",
102                           psz_name, strerror(errno));
103         return( -1 );
104     }
105
106     vlc_mutex_lock( &p_input->stream.stream_lock );
107
108     if( *p_input->psz_access && !strncmp( p_input->psz_access, "stream", 7 ) )
109     {
110         /* stream:%s */
111         p_input->stream.b_pace_control = 0;
112         p_input->stream.b_seekable = 0;
113         p_input->stream.p_selected_area->i_size = 0;
114     }
115     else
116     {
117         /* file:%s or %s */
118         p_input->stream.b_pace_control = 1;
119
120         if( b_stdin )
121         {
122             p_input->stream.b_seekable = 0;
123             p_input->stream.p_selected_area->i_size = 0;
124         }
125         else if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
126                   || S_ISBLK(stat_info.st_mode) )
127         {
128             p_input->stream.b_seekable = 1;
129             p_input->stream.p_selected_area->i_size = stat_info.st_size;
130         }
131         else if( S_ISFIFO(stat_info.st_mode)
132 #if !defined( SYS_BEOS ) && !defined( WIN32 )
133                   || S_ISSOCK(stat_info.st_mode)
134 #endif
135                )
136         {
137             p_input->stream.b_seekable = 0;
138             p_input->stream.p_selected_area->i_size = 0;
139         }
140         else
141         {
142             vlc_mutex_unlock( &p_input->stream.stream_lock );
143             msg_Err( p_input, "unknown file type for `%s'", psz_name );
144             return( -1 );
145         }
146     }
147  
148     p_input->stream.p_selected_area->i_tell = 0;
149     p_input->stream.i_method = INPUT_METHOD_FILE;
150     vlc_mutex_unlock( &p_input->stream.stream_lock );
151  
152     msg_Dbg( p_input, "opening file `%s'", psz_name );
153     p_access_data = malloc( sizeof(input_socket_t) );
154     p_input->p_access_data = (void *)p_access_data;
155     if( p_access_data == NULL )
156     {
157         msg_Err( p_input, "out of memory" );
158         return( -1 );
159     }
160
161     if( b_stdin )
162     {
163         p_access_data->i_handle = 0;
164     }
165     else if( (p_access_data->i_handle = open( psz_name,
166                                    /*O_NONBLOCK | O_LARGEFILE*/ 0 )) == (-1) )
167     {
168         msg_Err( p_input, "cannot open file %s (%s)", psz_name,
169                           strerror(errno) );
170         free( p_access_data );
171         return( -1 );
172     }
173
174     return( 0 );
175 }