~ubuntu-branches/ubuntu/karmic/transmission/karmic

« back to all changes in this revision

Viewing changes to libtransmission/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Hew McLachlan, Hew McLachlan, Krzysztof Klimonda
  • Date: 2009-06-12 22:41:35 UTC
  • mfrom: (1.1.24 upstream)
  • Revision ID: james.westby@ubuntu.com-20090612224135-t0q5q83h2pr5pa8d
Tags: 1.71-1ubuntu1
[ Hew McLachlan ]
* Merge from debian unstable (LP: #384215), remaining changes:
  - debian/control:
    + Added replaces & provides clutch (now included as part of transmission).
    + add quilt, liblaunchpad-integration and libtool to Build-Depends
  - debian/rules:
    + Create a PO template during package build.
    + re-enable quilt
    + run autoreconf for launchpad-integration
  - debian/patches/01_lpi.patch:
    + Integrate Transmission with Launchpad
  - debian/patches/20_add_X-Ubuntu-Gettext-Domain.diff:
    + Add X-Ubuntu-Gettext-Domain to .desktop file.
* Closes Launchpad bugs:
  - debian/transmission-daemon.default: comment typo (LP: #317576)
  - transmission crashed with SIGSEGV in g_main_context_dispatch() (LP: #359319)

[ Krzysztof Klimonda ]
  - Rename launchpad_integration.patch to 01_lpi.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 * This exemption does not extend to derived works not owned by
8
8
 * the Transmission project.
9
9
 *
10
 
 * $Id: utils.c 8368 2009-05-08 20:52:12Z charles $
 
10
 * $Id: utils.c 8592 2009-06-02 18:21:23Z charles $
11
11
 */
12
12
 
 
13
#ifdef HAVE_MEMMEM
 
14
 #define _GNU_SOURCE /* glibc's string.h needs this to pick up memmem */
 
15
#endif
 
16
 
13
17
#include <assert.h>
14
18
#include <ctype.h> /* isalpha, tolower */
15
19
#include <errno.h>
16
20
#include <stdarg.h>
17
21
#include <stdio.h>
18
22
#include <stdlib.h>
19
 
#include <string.h> /* strerror, memset */
 
23
#include <string.h> /* strerror, memset, memmem */
20
24
 
21
25
#include <libgen.h> /* basename */
22
26
#include <sys/time.h>
266
270
        evbuffer_add_vprintf( buf, fmt, args );
267
271
        va_end( args );
268
272
        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
 
273
        /* FIXME(libevent2) ifdef this out for nonwindows platforms */
269
274
        OutputDebugString( EVBUFFER_DATA( buf ) );
270
 
        if(fp)
 
275
        if(fp) /* FIXME(libevent2) tr_getLog() should return an fd, then use evbuffer_write() here ) */
271
276
            (void) fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );
272
277
 
273
278
        tr_free( base );
683
688
    return out;
684
689
}
685
690
 
 
691
const char*
 
692
tr_memmem( const char * haystack, size_t haystacklen,
 
693
           const char * needle, size_t needlelen )
 
694
{
 
695
#ifdef HAVE_MEMMEM
 
696
    return memmem( haystack, haystacklen, needle, needlelen );
 
697
#else
 
698
    size_t i;
 
699
    if( !needlelen )
 
700
        return haystack;
 
701
    if( needlelen > haystacklen || !haystack || !needle )
 
702
        return NULL;
 
703
    for( i=0; i<=haystacklen-needlelen; ++i )
 
704
        if( !memcmp( haystack+i, needle, needlelen ) )
 
705
            return haystack+i;
 
706
    return NULL;
 
707
#endif
 
708
}
 
709
 
686
710
char*
687
711
tr_strdup_printf( const char * fmt, ... )
688
712
{
1566
1590
        tr_snprintf( buf, buflen, "%'.0f", ratio );
1567
1591
    return buf;
1568
1592
}
 
1593
 
 
1594
/***
 
1595
****
 
1596
***/
 
1597
 
 
1598
int
 
1599
tr_moveFile( const char * oldpath, const char * newpath )
 
1600
{
 
1601
    int in;
 
1602
    int out;
 
1603
    char * buf;
 
1604
    struct stat st;
 
1605
    size_t bytesLeft;
 
1606
    size_t buflen;
 
1607
 
 
1608
    /* make sure the old file exists */
 
1609
    if( stat( oldpath, &st ) ) {
 
1610
        const int err = errno;
 
1611
        errno = err;
 
1612
        return -1;
 
1613
    }
 
1614
    if( !S_ISREG( st.st_mode ) ) {
 
1615
        errno = ENOENT;
 
1616
        return -1;
 
1617
    }
 
1618
    bytesLeft = st.st_size;
 
1619
 
 
1620
    /* make sure the target directory exists */
 
1621
    {
 
1622
        char * newdir = tr_dirname( newpath );
 
1623
        int i = tr_mkdirp( newdir, 0777 );
 
1624
        tr_free( newdir );
 
1625
        if( i )
 
1626
            return i;
 
1627
    }
 
1628
 
 
1629
    /* they  might be on the same filesystem... */
 
1630
    if( !rename( oldpath, newpath ) )
 
1631
        return 0;
 
1632
 
 
1633
    /* copy the file */
 
1634
    in = tr_open_file_for_scanning( oldpath );
 
1635
    tr_preallocate_file( newpath, bytesLeft );
 
1636
    out = tr_open_file_for_writing( newpath );
 
1637
    buflen = stat( newpath, &st ) ? 4096 : st.st_blksize;
 
1638
    buf = tr_new( char, buflen );
 
1639
    while( bytesLeft > 0 )
 
1640
    {
 
1641
        ssize_t bytesWritten;
 
1642
        const size_t bytesThisPass = MIN( bytesLeft, buflen );
 
1643
        const int numRead = read( in, buf, bytesThisPass );
 
1644
        if( numRead < 0 )
 
1645
            break;
 
1646
        bytesWritten = write( out, buf, numRead );
 
1647
        if( bytesWritten < 0 )
 
1648
            break;
 
1649
        bytesLeft -= bytesWritten;
 
1650
    }
 
1651
 
 
1652
    /* cleanup */
 
1653
    tr_free( buf );
 
1654
    tr_close_file( out );
 
1655
    tr_close_file( in );
 
1656
    if( bytesLeft != 0 )
 
1657
        return -1;
 
1658
 
 
1659
    unlink( oldpath );
 
1660
    return 0;
 
1661
}