From ab56a2f7f794f73a000f8d6b6a27832f41c234c7 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 4 Jun 2009 15:52:54 +0000 Subject: [PATCH] (trunk libT) fix potential permissions headache when saving json/benc files --- libtransmission/bencode.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index e02b11b14..9a9ad56cd 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -18,9 +18,6 @@ #include #include -#include /* open() */ -#include /* open() */ -#include /* open() */ #include #include /* close() */ @@ -39,10 +36,6 @@ #define ENODATA EIO #endif -#ifndef O_BINARY - #define O_BINARY 0 -#endif - /** *** **/ @@ -1541,9 +1534,9 @@ int tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename ) { int err = 0; - int fd = open( filename, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY ); + FILE * fp = fopen( filename, "wb+" ); - if( fd < 0 ) + if( fp == NULL ) { err = errno; tr_err( _( "Couldn't open \"%1$s\": %2$s" ), @@ -1556,7 +1549,7 @@ tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename ) while( !err && EVBUFFER_LENGTH( buf ) ) { - if( evbuffer_write( buf, fd ) == -1 ) + if( evbuffer_write( buf, fileno(fp) ) == -1 ) { err = errno; tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), @@ -1567,7 +1560,7 @@ tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename ) if( !err ) tr_dbg( "tr_bencToFile saved \"%s\"", filename ); evbuffer_free( buf ); - close( fd ); + fclose( fp ); } return err;