Ticket #4160: Transmission-r12426.diff
File Transmission-r12426.diff, 60.3 KB (added by rb07, 12 years ago) |
---|
-
libtransmission/utils.c
241 241 va_end( args ); 242 242 evbuffer_add_printf( buf, " (%s:%d)\n", base, line ); 243 243 /* FIXME(libevent2) ifdef this out for nonwindows platforms */ 244 OutputDebugString ( evbuffer_pullup( buf, -1 ) );244 OutputDebugStringA( evbuffer_pullup( buf, -1 ) ); 245 245 if( fp ) 246 246 fputs( (const char*)evbuffer_pullup( buf, -1 ), fp ); 247 247 … … 271 271 evutil_vsnprintf( buf, sizeof( buf ), fmt, ap ); 272 272 va_end( ap ); 273 273 274 OutputDebugString ( buf );274 OutputDebugStringA( buf ); 275 275 276 276 if( *buf ) 277 277 { … … 430 430 size_t * size ) 431 431 { 432 432 uint8_t * buf; 433 struct statsb;433 STAT sb; 434 434 int fd; 435 435 ssize_t n; 436 436 const char * const err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); 437 437 438 438 /* try to stat the file */ 439 439 errno = 0; 440 if( stat( path, &sb ) )440 if( tr_stati64( path, &sb ) ) 441 441 { 442 442 const int err = errno; 443 443 tr_dbg( err_fmt, path, tr_strerror( errno ) ); … … 516 516 #ifdef WIN32 517 517 if( path && isalpha( path[0] ) && path[1] == ':' && !path[2] ) 518 518 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; 520 527 #else 521 528 return mkdir( path, permissions ); 522 529 #endif … … 528 535 { 529 536 char * path = tr_strdup( path_in ); 530 537 char * p, * pp; 531 struct statsb;538 STAT sb; 532 539 int done; 533 540 534 541 /* walk past the root */ … … 546 553 else 547 554 *p = '\0'; 548 555 549 if( stat( path, &sb ) )556 if( tr_stati64( path, &sb ) ) 550 557 { 551 558 /* Folder doesn't exist yet */ 552 559 if( tr_mkdir( path, permissions ) ) … … 1472 1479 int in; 1473 1480 int out; 1474 1481 char * buf; 1475 struct statst;1482 STAT st; 1476 1483 off_t bytesLeft; 1477 1484 const size_t buflen = 1024 * 128; /* 128 KiB buffer */ 1478 1485 1479 1486 /* make sure the old file exists */ 1480 if( stat( oldpath, &st ) ) {1487 if( tr_stati64( oldpath, &st ) ) { 1481 1488 const int err = errno; 1482 1489 errno = err; 1483 1490 return -1; … … 1499 1506 1500 1507 /* they might be on the same filesystem... */ 1501 1508 { 1502 const int i = rename( oldpath, newpath );1509 const int i = tr_rename( oldpath, newpath ); 1503 1510 if( renamed != NULL ) 1504 1511 *renamed = i == 0; 1505 1512 if( !i ) … … 1530 1537 if( bytesLeft != 0 ) 1531 1538 return -1; 1532 1539 1533 unlink( oldpath );1540 tr_unlink( oldpath ); 1534 1541 return 0; 1535 1542 } 1536 1543 1537 1544 bool 1538 1545 tr_is_same_file( const char * filename1, const char * filename2 ) 1539 1546 { 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]; 1541 1555 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 ) 1544 1573 && ( sb1.st_dev == sb2.st_dev ) 1545 1574 && ( sb1.st_ino == sb2.st_ino ); 1575 #endif 1546 1576 } 1547 1577 1548 1578 /*** 1549 1579 **** 1550 1580 ***/ 1551 1581 1582 /* getpagesize for windows */ 1583 static long 1584 getpagesize( 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 1552 1597 void* 1553 1598 tr_valloc( size_t bufLen ) 1554 1599 { … … 1583 1628 return buf; 1584 1629 } 1585 1630 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 1643 static 1644 bool 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 1586 1736 char * 1587 1737 tr_realpath( const char * path, char * resolved_path ) 1588 1738 { 1589 1739 #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) 1592 1746 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); 1593 1753 return resolved_path; 1594 1754 #else 1595 1755 return realpath( path, resolved_path ); -
libtransmission/peer-msgs.c
16 16 #include <stdlib.h> 17 17 #include <string.h> 18 18 19 #ifndef WIN32 19 20 #include <alloca.h> 21 #endif 20 22 21 23 #include <event2/buffer.h> 22 24 #include <event2/bufferevent.h> -
libtransmission/blocklist.c
37 37 #include "blocklist.h" 38 38 #include "net.h" 39 39 #include "utils.h" 40 #include "platform.h" 40 41 41 42 #ifndef O_BINARY 42 43 #define O_BINARY 0 … … 56 57 struct tr_blocklist 57 58 { 58 59 bool isEnabled; 60 #ifdef WIN32 61 HANDLE fd; 62 #else 59 63 int fd; 64 #endif 60 65 size_t ruleCount; 61 66 size_t byteCount; 62 67 char * filename; 63 68 struct tr_ipv4_range * rules; 64 69 }; 65 70 71 #ifdef WIN32 72 static HANDLE fh; 73 #endif 74 66 75 static void 67 76 blocklistClose( tr_blocklist * b ) 68 77 { 69 78 if( b->rules ) 70 79 { 80 #ifdef WIN32 81 UnmapViewOfFile( b->rules ); 82 CloseHandle( fh ); 83 CloseHandle( b->fd ); 84 b->fd = NULL; 85 #else 71 86 munmap( b->rules, b->byteCount ); 72 87 close( b->fd ); 88 b->fd = -1; 89 #endif 73 90 b->rules = NULL; 74 91 b->ruleCount = 0; 75 92 b->byteCount = 0; 76 b->fd = -1;77 93 } 78 94 } 79 95 80 96 static void 81 97 blocklistLoad( tr_blocklist * b ) 82 98 { 99 #ifdef WIN32 100 HANDLE fd; 101 int n = strlen(b->filename) + 1; 102 wchar_t nameUTF16[n]; 103 #else 83 104 int fd; 105 #endif 84 106 size_t byteCount; 85 struct statst;107 STAT st; 86 108 const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" ); 87 109 88 110 blocklistClose( b ); 89 111 90 if( stat( b->filename, &st ) == -1 )112 if( tr_stati64( b->filename, &st ) == -1 ) 91 113 return; 92 114 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 ); 94 127 if( fd == -1 ) 95 128 { 96 129 tr_err( err_fmt, b->filename, tr_strerror( errno ) ); 97 130 return; 98 131 } 99 132 100 byteCount = (size_t) st.st_size;101 133 b->rules = mmap( NULL, byteCount, PROT_READ, MAP_PRIVATE, fd, 0 ); 134 #endif 102 135 if( !b->rules ) 103 136 { 104 137 tr_err( err_fmt, b->filename, tr_strerror( errno ) ); 138 #ifdef WIN32 139 CloseHandle( fd ); 140 #else 105 141 close( fd ); 142 #endif 106 143 return; 107 144 } 108 145 … … 140 177 blocklistDelete( tr_blocklist * b ) 141 178 { 142 179 blocklistClose( b ); 143 unlink( b->filename );180 tr_unlink( b->filename ); 144 181 } 145 182 146 183 /*** … … 153 190 tr_blocklist * b; 154 191 155 192 b = tr_new0( tr_blocklist, 1 ); 193 #ifdef WIN32 194 b->fd = NULL; 195 #else 156 196 b->fd = -1; 197 #endif 157 198 b->filename = tr_strdup( filename ); 158 199 b->isEnabled = isEnabled; 159 200 … … 177 218 int 178 219 _tr_blocklistExists( const tr_blocklist * b ) 179 220 { 180 struct statst;221 STAT st; 181 222 182 return ! stat( b->filename, &st );223 return !tr_stati64( b->filename, &st ); 183 224 } 184 225 185 226 int … … 333 374 return 0; 334 375 } 335 376 336 in = fopen( filename, "rb" );377 in = tr_fopen( filename, "rb" ); 337 378 if( !in ) 338 379 { 339 380 tr_err( err_fmt, filename, tr_strerror( errno ) ); … … 342 383 343 384 blocklistClose( b ); 344 385 345 out = fopen( b->filename, "wb+" );386 out = tr_fopen( b->filename, "wb+" ); 346 387 if( !out ) 347 388 { 348 389 tr_err( err_fmt, b->filename, tr_strerror( errno ) ); -
libtransmission/bencode.c
20 20 21 21 #ifdef WIN32 /* tr_mkstemp() */ 22 22 #include <fcntl.h> 23 # define _S_IREAD 25624 # define _S_IWRITE 12823 #include <share.h> 24 #include <sys/stat.h> 25 25 #endif 26 26 27 27 #include <locale.h> /* setlocale() */ … … 1651 1651 tr_mkstemp( char * template ) 1652 1652 { 1653 1653 #ifdef WIN32 1654 int n = strlen(template) + 1; 1654 1655 const int flags = O_RDWR | O_BINARY | O_CREAT | O_EXCL | _O_SHORT_LIVED; 1655 1656 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; 1658 1666 #else 1659 1667 return mkstemp( template ); 1660 1668 #endif … … 1706 1714 { 1707 1715 tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) ); 1708 1716 tr_close_file( fd ); 1709 unlink( tmp );1717 tr_unlink( tmp ); 1710 1718 } 1711 1719 else 1712 1720 { 1713 1721 //tr_fsync( fd ); 1714 1722 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 ) ) 1721 1724 { 1722 1725 tr_inf( _( "Saved \"%s\"" ), filename ); 1723 1726 } … … 1725 1728 { 1726 1729 err = errno; 1727 1730 tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) ); 1728 unlink( tmp );1731 tr_unlink( tmp ); 1729 1732 } 1730 1733 } 1731 1734 } -
libtransmission/metainfo.c
63 63 { 64 64 int i; 65 65 char * path; 66 struct statsb;66 STAT sb; 67 67 const int tagCount = 5; 68 68 const char * tags[] = { "beos", "cli", "daemon", "macosx", "wx" }; 69 69 70 70 /* test the beos, cli, daemon, macosx, wx tags */ 71 71 for( i=0; i<tagCount; ++i ) { 72 72 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 ) ) 74 74 return path; 75 75 tr_free( path ); 76 76 } 77 77 78 78 /* test a non-tagged file */ 79 79 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 ) ) 81 81 return path; 82 82 tr_free( path ); 83 83 … … 92 92 tr_metainfoMigrate( tr_session * session, 93 93 tr_info * inf ) 94 94 { 95 struct statnew_sb;95 STAT new_sb; 96 96 char * name = getTorrentFilename( session, inf ); 97 97 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 ) ) 99 99 { 100 100 char * old_name = getOldTorrentFilename( session, inf ); 101 101 size_t contentLen; … … 106 106 { 107 107 FILE * out; 108 108 errno = 0; 109 out = fopen( name, "wb+" );109 out = tr_fopen( name, "wb+" ); 110 110 if( !out ) 111 111 { 112 112 tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ), … … 120 120 tr_free( inf->torrent ); 121 121 inf->torrent = tr_strdup( name ); 122 122 tr_sessionSetTorrentFile( session, inf->hashString, name ); 123 unlink( old_name );123 tr_unlink( old_name ); 124 124 } 125 125 fclose( out ); 126 126 } … … 599 599 char * filename; 600 600 601 601 filename = getTorrentFilename( session, inf ); 602 unlink( filename );602 tr_unlink( filename ); 603 603 tr_free( filename ); 604 604 605 605 filename = getOldTorrentFilename( session, inf ); 606 unlink( filename );606 tr_unlink( filename ); 607 607 tr_free( filename ); 608 608 } -
libtransmission/rpcimpl.c
1185 1185 tr_close_file( fd ); 1186 1186 } 1187 1187 1188 unlink( filename );1188 tr_unlink( filename ); 1189 1189 tr_free( filename ); 1190 1190 filename = filename2; 1191 1191 } … … 1198 1198 tr_snprintf( result, sizeof( result ), "success" ); 1199 1199 } 1200 1200 1201 unlink( filename );1201 tr_unlink( filename ); 1202 1202 tr_free( filename ); 1203 1203 } 1204 1204 -
libtransmission/announcer-udp.c
13 13 #define __LIBTRANSMISSION_ANNOUNCER_MODULE___ 14 14 15 15 #include <string.h> /* memcpy(), memset() */ 16 #include <errno.h> 16 17 17 18 #include <event2/buffer.h> 18 19 #include <event2/dns.h> -
libtransmission/fdlimit.c
52 52 #include "net.h" 53 53 #include "session.h" 54 54 #include "torrent.h" /* tr_isTorrent() */ 55 #include "platform.h" 55 56 56 57 #define dbgmsg( ... ) \ 57 58 do { \ … … 107 108 108 109 #ifdef WIN32 109 110 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)) 112 115 { 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 } 117 126 } 118 127 119 128 #else 120 129 121 130 int flags = O_RDWR | O_CREAT | O_LARGEFILE; 122 int fd = open( filename, flags, 0666 );131 int fd = tr_open( filename, flags, 0666 ); 123 132 if( fd >= 0 ) 124 133 { 125 134 # ifdef HAVE_FALLOCATE64 … … 276 285 static int 277 286 open_local_file( const char * filename, int flags ) 278 287 { 279 const int fd = open( filename, flags, 0666 );288 const int fd = tr_open( filename, flags, 0666 ); 280 289 tr_set_file_for_single_pass( fd ); 281 290 return fd; 282 291 } … … 355 364 uint64_t file_size ) 356 365 { 357 366 int flags; 358 struct statsb;367 STAT sb; 359 368 bool alreadyExisted; 360 369 361 370 /* 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 ) ) 363 372 { 364 373 const int err = errno; 365 374 tr_err( _( "Couldn't open \"%1$s\": %2$s" ), existing_dir, tr_strerror( err ) ); … … 379 388 tr_free( dir ); 380 389 } 381 390 382 alreadyExisted = ! stat( filename, &sb ) && S_ISREG( sb.st_mode );391 alreadyExisted = !tr_stati64( filename, &sb ) && S_ISREG( sb.st_mode ); 383 392 384 393 if( writable && !alreadyExisted && ( allocation == TR_PREALLOCATE_FULL ) ) 385 394 if( preallocate_file_full( filename, file_size ) ) … … 388 397 /* open the file */ 389 398 flags = writable ? ( O_RDWR | O_CREAT ) : O_RDONLY; 390 399 flags |= O_LARGEFILE | O_BINARY | O_SEQUENTIAL; 391 o->fd = open( filename, flags, 0666 );400 o->fd = tr_open( filename, flags, 0666 ); 392 401 393 402 if( o->fd == -1 ) 394 403 { … … 775 784 776 785 ensureSessionFdInfoExists( session ); 777 786 778 max = FD_SETSIZE - session->fdInfo->socket_limit - buffer_slots;787 max = MAX( FD_SETSIZE - session->fdInfo->socket_limit - buffer_slots, limit ); 779 788 if( limit > max ) 780 789 limit = max; 781 790 -
libtransmission/fdlimit.h
16 16 17 17 #include "transmission.h" 18 18 #include "net.h" 19 #ifdef WIN32 20 #include <sys/types.h> /* ssize_t, off_t */ 21 #endif 19 22 20 23 /** 21 24 * @addtogroup file_io File IO -
libtransmission/tr-dht.c
122 122 123 123 rc = getaddrinfo(name, pp, &hints, &info); 124 124 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)); 126 126 return; 127 127 } 128 128 … … 200 200 tr_buildPath(cl->session->configDir, "dht.bootstrap", NULL); 201 201 202 202 if(bootstrap_file) 203 f = fopen(bootstrap_file, "rb");203 f = tr_fopen(bootstrap_file, "rb"); 204 204 if(f != NULL) { 205 205 tr_ninf("DHT", "Attempting manual bootstrap"); 206 206 for(;;) { -
libtransmission/torrent-magnet.c
26 26 #include "torrent-magnet.h" 27 27 #include "utils.h" 28 28 #include "web.h" 29 #include "platform.h" 29 30 30 31 #define dbgmsg( tor, ... ) \ 31 32 do { \ … … 161 162 assert( tor->infoDictLength > 0 ); 162 163 assert( tor->infoDictOffset >= 0 ); 163 164 164 fp = fopen( tor->info.torrent, "rb" );165 fp = tr_fopen( tor->info.torrent, "rb" ); 165 166 if( fp != NULL ) 166 167 { 167 168 const int o = piece * METADATA_PIECE_SIZE; … … 259 260 int infoDictLength; 260 261 261 262 /* remove any old .torrent and .resume files */ 262 remove( path );263 tr_remove( path ); 263 264 tr_torrentRemoveResume( tor ); 264 265 265 266 dbgmsg( tor, "Saving completed metadata to \"%s\"", path ); -
libtransmission/platform.c
247 247 248 248 if( !home ) 249 249 { 250 home = tr_strdup( getenv( "HOME" ) );250 home = tr_strdup( tr_getenv( "HOME" ) ); 251 251 252 252 if( !home ) 253 253 { 254 254 #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 */ 256 257 *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); 258 260 home = tr_strdup( appdata ); 259 261 #else 260 262 struct passwd * pw = getpwuid( getuid( ) ); … … 283 285 "Application Support", 284 286 "Transmission", NULL ); 285 287 #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); 288 292 path = tr_buildPath( appdata, "Transmission", NULL ); 289 293 #elif defined( __HAIKU__ ) 290 294 char buf[TR_PATH_MAX]; … … 342 346 { 343 347 if( oldDir && newDir && strcmp( oldDir, newDir ) ) 344 348 { 345 DIR * dirh = opendir( oldDir );349 DIR * dirh = tr_opendir( oldDir ); 346 350 if( dirh ) 347 351 { 348 int 349 struct dirent* dirp;350 while( ( dirp = readdir( dirh ) ) )352 int count = 0; 353 DIRENT * dirp; 354 while( ( dirp = tr_readdir( dirh ) ) ) 351 355 { 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 352 361 const char * name = dirp->d_name; 362 #endif 353 363 if( name && strcmp( name, "." ) && strcmp( name, ".." ) ) 354 364 { 355 365 char * o = tr_buildPath( oldDir, name, NULL ); 356 366 char * n = tr_buildPath( newDir, name, NULL ); 357 rename( o, n );367 tr_rename( o, n ); 358 368 ++count; 359 369 tr_free( n ); 360 370 tr_free( o ); … … 364 374 if( count ) 365 375 tr_inf( _( "Migrated %1$d files from \"%2$s\" to \"%3$s\"" ), 366 376 count, oldDir, newDir ); 367 closedir( dirh );377 tr_closedir( dirh ); 368 378 } 369 379 } 370 380 } … … 442 452 443 453 if( !s ) 444 454 { 445 if( ( s = getenv( "TRANSMISSION_HOME" ) ) )455 if( ( s = tr_getenv( "TRANSMISSION_HOME" ) ) ) 446 456 { 447 457 s = tr_strdup( s ); 448 458 } … … 452 462 s = tr_buildPath( getHomeDir( ), "Library", "Application Support", 453 463 appname, NULL ); 454 464 #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); 457 469 s = tr_buildPath( appdata, appname, NULL ); 458 470 #elif defined( __HAIKU__ ) 459 471 char buf[TR_PATH_MAX]; 460 472 find_directory( B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof(buf) ); 461 473 s = tr_buildPath( buf, appname, NULL ); 462 474 #else 463 if( ( s = getenv( "XDG_CONFIG_HOME" ) ) )475 if( ( s = tr_getenv( "XDG_CONFIG_HOME" ) ) ) 464 476 s = tr_buildPath( s, appname, NULL ); 465 477 else 466 478 s = tr_buildPath( getHomeDir( ), ".config", appname, NULL ); … … 484 496 size_t content_len; 485 497 486 498 /* figure out where to look for user-dirs.dirs */ 487 config_home = getenv( "XDG_CONFIG_HOME" );499 config_home = tr_getenv( "XDG_CONFIG_HOME" ); 488 500 if( config_home && *config_home ) 489 501 config_file = tr_buildPath( config_home, "user-dirs.dirs", NULL ); 490 502 else … … 534 546 static int 535 547 isWebClientDir( const char * path ) 536 548 { 537 struct statsb;549 STAT sb; 538 550 char * tmp = tr_buildPath( path, "index.html", NULL ); 539 const int ret = ! stat( tmp, &sb );551 const int ret = !tr_stati64( tmp, &sb ); 540 552 tr_inf( _( "Searching for web interface file \"%s\"" ), tmp ); 541 553 tr_free( tmp ); 542 554 return ret; … … 549 561 550 562 if( !s ) 551 563 { 552 if( ( s = getenv( "CLUTCH_HOME" ) ) )564 if( ( s = tr_getenv( "CLUTCH_HOME" ) ) ) 553 565 { 554 566 s = tr_strdup( s ); 555 567 } 556 else if( ( s = getenv( "TRANSMISSION_WEB_HOME" ) ) )568 else if( ( s = tr_getenv( "TRANSMISSION_WEB_HOME" ) ) ) 557 569 { 558 570 s = tr_strdup( s ); 559 571 } … … 597 609 #elif defined( WIN32 ) 598 610 599 611 /* SHGetFolderPath explicitly requires MAX_PATH length */ 600 char dir[MAX_PATH]; 612 wchar_t dirUTF16[MAX_PATH + 1]; 613 char dir[MAX_PATH + 1]; 601 614 602 615 /* Generally, Web interface should be stored in a Web subdir of 603 616 * calling executable dir. */ 604 617 605 618 if( s == NULL ) { 606 619 /* 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); 608 622 s = tr_buildPath( dir, "Transmission", "Web", NULL ); 609 623 if( !isWebClientDir( s ) ) { 610 624 tr_free( s ); … … 614 628 615 629 if( s == NULL ) { 616 630 /* 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); 618 633 s = tr_buildPath( dir, "Transmission", "Web", NULL ); 619 634 if( !isWebClientDir( s ) ) { 620 635 tr_free( s ); … … 624 639 625 640 if( s == NULL) { 626 641 /* 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); 628 644 s = tr_buildPath( dirname( dir ), "Web", NULL ); 629 645 if( !isWebClientDir( s ) ) { 630 646 tr_free( s ); … … 638 654 const char * tmp; 639 655 640 656 /* 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" ); 642 658 if( tmp && *tmp ) 643 659 tr_list_append( &candidates, tr_strdup( tmp ) ); 644 660 else { … … 649 665 /* XDG_DATA_DIRS are the backup directories */ 650 666 { 651 667 const char * pkg = PACKAGE_DATA_DIR; 652 const char * xdg = getenv( "XDG_DATA_DIRS" );668 const char * xdg = tr_getenv( "XDG_DATA_DIRS" ); 653 669 const char * fallback = "/usr/local/share:/usr/share"; 654 670 char * buf = tr_strdup_printf( "%s:%s:%s", (pkg?pkg:""), (xdg?xdg:""), fallback ); 655 671 tmp = buf; … … 697 713 { 698 714 #ifdef WIN32 699 715 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) 701 720 ? (int64_t)freeBytesAvailable 702 721 : -1; 703 722 #elif defined(HAVE_STATVFS) … … 713 732 **** 714 733 ***/ 715 734 735 int 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); 716 742 #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 } 717 756 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; 757 FILE *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; 724 773 #else 725 static LONG volatile g_sl __attribute__ ( ( aligned ( 4 ) ));774 return fopen(filenameUTF8, mode); 726 775 #endif 727 728 /* Wait for spin lock */729 static int730 slwait( LONG volatile *sl )731 {732 while( InterlockedCompareExchange ( sl, 1, 0 ) != 0 )733 Sleep ( 0 );734 735 return 0;736 776 } 737 777 738 /* Release spin lock */ 739 static int 740 slrelease( LONG volatile *sl ) 778 int tr_rename(const char *oldUTF8, const char *newUTF8) 741 779 { 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 744 795 } 745 796 746 /* getpagesize for windows */ 747 static long 748 getpagesize( void ) 797 int tr_remove(const char *filenameUTF8) 749 798 { 750 static long g_pagesize = 0; 751 752 if( !g_pagesize ) 799 #ifdef WIN32 800 if (filenameUTF8) 753 801 { 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 } 757 811 } 758 return g_pagesize; 812 errno = ENOENT; 813 return -1; 814 #else 815 return remove(filenameUTF8); 816 #endif 759 817 } 760 818 761 static long 762 getregionsize( void ) 819 int tr_unlink(const char *filenameUTF8) 763 820 { 764 static long g_regionsize = 0; 765 766 if( !g_regionsize ) 821 #ifdef WIN32 822 if (filenameUTF8) 767 823 { 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); 771 828 } 772 return g_regionsize; 829 errno = ENOENT; 830 return -1; 831 #else 832 return unlink(filenameUTF8); 833 #endif 773 834 } 774 835 775 void * 776 mmap( void *ptr, 777 long size, 778 long prot, 779 long type, 780 long handle, 781 long arg ) 836 int tr_stati64(const char *restrict pathUTF8, STAT *restrict buf) 782 837 { 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); 801 844 } 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 806 850 } 807 851 808 long 809 munmap( void *ptr, 810 long size ) 852 DIR *tr_opendir (const char *dirnameUTF8) 811 853 { 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 832 866 } 833 867 868 char *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); 834 889 #endif 890 } -
libtransmission/platform.h
96 96 /** @brief return nonzero if the specified lock is locked */ 97 97 int tr_lockHave( const tr_lock * ); 98 98 99 /* @} */ 100 101 /** 102 * @addtogroup utf16 Windows_UTF16_Support 103 * @{ 104 */ 105 106 /** 107 * @brief Portability wrapper for open() 108 */ 99 109 #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 101 113 102 long munmap( void *ptr, long size ); 114 int tr_open(const char *, int, ...); 115 116 /** 117 * @brief Portability wrapper for fopen() 118 */ 119 FILE *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 */ 134 int tr_rename(const char *, const char *); 135 136 /** 137 * @brief Portability wrapper for remove() 138 */ 139 int tr_remove(const char *); 140 141 /** 142 * @brief Portability wrapper for unlink() 143 */ 144 int 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; 103 154 #endif 104 155 156 int 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 171 DIR *tr_opendir (const char *); 172 173 /** 174 * @brief Portability wrapper for readdir() 175 */ 176 static inline 177 DIRENT *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 */ 189 static inline 190 int 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 */ 202 char *tr_getenv (const char *); 203 105 204 /* @} */ 106 205 107 206 #endif 207 -
libtransmission/resume.c
885 885 tr_torrentRemoveResume( const tr_torrent * tor ) 886 886 { 887 887 char * filename = getResumeFilename( tor ); 888 unlink( filename );888 tr_unlink( filename ); 889 889 tr_free( filename ); 890 890 } -
libtransmission/upnp.c
97 97 { 98 98 struct UPNPDev * devlist; 99 99 errno = 0; 100 devlist = upnpDiscover( 2000, NULL, NULL, 0 );100 devlist = upnpDiscover( 2000, NULL, NULL, 0, &errno ); 101 101 if( devlist == NULL ) 102 102 { 103 103 tr_ndbg( … … 106 106 } 107 107 errno = 0; 108 108 if( UPNP_GetValidIGD( devlist, &handle->urls, &handle->data, 109 handle->lanaddr, sizeof( handle->lanaddr ) ) == UPNP_IGD_VALID_CONNECTED)109 handle->lanaddr, sizeof( handle->lanaddr ) ) ) 110 110 { 111 111 tr_ninf( getKey( ), _( 112 112 "Found Internet Gateway Device \"%s\"" ), … … 144 144 145 145 tr_snprintf( portStr, sizeof( portStr ), "%d", handle->port ); 146 146 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 || 148 148 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 ) 150 150 { 151 151 tr_ninf( getKey( ), _( "Port %d isn't forwarded" ), handle->port ); 152 152 handle->isMapped = false; … … 198 198 err_tcp = UPNP_AddPortMapping( handle->urls.controlURL, 199 199 handle->data.first.servicetype, 200 200 portStr, portStr, handle->lanaddr, 201 desc, "TCP", NULL );201 desc, "TCP", NULL, NULL ); 202 202 if( err_tcp ) 203 203 tr_ndbg( getKey( ), "TCP Port forwarding failed with error %d (errno %d - %s)", 204 204 err_tcp, errno, tr_strerror( errno ) ); … … 207 207 err_udp = UPNP_AddPortMapping( handle->urls.controlURL, 208 208 handle->data.first.servicetype, 209 209 portStr, portStr, handle->lanaddr, 210 desc, "UDP", NULL );210 desc, "UDP", NULL, NULL ); 211 211 if( err_udp ) 212 212 tr_ndbg( getKey( ), "UDP Port forwarding failed with error %d (errno %d - %s)", 213 213 err_udp, errno, tr_strerror( errno ) ); -
libtransmission/session.c
1857 1857 int * setmeCount ) 1858 1858 { 1859 1859 int i, n = 0; 1860 struct statsb;1860 STAT sb; 1861 1861 DIR * odir = NULL; 1862 1862 const char * dirname = tr_getTorrentDir( session ); 1863 1863 tr_torrent ** torrents; … … 1867 1867 1868 1868 tr_ctorSetSave( ctor, false ); /* since we already have them */ 1869 1869 1870 if( ! stat( dirname, &sb )1870 if( !tr_stati64( dirname, &sb ) 1871 1871 && S_ISDIR( sb.st_mode ) 1872 && ( ( odir = opendir ( dirname ) ) ) )1872 && ( ( odir = tr_opendir ( dirname ) ) ) ) 1873 1873 { 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 ) ) 1876 1876 { 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" ) ) 1878 1885 { 1879 1886 tr_torrent * tor; 1880 char * path = tr_buildPath( dirname, d->d_name, NULL );1887 char * path = tr_buildPath( dirname, name, NULL ); 1881 1888 tr_ctorSetMetainfoFromFile( ctor, path ); 1882 1889 if(( tor = tr_torrentNew( ctor, NULL ))) 1883 1890 { … … 1887 1894 tr_free( path ); 1888 1895 } 1889 1896 } 1890 closedir( odir );1897 tr_closedir( odir ); 1891 1898 } 1892 1899 1893 1900 torrents = tr_new( tr_torrent *, n ); … … 2117 2124 { 2118 2125 int binCount = 0; 2119 2126 int newCount = 0; 2120 struct statsb;2127 STAT sb; 2121 2128 char * dirname; 2122 2129 DIR * odir = NULL; 2123 2130 tr_list * list = NULL; … … 2125 2132 2126 2133 /* walk through the directory and find blocklists */ 2127 2134 dirname = tr_buildPath( session->configDir, "blocklists", NULL ); 2128 if( ! stat( dirname,2135 if( !tr_stati64( dirname, 2129 2136 &sb ) && S_ISDIR( sb.st_mode ) 2130 && ( ( odir = opendir( dirname ) ) ) )2137 && ( ( odir = tr_opendir( dirname ) ) ) ) 2131 2138 { 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 ) ) 2134 2141 { 2135 2142 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 .. 2138 2151 */ 2139 2152 continue; 2140 2153 2141 filename = tr_buildPath( dirname, d->d_name, NULL );2154 filename = tr_buildPath( dirname, name, NULL ); 2142 2155 2143 2156 if( tr_stringEndsWith( filename, ".bin" ) ) 2144 2157 { … … 2156 2169 /* strip out the file suffix, if there is one, and add ".bin" 2157 2170 instead */ 2158 2171 tr_blocklist * b; 2159 const char * dot = strrchr( d->d_name, '.' );2160 const int len = dot ? dot - d->d_name2161 : (int)strlen( d->d_name );2172 const char * dot = strrchr( name, '.' ); 2173 const int len = dot ? dot - name 2174 : (int)strlen( name ); 2162 2175 char * tmp = tr_strdup_printf( 2163 2176 "%s" TR_PATH_DELIMITER_STR "%*.*s.bin", 2164 dirname, len, len, d->d_name );2177 dirname, len, len, name ); 2165 2178 b = _tr_blocklistNew( tmp, isEnabled ); 2166 2179 _tr_blocklistSetContent( b, filename ); 2167 2180 tr_list_append( &list, b ); … … 2172 2185 tr_free( filename ); 2173 2186 } 2174 2187 2175 closedir( odir );2188 tr_closedir( odir ); 2176 2189 } 2177 2190 2178 2191 session->blocklists = list; … … 2308 2321 static void 2309 2322 metainfoLookupInit( tr_session * session ) 2310 2323 { 2311 struct statsb;2324 STAT sb; 2312 2325 const char * dirname = tr_getTorrentDir( session ); 2313 2326 DIR * odir = NULL; 2314 2327 tr_ctor * ctor = NULL; … … 2322 2335 tr_bencInitDict( lookup, 0 ); 2323 2336 ctor = tr_ctorNew( session ); 2324 2337 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 ) ) ) ) 2326 2339 { 2327 struct dirent*d;2328 while(( d = readdir( odir )))2340 DIRENT *d; 2341 while(( d = tr_readdir( odir ))) 2329 2342 { 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" ) ) 2331 2351 { 2332 2352 tr_info inf; 2333 char * path = tr_buildPath( dirname, d->d_name, NULL );2353 char * path = tr_buildPath( dirname, name, NULL ); 2334 2354 tr_ctorSetMetainfoFromFile( ctor, path ); 2335 2355 if( !tr_torrentParse( ctor, &inf ) ) 2336 2356 { … … 2340 2360 tr_free( path ); 2341 2361 } 2342 2362 } 2343 closedir( odir );2363 tr_closedir( odir ); 2344 2364 } 2345 2365 tr_ctorFree( ctor ); 2346 2366 -
libtransmission/makemeta.c
51 51 { 52 52 int i; 53 53 char * buf; 54 struct statsb;54 STAT sb; 55 55 DIR * odir = NULL; 56 56 57 57 sb.st_size = 0; 58 58 59 59 buf = tr_buildPath( dir, base, NULL ); 60 i = stat( buf, &sb );60 i = tr_stati64( buf, &sb ); 61 61 if( i ) 62 62 { 63 63 tr_err( _( "Torrent Creator is skipping file \"%s\": %s" ), … … 66 66 return list; 67 67 } 68 68 69 if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )69 if( S_ISDIR( sb.st_mode ) && ( ( odir = tr_opendir ( buf ) ) ) ) 70 70 { 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 74 81 list = getFiles( buf, d->d_name, list ); 75 closedir( odir ); 82 #endif 83 } 84 tr_closedir( odir ); 76 85 } 77 86 else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) ) 78 87 { … … 126 135 ret->top = tr_strdup( topFile ); 127 136 128 137 { 129 struct statsb;130 stat( topFile, &sb );138 STAT sb; 139 tr_stati64( topFile, &sb ); 131 140 ret->isSingleFile = !S_ISDIR( sb.st_mode ); 132 141 } 133 142 -
libtransmission/net.c
75 75 { 76 76 *buf = '\0'; 77 77 #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 ); 79 79 #else 80 80 tr_strlcpy( buf, tr_strerror( err ), buflen ); 81 81 #endif -
libtransmission/net.h
38 38 #endif 39 39 40 40 #ifdef WIN32 41 #define ETIMEDOUT WSAETIMEDOUT 41 42 #define EADDRINUSE WSAEADDRINUSE 42 43 #define ECONNREFUSED WSAECONNREFUSED 43 44 #define ECONNRESET WSAECONNRESET -
libtransmission/torrent.c
791 791 uint64_t loaded; 792 792 const char * dir; 793 793 bool isNewTorrent; 794 struct statst;794 STAT st; 795 795 static int nextUniqueId = 1; 796 796 tr_session * session = tr_ctorGetSession( ctor ); 797 797 … … 874 874 ++session->torrentCount; 875 875 876 876 /* 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 ); 878 878 879 879 /* maybe save our own copy of the metainfo */ 880 880 if( tr_ctorGetSave( ctor ) ) … … 1533 1533 1534 1534 for( i=0; i<n; ++i ) 1535 1535 { 1536 struct statsb;1536 STAT sb; 1537 1537 char * filename = tr_torrentFindFile( tor, i ); 1538 1538 1539 1539 sb.st_size = 0; 1540 if( filename && ! stat( filename, &sb ) )1540 if( filename && !tr_stati64( filename, &sb ) ) 1541 1541 byte_count += sb.st_size; 1542 1542 1543 1543 tr_free( filename ); … … 1903 1903 tr_torinf( tor, "Calling script \"%s\"", script ); 1904 1904 1905 1905 #ifdef WIN32 1906 _spawnvpe( _P_NOWAIT, script, (const char* )cmd,env );1906 _spawnvpe( _P_NOWAIT, script, (const char* const *)cmd, (const char* const *)env ); 1907 1907 #else 1908 1908 signal( SIGCHLD, onSigCHLD ); 1909 1909 … … 2561 2561 { 2562 2562 if( !tor->info.files[i].dnd ) 2563 2563 { 2564 struct statsb;2564 STAT sb; 2565 2565 const uint64_t length = tor->info.files[i].length; 2566 2566 char * path = tr_torrentFindFile( tor, i ); 2567 2567 2568 2568 bytesLeft += length; 2569 2569 2570 if( ( path != NULL ) && ! stat( path, &sb )2570 if( ( path != NULL ) && !tr_stati64( path, &sb ) 2571 2571 && S_ISREG( sb.st_mode ) 2572 2572 && ( (uint64_t)sb.st_size <= length ) ) 2573 2573 bytesLeft -= sb.st_size; … … 2633 2633 tr_ptrArray * folders, 2634 2634 tr_ptrArray * dirtyFolders ) 2635 2635 { 2636 struct statsb;2636 STAT sb; 2637 2637 char * buf = tr_buildPath( dir, base, NULL ); 2638 int i = stat( buf, &sb );2638 int i = tr_stati64( buf, &sb ); 2639 2639 2640 2640 if( !i ) 2641 2641 { 2642 2642 DIR * odir = NULL; 2643 2643 2644 if( S_ISDIR( sb.st_mode ) && ( ( odir = opendir ( buf ) ) ) )2644 if( S_ISDIR( sb.st_mode ) && ( ( odir = tr_opendir ( buf ) ) ) ) 2645 2645 { 2646 struct dirent*d;2646 DIRENT *d; 2647 2647 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 2650 2657 walkLocalData( tor, root, buf, d->d_name, torrentFiles, folders, dirtyFolders ); 2651 closedir( odir ); 2658 #endif 2659 } 2660 tr_closedir( odir ); 2652 2661 } 2653 2662 else if( S_ISREG( sb.st_mode ) && ( sb.st_size > 0 ) ) 2654 2663 { … … 2665 2674 static void 2666 2675 deleteLocalFile( const char * filename, tr_fileFunc fileFunc ) 2667 2676 { 2668 struct statsb;2669 if( ! stat( filename, &sb ) ) /* if file exists... */2677 STAT sb; 2678 if( !tr_stati64( filename, &sb ) ) /* if file exists... */ 2670 2679 fileFunc( filename ); 2671 2680 } 2672 2681 … … 2741 2750 assert( tr_isTorrent( tor ) ); 2742 2751 2743 2752 if( fileFunc == NULL ) 2744 fileFunc = remove;2753 fileFunc = tr_remove; 2745 2754 2746 2755 /* close all the files because we're about to delete them */ 2747 2756 tr_cacheFlushTorrent( tor->session->cache, tor ); … … 2849 2858 { 2850 2859 /* blow away the leftover subdirectories in the old location */ 2851 2860 if( do_move ) 2852 tr_torrentDeleteLocalData( tor, remove );2861 tr_torrentDeleteLocalData( tor, tr_remove ); 2853 2862 2854 2863 /* set the new location and reverify */ 2855 2864 tr_torrentSetDownloadDir( tor, location ); … … 2930 2939 char * oldpath = tr_buildPath( base, sub, NULL ); 2931 2940 char * newpath = tr_buildPath( base, f->name, NULL ); 2932 2941 2933 if( rename( oldpath, newpath ) )2942 if( tr_rename( oldpath, newpath ) ) 2934 2943 tr_torerr( tor, "Error moving \"%s\" to \"%s\": %s", oldpath, newpath, tr_strerror( errno ) ); 2935 2944 2936 2945 tr_free( newpath ); … … 2955 2964 static bool 2956 2965 fileExists( const char * filename, time_t * mtime ) 2957 2966 { 2958 struct statsb;2959 const bool ok = ! stat( filename, &sb );2967 STAT sb; 2968 const bool ok = !tr_stati64( filename, &sb ); 2960 2969 2961 2970 if( ok && ( mtime != NULL ) ) 2962 2971 *mtime = TR_STAT_MTIME( sb ); -
qt/qtr.pro
24 24 LIBS += $${TRANSMISSION_TOP}/third-party/miniupnp/libminiupnp.a 25 25 LIBS += $${TRANSMISSION_TOP}/third-party/libnatpmp/libnatpmp.a 26 26 unix: 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 27 win32:LIBS += -lidn -liconv -lwldap32 -liphlpapi -lintl -lpsapi 30 28 31 29 TRANSLATIONS += translations/transmission_en.ts \ 32 30 translations/transmission_es.ts \ -
qt/make-dialog.cc
58 58 { 59 59 case QDialogButtonBox::Open: 60 60 std::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() ); 62 62 break; 63 63 case QDialogButtonBox::Abort: 64 64 myBuilder->abortFlag = true; … … 79 79 myNewProgress->setValue( (int) ((100.0 * b->pieceIndex) / denom ) ); 80 80 81 81 // progress label 82 const QString base( QFileInfo( b->top).baseName() );82 const QString base( QFileInfo(QString::fromUtf8(b->top)).completeBaseName() ); 83 83 QString str; 84 84 if( !b->isDone ) 85 85 str = tr( "Creating \"%1\"" ).arg( base ); … … 144 144 myTimer.start( 100 ); 145 145 146 146 // 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" ); 148 148 std::cerr << qPrintable(myTarget) << std::endl; 149 149 150 150 // comment -
qt/session.cc
44 44 #include "prefs.h" 45 45 #include "session.h" 46 46 #include "session-dialog.h" 47 48 #ifdef WIN32 49 #undef ERROR /* defined in wingdi.h, included by windows.h */ 50 #endif 51 47 52 #include "torrent.h" 48 53 #include "utils.h" 49 54 -
third-party/dht/dht.c
83 83 { 84 84 return rand(); 85 85 } 86 extern const char *inet_ntop(int, const void *, char *, socklen_t); 86 extern 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) 87 88 88 89 #else 89 90 -
third-party/libutp/utp.cpp
12 12 #include <limits.h> // for UINT_MAX 13 13 14 14 #ifdef WIN32 15 #include "win32_inet_ntop.h" 15 /* libtransmission switched to using libevent's inet_ntop */ 16 extern 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) 16 18 17 19 // newer versions of MSVC define these in errno.h 18 20 #ifndef ECONNRESET