Changeset 785b6e2bf9a83baee6becb9b324824ce99fa331c
- Timestamp:
- 05/25/08 17:06:57
(3 months ago)
- Author:
- Rémi Denis-Courmont <rem@videolan.org>
- git-committer:
- Rémi Denis-Courmont <rem@videolan.org> 1211728017 +0300
- git-parent:
[77acf509d89e39c0bc48b5bdbb643b2e11ce2373]
- git-author:
- Rémi Denis-Courmont <rem@videolan.org> 1211727960 +0300
- Message:
access_out_file: fix append mode, relax stdout support, simplify
O_APPEND puts the file pointer to the end after _each_ write.
We want to put it at the end (only) after open.
Also allow reading/seeking stdout, as it could be a redirected file
(of course, it won't work if it's a terminal)
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r13ae40b |
r785b6e2 |
|
| 43 | 43 | #include "vlc_strings.h" |
|---|
| 44 | 44 | |
|---|
| 45 | | #ifdef HAVE_UNISTD_H |
|---|
| | 45 | #if defined( WIN32 ) && !defined( UNDER_CE ) |
|---|
| | 46 | # include <io.h> |
|---|
| | 47 | # define lseek _lseeki64 |
|---|
| | 48 | #else |
|---|
| 46 | 49 | # include <unistd.h> |
|---|
| 47 | | #elif defined( WIN32 ) && !defined( UNDER_CE ) |
|---|
| 48 | | # include <io.h> |
|---|
| 49 | | #endif |
|---|
| 50 | | |
|---|
| 51 | | /* For those platforms that don't use these */ |
|---|
| 52 | | #ifndef STDOUT_FILENO |
|---|
| 53 | | # define STDOUT_FILENO 1 |
|---|
| 54 | | #endif |
|---|
| | 50 | #endif |
|---|
| | 51 | |
|---|
| 55 | 52 | #ifndef O_LARGEFILE |
|---|
| 56 | 53 | # define O_LARGEFILE 0 |
|---|
| … | … | |
| 104 | 101 | { |
|---|
| 105 | 102 | sout_access_out_t *p_access = (sout_access_out_t*)p_this; |
|---|
| 106 | | int i_flags; |
|---|
| 107 | | vlc_value_t val; |
|---|
| | 103 | int fd; |
|---|
| 108 | 104 | |
|---|
| 109 | 105 | config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg ); |
|---|
| … | … | |
| 115 | 111 | } |
|---|
| 116 | 112 | |
|---|
| 117 | | if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) ) |
|---|
| 118 | | { |
|---|
| 119 | | return( VLC_ENOMEM ); |
|---|
| 120 | | } |
|---|
| 121 | | |
|---|
| 122 | | i_flags = O_RDWR|O_CREAT|O_LARGEFILE; |
|---|
| 123 | | |
|---|
| 124 | | var_Get( p_access, SOUT_CFG_PREFIX "append", &val ); |
|---|
| 125 | | i_flags |= val.b_bool ? O_APPEND : O_TRUNC; |
|---|
| | 113 | bool append = var_GetBool( p_access, SOUT_CFG_PREFIX "append" ); |
|---|
| 126 | 114 | |
|---|
| 127 | 115 | if( !strcmp( p_access->psz_path, "-" ) ) |
|---|
| 128 | 116 | { |
|---|
| 129 | | #if defined(WIN32) |
|---|
| 130 | | setmode (STDOUT_FILENO, O_BINARY); |
|---|
| 131 | | #endif |
|---|
| 132 | | p_access->p_sys->i_handle = STDOUT_FILENO; |
|---|
| | 117 | #ifdef WIN32 |
|---|
| | 118 | setmode (fileno (stdout), O_BINARY); |
|---|
| | 119 | #endif |
|---|
| | 120 | fd = dup (fileno (stdout)); |
|---|
| 133 | 121 | msg_Dbg( p_access, "using stdout" ); |
|---|
| 134 | 122 | } |
|---|
| 135 | 123 | else |
|---|
| 136 | 124 | { |
|---|
| 137 | | int fd; |
|---|
| 138 | 125 | char *psz_tmp = str_format( p_access, p_access->psz_path ); |
|---|
| 139 | 126 | path_sanitize( psz_tmp ); |
|---|
| 140 | 127 | |
|---|
| 141 | | fd = utf8_open( psz_tmp, i_flags, 0666 ); |
|---|
| | 128 | fd = utf8_open( psz_tmp, O_RDWR | O_CREAT | O_LARGEFILE | |
|---|
| | 129 | (append ? 0 : O_TRUNC), 0666 ); |
|---|
| 142 | 130 | free( psz_tmp ); |
|---|
| 143 | | |
|---|
| 144 | | if( fd == -1 ) |
|---|
| 145 | | { |
|---|
| 146 | | msg_Err( p_access, "cannot open `%s' (%m)", p_access->psz_path ); |
|---|
| 147 | | free( p_access->p_sys ); |
|---|
| 148 | | return( VLC_EGENERIC ); |
|---|
| 149 | | } |
|---|
| 150 | | p_access->p_sys->i_handle = fd; |
|---|
| | 131 | } |
|---|
| | 132 | |
|---|
| | 133 | if (fd == -1) |
|---|
| | 134 | { |
|---|
| | 135 | msg_Err( p_access, "cannot open `%s' (%m)", p_access->psz_path ); |
|---|
| | 136 | return VLC_EGENERIC; |
|---|
| 151 | 137 | } |
|---|
| 152 | 138 | |
|---|
| … | … | |
| 154 | 140 | p_access->pf_read = Read; |
|---|
| 155 | 141 | p_access->pf_seek = Seek; |
|---|
| 156 | | |
|---|
| 157 | | msg_Dbg( p_access, "file access output opened (`%s')", p_access->psz_path ); |
|---|
| | 142 | p_access->p_sys = (void *)(intptr_t)fd; |
|---|
| | 143 | |
|---|
| | 144 | msg_Dbg( p_access, "file access output opened (%s)", p_access->psz_path ); |
|---|
| | 145 | if (append) |
|---|
| | 146 | lseek (fd, 0, SEEK_END); |
|---|
| 158 | 147 | |
|---|
| 159 | 148 | /* Update pace control flag */ |
|---|
| … | … | |
| 171 | 160 | sout_access_out_t *p_access = (sout_access_out_t*)p_this; |
|---|
| 172 | 161 | |
|---|
| 173 | | if( strcmp( p_access->psz_path, "-" ) ) |
|---|
| 174 | | { |
|---|
| 175 | | if( p_access->p_sys->i_handle ) |
|---|
| 176 | | { |
|---|
| 177 | | close( p_access->p_sys->i_handle ); |
|---|
| 178 | | } |
|---|
| 179 | | } |
|---|
| 180 | | free( p_access->p_sys ); |
|---|
| | 162 | close( (intptr_t)p_access->p_sys ); |
|---|
| 181 | 163 | |
|---|
| 182 | 164 | /* Update pace control flag */ |
|---|
| … | … | |
| 192 | 174 | static ssize_t Read( sout_access_out_t *p_access, block_t *p_buffer ) |
|---|
| 193 | 175 | { |
|---|
| 194 | | if( strcmp( p_access->psz_path, "-" ) ) |
|---|
| 195 | | { |
|---|
| 196 | | return read( p_access->p_sys->i_handle, p_buffer->p_buffer, |
|---|
| 197 | | p_buffer->i_buffer ); |
|---|
| 198 | | } |
|---|
| 199 | | |
|---|
| 200 | | msg_Err( p_access, "cannot read while using stdout" ); |
|---|
| 201 | | return VLC_EGENERIC; |
|---|
| | 176 | return read( (intptr_t)p_access->p_sys, p_buffer->p_buffer, |
|---|
| | 177 | p_buffer->i_buffer ); |
|---|
| 202 | 178 | } |
|---|
| 203 | 179 | |
|---|
| … | … | |
| 213 | 189 | block_t *p_next = p_buffer->p_next;; |
|---|
| 214 | 190 | |
|---|
| 215 | | i_write += write( p_access->p_sys->i_handle, |
|---|
| | 191 | i_write += write( (intptr_t)p_access->p_sys, |
|---|
| 216 | 192 | p_buffer->p_buffer, p_buffer->i_buffer ); |
|---|
| 217 | 193 | block_Release( p_buffer ); |
|---|
| … | … | |
| 228 | 204 | static int Seek( sout_access_out_t *p_access, off_t i_pos ) |
|---|
| 229 | 205 | { |
|---|
| 230 | | if( strcmp( p_access->psz_path, "-" ) ) |
|---|
| 231 | | { |
|---|
| 232 | | #if defined( WIN32 ) && !defined( UNDER_CE ) |
|---|
| 233 | | return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) ); |
|---|
| 234 | | #else |
|---|
| 235 | | return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) ); |
|---|
| 236 | | #endif |
|---|
| 237 | | } |
|---|
| 238 | | else |
|---|
| 239 | | { |
|---|
| 240 | | msg_Err( p_access, "cannot seek while using stdout" ); |
|---|
| 241 | | return VLC_EGENERIC; |
|---|
| 242 | | } |
|---|
| 243 | | } |
|---|
| | 206 | return lseek( (intptr_t)p_access->p_sys, i_pos, SEEK_SET ); |
|---|
| | 207 | } |
|---|