Changeset 10068


Ignore:
Timestamp:
Feb 1, 2010, 9:09:31 PM (12 years ago)
Author:
charles
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode.c

    r9979 r10068  
    1919#include <string.h>
    2020
     21#include <sys/types.h> /* stat() */
     22#include <sys/stat.h> /* stat() */
    2123#include <locale.h>
    22 #include <unistd.h> /* close() */
     24#include <unistd.h> /* stat(), close() */
    2325
    2426#include <event.h> /* evbuffer */
     
    15301532tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
    15311533{
     1534    FILE * fp;
     1535    struct stat sb;
    15321536    int err = 0;
    1533     FILE * fp = fopen( filename, "wb+" );
    1534 
     1537    char * backup = NULL;
     1538    tr_bool have_backup = FALSE;
     1539
     1540    /* if the file already exists, try to move it out of the way & keep it as a backup */
     1541    if( !stat( filename, &sb ) && S_ISREG( sb.st_mode ) ) {
     1542        backup = tr_strdup_printf( "%s.temp-backup", filename );
     1543        if( stat( backup, &sb ) && ( errno == ENOENT ) )
     1544            have_backup = !rename( filename, backup );
     1545    }
     1546
     1547    /* save the bencoded data to the file */
     1548    fp = fopen( filename, "wb+" );
    15351549    if( fp == NULL )
    15361550    {
     
    15551569    }
    15561570
     1571    if( have_backup ) {
     1572        if( err )
     1573            rename( backup, filename );
     1574        else
     1575            unlink( backup );
     1576    }
     1577
     1578    tr_free( backup );
    15571579    return err;
    15581580}
Note: See TracChangeset for help on using the changeset viewer.