]> git.sesse.net Git - vlc/blob - modules/access_output/file.c
88fb86bb8f1eb04b6e2a56cfab77372d5df388ee
[vlc] / modules / access_output / file.c
1 /*****************************************************************************
2  * file.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <sys/types.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <errno.h>
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_sout.h>
41 #include <vlc_block.h>
42 #include <vlc_fs.h>
43 #include <vlc_strings.h>
44
45 #if defined( WIN32 ) && !defined( UNDER_CE )
46 #   include <io.h>
47 #   define lseek _lseeki64
48 #elif defined( __OS2__ )
49 #   include <io.h>
50 #else
51 #   include <unistd.h>
52 #endif
53
54 #ifndef O_LARGEFILE
55 #   define O_LARGEFILE 0
56 #endif
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
64 #define SOUT_CFG_PREFIX "sout-file-"
65 #define APPEND_TEXT N_("Append to file")
66 #define APPEND_LONGTEXT N_( "Append to file if it exists instead " \
67                             "of replacing it.")
68 #define SYNC_TEXT N_("Synchronous writing")
69 #define SYNC_LONGTEXT N_( "Open the file with synchronous writing.")
70
71 vlc_module_begin ()
72     set_description( N_("File stream output") )
73     set_shortname( N_("File" ))
74     set_capability( "sout access", 50 )
75     set_category( CAT_SOUT )
76     set_subcategory( SUBCAT_SOUT_ACO )
77     add_shortcut( "file", "stream", "fd" )
78     add_bool( SOUT_CFG_PREFIX "append", false, APPEND_TEXT,APPEND_LONGTEXT,
79               true )
80 #ifdef O_SYNC
81     add_bool( SOUT_CFG_PREFIX "sync", false, SYNC_TEXT,SYNC_LONGTEXT,
82               false )
83 #endif
84     set_callbacks( Open, Close )
85 vlc_module_end ()
86
87
88 /*****************************************************************************
89  * Exported prototypes
90  *****************************************************************************/
91 static const char *const ppsz_sout_options[] = {
92     "append",
93 #ifdef O_SYNC
94     "sync",
95 #endif
96     NULL
97 };
98
99 static ssize_t Write( sout_access_out_t *, block_t * );
100 static int Seek ( sout_access_out_t *, off_t  );
101 static ssize_t Read ( sout_access_out_t *, block_t * );
102 static int Control( sout_access_out_t *, int, va_list );
103
104 struct sout_access_out_sys_t
105 {
106     int i_handle;
107 };
108
109 /*****************************************************************************
110  * Open: open the file
111  *****************************************************************************/
112 static int Open( vlc_object_t *p_this )
113 {
114     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
115     int                 fd;
116
117     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
118
119     if( !p_access->psz_path )
120     {
121         msg_Err( p_access, "no file name specified" );
122         return VLC_EGENERIC;
123     }
124
125     bool append = var_GetBool( p_access, SOUT_CFG_PREFIX "append" );
126
127     if (!strcmp (p_access->psz_access, "fd"))
128     {
129         char *end;
130
131         fd = strtol (p_access->psz_path, &end, 0);
132         if (!*p_access->psz_path || *end)
133         {
134             msg_Err (p_access, "invalid file descriptor: %s",
135                      p_access->psz_path);
136             return VLC_EGENERIC;
137         }
138         fd = vlc_dup (fd);
139         if (fd == -1)
140         {
141             msg_Err (p_access, "cannot use file descriptor: %m");
142             return VLC_EGENERIC;
143         }
144     }
145 #ifndef UNDER_CE
146     else
147     if( !strcmp( p_access->psz_path, "-" ) )
148     {
149 #if defined( WIN32 ) || defined( __OS2__ )
150         setmode (fileno (stdout), O_BINARY);
151 #endif
152         fd = vlc_dup (fileno (stdout));
153         if (fd == -1)
154         {
155             msg_Err (p_access, "cannot use standard output: %m");
156             return VLC_EGENERIC;
157         }
158         msg_Dbg( p_access, "using stdout" );
159     }
160 #endif
161     else
162     {
163         char *psz_tmp = str_format( p_access, p_access->psz_path );
164         path_sanitize( psz_tmp );
165
166         fd = vlc_open( psz_tmp, O_RDWR | O_CREAT | O_LARGEFILE |
167 #ifdef O_SYNC
168                 (var_GetBool( p_access, SOUT_CFG_PREFIX "sync" ) ? O_SYNC : 0) |
169 #endif
170                 (append ? 0 : O_TRUNC), 0666 );
171         free( psz_tmp );
172         if (fd == -1)
173         {
174             msg_Err (p_access, "cannot create %s: %m", p_access->psz_path);
175             return VLC_EGENERIC;
176         }
177     }
178
179     p_access->pf_write = Write;
180     p_access->pf_read  = Read;
181     p_access->pf_seek  = Seek;
182     p_access->pf_control = Control;
183     p_access->p_sys    = (void *)(intptr_t)fd;
184
185     msg_Dbg( p_access, "file access output opened (%s)", p_access->psz_path );
186     if (append)
187         lseek (fd, 0, SEEK_END);
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * Close: close the target
194  *****************************************************************************/
195 static void Close( vlc_object_t * p_this )
196 {
197     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
198
199     close( (intptr_t)p_access->p_sys );
200
201     msg_Dbg( p_access, "file access output closed" );
202 }
203
204 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
205 {
206     switch( i_query )
207     {
208         case ACCESS_OUT_CONTROLS_PACE:
209         {
210             bool *pb = va_arg( args, bool * );
211             *pb = strcmp( p_access->psz_access, "stream" );
212             break;
213         }
214
215         default:
216             return VLC_EGENERIC;
217     }
218     return VLC_SUCCESS;
219 }
220
221 /*****************************************************************************
222  * Read: standard read on a file descriptor.
223  *****************************************************************************/
224 static ssize_t Read( sout_access_out_t *p_access, block_t *p_buffer )
225 {
226     ssize_t val;
227
228     do
229         val = read( (intptr_t)p_access->p_sys, p_buffer->p_buffer,
230                     p_buffer->i_buffer );
231     while (val == -1 && errno == EINTR);
232     return val;
233 }
234
235 /*****************************************************************************
236  * Write: standard write on a file descriptor.
237  *****************************************************************************/
238 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
239 {
240     size_t i_write = 0;
241
242     while( p_buffer )
243     {
244         ssize_t val = write ((intptr_t)p_access->p_sys,
245                              p_buffer->p_buffer, p_buffer->i_buffer);
246         if (val == -1)
247         {
248             if (errno == EINTR)
249                 continue;
250             block_ChainRelease (p_buffer);
251             return -1;
252         }
253
254         if ((size_t)val >= p_buffer->i_buffer)
255         {
256             block_t *p_next = p_buffer->p_next;
257             block_Release (p_buffer);
258             p_buffer = p_next;
259         }
260         else
261         {
262             p_buffer->p_buffer += val;
263             p_buffer->i_buffer -= val;
264         }
265         i_write += val;
266     }
267     return i_write;
268 }
269
270 /*****************************************************************************
271  * Seek: seek to a specific location in a file
272  *****************************************************************************/
273 static int Seek( sout_access_out_t *p_access, off_t i_pos )
274 {
275     return lseek( (intptr_t)p_access->p_sys, i_pos, SEEK_SET );
276 }