Changeset 785b6e2bf9a83baee6becb9b324824ce99fa331c

Show
Ignore:
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
  • modules/access_output/file.c

    r13ae40b r785b6e2  
    4343#include "vlc_strings.h" 
    4444 
    45 #ifdef HAVE_UNISTD_H 
     45#if defined( WIN32 ) && !defined( UNDER_CE ) 
     46#   include <io.h> 
     47#   define lseek _lseeki64 
     48#else 
    4649#   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 
    5552#ifndef O_LARGEFILE 
    5653#   define O_LARGEFILE 0 
     
    104101{ 
    105102    sout_access_out_t   *p_access = (sout_access_out_t*)p_this; 
    106     int                 i_flags; 
    107     vlc_value_t         val; 
     103    int                 fd; 
    108104 
    109105    config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg ); 
     
    115111    } 
    116112 
    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" ); 
    126114 
    127115    if( !strcmp( p_access->psz_path, "-" ) ) 
    128116    { 
    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))
    133121        msg_Dbg( p_access, "using stdout" ); 
    134122    } 
    135123    else 
    136124    { 
    137         int fd; 
    138125        char *psz_tmp = str_format( p_access, p_access->psz_path ); 
    139126        path_sanitize( psz_tmp ); 
    140127 
    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 ); 
    142130        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; 
    151137    } 
    152138 
     
    154140    p_access->pf_read  = Read; 
    155141    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); 
    158147 
    159148    /* Update pace control flag */ 
     
    171160    sout_access_out_t *p_access = (sout_access_out_t*)p_this; 
    172161 
    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 ); 
    181163 
    182164    /* Update pace control flag */ 
     
    192174static ssize_t Read( sout_access_out_t *p_access, block_t *p_buffer ) 
    193175{ 
    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 ); 
    202178} 
    203179 
     
    213189        block_t *p_next = p_buffer->p_next;; 
    214190 
    215         i_write += write( p_access->p_sys->i_handle
     191        i_write += write( (intptr_t)p_access->p_sys
    216192                          p_buffer->p_buffer, p_buffer->i_buffer ); 
    217193        block_Release( p_buffer ); 
     
    228204static int Seek( sout_access_out_t *p_access, off_t i_pos ) 
    229205{ 
    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