Ticket #4160: Transmission-r12283.diff

File Transmission-r12283.diff, 57.8 KB (added by jordan, 12 years ago)

same diff, uncompressed

  • 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/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    wchar_t *nameUTF16;
     102    int 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    n = strlen(b->filename) + 1;
     119    nameUTF16 = malloc(n);
     120    MultiByteToWideChar(CP_UTF8, 0, b->filename, -1, nameUTF16, n);
     121    fd = CreateFileW( nameUTF16, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0 );
     122    fh = CreateFileMapping( fd, NULL, PAGE_READONLY, 0, byteCount, NULL);
     123    free(nameUTF16);
     124    if ( fh != NULL )
     125        b->rules = MapViewOfFile( fh, FILE_MAP_READ, 0, 0, 0 );
     126    else
     127        b->rules = NULL;
     128#else
     129    fd = tr_open( b->filename, O_RDONLY | O_BINARY );
    94130    if( fd == -1 )
    95131    {
    96132        tr_err( err_fmt, b->filename, tr_strerror( errno ) );
    97133        return;
    98134    }
    99135
    100     byteCount = (size_t) st.st_size;
    101136    b->rules = mmap( NULL, byteCount, PROT_READ, MAP_PRIVATE, fd, 0 );
     137#endif
    102138    if( !b->rules )
    103139    {
    104140        tr_err( err_fmt, b->filename, tr_strerror( errno ) );
     141#ifdef WIN32
     142        CloseHandle( fd );
     143#else
    105144        close( fd );
     145#endif
    106146        return;
    107147    }
    108148
     
    140180blocklistDelete( tr_blocklist * b )
    141181{
    142182    blocklistClose( b );
    143     unlink( b->filename );
     183    tr_unlink( b->filename );
    144184}
    145185
    146186/***
     
    153193    tr_blocklist * b;
    154194
    155195    b = tr_new0( tr_blocklist, 1 );
     196#ifdef WIN32
     197    b->fd = NULL;
     198#else
    156199    b->fd = -1;
     200#endif
    157201    b->filename = tr_strdup( filename );
    158202    b->isEnabled = isEnabled;
    159203
     
    177221int
    178222_tr_blocklistExists( const tr_blocklist * b )
    179223{
    180     struct stat st;
     224    STAT st;
    181225
    182     return !stat( b->filename, &st );
     226    return !tr_stati64( b->filename, &st );
    183227}
    184228
    185229int
     
    333377        return 0;
    334378    }
    335379
    336     in = fopen( filename, "rb" );
     380    in = tr_fopen( filename, "rb" );
    337381    if( !in )
    338382    {
    339383        tr_err( err_fmt, filename, tr_strerror( errno ) );
     
    342386
    343387    blocklistClose( b );
    344388
    345     out = fopen( b->filename, "wb+" );
     389    out = tr_fopen( b->filename, "wb+" );
    346390    if( !out )
    347391    {
    348392        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
     
    17031711        {
    17041712            tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
    17051713            tr_close_file( fd );
    1706             unlink( tmp );
     1714            tr_unlink( tmp );
    17071715        }
    17081716        else
    17091717        {
    17101718            //tr_fsync( fd );
    17111719            tr_close_file( fd );
    1712 
    1713 #ifdef WIN32
    1714             if( MoveFileEx( tmp, filename, MOVEFILE_REPLACE_EXISTING ) )
    1715 #else
    1716             if( !rename( tmp, filename ) )
    1717 #endif
     1720            if( !tr_rename( tmp, filename ) )
    17181721            {
    17191722                tr_inf( _( "Saved \"%s\"" ), filename );
    17201723            }
     
    17221725            {
    17231726                err = errno;
    17241727                tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
    1725                 unlink( tmp );
     1728                tr_unlink( tmp );
    17261729            }
    17271730        }
    17281731    }
  • libtransmission/metainfo.c

     
    6565{
    6666    int i;
    6767    char * path;
    68     struct stat sb;
     68    STAT sb;
    6969    const int tagCount = 5;
    7070    const char * tags[] = { "beos", "cli", "daemon", "macosx", "wx" };
    7171
    7272    /* test the beos, cli, daemon, macosx, wx tags */
    7373    for( i=0; i<tagCount; ++i ) {
    7474        path = tr_strdup_printf( "%s%c%s-%s", tr_getTorrentDir( session ), '/', inf->hashString, tags[i] );
    75         if( !stat( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
     75        if( !tr_stati64( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
    7676            return path;
    7777        tr_free( path );
    7878    }
    7979
    8080    /* test a non-tagged file */
    8181    path = tr_buildPath( tr_getTorrentDir( session ), inf->hashString, NULL );
    82     if( !stat( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
     82    if( !tr_stati64( path, &sb ) && ( ( sb.st_mode & S_IFMT ) == S_IFREG ) )
    8383        return path;
    8484    tr_free( path );
    8585
     
    9494tr_metainfoMigrate( tr_session * session,
    9595                    tr_info *   inf )
    9696{
    97     struct stat new_sb;
     97    STAT        new_sb;
    9898    char *      name = getTorrentFilename( session, inf );
    9999
    100     if( stat( name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
     100    if( tr_stati64( name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) )
    101101    {
    102102        char *    old_name = getOldTorrentFilename( session, inf );
    103103        size_t    contentLen;
     
    108108        {
    109109            FILE * out;
    110110            errno = 0;
    111             out = fopen( name, "wb+" );
     111            out = tr_fopen( name, "wb+" );
    112112            if( !out )
    113113            {
    114114                tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ),
     
    122122                    tr_free( inf->torrent );
    123123                    inf->torrent = tr_strdup( name );
    124124                    tr_sessionSetTorrentFile( session, inf->hashString, name );
    125                     unlink( old_name );
     125                    tr_unlink( old_name );
    126126                }
    127127                fclose( out );
    128128            }
     
    590590    char * filename;
    591591
    592592    filename = getTorrentFilename( session, inf );
    593     unlink( filename );
     593    tr_unlink( filename );
    594594    tr_free( filename );
    595595
    596596    filename = getOldTorrentFilename( session, inf );
    597     unlink( filename );
     597    tr_unlink( filename );
    598598    tr_free( filename );
    599599}
  • libtransmission/rpcimpl.c

     
    11791179                tr_close_file( fd );
    11801180            }
    11811181
    1182             unlink( filename );
     1182            tr_unlink( filename );
    11831183            tr_free( filename );
    11841184            filename = filename2;
    11851185        }
     
    11921192            tr_snprintf( result, sizeof( result ), "success" );
    11931193        }
    11941194
    1195         unlink( filename );
     1195        tr_unlink( filename );
    11961196        tr_free( filename );
    11971197    }
    11981198
  • 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    {
  • 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

     
    898898tr_torrentRemoveResume( const tr_torrent * tor )
    899899{
    900900    char * filename = getResumeFilename( tor );
    901     unlink( filename );
     901    tr_unlink( filename );
    902902    tr_free( filename );
    903903}
    904904
  • libtransmission/session.c

     
    18801880                        int        * setmeCount )
    18811881{
    18821882    int           i, n = 0;
    1883     struct stat   sb;
     1883    STAT          sb;
    18841884    DIR *         odir = NULL;
    18851885    const char *  dirname = tr_getTorrentDir( session );
    18861886    tr_torrent ** torrents;
     
    18901890
    18911891    tr_ctorSetSave( ctor, false ); /* since we already have them */
    18921892
    1893     if( !stat( dirname, &sb )
     1893    if( !tr_stati64( dirname, &sb )
    18941894      && S_ISDIR( sb.st_mode )
    1895       && ( ( odir = opendir ( dirname ) ) ) )
     1895      && ( ( odir = tr_opendir ( dirname ) ) ) )
    18961896    {
    1897         struct dirent *d;
    1898         for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
     1897        DIRENT *d;
     1898        for( d = tr_readdir( odir ); d != NULL; d = tr_readdir( odir ) )
    18991899        {
    1900             if( tr_str_has_suffix( d->d_name, ".torrent" ) )
     1900#ifdef WIN32
     1901            int m = WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL );
     1902            char name[m];
     1903            WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, name, m, NULL, NULL );
     1904#else
     1905            char * name = d->d_name;
     1906#endif
     1907            if( tr_str_has_suffix( name, ".torrent" ) )
    19011908            {
    19021909                tr_torrent * tor;
    1903                 char * path = tr_buildPath( dirname, d->d_name, NULL );
     1910                char * path = tr_buildPath( dirname, name, NULL );
    19041911                tr_ctorSetMetainfoFromFile( ctor, path );
    19051912                if(( tor = tr_torrentNew( ctor, NULL )))
    19061913                {
     
    19101917                tr_free( path );
    19111918            }
    19121919        }
    1913         closedir( odir );
     1920        tr_closedir( odir );
    19141921    }
    19151922
    19161923    torrents = tr_new( tr_torrent *, n );
     
    21402147{
    21412148    int         binCount = 0;
    21422149    int         newCount = 0;
    2143     struct stat sb;
     2150    STAT        sb;
    21442151    char      * dirname;
    21452152    DIR *       odir = NULL;
    21462153    tr_list *   list = NULL;
     
    21482155
    21492156    /* walk through the directory and find blocklists */
    21502157    dirname = tr_buildPath( session->configDir, "blocklists", NULL );
    2151     if( !stat( dirname,
     2158    if( !tr_stati64( dirname,
    21522159               &sb ) && S_ISDIR( sb.st_mode )
    2153       && ( ( odir = opendir( dirname ) ) ) )
     2160      && ( ( odir = tr_opendir( dirname ) ) ) )
    21542161    {
    2155         struct dirent *d;
    2156         for( d = readdir( odir ); d; d = readdir( odir ) )
     2162        DIRENT *d;
     2163        for( d = tr_readdir( odir ); d; d = tr_readdir( odir ) )
    21572164        {
    21582165            char * filename;
    2159 
    2160             if( !d->d_name || d->d_name[0] == '.' ) /* skip dotfiles, ., and ..
     2166#ifdef WIN32
     2167                int  n = WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL);
     2168                char name[n];
     2169                WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, name, n, NULL, NULL);
     2170#else
     2171                const char * name = d->d_name;
     2172#endif
     2173            if( !name || IS_DOT( name[0] ) ) /* skip dotfiles, ., and ..
    21612174                                                      */
    21622175                continue;
    21632176
    2164             filename = tr_buildPath( dirname, d->d_name, NULL );
     2177            filename = tr_buildPath( dirname, name, NULL );
    21652178
    21662179            if( tr_stringEndsWith( filename, ".bin" ) )
    21672180            {
     
    21792192                /* strip out the file suffix, if there is one, and add ".bin"
    21802193                  instead */
    21812194                tr_blocklist * b;
    2182                 const char *   dot = strrchr( d->d_name, '.' );
    2183                 const int      len = dot ? dot - d->d_name
    2184                                          : (int)strlen( d->d_name );
     2195                const char *   dot = strrchr( name, '.' );
     2196                const int      len = dot ? dot - name
     2197                                         : (int)strlen( name );
    21852198                char         * tmp = tr_strdup_printf(
    21862199                                        "%s" TR_PATH_DELIMITER_STR "%*.*s.bin",
    2187                                         dirname, len, len, d->d_name );
     2200                                        dirname, len, len, name );
    21882201                b = _tr_blocklistNew( tmp, isEnabled );
    21892202                _tr_blocklistSetContent( b, filename );
    21902203                tr_list_append( &list, b );
     
    21952208            tr_free( filename );
    21962209        }
    21972210
    2198         closedir( odir );
     2211        tr_closedir( odir );
    21992212    }
    22002213
    22012214    session->blocklists = list;
     
    23312344static void
    23322345metainfoLookupInit( tr_session * session )
    23332346{
    2334     struct stat  sb;
     2347    STAT         sb;
    23352348    const char * dirname = tr_getTorrentDir( session );
    23362349    DIR *        odir = NULL;
    23372350    tr_ctor *    ctor = NULL;
     
    23452358    tr_bencInitDict( lookup, 0 );
    23462359    ctor = tr_ctorNew( session );
    23472360    tr_ctorSetSave( ctor, false ); /* since we already have them */
    2348     if( !stat( dirname, &sb ) && S_ISDIR( sb.st_mode ) && ( ( odir = opendir( dirname ) ) ) )
     2361    if( !tr_stati64( dirname, &sb ) && S_ISDIR( sb.st_mode ) && ( ( odir = tr_opendir( dirname ) ) ) )
    23492362    {
    2350         struct dirent *d;
    2351         while(( d = readdir( odir )))
     2363        DIRENT *d;
     2364        while(( d = tr_readdir( odir )))
    23522365        {
    2353             if( tr_str_has_suffix( d->d_name, ".torrent" ) )
     2366#ifdef WIN32
     2367            int  n = WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL );
     2368            char name[n];
     2369            WideCharToMultiByte( CP_UTF8, 0, d->d_name, -1, name, n, NULL, NULL );
     2370#else
     2371            char * name = d->d_name;
     2372#endif
     2373            if( tr_str_has_suffix( name, ".torrent" ) )
    23542374            {
    23552375                tr_info inf;
    2356                 char * path = tr_buildPath( dirname, d->d_name, NULL );
     2376                char * path = tr_buildPath( dirname, name, NULL );
    23572377                tr_ctorSetMetainfoFromFile( ctor, path );
    23582378                if( !tr_torrentParse( ctor, &inf ) )
    23592379                {
     
    23632383                tr_free( path );
    23642384            }
    23652385        }
    2366         closedir( odir );
     2386        tr_closedir( odir );
    23672387    }
    23682388    tr_ctorFree( ctor );
    23692389
  • 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

     
    7373{
    7474    *buf = '\0';
    7575#ifdef WIN32
    76     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL );
     76    FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL );
    7777#else
    7878    tr_strlcpy( buf, tr_strerror( err ), buflen );
    7979#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

     
    767767
    768768    for( i=0; i<n && !has_local_data; ++i )
    769769    {
    770         struct stat sb;
     770        STAT  sb;
    771771        char * filename = tr_torrentFindFile( tor, i );
    772772
    773         if( filename && !stat( filename, &sb ) )
     773        if( filename && !tr_stati64( filename, &sb ) )
    774774            has_local_data = true;
    775775
    776776        tr_free( filename );
     
    800800    uint64_t loaded;
    801801    const char * dir;
    802802    bool isNewTorrent;
    803     struct stat st;
     803    STAT      st;
    804804    static int nextUniqueId = 1;
    805805    tr_session * session = tr_ctorGetSession( ctor );
    806806
     
    891891    }
    892892
    893893    /* if we don't have a local .torrent file already, assume the torrent is new */
    894     isNewTorrent = stat( tor->info.torrent, &st );
     894    isNewTorrent = tr_stati64( tor->info.torrent, &st );
    895895
    896896    /* maybe save our own copy of the metainfo */
    897897    if( tr_ctorGetSave( ctor ) )
     
    15501550
    15511551    for( i=0; i<n; ++i )
    15521552    {
    1553         struct stat sb;
     1553        STAT  sb;
    15541554        char * filename = tr_torrentFindFile( tor, i );
    15551555
    15561556        sb.st_size = 0;
    1557         if( filename && !stat( filename, &sb ) )
     1557        if( filename && !tr_stati64( filename, &sb ) )
    15581558            byte_count += sb.st_size;
    15591559
    15601560        tr_free( filename );
     
    19201920        tr_torinf( tor, "Calling script \"%s\"", script );
    19211921
    19221922#ifdef WIN32
    1923         _spawnvpe( _P_NOWAIT, script, (const char*)cmd, env );
     1923        _spawnvpe( _P_NOWAIT, script, (const char* const *)cmd, (const char* const *)env );
    19241924#else
    19251925        signal( SIGCHLD, onSigCHLD );
    19261926
     
    23802380time_t
    23812381tr_torrentGetFileMTime( const tr_torrent * tor, tr_file_index_t i )
    23822382{
    2383     struct stat sb;
     2383    STAT  sb;
    23842384    time_t mtime = 0;
    23852385    char * path = tr_torrentFindFile( tor, i );
    23862386
    2387     if( ( path != NULL ) && !stat( path, &sb ) && S_ISREG( sb.st_mode ) )
     2387    if( ( path != NULL ) && !tr_stati64( path, &sb ) && S_ISREG( sb.st_mode ) )
    23882388    {
    23892389#ifdef SYS_DARWIN
    23902390        mtime = sb.st_mtimespec.tv_sec;
     
    25892589    {
    25902590        if( !tor->info.files[i].dnd )
    25912591        {
    2592             struct stat sb;
     2592            STAT sb;
    25932593            const uint64_t length = tor->info.files[i].length;
    25942594            char * path = tr_torrentFindFile( tor, i );
    25952595
    25962596            bytesLeft += length;
    25972597
    2598             if( ( path != NULL ) && !stat( path, &sb )
     2598            if( ( path != NULL ) && !tr_stati64( path, &sb )
    25992599                                 && S_ISREG( sb.st_mode )
    26002600                                 && ( (uint64_t)sb.st_size <= length ) )
    26012601                bytesLeft -= sb.st_size;
     
    26612661               tr_ptrArray      * folders,
    26622662               tr_ptrArray      * dirtyFolders )
    26632663{
    2664     struct stat sb;
     2664    STAT  sb;
    26652665    char * buf = tr_buildPath( dir, base, NULL );
    2666     int i = stat( buf, &sb );
     2666    int i = tr_stati64( buf, &sb );
    26672667
    26682668    if( !i )
    26692669    {
    26702670        DIR * odir = NULL;
    26712671
    2672         if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )
     2672        if( S_ISDIR( sb.st_mode ) && ( ( odir = tr_opendir ( buf ) ) ) )
    26732673        {
    2674             struct dirent *d;
     2674            DIRENT *d;
    26752675            tr_ptrArrayInsertSorted( folders, tr_strdup( buf ), vstrcmp );
    2676             for( d = readdir( odir ); d != NULL; d = readdir( odir ) )
    2677                 if( d->d_name && strcmp( d->d_name, "." ) && strcmp( d->d_name, ".." ) )
     2676            for( d = tr_readdir( odir ); d != NULL; d = tr_readdir( odir ) )
     2677                if( d->d_name && !IS_DOT( d->d_name[0] ) )
     2678                {
     2679#ifdef WIN32
     2680                    int  n = WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, NULL, 0, NULL, NULL);
     2681                    char name[n];
     2682                    WideCharToMultiByte(CP_UTF8, 0, d->d_name, -1, name, n, NULL, NULL);
     2683                    walkLocalData( tor, root, buf, name, torrentFiles, folders, dirtyFolders );
     2684#else
    26782685                    walkLocalData( tor, root, buf, d->d_name, torrentFiles, folders, dirtyFolders );
    2679             closedir( odir );
     2686#endif
     2687                    }
     2688            tr_closedir( odir );
    26802689        }
    26812690        else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) )
    26822691        {
     
    26932702static void
    26942703deleteLocalFile( const char * filename, tr_fileFunc fileFunc )
    26952704{
    2696     struct stat sb;
    2697     if( !stat( filename, &sb ) ) /* if file exists... */
     2705    STAT sb;
     2706    if( !tr_stati64( filename, &sb ) ) /* if file exists... */
    26982707        fileFunc( filename );
    26992708}
    27002709
     
    27692778    assert( tr_isTorrent( tor ) );
    27702779
    27712780    if( fileFunc == NULL )
    2772         fileFunc = remove;
     2781        fileFunc = tr_remove;
    27732782
    27742783    /* close all the files because we're about to delete them */
    27752784    tr_cacheFlushTorrent( tor->session->cache, tor );
     
    28772886        {
    28782887            /* blow away the leftover subdirectories in the old location */
    28792888            if( do_move )
    2880                 tr_torrentDeleteLocalData( tor, remove );
     2889                tr_torrentDeleteLocalData( tor, tr_remove );
    28812890
    28822891            /* set the new location and reverify */
    28832892            tr_torrentSetDownloadDir( tor, location );
     
    29582967            char * oldpath = tr_buildPath( base, sub, NULL );
    29592968            char * newpath = tr_buildPath( base, f->name, NULL );
    29602969
    2961             if( rename( oldpath, newpath ) )
     2970            if( tr_rename( oldpath, newpath ) )
    29622971                tr_torerr( tor, "Error moving \"%s\" to \"%s\": %s", oldpath, newpath, tr_strerror( errno ) );
    29632972
    29642973            tr_free( newpath );
     
    29762985static bool
    29772986fileExists( const char * filename )
    29782987{
    2979     struct stat sb;
    2980     const bool ok = !stat( filename, &sb );
     2988    STAT      sb;
     2989    const bool ok = !tr_stati64( filename, &sb );
    29812990    return ok;
    29822991}
    29832992
  • 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