]> git.sesse.net Git - vlc/blob - modules/access_output/file.c
* modules/access_output/file.c: make the output file writeable by the owner.
[vlc] / modules / access_output / file.c
1 /*****************************************************************************
2  * file.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: file.c,v 1.8 2003/06/21 14:24:30 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/input.h>
37 #include <vlc/sout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #elif defined( WIN32 ) && !defined( UNDER_CE )
42 #   include <io.h>
43 #endif
44
45 /* For those platforms that don't use these */
46 #ifndef S_IRGRP
47 #   define S_IRGRP 0
48 #endif
49 #ifndef S_IROTH
50 #   define S_IROTH 0
51 #endif
52
53 /*****************************************************************************
54  * Exported prototypes
55  *****************************************************************************/
56 static int     Open   ( vlc_object_t * );
57 static void    Close  ( vlc_object_t * );
58
59 static int     Write( sout_access_out_t *, sout_buffer_t * );
60 static int     Seek ( sout_access_out_t *, off_t  );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_description( _("File stream ouput") );
67     set_capability( "sout access", 50 );
68     add_shortcut( "file" );
69     set_callbacks( Open, Close );
70 vlc_module_end();
71
72 struct sout_access_out_sys_t
73 {
74     int i_handle;
75
76 };
77
78 /*****************************************************************************
79  * Open: open the file
80  *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
82 {
83     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
84
85     if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
86     {
87         msg_Err( p_access, "out of memory" );
88         return( VLC_EGENERIC );
89     }
90
91     if( !p_access->psz_name )
92     {
93         msg_Err( p_access, "no file name specified" );
94         return VLC_EGENERIC;
95     }
96     if( !strcmp( p_access->psz_name, "-" ) )
97     {
98         p_access->p_sys->i_handle = STDOUT_FILENO;
99         msg_Dbg( p_access, "using stdout" );
100     }
101     else if( ( p_access->p_sys->i_handle =
102                open( p_access->psz_name, O_WRONLY|O_CREAT|O_TRUNC,
103                      S_IWRITE | S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
104     {
105         msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
106         free( p_access->p_sys );
107         return( VLC_EGENERIC );
108     }
109
110     p_access->pf_write        = Write;
111     p_access->pf_seek         = Seek;
112
113     msg_Info( p_access, "Open: name:`%s'", p_access->psz_name );
114     return VLC_SUCCESS;
115 }
116
117 /*****************************************************************************
118  * Close: close the target
119  *****************************************************************************/
120 static void Close( vlc_object_t * p_this )
121 {
122     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
123
124     if( strcmp( p_access->psz_name, "-" ) )
125     {
126         if( p_access->p_sys->i_handle )
127         {
128             close( p_access->p_sys->i_handle );
129         }
130     }
131     free( p_access->p_sys );
132
133     msg_Info( p_access, "Close" );
134 }
135
136 /*****************************************************************************
137  * Read: standard read on a file descriptor.
138  *****************************************************************************/
139 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
140 {
141     size_t i_write = 0;
142
143     do
144     {
145         sout_buffer_t *p_next;
146
147         i_write += write( p_access->p_sys->i_handle, p_buffer->p_buffer,
148                           p_buffer->i_size );
149         p_next = p_buffer->p_next;
150         sout_BufferDelete( p_access->p_sout, p_buffer );
151         p_buffer = p_next;
152
153     } while( p_buffer );
154
155     return( i_write );
156 }
157
158 /*****************************************************************************
159  * Seek: seek to a specific location in a file
160  *****************************************************************************/
161 static int Seek( sout_access_out_t *p_access, off_t i_pos )
162 {
163     msg_Dbg( p_access, "Seek: pos:"I64Fd, (int64_t)i_pos );
164
165     if( strcmp( p_access->psz_name, "-" ) )
166     {
167 #if defined( WIN32 ) && !defined( UNDER_CE )
168         return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
169 #else
170         return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
171 #endif
172     }
173     else
174     {
175         msg_Err( p_access, "cannot seek while using stdout" );
176         return VLC_EGENERIC;
177     }
178 }