From b6f3b15f0a267ce1e20022e6b308d989beb3e0c0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 3 Jun 2009 21:08:54 +0000 Subject: [PATCH] (trunk libT) handle windows when saving json files; use evbuffer_write() more safely --- libtransmission/bencode.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/libtransmission/bencode.c b/libtransmission/bencode.c index 6af846480..e02b11b14 100644 --- a/libtransmission/bencode.c +++ b/libtransmission/bencode.c @@ -18,6 +18,9 @@ #include #include +#include /* open() */ +#include /* open() */ +#include /* open() */ #include #include /* close() */ @@ -27,7 +30,6 @@ #include "transmission.h" #include "bencode.h" -#include "fdlimit.h" /* tr_open_file_for_writing() */ #include "json.h" #include "list.h" #include "ptrarray.h" @@ -37,6 +39,10 @@ #define ENODATA EIO #endif +#ifndef O_BINARY + #define O_BINARY 0 +#endif + /** *** **/ @@ -1535,7 +1541,7 @@ int tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename ) { int err = 0; - int fd = tr_open_file_for_writing( filename ); + int fd = open( filename, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY ); if( fd < 0 ) { @@ -1547,15 +1553,20 @@ tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename ) { struct evbuffer * buf = evbuffer_new( ); tr_bencToBuf( top, mode, buf ); - if( evbuffer_write( buf, fd ) == -1 ) + + while( !err && EVBUFFER_LENGTH( buf ) ) { - err = errno; - tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), - filename, tr_strerror( errno ) ); + if( evbuffer_write( buf, fd ) == -1 ) + { + err = errno; + tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), + filename, tr_strerror( errno ) ); + } } if( !err ) tr_dbg( "tr_bencToFile saved \"%s\"", filename ); + evbuffer_free( buf ); close( fd ); }