1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 10:07:40 +00:00

(trunk libT) #2505 "Transmission loses config when disk is full" -- implemented in trunk for 1.90

This commit is contained in:
Charles Kerr 2010-02-01 21:09:31 +00:00
parent 12e6074b64
commit 8f0f6679aa

View file

@ -18,8 +18,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> /* stat() */
#include <sys/stat.h> /* stat() */
#include <locale.h> #include <locale.h>
#include <unistd.h> /* close() */ #include <unistd.h> /* stat(), close() */
#include <event.h> /* evbuffer */ #include <event.h> /* evbuffer */
@ -1529,9 +1531,21 @@ tr_bencToStr( const tr_benc * top, tr_fmt_mode mode, int * len )
int int
tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename ) tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
{ {
FILE * fp;
struct stat sb;
int err = 0; int err = 0;
FILE * fp = fopen( filename, "wb+" ); char * backup = NULL;
tr_bool have_backup = FALSE;
/* if the file already exists, try to move it out of the way & keep it as a backup */
if( !stat( filename, &sb ) && S_ISREG( sb.st_mode ) ) {
backup = tr_strdup_printf( "%s.temp-backup", filename );
if( stat( backup, &sb ) && ( errno == ENOENT ) )
have_backup = !rename( filename, backup );
}
/* save the bencoded data to the file */
fp = fopen( filename, "wb+" );
if( fp == NULL ) if( fp == NULL )
{ {
err = errno; err = errno;
@ -1554,6 +1568,14 @@ tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
fclose( fp ); fclose( fp );
} }
if( have_backup ) {
if( err )
rename( backup, filename );
else
unlink( backup );
}
tr_free( backup );
return err; return err;
} }