Ticket #4160: Transmission-r12426.diff

File Transmission-r12426.diff, 60.3 KB (added by rb07, 12 years ago)

Fixes and changes needed for newer revisions.

  • libtransmission/utils.c

     
    241241        va_end( args );
    242242        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
    243243        /* FIXME(libevent2) ifdef this out for nonwindows platforms */
    244         OutputDebugString( evbuffer_pullup( buf, -1 ) );
     244        OutputDebugStringA( evbuffer_pullup( buf, -1 ) );
    245245        if( fp )
    246246            fputs( (const char*)evbuffer_pullup( buf, -1 ), fp );
    247247
     
    271271    evutil_vsnprintf( buf, sizeof( buf ), fmt, ap );
    272272    va_end( ap );
    273273
    274     OutputDebugString( buf );
     274    OutputDebugStringA( buf );
    275275
    276276    if( *buf )
    277277    {
     
    430430             size_t *     size )
    431431{
    432432    uint8_t * buf;
    433     struct stat  sb;
     433    STAT  sb;
    434434    int fd;
    435435    ssize_t n;
    436436    const char * const err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
    437437
    438438    /* try to stat the file */
    439439    errno = 0;
    440     if( stat( path, &sb ) )
     440    if( tr_stati64( path, &sb ) )
    441441    {
    442442        const int err = errno;
    443443        tr_dbg( err_fmt, path, tr_strerror( errno ) );
     
    516516#ifdef WIN32
    517517    if( path && isalpha( path[0] ) && path[1] == ':' && !path[2] )
    518518        return 0;
    519     return mkdir( path );
     519    if (path) {
     520        int n = strlen(path) + 1;
     521        wchar_t pathUTF16[n];
     522        if (MultiByteToWideChar(CP_UTF8, 0, path, -1, pathUTF16, n))
     523            return _wmkdir(pathUTF16);
     524    }
     525    errno = ENOENT;
     526    return -1;
    520527#else
    521528    return mkdir( path, permissions );
    522529#endif
     
    528535{
    529536    char *      path = tr_strdup( path_in );
    530537    char *      p, * pp;
    531     struct stat sb;
     538    STAT        sb;
    532539    int         done;
    533540
    534541    /* walk past the root */
     
    546553        else
    547554            *p = '\0';
    548555
    549         if( stat( path, &sb ) )
     556        if( tr_stati64( path, &sb ) )
    550557        {
    551558            /* Folder doesn't exist yet */
    552559            if( tr_mkdir( path, permissions ) )
     
    14721479    int in;
    14731480    int out;
    14741481    char * buf;
    1475     struct stat st;
     1482    STAT st;
    14761483    off_t bytesLeft;
    14771484    const size_t buflen = 1024 * 128; /* 128 KiB buffer */
    14781485
    14791486    /* make sure the old file exists */
    1480     if( stat( oldpath, &st ) ) {
     1487    if( tr_stati64( oldpath, &st ) ) {
    14811488        const int err = errno;
    14821489        errno = err;
    14831490        return -1;
     
    14991506
    15001507    /* they might be on the same filesystem... */
    15011508    {
    1502         const int i = rename( oldpath, newpath );
     1509        const int i = tr_rename( oldpath, newpath );
    15031510        if( renamed != NULL )
    15041511            *renamed = i == 0;
    15051512        if( !i )
     
    15301537    if( bytesLeft != 0 )
    15311538        return -1;
    15321539
    1533     unlink( oldpath );
     1540    tr_unlink( oldpath );
    15341541    return 0;
    15351542}
    15361543
    15371544bool
    15381545tr_is_same_file( const char * filename1, const char * filename2 )
    15391546{
    1540     struct stat sb1, sb2;
     1547#ifdef WIN32    /* no inodes on Windows */
     1548    bool res;
     1549    HANDLE fh1, fh2;
     1550    BY_HANDLE_FILE_INFORMATION fi1, fi2;
     1551    int n = strlen(filename1) + 1;
     1552    int m = strlen(filename2) + 1;
     1553    wchar_t f1nameUTF16[n];
     1554    wchar_t f2nameUTF16[m];
    15411555
    1542     return !stat( filename1, &sb1 )
    1543         && !stat( filename2, &sb2 )
     1556    MultiByteToWideChar(CP_UTF8, 0, filename1, -1, f1nameUTF16, n);
     1557    MultiByteToWideChar(CP_UTF8, 0, filename2, -1, f2nameUTF16, m);
     1558    fh1 = CreateFileW(f1nameUTF16, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
     1559    fh2 = CreateFileW(f2nameUTF16, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
     1560    res =  GetFileInformationByHandle( fh1, &fi1 )
     1561        && GetFileInformationByHandle( fh2, &fi2 )
     1562        && ( fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber )
     1563        && ( fi1.nFileIndexHigh == fi2.nFileIndexHigh )
     1564        && ( fi1.nFileIndexLow  == fi2.nFileIndexLow );
     1565    CloseHandle(fh1);
     1566    CloseHandle(fh2);
     1567    return res;
     1568#else
     1569    STAT sb1, sb2;
     1570
     1571    return !tr_stati64( filename1, &sb1 )
     1572        && !tr_stati64( filename2, &sb2 )
    15441573        && ( sb1.st_dev == sb2.st_dev )
    15451574        && ( sb1.st_ino == sb2.st_ino );
     1575#endif
    15461576}
    15471577
    15481578/***
    15491579****
    15501580***/
    15511581
     1582/* getpagesize for windows */
     1583static long
     1584getpagesize( void )
     1585{
     1586    static long g_pagesize = 0;
     1587
     1588    if( !g_pagesize )
     1589    {
     1590        SYSTEM_INFO system_info;
     1591        GetSystemInfo ( &system_info );
     1592        g_pagesize = system_info.dwPageSize;
     1593    }
     1594    return g_pagesize;
     1595}
     1596
    15521597void*
    15531598tr_valloc( size_t bufLen )
    15541599{
     
    15831628    return buf;
    15841629}
    15851630
     1631#ifdef WIN32
     1632/* From:
     1633 *      "Obtaining a File Name From a File Handle"
     1634 *      http://msdn.microsoft.com/en-us/library/aa366789.aspx
     1635 * See also:
     1636 *      "GetFinalPathNameByHandle Function" (for Vista or newer)
     1637 *      http://msdn.microsoft.com/en-us/library/aa364962%28v=vs.85%29.aspx
     1638 * Note:
     1639 *     GetFileNameFromHandleW only works with files (not directories).
     1640 */
     1641#include <psapi.h>      /* needs linking with -lpsapi */
     1642
     1643static
     1644bool GetFileNameFromHandleW(HANDLE hFile,
     1645    wchar_t *filePath,
     1646    DWORD    filePathSize)
     1647{
     1648  bool   bSuccess = FALSE;
     1649  HANDLE hFileMap;
     1650
     1651  // Get the file size.
     1652  DWORD dwFileSizeHi = 0;
     1653  DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
     1654
     1655  if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
     1656  {
     1657     /* error "Cannot map a file with a length of zero.\n" */
     1658     return FALSE;
     1659  }
     1660
     1661  // Create a file mapping object.
     1662  hFileMap = CreateFileMapping(hFile,
     1663                    NULL,
     1664                    PAGE_READONLY,
     1665                    0,
     1666                    1,
     1667                    NULL);
     1668
     1669  if (hFileMap)
     1670  {
     1671    // Create a file mapping to get the file name.
     1672    void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
     1673
     1674    if (pMem)
     1675    {
     1676      if (GetMappedFileName (GetCurrentProcess(),
     1677                             pMem,
     1678                             filePath,
     1679                             filePathSize))
     1680      {
     1681        // Translate path with device name to drive letters.
     1682        wchar_t szTemp[MAX_PATH + 1];
     1683        szTemp[0] = '\0';
     1684
     1685        if (GetLogicalDriveStringsW(MAX_PATH, szTemp))
     1686        {
     1687          wchar_t szName[MAX_PATH + 1];
     1688          wchar_t szDrive[3] = L" :";
     1689          bool bFound = FALSE;
     1690          wchar_t * p = szTemp;
     1691
     1692          do
     1693          {
     1694            // Copy the drive letter to the template string
     1695            *szDrive = *p;
     1696
     1697            // Look up each device name
     1698            if (QueryDosDevice(szDrive, szName, MAX_PATH))
     1699            {
     1700              size_t uNameLen = wcslen(szName);
     1701
     1702              if (uNameLen < MAX_PATH)
     1703              {
     1704                bFound = _wcsnicmp(filePath, szName, uNameLen) == 0;
     1705
     1706                if (bFound && *(filePath + uNameLen) == L'\\')
     1707                {
     1708                  // Reconstruct filePath using szTempFile
     1709                  // Replace device path with DOS path
     1710                  wchar_t szTempFile[MAX_PATH + 1];
     1711                  _snwprintf( szTempFile,
     1712                            MAX_PATH,
     1713                            L"%s%s",
     1714                            szDrive,
     1715                            filePath + uNameLen);
     1716                  wcsncpy(filePath, szTempFile, wcslen(szTempFile));
     1717                  filePath[wcslen(szTempFile)] = L'\0';
     1718                }
     1719              }
     1720            }
     1721
     1722            // Go to the next NULL character.
     1723            while (*p++);
     1724          } while (!bFound && *p); // end of string
     1725        }
     1726      }
     1727      bSuccess = TRUE;
     1728      UnmapViewOfFile(pMem);
     1729    }
     1730    CloseHandle(hFileMap);
     1731  }
     1732  return(bSuccess);
     1733}
     1734#endif
     1735
    15861736char *
    15871737tr_realpath( const char * path, char * resolved_path )
    15881738{
    15891739#ifdef WIN32
    1590     /* From a message to the Mingw-msys list, Jun 2, 2005 by Mark Junker. */
    1591     if( GetFullPathNameA( path, TR_PATH_MAX, resolved_path, NULL ) == 0 )
     1740    HANDLE  hFile;
     1741    wchar_t pathUTF16[MAX_PATH+1];
     1742   
     1743    MultiByteToWideChar(CP_UTF8, 0, path, -1, pathUTF16, MAX_PATH+1);
     1744    hFile = CreateFileW(pathUTF16, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
     1745    if(hFile == INVALID_HANDLE_VALUE)
    15921746        return NULL;
     1747    if( !GetFileNameFromHandleW( hFile, pathUTF16, MAX_PATH ) )
     1748        return CloseHandle(hFile), NULL;
     1749    if (!resolved_path)
     1750        resolved_path = malloc(MAX_PATH + 1);
     1751    WideCharToMultiByte(CP_UTF8, 0, pathUTF16, -1, resolved_path, MAX_PATH+1, NULL, NULL);
     1752    CloseHandle(hFile);
    15931753    return resolved_path;
    15941754#else
    15951755    return realpath( path, resolved_path );
  • libtransmission/peer-msgs.c

     
    1616#include <stdlib.h>
    1717#include <string.h>
    1818
     19#ifndef WIN32
    1920#include <alloca.h>
     21#endif
    2022
    2123#include <event2/buffer.h>
    2224#include <event2/bufferevent.h>
  • libtransmission/blocklist.c

     
    3737#include "blocklist.h"
    3838#include "net.h"
    3939#include "utils.h"
     40#include "platform.h"
    4041
    4142#ifndef O_BINARY
    4243 #define O_BINARY 0
     
    5657struct tr_blocklist
    5758{
    5859    bool                   isEnabled;
     60#ifdef WIN32
     61    HANDLE                 fd;
     62#else
    5963    int                    fd;
     64#endif
    6065    size_t                 ruleCount;
    6166    size_t                 byteCount;
    6267    char *                 filename;
    6368    struct tr_ipv4_range * rules;
    6469};
    6570
     71#ifdef WIN32
     72    static HANDLE fh;
     73#endif
     74
    6675static void
    6776blocklistClose( tr_blocklist * b )
    6877{
    6978    if( b->rules )
    7079    {
     80#ifdef WIN32
     81        UnmapViewOfFile( b->rules );
     82        CloseHandle( fh );
     83        CloseHandle( b->fd );
     84        b->fd = NULL;
     85#else
    7186        munmap( b->rules, b->byteCount );
    7287        close( b->fd );
     88        b->fd = -1;
     89#endif
    7390        b->rules = NULL;
    7491        b->ruleCount = 0;
    7592        b->byteCount = 0;
    76         b->fd = -1;
    7793    }
    7894}
    7995
    8096static void
    8197blocklistLoad( tr_blocklist * b )
    8298{
     99#ifdef WIN32
     100    HANDLE fd;
     101    int n = strlen(b->filename) + 1;
     102    wchar_t nameUTF16[n];
     103#else
    83104    int fd;
     105#endif
    84106    size_t byteCount;
    85     struct stat st;
     107    STAT st;
    86108    const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
    87109
    88110    blocklistClose( b );
    89111
    90     if( stat( b->filename, &st ) == -1 )
     112    if( tr_stati64( b->filename, &st ) == -1 )
    91113        return;
    92114
    93     fd = open( b->filename, O_RDONLY | O_BINARY );
     115    byteCount = (size_t) st.st_size;
     116
     117#ifdef WIN32
     118    MultiByteToWideChar(CP_UTF8, 0, b->filename, -1, nameUTF16, n);
     119    fd = CreateFileW( nameUTF16, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0 );
     120    fh = CreateFileMapping( fd, NULL, PAGE_READONLY, 0, byteCount, NULL);
     121    if ( fh != NULL )
     122        b->rules = MapViewOfFile( fh, FILE_MAP_READ, 0, 0, 0 );
     123    else
     124        b->rules = NULL;
     125#else
     126    fd = tr_open( b->filename, O_RDONLY | O_BINARY );
    94127    if( fd == -1 )
    95128    {
    96129        tr_err( err_fmt, b->filename, tr_strerror( errno ) );
    97130        return;
    98131    }
    99132
    100     byteCount = (size_t) st.st_size;
    101133    b->rules = mmap( NULL, byteCount, PROT_READ, MAP_PRIVATE, fd, 0 );
     134#endif
    102135    if( !b->rules )
    103136    {
    104137        tr_err( err_fmt, b->filename, tr_strerror( errno ) );
     138#ifdef WIN32
     139        CloseHandle( fd );
     140#else
    105141        close( fd );
     142#endif
    106143        return;
    107144    }
    108145
     
    140177blocklistDelete( tr_blocklist * b )
    141178{
    142179    blocklistClose( b );
    143     unlink( b->filename );
     180    tr_unlink( b->filename );
    144181}
    145182
    146183/***
     
    153190    tr_blocklist * b;
    154191
    155192    b = tr_new0( tr_blocklist, 1 );
     193#ifdef WIN32
     194    b->fd = NULL;
     195#else
    156196    b->fd = -1;
     197#endif
    157198    b->filename = tr_strdup( filename );
    158199    b->isEnabled = isEnabled;
    159200
     
    177218int
    178219_tr_blocklistExists( const tr_blocklist * b )
    179220{
    180     struct stat st;
     221    STAT st;
    181222
    182     return !stat( b->filename, &st );
     223    return !tr_stati64( b->filename, &st );
    183224}
    184225
    185226int
     
    333374        return 0;
    334375    }
    335376
    336     in = fopen( filename, "rb" );
     377    in = tr_fopen( filename, "rb" );
    337378    if( !in )
    338379    {
    339380        tr_err( err_fmt, filename, tr_strerror( errno ) );
     
    342383
    343384    blocklistClose( b );
    344385
    345     out = fopen( b->filename, "wb+" );
     386    out = tr_fopen( b->filename, "wb+" );
    346387    if( !out )
    347388    {
    348389        tr_err( err_fmt, b->filename, tr_strerror( errno ) );
  • libtransmission/bencode.c

     
    2020
    2121#ifdef WIN32 /* tr_mkstemp() */
    2222 #include <fcntl.h>
    23  #define _S_IREAD 256
    24  #define _S_IWRITE 128
     23 #include <share.h>
     24 #include <sys/stat.h>
    2525#endif
    2626
    2727#include <locale.h> /* setlocale() */
     
    16511651tr_mkstemp( char * template )
    16521652{
    16531653#ifdef WIN32
     1654    int   n = strlen(template) + 1;
    16541655    const int flags = O_RDWR | O_BINARY | O_CREAT | O_EXCL | _O_SHORT_LIVED;
    16551656    const mode_t mode = _S_IREAD | _S_IWRITE;
    1656     mktemp( template );
    1657     return open( template, flags, mode );
     1657    wchar_t templateUTF16[n];
     1658    if (MultiByteToWideChar(CP_UTF8, 0, template, -1, templateUTF16, n))
     1659    {
     1660        _wmktemp(templateUTF16);
     1661        WideCharToMultiByte(CP_UTF8, 0, templateUTF16, -1, template, n, NULL, NULL);
     1662        return _wopen(templateUTF16, flags, mode);
     1663    }
     1664    errno = EINVAL;
     1665    return -1;
    16581666#else
    16591667    return mkstemp( template );
    16601668#endif
     
    17061714        {
    17071715            tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
    17081716            tr_close_file( fd );
    1709             unlink( tmp );
     1717            tr_unlink( tmp );
    17101718        }
    17111719        else
    17121720        {
    17131721            //tr_fsync( fd );
    17141722            tr_close_file( fd );
    1715 
    1716 #ifdef WIN32
    1717             if( MoveFileEx( tmp, filename, MOVEFILE_REPLACE_EXISTING ) )
    1718 #else
    1719             if( !rename( tmp, filename ) )
    1720 #endif
     1723            if( !tr_rename( tmp, filename ) )
    17211724            {
    17221725                tr_inf( _( "Saved \"%s\"" ), filename );
    17231726            }
     
    17251728            {
    17261729                err = errno;
    17271730                tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
    1728                 unlink( tmp );
     1731                tr_unlink( tmp );
    17291732            }
    17301733        }
    17311734    }
  • libtransmission/metainfo.c

     
    6363{
    6464    int i;
    6565    char * path;
    66     struct stat sb;
     66    STAT sb;
    6767    const int tagCount = 5;
    6868    const char * tags[] = { "beos", "cli", "daemon", "macosx", "wx" };
    6969
    7070    /* test the beos, cli, daemon, macosx, wx tags */
    7171    for( i=0; i<tagCount; ++i ) {
    7272        path = tr_strdup_printf( "%s%c%s-%s", tr_getTorrentDir( session ), '/', inf->hashString, tags[i] );
    73         if( !stat( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
     73        if( !tr_stati64( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
    7474            return path;
    7575        tr_free( path );
    7676    }
    7777
    7878    /* test a non-tagged file */
    7979    path = tr_buildPath( tr_getTorrentDir( session ), inf->hashString, NULL );
    80     if( !stat( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
     80    if( !tr_stati64( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
    8181        return path;
    8282    tr_free( path );
    8383
     
    9292tr_metainfoMigrate( tr_session * session,
    9393                    tr_info *   inf )
    9494{
    95     struct stat new_sb;
     95    STAT        new_sb;
    9696    char *      name = getTorrentFilename( session, inf );
    9797
    98     if( stat( name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
     98    if( tr_stati64( name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
    9999    {
    100100        char *    old_name = getOldTorrentFilename( session, inf );
    101101        size_t    contentLen;
     
    106106        {
    107107            FILE * out;
    108108            errno = 0;
    109             out = fopen( name, "wb+" );
     109            out = tr_fopen( name, "wb+" );
    110110            if( !out )
    111111            {
    112112                tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ),
     
    120120                    tr_free( inf->torrent );
    121121                    inf->torrent = tr_strdup( name );
    122122                    tr_sessionSetTorrentFile( session, inf->hashString, name );
    123                     unlink( old_name );
     123                    tr_unlink( old_name );
    124124                }
    125125                fclose( out );
    126126            }
     
    599599    char * filename;
    600600
    601601    filename = getTorrentFilename( session, inf );
    602     unlink( filename );
     602    tr_unlink( filename );
    603603    tr_free( filename );
    604604
    605605    filename = getOldTorrentFilename( session, inf );
    606     unlink( filename );
     606    tr_unlink( filename );
    607607    tr_free( filename );
    608608}
  • libtransmission/rpcimpl.c

     
    11851185                tr_close_file( fd );
    11861186            }
    11871187
    1188             unlink( filename );
     1188            tr_unlink( filename );
    11891189            tr_free( filename );
    11901190            filename = filename2;
    11911191        }
     
    11981198            tr_snprintf( result, sizeof( result ), "success" );
    11991199        }
    12001200
    1201         unlink( filename );
     1201        tr_unlink( filename );
    12021202        tr_free( filename );
    12031203    }
    12041204
  • libtransmission/announcer-udp.c

     
    1313#define __LIBTRANSMISSION_ANNOUNCER_MODULE___
    1414
    1515#include <string.h> /* memcpy(), memset() */
     16#include <errno.h>
    1617
    1718#include <event2/buffer.h>
    1819#include <event2/dns.h>
  • libtransmission/fdlimit.c

     
    5252#include "net.h"
    5353#include "session.h"
    5454#include "torrent.h" /* tr_isTorrent() */
     55#include "platform.h"
    5556
    5657#define dbgmsg( ... ) \
    5758    do { \
     
    107108
    108109#ifdef WIN32
    109110
    110     HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0 );
    111     if( hFile != INVALID_HANDLE_VALUE )
     111    int n = strlen(filename) + 1;
     112    wchar_t filenameUTF16[n];
     113    HANDLE hFile;
     114    if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameUTF16, n))
    112115    {
    113         LARGE_INTEGER li;
    114         li.QuadPart = length;
    115         success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
    116         CloseHandle( hFile );
     116        hFile = CreateFileW( filenameUTF16, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0 );
     117        if( hFile != INVALID_HANDLE_VALUE )
     118        {
     119            LARGE_INTEGER li;
     120            li.QuadPart = length;
     121            success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN )
     122                    && SetEndOfFile( hFile );
     123/*                  && SetFileValidData( hFile, length );*/
     124            CloseHandle( hFile );
     125        }
    117126    }
    118127
    119128#else
    120129
    121130    int flags = O_RDWR | O_CREAT | O_LARGEFILE;
    122     int fd = open( filename, flags, 0666 );
     131    int fd = tr_open( filename, flags, 0666 );
    123132    if( fd >= 0 )
    124133    {
    125134# ifdef HAVE_FALLOCATE64
     
    276285static int
    277286open_local_file( const char * filename, int flags )
    278287{
    279     const int fd = open( filename, flags, 0666 );
     288    const int fd = tr_open( filename, flags, 0666 );
    280289    tr_set_file_for_single_pass( fd );
    281290    return fd;
    282291}
     
    355364                  uint64_t                 file_size )
    356365{
    357366    int flags;
    358     struct stat sb;
     367    STAT sb;
    359368    bool alreadyExisted;
    360369
    361370    /* confirm that existing_dir, if specified, exists on the disk */
    362     if( existing_dir && *existing_dir && stat( existing_dir, &sb ) )
     371    if( existing_dir && *existing_dir && tr_stati64( existing_dir, &sb ) )
    363372    {
    364373        const int err = errno;
    365374        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), existing_dir, tr_strerror( err ) );
     
    379388        tr_free( dir );
    380389    }
    381390
    382     alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
     391    alreadyExisted = !tr_stati64( filename, &sb ) && S_ISREG( sb.st_mode );
    383392
    384393    if( writable && !alreadyExisted && ( allocation == TR_PREALLOCATE_FULL ) )
    385394        if( preallocate_file_full( filename, file_size ) )
     
    388397    /* open the file */
    389398    flags = writable ? ( O_RDWR | O_CREAT ) : O_RDONLY;
    390399    flags |= O_LARGEFILE | O_BINARY | O_SEQUENTIAL;
    391     o->fd = open( filename, flags, 0666 );
     400    o->fd = tr_open( filename, flags, 0666 );
    392401
    393402    if( o->fd == -1 )
    394403    {
     
    775784
    776785    ensureSessionFdInfoExists( session );
    777786
    778     max = FD_SETSIZE - session->fdInfo->socket_limit - buffer_slots;
     787    max = MAX( FD_SETSIZE - session->fdInfo->socket_limit - buffer_slots, limit );
    779788    if( limit > max )
    780789        limit = max;
    781790
  • libtransmission/fdlimit.h

     
    1616
    1717#include "transmission.h"
    1818#include "net.h"
     19#ifdef WIN32
     20 #include <sys/types.h> /* ssize_t, off_t */
     21#endif
    1922
    2023/**
    2124 * @addtogroup file_io File IO
  • libtransmission/tr-dht.c

     
    122122
    123123    rc = getaddrinfo(name, pp, &hints, &info);
    124124    if(rc != 0) {
    125         tr_nerr("DHT", "%s:%s: %s", name, pp, gai_strerror(rc));
     125        tr_nerr("DHT", "%s:%s: %s", name, pp, gai_strerrorA(rc));
    126126        return;
    127127    }
    128128
     
    200200            tr_buildPath(cl->session->configDir, "dht.bootstrap", NULL);
    201201
    202202        if(bootstrap_file)
    203             f = fopen(bootstrap_file, "rb");
     203            f = tr_fopen(bootstrap_file, "rb");
    204204        if(f != NULL) {
    205205            tr_ninf("DHT", "Attempting manual bootstrap");
    206206            for(;;) {
  • libtransmission/torrent-magnet.c

     
    2626#include "torrent-magnet.h"
    2727#include "utils.h"
    2828#include "web.h"
     29#include "platform.h"
    2930
    3031#define dbgmsg( tor, ... ) \
    3132    do { \
     
    161162        assert( tor->infoDictLength > 0 );
    162163        assert( tor->infoDictOffset >= 0 );
    163164
    164         fp = fopen( tor->info.torrent, "rb" );
     165        fp = tr_fopen( tor->info.torrent, "rb" );
    165166        if( fp != NULL )
    166167        {
    167168            const int o = piece  * METADATA_PIECE_SIZE;
     
    259260                    int infoDictLength;
    260261
    261262                    /* remove any old .torrent and .resume files */
    262                     remove( path );
     263                    tr_remove( path );
    263264                    tr_torrentRemoveResume( tor );
    264265
    265266                    dbgmsg( tor, "Saving completed metadata to \"%s\"", path );
  • libtransmission/platform.c

     
    247247
    248248    if( !home )
    249249    {
    250         home = tr_strdup( getenv( "HOME" ) );
     250        home = tr_strdup( tr_getenv( "HOME" ) );
    251251
    252252        if( !home )
    253253        {
    254254#ifdef WIN32
    255             char appdata[MAX_PATH]; /* SHGetFolderPath() requires MAX_PATH */
     255            wchar_t appdataUTF16[MAX_PATH + 1];
     256            char    appdata[MAX_PATH + 1]; /* SHGetFolderPath() requires MAX_PATH */
    256257            *appdata = '\0';
    257             SHGetFolderPath( NULL, CSIDL_PERSONAL, NULL, 0, appdata );
     258            SHGetFolderPathW( NULL, CSIDL_PERSONAL, NULL, 0, appdataUTF16 );
     259            WideCharToMultiByte(CP_UTF8, 0, appdataUTF16, -1, appdata, MAX_PATH+1, NULL, NULL);
    258260            home = tr_strdup( appdata );
    259261#else
    260262            struct passwd * pw = getpwuid( getuid( ) );
     
    283285                              "Application Support",
    284286                              "Transmission", NULL );
    285287#elif defined( WIN32 )
    286         char appdata[MAX_PATH]; /* SHGetFolderPath() requires MAX_PATH */
    287         SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, appdata );
     288        wchar_t appdataUTF16[MAX_PATH + 1];
     289        char    appdata[MAX_PATH + 1]; /* SHGetFolderPath() requires MAX_PATH */
     290        SHGetFolderPathW( NULL, CSIDL_APPDATA, NULL, 0, appdataUTF16 );
     291        WideCharToMultiByte(CP_UTF8, 0, appdataUTF16, -1, appdata, MAX_PATH+1, NULL, NULL);
    288292        path = tr_buildPath( appdata, "Transmission", NULL );
    289293#elif defined( __HAIKU__ )
    290294        char buf[TR_PATH_MAX];
     
    342346{
    343347    if( oldDir && newDir && strcmp( oldDir, newDir ) )
    344348    {
    345         DIR * dirh = opendir( oldDir );
     349        DIR * dirh = tr_opendir( oldDir );
    346350        if( dirh )
    347351        {
    348             int             count = 0;
    349             struct dirent * dirp;
    350             while( ( dirp = readdir( dirh ) ) )
     352            int      count = 0;
     353            DIRENT * dirp;
     354            while( ( dirp = tr_readdir( dirh ) ) )
    351355            {
     356#ifdef WIN32
     357                int  n = WideCharToMultiByte(CP_UTF8, 0, dirp->d_name, -1, NULL, 0, NULL, NULL);
     358                char name[n];
     359                WideCharToMultiByte(CP_UTF8, 0, dirp->d_name, -1, name, n, NULL, NULL);
     360#else
    352361                const char * name = dirp->d_name;
     362#endif
    353363                if( name && strcmp( name, "." ) && strcmp( name, ".." ) )
    354364                {
    355365                    char * o = tr_buildPath( oldDir, name, NULL );
    356366                    char * n = tr_buildPath( newDir, name, NULL );
    357                     rename( o, n );
     367                    tr_rename( o, n );
    358368                    ++count;
    359369                    tr_free( n );
    360370                    tr_free( o );
     
    364374            if( count )
    365375                tr_inf( _( "Migrated %1$d files from \"%2$s\" to \"%3$s\"" ),
    366376                        count, oldDir, newDir );
    367             closedir( dirh );
     377            tr_closedir( dirh );
    368378        }
    369379    }
    370380}
     
    442452
    443453    if( !s )
    444454    {
    445         if( ( s = getenv( "TRANSMISSION_HOME" ) ) )
     455        if( ( s = tr_getenv( "TRANSMISSION_HOME" ) ) )
    446456        {
    447457            s = tr_strdup( s );
    448458        }
     
    452462            s = tr_buildPath( getHomeDir( ), "Library", "Application Support",
    453463                              appname, NULL );
    454464#elif defined( WIN32 )
    455             char appdata[TR_PATH_MAX]; /* SHGetFolderPath() requires MAX_PATH */
    456             SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, appdata );
     465            wchar_t appdataUTF16[MAX_PATH + 1];
     466            char    appdata[TR_PATH_MAX + 1]; /* SHGetFolderPath() requires MAX_PATH */
     467            SHGetFolderPathW( NULL, CSIDL_APPDATA, NULL, 0, appdataUTF16 );
     468            WideCharToMultiByte(CP_UTF8, 0, appdataUTF16, -1, appdata, MAX_PATH+1, NULL, NULL);
    457469            s = tr_buildPath( appdata, appname, NULL );
    458470#elif defined( __HAIKU__ )
    459471            char buf[TR_PATH_MAX];
    460472            find_directory( B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof(buf) );
    461473            s = tr_buildPath( buf, appname, NULL );
    462474#else
    463             if( ( s = getenv( "XDG_CONFIG_HOME" ) ) )
     475            if( ( s = tr_getenv( "XDG_CONFIG_HOME" ) ) )
    464476                s = tr_buildPath( s, appname, NULL );
    465477            else
    466478                s = tr_buildPath( getHomeDir( ), ".config", appname, NULL );
     
    484496        size_t content_len;
    485497
    486498        /* figure out where to look for user-dirs.dirs */
    487         config_home = getenv( "XDG_CONFIG_HOME" );
     499        config_home = tr_getenv( "XDG_CONFIG_HOME" );
    488500        if( config_home && *config_home )
    489501            config_file = tr_buildPath( config_home, "user-dirs.dirs", NULL );
    490502        else
     
    534546static int
    535547isWebClientDir( const char * path )
    536548{
    537     struct stat sb;
     549    STAT  sb;
    538550    char * tmp = tr_buildPath( path, "index.html", NULL );
    539     const int ret = !stat( tmp, &sb );
     551    const int ret = !tr_stati64( tmp, &sb );
    540552    tr_inf( _( "Searching for web interface file \"%s\"" ), tmp );
    541553    tr_free( tmp );
    542554    return ret;
     
    549561
    550562    if( !s )
    551563    {
    552         if( ( s = getenv( "CLUTCH_HOME" ) ) )
     564        if( ( s = tr_getenv( "CLUTCH_HOME" ) ) )
    553565        {
    554566            s = tr_strdup( s );
    555567        }
    556         else if( ( s = getenv( "TRANSMISSION_WEB_HOME" ) ) )
     568        else if( ( s = tr_getenv( "TRANSMISSION_WEB_HOME" ) ) )
    557569        {
    558570            s = tr_strdup( s );
    559571        }
     
    597609#elif defined( WIN32 )
    598610
    599611            /* SHGetFolderPath explicitly requires MAX_PATH length */
    600             char dir[MAX_PATH];
     612            wchar_t dirUTF16[MAX_PATH + 1];
     613            char    dir[MAX_PATH + 1];
    601614
    602615            /* Generally, Web interface should be stored in a Web subdir of
    603616             * calling executable dir. */
    604617
    605618            if( s == NULL ) {
    606619                /* First, we should check personal AppData/Transmission/Web */
    607                 SHGetFolderPath( NULL, CSIDL_COMMON_APPDATA, NULL, 0, dir );
     620                SHGetFolderPathW( NULL, CSIDL_COMMON_APPDATA, NULL, 0, dirUTF16 );
     621                WideCharToMultiByte(CP_UTF8, 0, dirUTF16, -1, dir, MAX_PATH+1, NULL, NULL);
    608622                s = tr_buildPath( dir, "Transmission", "Web", NULL );
    609623                if( !isWebClientDir( s ) ) {
    610624                    tr_free( s );
     
    614628
    615629            if( s == NULL ) {
    616630                /* check personal AppData */
    617                 SHGetFolderPath( NULL, CSIDL_APPDATA, NULL, 0, dir );
     631                SHGetFolderPathW( NULL, CSIDL_APPDATA, NULL, 0, dirUTF16 );
     632                WideCharToMultiByte(CP_UTF8, 0, dirUTF16, -1, dir, MAX_PATH+1, NULL, NULL);
    618633                s = tr_buildPath( dir, "Transmission", "Web", NULL );
    619634                if( !isWebClientDir( s ) ) {
    620635                    tr_free( s );
     
    624639
    625640            if( s == NULL) {
    626641                /* check calling module place */
    627                 GetModuleFileName( GetModuleHandle( NULL ), dir, sizeof( dir ) );
     642                GetModuleFileNameW( NULL, dirUTF16, sizeof( dirUTF16 ) );
     643                WideCharToMultiByte(CP_UTF8, 0, dirUTF16, -1, dir, MAX_PATH+1, NULL, NULL);
    628644                s = tr_buildPath( dirname( dir ), "Web", NULL );
    629645                if( !isWebClientDir( s ) ) {
    630646                    tr_free( s );
     
    638654            const char * tmp;
    639655
    640656            /* XDG_DATA_HOME should be the first in the list of candidates */
    641             tmp = getenv( "XDG_DATA_HOME" );
     657            tmp = tr_getenv( "XDG_DATA_HOME" );
    642658            if( tmp && *tmp )
    643659                tr_list_append( &candidates, tr_strdup( tmp ) );
    644660            else {
     
    649665            /* XDG_DATA_DIRS are the backup directories */
    650666            {
    651667                const char * pkg = PACKAGE_DATA_DIR;
    652                 const char * xdg = getenv( "XDG_DATA_DIRS" );
     668                const char * xdg = tr_getenv( "XDG_DATA_DIRS" );
    653669                const char * fallback = "/usr/local/share:/usr/share";
    654670                char * buf = tr_strdup_printf( "%s:%s:%s", (pkg?pkg:""), (xdg?xdg:""), fallback );
    655671                tmp = buf;
     
    697713{
    698714#ifdef WIN32
    699715    uint64_t freeBytesAvailable = 0;
    700     return GetDiskFreeSpaceEx( path, &freeBytesAvailable, NULL, NULL)
     716    int n = strlen(path) + 1;
     717    wchar_t pathUTF16[n];
     718    MultiByteToWideChar(CP_UTF8, 0, path, -1, pathUTF16, n);
     719    return GetDiskFreeSpaceExW( pathUTF16, &freeBytesAvailable, NULL, NULL)
    701720        ? (int64_t)freeBytesAvailable
    702721        : -1;
    703722#elif defined(HAVE_STATVFS)
     
    713732****
    714733***/
    715734
     735int tr_open(const char *pathUTF8, int oflag, ...)
     736{
     737    va_list a;
     738    int mode = 0;
     739    va_start(a, oflag);
     740    mode = va_arg(a, int);
     741    va_end(a);
    716742#ifdef WIN32
     743    if (pathUTF8) {
     744        int n = strlen(pathUTF8) + 1;
     745        wchar_t pathUTF16[n];
     746        if (MultiByteToWideChar(CP_UTF8, 0, pathUTF8, -1, pathUTF16, n))
     747            /* binary mode is mandatory, otherwise UTF16 data is assumed */
     748            return _wopen(pathUTF16, oflag | _O_BINARY, mode);
     749    }
     750    errno = ENOENT;
     751    return -1;
     752#else
     753    return open(pathUTF8, oflag, mode);
     754#endif
     755}
    717756
    718 /* The following mmap functions are by Joerg Walter, and were taken from
    719  * his paper at: http://www.genesys-e.de/jwalter/mix4win.htm
    720  */
    721 
    722 #if defined(_MSC_VER)
    723 __declspec( align( 4 ) ) static LONG volatile g_sl;
     757FILE *tr_fopen(const char *restrict filenameUTF8, const char *restrict mode)
     758{   
     759#ifdef WIN32
     760    if (filenameUTF8) {
     761        int n = strlen(filenameUTF8) + 1;
     762        int m = strlen(mode) + 2;
     763        wchar_t filenameUTF16[n];
     764        wchar_t modeUTF16[m];
     765        MultiByteToWideChar(CP_UTF8, 0, filenameUTF8, -1, filenameUTF16, n);
     766        MultiByteToWideChar(CP_UTF8, 0, mode, -1, modeUTF16, m);
     767        /* binary mode is mandatory, otherwise UTF16 data is assumed */
     768        if (!strchr(mode, 'b'))
     769            wcscat(modeUTF16, L"b");
     770        return _wfopen(filenameUTF16, modeUTF16);
     771    }
     772    return NULL;
    724773#else
    725 static LONG volatile g_sl __attribute__ ( ( aligned ( 4 ) ) );
     774    return fopen(filenameUTF8, mode);
    726775#endif
    727 
    728 /* Wait for spin lock */
    729 static int
    730 slwait( LONG volatile *sl )
    731 {
    732     while( InterlockedCompareExchange ( sl, 1, 0 ) != 0 )
    733         Sleep ( 0 );
    734 
    735     return 0;
    736776}
    737777
    738 /* Release spin lock */
    739 static int
    740 slrelease( LONG volatile *sl )
     778int tr_rename(const char *oldUTF8, const char *newUTF8)
    741779{
    742     InterlockedExchange ( sl, 0 );
    743     return 0;
     780#ifdef WIN32
     781    if (oldUTF8 && newUTF8) {
     782        int n = strlen(oldUTF8) + 1;
     783        int m = strlen(newUTF8) + 1;
     784        wchar_t oldUTF16[n];
     785        wchar_t newUTF16[m];
     786        if ( MultiByteToWideChar(CP_UTF8, 0, oldUTF8, -1, oldUTF16, n) && \
     787             MultiByteToWideChar(CP_UTF8, 0, newUTF8, -1, newUTF16, m) )
     788            return MoveFileExW(oldUTF16, newUTF16, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)? 0 : -1;
     789    }
     790    errno = EINVAL;
     791    return -1;
     792#else
     793    return rename(oldUTF8, newUTF8);
     794#endif
    744795}
    745796
    746 /* getpagesize for windows */
    747 static long
    748 getpagesize( void )
     797int tr_remove(const char *filenameUTF8)
    749798{
    750     static long g_pagesize = 0;
    751 
    752     if( !g_pagesize )
     799#ifdef WIN32
     800    if (filenameUTF8)
    753801    {
    754         SYSTEM_INFO system_info;
    755         GetSystemInfo ( &system_info );
    756         g_pagesize = system_info.dwPageSize;
     802        int n = strlen(filenameUTF8) + 1;
     803        wchar_t filenameUTF16[n];
     804        if ( MultiByteToWideChar(CP_UTF8, 0, filenameUTF8, -1, filenameUTF16, n) )
     805        {
     806            int res = _wremove(filenameUTF16);
     807            if (res)    /* EACCES or ENOENT, might be a directory */
     808                return _wrmdir(filenameUTF16);
     809            return res;
     810        }
    757811    }
    758     return g_pagesize;
     812    errno = ENOENT;
     813    return -1;
     814#else
     815    return remove(filenameUTF8);
     816#endif
    759817}
    760818
    761 static long
    762 getregionsize( void )
     819int tr_unlink(const char *filenameUTF8)
    763820{
    764     static long g_regionsize = 0;
    765 
    766     if( !g_regionsize )
     821#ifdef WIN32
     822    if (filenameUTF8)
    767823    {
    768         SYSTEM_INFO system_info;
    769         GetSystemInfo ( &system_info );
    770         g_regionsize = system_info.dwAllocationGranularity;
     824        int n = strlen(filenameUTF8) + 1;
     825        wchar_t filenameUTF16[n];
     826        if ( MultiByteToWideChar(CP_UTF8, 0, filenameUTF8, -1, filenameUTF16, n) )
     827            return _wunlink(filenameUTF16);
    771828    }
    772     return g_regionsize;
     829    errno = ENOENT;
     830    return -1;
     831#else
     832    return unlink(filenameUTF8);
     833#endif
    773834}
    774835
    775 void *
    776 mmap( void *ptr,
    777       long  size,
    778       long  prot,
    779       long  type,
    780       long  handle,
    781       long  arg )
     836int tr_stati64(const char *restrict pathUTF8, STAT *restrict buf)
    782837{
    783     static long g_pagesize;
    784     static long g_regionsize;
    785 
    786     /* Wait for spin lock */
    787     slwait ( &g_sl );
    788     /* First time initialization */
    789     if( !g_pagesize )
    790         g_pagesize = getpagesize ( );
    791     if( !g_regionsize )
    792         g_regionsize = getregionsize ( );
    793     /* Allocate this */
    794     ptr = VirtualAlloc ( ptr, size,
    795                          MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN,
    796                          PAGE_READWRITE );
    797     if( !ptr )
    798     {
    799         ptr = (void *) -1;
    800         goto mmap_exit;
     838#ifdef WIN32
     839    if (pathUTF8) {
     840        int n = strlen(pathUTF8) + 1;
     841        wchar_t pathUTF16[n];
     842        if (MultiByteToWideChar(CP_UTF8, 0, pathUTF8, -1, pathUTF16, n))
     843            return _wstati64(pathUTF16, buf);
    801844    }
    802 mmap_exit:
    803     /* Release spin lock */
    804     slrelease ( &g_sl );
    805     return ptr;
     845    errno = ENOENT;
     846    return -1;
     847#else
     848    return stat(pathUTF8, buf);
     849#endif
    806850}
    807851
    808 long
    809 munmap( void *ptr,
    810         long  size )
     852DIR *tr_opendir (const char *dirnameUTF8)
    811853{
    812     static long g_pagesize;
    813     static long g_regionsize;
    814     int         rc = -1;
    815 
    816     /* Wait for spin lock */
    817     slwait ( &g_sl );
    818     /* First time initialization */
    819     if( !g_pagesize )
    820         g_pagesize = getpagesize ( );
    821     if( !g_regionsize )
    822         g_regionsize = getregionsize ( );
    823     /* Free this */
    824     if( !VirtualFree ( ptr, 0,
    825                        MEM_RELEASE ) )
    826         goto munmap_exit;
    827     rc = 0;
    828 munmap_exit:
    829     /* Release spin lock */
    830     slrelease ( &g_sl );
    831     return rc;
     854#ifdef WIN32
     855    if (dirnameUTF8) {
     856        int n = strlen(dirnameUTF8) + 1;
     857        wchar_t dirnameUTF16[n];
     858        if (MultiByteToWideChar(CP_UTF8, 0, dirnameUTF8, -1, dirnameUTF16, n))
     859            return _wopendir(dirnameUTF16);
     860    }
     861    errno = ENOENT;
     862    return NULL;
     863#else
     864    return opendir( dirnameUTF8 );
     865#endif
    832866}
    833867
     868char *tr_getenv (const char *name)
     869{
     870#ifdef WIN32
     871    wchar_t *valueUTF16 = NULL;
     872    char    *value;
     873    int     n = strlen(name) + 1;
     874    wchar_t nameUTF16[n];
     875   
     876    if (MultiByteToWideChar(CP_UTF8, 0, name, -1, nameUTF16, n))
     877        valueUTF16= _wgetenv(nameUTF16);
     878    if (valueUTF16)
     879    {
     880        n = WideCharToMultiByte(CP_UTF8, 0, valueUTF16, -1, NULL, 0, NULL, NULL);
     881        value = malloc(n);      /* leak, could be improved if getenv() returns the same string as the converted one */
     882        WideCharToMultiByte(CP_UTF8, 0, valueUTF16, -1, value, n, NULL, NULL);
     883    }
     884    else
     885        value= NULL;
     886    return value;
     887#else
     888    return getenv(name);
    834889#endif
     890}
  • libtransmission/platform.h

     
    9696/** @brief return nonzero if the specified lock is locked */
    9797int tr_lockHave( const tr_lock * );
    9898
     99/* @} */
     100
     101/**
     102 * @addtogroup utf16 Windows_UTF16_Support
     103 * @{
     104 */
     105
     106/**
     107 * @brief Portability wrapper for open()
     108 */
    99109#ifdef WIN32
    100 void * mmap( void *ptr, long  size, long  prot, long  type, long  handle, long  arg );
     110 #include <wchar.h>     /* _wopen() */
     111 #include <errno.h>     /* ENOENT */
     112#endif
    101113
    102 long munmap( void *ptr, long  size );
     114int tr_open(const char *, int, ...);
     115
     116/**
     117 * @brief Portability wrapper for fopen()
     118 */
     119FILE *tr_fopen(const char *, const char *);
     120
     121/**
     122 * @brief Portability wrapper for mkdir()
     123 * @see tr_mkdir() in utils.c
     124 */
     125
     126/**
     127 * @brief Portability wrapper for mkstemp()
     128 * @see tr_mkstemp() in bencode.c
     129 */
     130
     131/**
     132 * @brief Portability wrapper for rename()
     133 */
     134int tr_rename(const char *, const char *);
     135
     136/**
     137 * @brief Portability wrapper for remove()
     138 */
     139int tr_remove(const char *);
     140
     141/**
     142 * @brief Portability wrapper for unlink()
     143 */
     144int tr_unlink(const char *);
     145
     146/**
     147 * @brief Portability wrapper for stat()
     148 */
     149#ifdef WIN32
     150 #include <sys/stat.h>
     151 typedef struct _stati64 STAT;
     152#else
     153 typedef struct stat STAT;
    103154#endif
    104155
     156int tr_stati64(const char *, STAT *);
     157
     158/**
     159 * @brief Portability wrapper for opendir()
     160 */
     161#ifdef WIN32
     162 #include <dirent.h>    /* DIR */
     163 #define DIR    _WDIR
     164 typedef struct _wdirent DIRENT;
     165 #define IS_DOT(c)      (c == 0x002e)
     166#else
     167 typedef struct dirent DIRENT;
     168 #define IS_DOT(c)      (c == '.')
     169#endif
     170
     171DIR *tr_opendir (const char *);
     172
     173/**
     174 * @brief Portability wrapper for readdir()
     175 */
     176static inline
     177DIRENT *tr_readdir (DIR *dirp)
     178{
     179#ifdef WIN32
     180    return _wreaddir(dirp);
     181#else
     182    return readdir(dirp);
     183#endif
     184}
     185
     186/**
     187 * @brief Portability wrapper for closedir()
     188 */
     189static inline
     190int tr_closedir (DIR *dirp)
     191{
     192#ifdef WIN32
     193    return _wclosedir(dirp);
     194#else
     195    return closedir(dirp);
     196#endif
     197}
     198
     199/**
     200 * @brief Portability wrapper for getenv()
     201 */
     202char *tr_getenv (const char *);
     203
    105204/* @} */
    106205
    107206#endif
     207
  • libtransmission/resume.c

     
    885885tr_torrentRemoveResume( const tr_torrent * tor )
    886886{
    887887    char * filename = getResumeFilename( tor );
    888     unlink( filename );
     888    tr_unlink( filename );
    889889    tr_free( filename );
    890890}
  • libtransmission/upnp.c

     
    9797    {
    9898        struct UPNPDev * devlist;
    9999        errno = 0;
    100         devlist = upnpDiscover( 2000, NULL, NULL, 0 );
     100        devlist = upnpDiscover( 2000, NULL, NULL, 0, &errno );
    101101        if( devlist == NULL )
    102102        {
    103103            tr_ndbg(
     
    106106        }
    107107        errno = 0;
    108108        if( UPNP_GetValidIGD( devlist, &handle->urls, &handle->data,
    109                              handle->lanaddr, sizeof( handle->lanaddr ) ) == UPNP_IGD_VALID_CONNECTED )
     109                             handle->lanaddr, sizeof( handle->lanaddr ) ) )
    110110        {
    111111            tr_ninf( getKey( ), _(
    112112                         "Found Internet Gateway Device \"%s\"" ),
     
    144144
    145145        tr_snprintf( portStr, sizeof( portStr ), "%d", handle->port );
    146146        if( UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype,
    147             portStr, "TCP", intClient, intPort ) != UPNPCOMMAND_SUCCESS  ||
     147            portStr, "TCP", intClient, intPort, NULL, NULL, NULL ) != UPNPCOMMAND_SUCCESS  ||
    148148            UPNP_GetSpecificPortMappingEntry( handle->urls.controlURL, handle->data.first.servicetype,
    149             portStr, "UDP", intClient, intPort ) != UPNPCOMMAND_SUCCESS )
     149            portStr, "UDP", intClient, intPort, NULL, NULL, NULL ) != UPNPCOMMAND_SUCCESS )
    150150        {
    151151            tr_ninf( getKey( ), _( "Port %d isn't forwarded" ), handle->port );
    152152            handle->isMapped = false;
     
    198198            err_tcp = UPNP_AddPortMapping( handle->urls.controlURL,
    199199                                       handle->data.first.servicetype,
    200200                                       portStr, portStr, handle->lanaddr,
    201                                        desc, "TCP", NULL );
     201                                       desc, "TCP", NULL, NULL );
    202202            if( err_tcp )
    203203                tr_ndbg( getKey( ), "TCP Port forwarding failed with error %d (errno %d - %s)",
    204204                         err_tcp, errno, tr_strerror( errno ) );
     
    207207            err_udp = UPNP_AddPortMapping( handle->urls.controlURL,
    208208                                       handle->data.first.servicetype,
    209209                                       portStr, portStr, handle->lanaddr,
    210                                        desc, "UDP", NULL );
     210                                       desc, "UDP", NULL, NULL );
    211211            if( err_udp )
    212212                tr_ndbg( getKey( ), "UDP Port forwarding failed with error %d (errno %d - %s)",
    213213                         err_udp, errno, tr_strerror( errno ) );
  • libtransmission/session.c

     
    18571857                        int        * setmeCount )
    18581858{
    18591859    int           i, n = 0;
    1860     struct stat   sb;
     1860    STAT          sb;
    18611861    DIR *         odir = NULL;
    18621862    const char *  dirname = tr_getTorrentDir( session );
    18631863    tr_torrent ** torrents;
     
    18671867
    18681868    tr_ctorSetSave( ctor, false ); /* since we already have them */
    18691869
    1870     if( !stat( dirname, &sb )
     1870    if( !tr_stati64( dirname, &sb )
    18711871      && S_ISDIR( sb.st_mode )
    1872       && ( ( odir = opendir ( dirname ) ) ) )
     1872      && ( ( odir = tr_opendir ( dirname ) ) ) )
    18731873    {
    1874         struct dirent *d;
    1875         for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
     1874        DIRENT *d;
     1875        for( d = tr_readdir( odir ); d != NULL; d = tr_readdir( odir ) )
    18761876        {
    1877             if( tr_str_has_suffix( d->d_name, ".torrent" ) )
     1877#ifdef WIN32
     1878            int m = WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL );
     1879            char name[m];
     1880            WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, name, m, NULL, NULL );
     1881#else
     1882            char * name = d->d_name;
     1883#endif
     1884            if( tr_str_has_suffix( name, ".torrent" ) )
    18781885            {
    18791886                tr_torrent * tor;
    1880                 char * path = tr_buildPath( dirname, d->d_name, NULL );
     1887                char * path = tr_buildPath( dirname, name, NULL );
    18811888                tr_ctorSetMetainfoFromFile( ctor, path );
    18821889                if(( tor = tr_torrentNew( ctor, NULL )))
    18831890                {
     
    18871894                tr_free( path );
    18881895            }
    18891896        }
    1890         closedir( odir );
     1897        tr_closedir( odir );
    18911898    }
    18921899
    18931900    torrents = tr_new( tr_torrent *, n );
     
    21172124{
    21182125    int         binCount = 0;
    21192126    int         newCount = 0;
    2120     struct stat sb;
     2127    STAT        sb;
    21212128    char      * dirname;
    21222129    DIR *       odir = NULL;
    21232130    tr_list *   list = NULL;
     
    21252132
    21262133    /* walk through the directory and find blocklists */
    21272134    dirname = tr_buildPath( session->configDir, "blocklists", NULL );
    2128     if( !stat( dirname,
     2135    if( !tr_stati64( dirname,
    21292136               &sb ) && S_ISDIR( sb.st_mode )
    2130       && ( ( odir = opendir( dirname ) ) ) )
     2137      && ( ( odir = tr_opendir( dirname ) ) ) )
    21312138    {
    2132         struct dirent *d;
    2133         for( d = readdir( odir ); d; d = readdir( odir ) )
     2139        DIRENT *d;
     2140        for( d = tr_readdir( odir ); d; d = tr_readdir( odir ) )
    21342141        {
    21352142            char * filename;
    2136 
    2137             if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and ..
     2143#ifdef WIN32
     2144                int  n = WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL);
     2145                char name[n];
     2146                WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, name, n, NULL, NULL);
     2147#else
     2148                const char * name = d->d_name;
     2149#endif
     2150            if( !name || IS_DOT( name[0] ) ) /* skip dotfiles, ., and ..
    21382151                                                      */
    21392152                continue;
    21402153
    2141             filename = tr_buildPath( dirname, d->d_name, NULL );
     2154            filename = tr_buildPath( dirname, name, NULL );
    21422155
    21432156            if( tr_stringEndsWith( filename, ".bin" ) )
    21442157            {
     
    21562169                /* strip out the file suffix, if there is one, and add ".bin"
    21572170                  instead */
    21582171                tr_blocklist * b;
    2159                 const char *   dot = strrchr( d->d_name, '.' );
    2160                 const int      len = dot ? dot - d->d_name
    2161                                          : (int)strlen( d->d_name );
     2172                const char *   dot = strrchr( name, '.' );
     2173                const int      len = dot ? dot - name
     2174                                         : (int)strlen( name );
    21622175                char         * tmp = tr_strdup_printf(
    21632176                                        "%s" TR_PATH_DELIMITER_STR "%*.*s.bin",
    2164                                         dirname, len, len, d->d_name );
     2177                                        dirname, len, len, name );
    21652178                b = _tr_blocklistNew( tmp, isEnabled );
    21662179                _tr_blocklistSetContent( b, filename );
    21672180                tr_list_append( &list, b );
     
    21722185            tr_free( filename );
    21732186        }
    21742187
    2175         closedir( odir );
     2188        tr_closedir( odir );
    21762189    }
    21772190
    21782191    session->blocklists = list;
     
    23082321static void
    23092322metainfoLookupInit( tr_session * session )
    23102323{
    2311     struct stat  sb;
     2324    STAT         sb;
    23122325    const char * dirname = tr_getTorrentDir( session );
    23132326    DIR *        odir = NULL;
    23142327    tr_ctor *    ctor = NULL;
     
    23222335    tr_bencInitDict( lookup, 0 );
    23232336    ctor = tr_ctorNew( session );
    23242337    tr_ctorSetSave( ctor, false ); /* since we already have them */
    2325     if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && ( ( odir = opendir( dirname ) ) ) )
     2338    if( !tr_stati64( dirname, &sb ) && S_ISDIR( sb.st_mode ) && ( ( odir = tr_opendir( dirname ) ) ) )
    23262339    {
    2327         struct dirent *d;
    2328         while(( d = readdir( odir )))
     2340        DIRENT *d;
     2341        while(( d = tr_readdir( odir )))
    23292342        {
    2330             if( tr_str_has_suffix( d->d_name, ".torrent" ) )
     2343#ifdef WIN32
     2344            int  n = WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL );
     2345            char name[n];
     2346            WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, name, n, NULL, NULL );
     2347#else
     2348            char * name = d->d_name;
     2349#endif
     2350            if( tr_str_has_suffix( name, ".torrent" ) )
    23312351            {
    23322352                tr_info inf;
    2333                 char * path = tr_buildPath( dirname, d->d_name, NULL );
     2353                char * path = tr_buildPath( dirname, name, NULL );
    23342354                tr_ctorSetMetainfoFromFile( ctor, path );
    23352355                if( !tr_torrentParse( ctor, &inf ) )
    23362356                {
     
    23402360                tr_free( path );
    23412361            }
    23422362        }
    2343         closedir( odir );
     2363        tr_closedir( odir );
    23442364    }
    23452365    tr_ctorFree( ctor );
    23462366
  • libtransmission/makemeta.c

     
    5151{
    5252    int         i;
    5353    char        * buf;
    54     struct stat sb;
     54    STAT        sb;
    5555    DIR *       odir = NULL;
    5656
    5757    sb.st_size = 0;
    5858
    5959    buf = tr_buildPath( dir, base, NULL );
    60     i = stat( buf, &sb );
     60    i = tr_stati64( buf, &sb );
    6161    if( i )
    6262    {
    6363        tr_err( _( "Torrent Creator is skipping file \"%s\": %s" ),
     
    6666        return list;
    6767    }
    6868
    69     if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )
     69    if( S_ISDIR( sb.st_mode ) && ( ( odir = tr_opendir ( buf ) ) ) )
    7070    {
    71         struct dirent *d;
    72         for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
    73             if( d->d_name && d->d_name[0] != '.' ) /* skip dotfiles */
     71        DIRENT *d;
     72        for( d = tr_readdir( odir ); d != NULL; d = tr_readdir( odir ) )
     73            if( d->d_name && !IS_DOT( d->d_name[0] ) ) /* skip dotfiles */
     74            {
     75#ifdef WIN32
     76                int n = WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL);
     77                char name[n];
     78                WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, name, n, NULL, NULL);
     79                list = getFiles( buf, name, list );
     80#else
    7481                list = getFiles( buf, d->d_name, list );
    75         closedir( odir );
     82#endif
     83            }
     84        tr_closedir( odir );
    7685    }
    7786    else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) )
    7887    {
     
    126135    ret->top = tr_strdup( topFile );
    127136
    128137    {
    129         struct stat sb;
    130         stat( topFile, &sb );
     138        STAT sb;
     139        tr_stati64( topFile, &sb );
    131140        ret->isSingleFile = !S_ISDIR( sb.st_mode );
    132141    }
    133142
  • libtransmission/net.c

     
    7575{
    7676    *buf = '\0';
    7777#ifdef WIN32
    78     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL );
     78    FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL );
    7979#else
    8080    tr_strlcpy( buf, tr_strerror( err ), buflen );
    8181#endif
  • libtransmission/net.h

     
    3838#endif
    3939
    4040#ifdef WIN32
     41 #define ETIMEDOUT               WSAETIMEDOUT
    4142 #define EADDRINUSE              WSAEADDRINUSE
    4243 #define ECONNREFUSED            WSAECONNREFUSED
    4344 #define ECONNRESET              WSAECONNRESET
  • libtransmission/torrent.c

     
    791791    uint64_t loaded;
    792792    const char * dir;
    793793    bool isNewTorrent;
    794     struct stat st;
     794    STAT st;
    795795    static int nextUniqueId = 1;
    796796    tr_session * session = tr_ctorGetSession( ctor );
    797797
     
    874874    ++session->torrentCount;
    875875
    876876    /* if we don't have a local .torrent file already, assume the torrent is new */
    877     isNewTorrent = stat( tor->info.torrent, &st );
     877    isNewTorrent = tr_stati64( tor->info.torrent, &st );
    878878
    879879    /* maybe save our own copy of the metainfo */
    880880    if( tr_ctorGetSave( ctor ) )
     
    15331533
    15341534    for( i=0; i<n; ++i )
    15351535    {
    1536         struct stat sb;
     1536        STAT sb;
    15371537        char * filename = tr_torrentFindFile( tor, i );
    15381538
    15391539        sb.st_size = 0;
    1540         if( filename && !stat( filename, &sb ) )
     1540        if( filename && !tr_stati64( filename, &sb ) )
    15411541            byte_count += sb.st_size;
    15421542
    15431543        tr_free( filename );
     
    19031903        tr_torinf( tor, "Calling script \"%s\"", script );
    19041904
    19051905#ifdef WIN32
    1906         _spawnvpe( _P_NOWAIT, script, (const char*)cmd, env );
     1906        _spawnvpe( _P_NOWAIT, script, (const char* const *)cmd, (const char* const *)env );
    19071907#else
    19081908        signal( SIGCHLD, onSigCHLD );
    19091909
     
    25612561    {
    25622562        if( !tor->info.files[i].dnd )
    25632563        {
    2564             struct stat sb;
     2564            STAT sb;
    25652565            const uint64_t length = tor->info.files[i].length;
    25662566            char * path = tr_torrentFindFile( tor, i );
    25672567
    25682568            bytesLeft += length;
    25692569
    2570             if( ( path != NULL ) && !stat( path, &sb )
     2570            if( ( path != NULL ) && !tr_stati64( path, &sb )
    25712571                                 && S_ISREG( sb.st_mode )
    25722572                                 && ( (uint64_t)sb.st_size <= length ) )
    25732573                bytesLeft -= sb.st_size;
     
    26332633               tr_ptrArray      * folders,
    26342634               tr_ptrArray      * dirtyFolders )
    26352635{
    2636     struct stat sb;
     2636    STAT sb;
    26372637    char * buf = tr_buildPath( dir, base, NULL );
    2638     int i = stat( buf, &sb );
     2638    int i = tr_stati64( buf, &sb );
    26392639
    26402640    if( !i )
    26412641    {
    26422642        DIR * odir = NULL;
    26432643
    2644         if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )
     2644        if( S_ISDIR( sb.st_mode ) && ( ( odir = tr_opendir ( buf ) ) ) )
    26452645        {
    2646             struct dirent *d;
     2646            DIRENT *d;
    26472647            tr_ptrArrayInsertSorted( folders, tr_strdup( buf ), vstrcmp );
    2648             for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
    2649                 if( d->d_name && strcmp( d->d_name, "." ) && strcmp( d->d_name, ".." ) )
     2648            for( d = tr_readdir( odir ); d != NULL; d = tr_readdir( odir ) )
     2649                if( d->d_name && !IS_DOT( d->d_name[0] ) )
     2650                {
     2651#ifdef WIN32
     2652                    int  n = WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL);
     2653                    char name[n];
     2654                    WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, name, n, NULL, NULL);
     2655                    walkLocalData( tor, root, buf, name, torrentFiles, folders, dirtyFolders );
     2656#else
    26502657                    walkLocalData( tor, root, buf, d->d_name, torrentFiles, folders, dirtyFolders );
    2651             closedir( odir );
     2658#endif
     2659                }
     2660            tr_closedir( odir );
    26522661        }
    26532662        else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) )
    26542663        {
     
    26652674static void
    26662675deleteLocalFile( const char * filename, tr_fileFunc fileFunc )
    26672676{
    2668     struct stat sb;
    2669     if( !stat( filename, &sb ) ) /* if file exists... */
     2677    STAT sb;
     2678    if( !tr_stati64( filename, &sb ) ) /* if file exists... */
    26702679        fileFunc( filename );
    26712680}
    26722681
     
    27412750    assert( tr_isTorrent( tor ) );
    27422751
    27432752    if( fileFunc == NULL )
    2744         fileFunc = remove;
     2753        fileFunc = tr_remove;
    27452754
    27462755    /* close all the files because we're about to delete them */
    27472756    tr_cacheFlushTorrent( tor->session->cache, tor );
     
    28492858        {
    28502859            /* blow away the leftover subdirectories in the old location */
    28512860            if( do_move )
    2852                 tr_torrentDeleteLocalData( tor, remove );
     2861                tr_torrentDeleteLocalData( tor, tr_remove );
    28532862
    28542863            /* set the new location and reverify */
    28552864            tr_torrentSetDownloadDir( tor, location );
     
    29302939            char * oldpath = tr_buildPath( base, sub, NULL );
    29312940            char * newpath = tr_buildPath( base, f->name, NULL );
    29322941
    2933             if( rename( oldpath, newpath ) )
     2942            if( tr_rename( oldpath, newpath ) )
    29342943                tr_torerr( tor, "Error moving \"%s\" to \"%s\": %s", oldpath, newpath, tr_strerror( errno ) );
    29352944
    29362945            tr_free( newpath );
     
    29552964static bool
    29562965fileExists( const char * filename, time_t * mtime )
    29572966{
    2958     struct stat sb;
    2959     const bool ok = !stat( filename, &sb );
     2967    STAT sb;
     2968    const bool ok = !tr_stati64( filename, &sb );
    29602969
    29612970    if( ok && ( mtime != NULL ) )
    29622971        *mtime = TR_STAT_MTIME( sb );
  • qt/qtr.pro

     
    2424LIBS += $${TRANSMISSION_TOP}/third-party/miniupnp/libminiupnp.a
    2525LIBS += $${TRANSMISSION_TOP}/third-party/libnatpmp/libnatpmp.a
    2626unix: LIBS += -L$${EVENT_TOP}/lib -lz -lrt
    27 win32:DEFINES += QT_DBUS
    28 win32:LIBS += -levent-2.0 -lws2_32 -lintl
    29 win32:LIBS += -lidn -liconv -lwldap32 -liphlpapi
     27win32:LIBS += -lidn -liconv -lwldap32 -liphlpapi -lintl -lpsapi
    3028
    3129TRANSLATIONS += translations/transmission_en.ts \
    3230                translations/transmission_es.ts \
  • qt/make-dialog.cc

     
    5858    {
    5959        case QDialogButtonBox::Open:
    6060std::cerr << "calling mySession.addTorrent( " << qPrintable(myTarget) << ", " << qPrintable(QFileInfo(myBuilder->top).dir().path()) << ')' << std::endl;
    61             mySession.addNewlyCreatedTorrent( myTarget, QFileInfo(myBuilder->top).dir().path() );
     61            mySession.addNewlyCreatedTorrent( myTarget, QFileInfo(QString::fromUtf8(myBuilder->top)).dir().path() );
    6262            break;
    6363        case QDialogButtonBox::Abort:
    6464            myBuilder->abortFlag = true;
     
    7979    myNewProgress->setValue( (int) ((100.0 * b->pieceIndex) / denom ) );
    8080
    8181    // progress label
    82     const QString base( QFileInfo(b->top).baseName() );
     82    const QString base( QFileInfo(QString::fromUtf8(b->top)).completeBaseName() );
    8383    QString str;
    8484    if( !b->isDone )
    8585        str = tr( "Creating \"%1\"" ).arg( base );
     
    144144    myTimer.start( 100 );
    145145
    146146    // the file to create
    147     myTarget = QDir( myDestination ).filePath( QFileInfo(myBuilder->top).baseName() + ".torrent" );
     147    myTarget = QDir( myDestination ).filePath( QFileInfo(QString::fromUtf8(myBuilder->top)).completeBaseName() + ".torrent" );
    148148    std::cerr << qPrintable(myTarget) << std::endl;
    149149
    150150    // comment
  • qt/session.cc

     
    4444#include "prefs.h"
    4545#include "session.h"
    4646#include "session-dialog.h"
     47
     48#ifdef WIN32
     49 #undef ERROR   /* defined in wingdi.h, included by windows.h */
     50#endif
     51
    4752#include "torrent.h"
    4853#include "utils.h"
    4954
  • third-party/dht/dht.c

     
    8383{
    8484    return rand();
    8585}
    86 extern const char *inet_ntop(int, const void *, char *, socklen_t);
     86extern const char *evutil_inet_ntop(int, const void *, char *, socklen_t);
     87#define inet_ntop(a,b,c,d)      evutil_inet_ntop(a,b,c,d)
    8788
    8889#else
    8990
  • third-party/libutp/utp.cpp

     
    1212#include <limits.h> // for UINT_MAX
    1313
    1414#ifdef WIN32
    15 #include "win32_inet_ntop.h"
     15/* libtransmission switched to using libevent's inet_ntop */
     16extern const char *evutil_inet_ntop(int, const void *, char *, socklen_t);
     17#define inet_ntop(a,b,c,d)     evutil_inet_ntop(a,b,c,d)
    1618
    1719// newer versions of MSVC define these in errno.h
    1820#ifndef ECONNRESET