]> git.sesse.net Git - vlc/blob - modules/access/mtp.c
bluray: blurayInitTitles: fix memleaks on error paths
[vlc] / modules / access / mtp.c
1 /*****************************************************************************
2  * mtp.c: mtp input (mtp: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2001-2006 VLC authors and VideoLAN
5  * Copyright © 2006-2008 Rémi Denis-Courmont
6  *
7  * Authors: Fabio Ritrovato <exsephiroth87@gmail.com>
8  * Original file.c: Christophe Massiot <massiot@via.ecp.fr>
9  *                  Rémi Denis-Courmont <rem # videolan # org>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
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 Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <assert.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_input.h>
43 #include <vlc_access.h>
44 #include <vlc_dialog.h>
45 #include <vlc_fs.h>
46
47 #include "libmtp.h"
48
49 /*****************************************************************************
50  * Module descriptor
51  *****************************************************************************/
52
53 static int  Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
55
56 vlc_module_begin()
57     set_description( N_("MTP input") )
58     set_shortname( N_("MTP") )
59     set_category( CAT_INPUT )
60     set_subcategory( SUBCAT_INPUT_ACCESS )
61     set_capability( "access", 0 )
62     add_shortcut( "mtp" )
63     set_callbacks( Open, Close )
64 vlc_module_end()
65
66 /*****************************************************************************
67  * Exported prototypes
68  *****************************************************************************/
69
70 static int  Seek( access_t *, uint64_t );
71 static ssize_t Read( access_t *, uint8_t *, size_t );
72 static int  Control( access_t *, int, va_list );
73
74 static int  open_file( access_t *, const char * );
75
76 struct access_sys_t
77 {
78     int fd;
79 };
80
81 /*****************************************************************************
82  * Open: open the file
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     access_t     *p_access = ( access_t* )p_this;
87     access_sys_t *p_sys;
88     uint32_t i_bus;
89     uint8_t i_dev;
90     uint16_t i_product_id;
91     int i_track_id;
92     LIBMTP_raw_device_t *p_rawdevices;
93     LIBMTP_mtpdevice_t *p_device;
94     int i_numrawdevices;
95     int i_ret;
96
97     if( sscanf( p_access->psz_location, "%"SCNu32":%"SCNu8":%"SCNu16":%d",
98                 &i_bus, &i_dev, &i_product_id, &i_track_id ) != 4 )
99         return VLC_EGENERIC;
100     i_ret = LIBMTP_Detect_Raw_Devices( &p_rawdevices, &i_numrawdevices );
101     if( i_ret != 0 || i_numrawdevices <= 0 || !p_rawdevices )
102         return VLC_EGENERIC;
103
104     for( int i = 0; i < i_numrawdevices; i++ )
105     {
106         if( i_bus == p_rawdevices[i].bus_location &&
107             i_dev == p_rawdevices[i].devnum &&
108             i_product_id == p_rawdevices[i].device_entry.product_id )
109         {
110             if( ( p_device = LIBMTP_Open_Raw_Device( &p_rawdevices[i] )
111                 ) != NULL )
112             {
113                 free( p_access->psz_filepath );
114 #warning Oooh no! Not tempnam()!
115                 p_access->psz_filepath = tempnam( NULL, "vlc" );
116                 if( p_access->psz_filepath == NULL )
117                 {
118                     LIBMTP_Release_Device( p_device );
119                     free( p_rawdevices );
120                     return VLC_ENOMEM;
121                 }
122                 else
123                 {
124                     msg_Dbg( p_access, "About to write %s",
125                              p_access->psz_filepath );
126                     LIBMTP_Get_File_To_File( p_device, i_track_id,
127                                              p_access->psz_filepath, NULL,
128                                              NULL );
129                     LIBMTP_Release_Device( p_device );
130                     i = i_numrawdevices;
131                 }
132             }
133             else
134             {
135                 free( p_rawdevices );
136                 return VLC_EGENERIC;
137             }
138         }
139     }
140     free( p_rawdevices );
141
142     STANDARD_READ_ACCESS_INIT;
143     int fd = p_sys->fd = -1;
144
145     /* Open file */
146     msg_Dbg( p_access, "opening file `%s'", p_access->psz_filepath );
147     fd = open_file( p_access, p_access->psz_filepath );
148
149     if( fd == -1 )
150     {
151         free( p_sys );
152         return VLC_EGENERIC;
153     }
154     p_sys->fd = fd;
155
156     return VLC_SUCCESS;
157 }
158
159 /*****************************************************************************
160  * Close: close the target
161  *****************************************************************************/
162 static void Close( vlc_object_t * p_this )
163 {
164     access_t     *p_access = ( access_t* )p_this;
165     access_sys_t *p_sys = p_access->p_sys;
166
167     close ( p_sys->fd );
168     if( vlc_unlink( p_access->psz_filepath ) != 0 )
169         msg_Err( p_access, "Error deleting file %s, %m",
170                  p_access->psz_filepath );
171     free( p_sys );
172 }
173
174 /*****************************************************************************
175  * Read: standard read on a file descriptor.
176  *****************************************************************************/
177 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
178 {
179     access_sys_t *p_sys = p_access->p_sys;
180     ssize_t i_ret;
181     int fd = p_sys->fd;
182
183     i_ret = read( fd, p_buffer, i_len );
184
185     if( i_ret < 0 )
186     {
187         switch( errno )
188         {
189             case EINTR:
190             case EAGAIN:
191                 break;
192
193             default:
194                 msg_Err( p_access, "read failed (%m)" );
195                 dialog_Fatal( p_access, _( "File reading failed" ), "%s (%m)",
196                                 _( "VLC could not read the file." ) );
197                 p_access->info.b_eof = true;
198                 return 0;
199         }
200     }
201     else if( i_ret > 0 )
202         p_access->info.i_pos += i_ret;
203     else
204         p_access->info.b_eof = true;
205
206     return i_ret;
207 }
208
209
210 /*****************************************************************************
211  * Seek: seek to a specific location in a file
212  *****************************************************************************/
213 static int Seek( access_t *p_access, uint64_t i_pos )
214 {
215     p_access->info.i_pos = i_pos;
216     p_access->info.b_eof = false;
217
218     lseek( p_access->p_sys->fd, i_pos, SEEK_SET );
219     return VLC_SUCCESS;
220 }
221
222 /*****************************************************************************
223  * Control:
224  *****************************************************************************/
225 static int Control( access_t *p_access, int i_query, va_list args )
226 {
227     access_sys_t *sys = p_access->p_sys;
228     bool   *pb_bool;
229     int64_t      *pi_64;
230
231     switch( i_query )
232     {
233         /* */
234         case ACCESS_CAN_SEEK:
235         case ACCESS_CAN_FASTSEEK:
236             pb_bool = ( bool* )va_arg( args, bool* );
237             *pb_bool = true;
238             break;
239
240         case ACCESS_CAN_PAUSE:
241         case ACCESS_CAN_CONTROL_PACE:
242             pb_bool = ( bool* )va_arg( args, bool* );
243             *pb_bool = true;
244             break;
245
246         case ACCESS_GET_SIZE:
247         {
248             uint64_t *s = va_arg( args, uint64_t * );
249             struct stat st;
250             if( fstat( sys->fd, &st ) )
251             {
252                 msg_Err( p_access, "fstat error: %m" );
253                 return VLC_EGENERIC;
254             }
255             *s = st.st_size;
256             break;
257         }
258
259         case ACCESS_GET_PTS_DELAY:
260             pi_64 = ( int64_t* )va_arg( args, int64_t * );
261             *pi_64 = INT64_C(1000)
262                    * var_InheritInteger( p_access, "file-caching" );
263             break;
264
265         /* */
266         case ACCESS_SET_PAUSE_STATE:
267             /* Nothing to do */
268             break;
269
270         case ACCESS_GET_TITLE_INFO:
271         case ACCESS_SET_TITLE:
272         case ACCESS_SET_SEEKPOINT:
273         case ACCESS_SET_PRIVATE_ID_STATE:
274         case ACCESS_GET_META:
275         case ACCESS_GET_PRIVATE_ID_STATE:
276         case ACCESS_GET_CONTENT_TYPE:
277             return VLC_EGENERIC;
278
279         default:
280             msg_Warn( p_access, "unimplemented query %d in control", i_query );
281             return VLC_EGENERIC;
282
283     }
284     return VLC_SUCCESS;
285 }
286
287 /*****************************************************************************
288  * open_file: Opens a specific file
289  *****************************************************************************/
290 static int open_file( access_t *p_access, const char *path )
291 {
292     int fd = vlc_open( path, O_RDONLY | O_NONBLOCK );
293     if( fd == -1 )
294     {
295         msg_Err( p_access, "cannot open file %s (%m)", path );
296         dialog_Fatal( p_access, _( "File reading failed" ),
297                         _( "VLC could not open the file \"%s\". (%m)" ), path );
298         return -1;
299     }
300 #ifdef F_RDAHEAD
301     fcntl( fd, F_RDAHEAD, 1 );
302 #endif
303 #ifdef F_NOCACHE
304     fcntl( fd, F_NOCACHE, 0 );
305 #endif
306     return fd;
307 }