2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2005-2008 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-11-23 17:05:44 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
#define HAVE_GETRLIMIT
|
|
|
|
#endif
|
|
|
|
|
2009-01-25 23:47:07 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
#ifdef _XOPEN_SOURCE
|
|
|
|
#undef _XOPEN_SOURCE
|
|
|
|
#endif
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#endif
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
2007-10-31 18:10:55 +00:00
|
|
|
#include <inttypes.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-11-05 05:56:06 +00:00
|
|
|
#ifdef SYS_DARWIN
|
2009-01-08 19:20:24 +00:00
|
|
|
#include <fcntl.h>
|
2008-11-05 05:56:06 +00:00
|
|
|
#endif
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2009-04-05 14:04:07 +00:00
|
|
|
#ifdef HAVE_XFS_XFS_H
|
|
|
|
#include <xfs/xfs.h>
|
|
|
|
#endif
|
|
|
|
|
2007-07-13 00:15:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-11-23 17:05:44 +00:00
|
|
|
#ifdef HAVE_GETRLIMIT
|
|
|
|
#include <sys/time.h> /* getrlimit */
|
|
|
|
#include <sys/resource.h> /* getrlimit */
|
|
|
|
#endif
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <unistd.h>
|
2009-01-25 23:47:07 +00:00
|
|
|
#include <fcntl.h> /* O_LARGEFILE posix_fadvise */
|
2009-01-25 23:40:08 +00:00
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
#include <evutil.h>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2008-08-11 19:05:02 +00:00
|
|
|
#include "fdlimit.h"
|
2007-10-31 18:10:55 +00:00
|
|
|
#include "list.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "net.h"
|
2009-07-04 04:33:23 +00:00
|
|
|
#include "platform.h" /* MAX_PATH_LENGTH, TR_PATH_DELIMITER */
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-10-26 15:39:04 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
2007-01-21 07:16:18 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
enum
|
|
|
|
{
|
2008-01-03 16:36:20 +00:00
|
|
|
NOFILE_BUFFER = 512, /* the process' number of open files is
|
|
|
|
globalMaxPeers + NOFILE_BUFFER */
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
struct tr_openfile
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-11-28 22:11:41 +00:00
|
|
|
tr_bool isCheckedOut;
|
|
|
|
tr_bool isWritable;
|
2009-06-21 07:36:51 +00:00
|
|
|
int torrentId;
|
2008-11-28 22:11:41 +00:00
|
|
|
char filename[MAX_PATH_LENGTH];
|
|
|
|
int fd;
|
|
|
|
uint64_t date;
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_fd_s
|
|
|
|
{
|
2008-10-14 20:44:41 +00:00
|
|
|
int socketCount;
|
2009-01-08 19:20:24 +00:00
|
|
|
int socketLimit;
|
|
|
|
int openFileLimit;
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * openFiles;
|
2007-10-30 18:35:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct tr_fd_s * gFd = NULL;
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Local Files
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
#ifndef O_LARGEFILE
|
2009-01-08 19:20:24 +00:00
|
|
|
#define O_LARGEFILE 0
|
2008-11-15 19:59:18 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-16 16:38:16 +00:00
|
|
|
static tr_bool
|
|
|
|
preallocateFileSparse( int fd, uint64_t length )
|
|
|
|
{
|
|
|
|
const char zero = '\0';
|
|
|
|
|
|
|
|
if( length == 0 )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if( lseek( fd, length-1, SEEK_SET ) == -1 )
|
|
|
|
return FALSE;
|
|
|
|
if( write( fd, &zero, 1 ) == -1 )
|
|
|
|
return FALSE;
|
|
|
|
if( ftruncate( fd, length ) == -1 )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
preallocateFileFull( const char * filename, uint64_t length )
|
2008-11-05 05:56:06 +00:00
|
|
|
{
|
2009-01-16 16:38:16 +00:00
|
|
|
tr_bool success = 0;
|
2008-11-15 19:59:18 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0 );
|
|
|
|
if( hFile != INVALID_HANDLE_VALUE )
|
|
|
|
{
|
|
|
|
LARGE_INTEGER li;
|
2008-11-19 16:06:31 +00:00
|
|
|
li.QuadPart = length;
|
2008-11-19 17:48:24 +00:00
|
|
|
success = SetFilePointerEx( hFile, li, NULL, FILE_BEGIN ) && SetEndOfFile( hFile );
|
2008-11-15 19:59:18 +00:00
|
|
|
CloseHandle( hFile );
|
|
|
|
}
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
#else
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
int flags = O_RDWR | O_CREAT | O_LARGEFILE;
|
|
|
|
int fd = open( filename, flags, 0666 );
|
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
2009-04-05 14:04:07 +00:00
|
|
|
# ifdef HAVE_XFS_XFS_H
|
|
|
|
if( !success && platform_test_xfs_fd( fd ) )
|
|
|
|
{
|
|
|
|
xfs_flock64_t fl;
|
|
|
|
fl.l_whence = 0;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = length;
|
|
|
|
success = !xfsctl( NULL, fd, XFS_IOC_RESVSP64, &fl );
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# ifdef SYS_DARWIN
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
fstore_t fst;
|
|
|
|
fst.fst_flags = F_ALLOCATECONTIG;
|
|
|
|
fst.fst_posmode = F_PEOFPOSMODE;
|
|
|
|
fst.fst_offset = 0;
|
|
|
|
fst.fst_length = length;
|
|
|
|
fst.fst_bytesalloc = 0;
|
|
|
|
success = !fcntl( fd, F_PREALLOCATE, &fst );
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_POSIX_FALLOCATE
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
success = !posix_fallocate( fd, 0, length );
|
|
|
|
}
|
2008-11-15 19:59:18 +00:00
|
|
|
# endif
|
|
|
|
|
2009-06-16 17:10:47 +00:00
|
|
|
if( !success ) /* if nothing else works, do it the old-fashioned way */
|
|
|
|
{
|
|
|
|
uint8_t buf[ 4096 ];
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
|
|
|
success = TRUE;
|
|
|
|
while ( success && ( length > 0 ) )
|
|
|
|
{
|
|
|
|
const int thisPass = MIN( length, sizeof( buf ) );
|
|
|
|
success = write( fd, buf, thisPass ) == thisPass;
|
|
|
|
length -= thisPass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-15 19:59:18 +00:00
|
|
|
close( fd );
|
|
|
|
}
|
2008-11-05 05:56:06 +00:00
|
|
|
|
|
|
|
#endif
|
2008-11-15 19:59:18 +00:00
|
|
|
|
|
|
|
return success;
|
2008-11-05 05:56:06 +00:00
|
|
|
}
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
tr_bool
|
|
|
|
tr_preallocate_file( const char * filename, uint64_t length )
|
|
|
|
{
|
|
|
|
return preallocateFileFull( filename, length );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_open_file_for_writing( const char * filename )
|
|
|
|
{
|
|
|
|
int flags = O_WRONLY | O_CREAT;
|
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
|
|
|
return open( filename, flags, 0666 );
|
|
|
|
}
|
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
int
|
2009-04-26 00:51:51 +00:00
|
|
|
tr_open_file_for_scanning( const char * filename )
|
2009-04-07 20:25:32 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* build the flags */
|
|
|
|
flags = O_RDONLY;
|
|
|
|
#ifdef O_SEQUENTIAL
|
2009-05-06 13:33:40 +00:00
|
|
|
flags |= O_SEQUENTIAL;
|
2009-04-07 20:25:32 +00:00
|
|
|
#endif
|
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* open the file */
|
|
|
|
fd = open( filename, flags, 0666 );
|
2009-04-26 20:44:18 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
2009-05-06 19:06:19 +00:00
|
|
|
/* Set hints about the lookahead buffer and caching. It's okay
|
|
|
|
for these to fail silently, so don't let them affect errno */
|
|
|
|
const int err = errno;
|
2009-04-10 14:26:01 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
2009-04-26 20:44:18 +00:00
|
|
|
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
|
2009-04-07 20:25:32 +00:00
|
|
|
#endif
|
2009-04-26 19:50:31 +00:00
|
|
|
#ifdef SYS_DARWIN
|
2009-04-26 20:44:18 +00:00
|
|
|
fcntl( fd, F_NOCACHE, 1 );
|
|
|
|
fcntl( fd, F_RDAHEAD, 1 );
|
2009-04-26 19:50:31 +00:00
|
|
|
#endif
|
2009-05-06 19:06:19 +00:00
|
|
|
errno = err;
|
2009-04-26 20:44:18 +00:00
|
|
|
}
|
2009-04-07 20:25:32 +00:00
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
return fd;
|
2009-04-07 20:25:32 +00:00
|
|
|
}
|
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
void
|
|
|
|
tr_close_file( int fd )
|
2009-04-26 00:51:51 +00:00
|
|
|
{
|
|
|
|
#if defined(HAVE_POSIX_FADVISE)
|
2009-05-07 11:54:09 +00:00
|
|
|
/* Set hint about not caching this file.
|
|
|
|
It's okay for this to fail silently, so don't let it affect errno */
|
2009-05-07 13:03:39 +00:00
|
|
|
const int err = errno;
|
2009-04-26 00:51:51 +00:00
|
|
|
posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
|
2009-05-07 11:54:09 +00:00
|
|
|
errno = err;
|
2009-04-26 00:51:51 +00:00
|
|
|
#endif
|
2009-04-26 20:44:18 +00:00
|
|
|
close( fd );
|
2009-04-26 00:51:51 +00:00
|
|
|
}
|
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
/**
|
|
|
|
* returns 0 on success, or an errno value on failure.
|
|
|
|
* errno values include ENOENT if the parent folder doesn't exist,
|
|
|
|
* plus the errno values set by tr_mkdirp() and open().
|
|
|
|
*/
|
|
|
|
static int
|
2009-01-16 16:38:16 +00:00
|
|
|
TrOpenFile( int i,
|
|
|
|
const char * folder,
|
|
|
|
const char * torrentFile,
|
|
|
|
tr_bool doWrite,
|
|
|
|
tr_preallocation_mode preallocationMode,
|
|
|
|
uint64_t desiredFileSize )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-01-08 19:20:24 +00:00
|
|
|
struct tr_openfile * file = &gFd->openFiles[i];
|
2008-09-23 19:11:04 +00:00
|
|
|
int flags;
|
2008-10-14 03:03:29 +00:00
|
|
|
char * filename;
|
2008-09-23 19:11:04 +00:00
|
|
|
struct stat sb;
|
2009-07-04 04:33:23 +00:00
|
|
|
tr_bool alreadyExisted;
|
2008-01-18 19:13:32 +00:00
|
|
|
|
|
|
|
/* confirm the parent folder exists */
|
|
|
|
if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
|
2009-01-17 15:08:20 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
tr_err( _( "Couldn't create \"%1$s\": \"%2$s\" is not a folder" ), torrentFile, folder );
|
2008-10-03 04:49:06 +00:00
|
|
|
return ENOENT;
|
2009-01-17 15:08:20 +00:00
|
|
|
}
|
2007-01-21 07:16:18 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/* create subfolders, if any */
|
2008-10-14 03:03:29 +00:00
|
|
|
filename = tr_buildPath( folder, torrentFile, NULL );
|
2008-11-05 05:56:06 +00:00
|
|
|
if( doWrite )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-10-14 03:39:16 +00:00
|
|
|
char * tmp = tr_dirname( filename );
|
|
|
|
const int err = tr_mkdirp( tmp, 0777 ) ? errno : 0;
|
2008-10-14 04:51:42 +00:00
|
|
|
if( err ) {
|
2009-01-17 15:08:20 +00:00
|
|
|
tr_err( _( "Couldn't create \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
|
|
|
|
tr_free( tmp );
|
2008-10-14 04:51:42 +00:00
|
|
|
tr_free( filename );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2008-10-14 04:51:42 +00:00
|
|
|
}
|
2009-01-17 15:08:20 +00:00
|
|
|
tr_free( tmp );
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
alreadyExisted = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
|
2008-11-15 19:59:18 +00:00
|
|
|
|
2009-01-16 16:38:16 +00:00
|
|
|
if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_FULL ) )
|
|
|
|
if( preallocateFileFull( filename, desiredFileSize ) )
|
2008-11-15 19:59:18 +00:00
|
|
|
tr_inf( _( "Preallocated file \"%s\"" ), filename );
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2007-10-31 18:10:55 +00:00
|
|
|
/* open the file */
|
2008-11-05 05:56:06 +00:00
|
|
|
flags = doWrite ? ( O_RDWR | O_CREAT ) : O_RDONLY;
|
2009-05-31 21:20:49 +00:00
|
|
|
#ifdef O_SEQUENTIAL
|
|
|
|
flags |= O_SEQUENTIAL;
|
2009-01-25 23:33:10 +00:00
|
|
|
#endif
|
2007-11-09 04:32:19 +00:00
|
|
|
#ifdef O_LARGEFILE
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
#endif
|
2007-10-30 18:35:06 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
flags |= O_BINARY;
|
2007-01-21 19:42:11 +00:00
|
|
|
#endif
|
2007-11-19 12:59:26 +00:00
|
|
|
file->fd = open( filename, flags, 0666 );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( file->fd == -1 )
|
|
|
|
{
|
2008-01-18 19:13:32 +00:00
|
|
|
const int err = errno;
|
2009-01-17 15:08:20 +00:00
|
|
|
tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( filename );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 07:53:51 +00:00
|
|
|
/* If the file already exists and it's too large, truncate it.
|
|
|
|
* This is a fringe case that happens if a torrent's been updated
|
|
|
|
* and one of the updated torrent's files is smaller.
|
|
|
|
* http://trac.transmissionbt.com/ticket/2228
|
|
|
|
* https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
|
|
|
|
*/
|
|
|
|
if( alreadyExisted && ( desiredFileSize < (uint64_t)sb.st_size ) )
|
|
|
|
ftruncate( file->fd, desiredFileSize );
|
|
|
|
|
2009-01-16 16:38:16 +00:00
|
|
|
if( doWrite && !alreadyExisted && ( preallocationMode == TR_PREALLOCATE_SPARSE ) )
|
|
|
|
preallocateFileSparse( file->fd, desiredFileSize );
|
|
|
|
|
2009-04-10 14:26:01 +00:00
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
2009-07-04 04:33:23 +00:00
|
|
|
/* this doubles the OS level readahead buffer, which in practice
|
|
|
|
* turns out to be a good thing, because many (most?) clients request
|
|
|
|
* chunks of blocks in order */
|
2009-05-31 21:20:49 +00:00
|
|
|
posix_fadvise( file->fd, 0, 0, POSIX_FADV_SEQUENTIAL );
|
2009-01-25 23:33:10 +00:00
|
|
|
#endif
|
|
|
|
|
2008-10-14 03:03:29 +00:00
|
|
|
tr_free( filename );
|
2008-10-03 04:49:06 +00:00
|
|
|
return 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
static TR_INLINE tr_bool
|
2007-10-30 18:35:06 +00:00
|
|
|
fileIsOpen( const struct tr_openfile * o )
|
|
|
|
{
|
2007-10-31 18:10:55 +00:00
|
|
|
return o->fd >= 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
static void
|
2009-07-04 04:33:23 +00:00
|
|
|
TrCloseFile( struct tr_openfile * o )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
assert( o != NULL );
|
2007-10-31 18:10:55 +00:00
|
|
|
assert( fileIsOpen( o ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-04-26 20:44:18 +00:00
|
|
|
tr_close_file( o->fd );
|
2007-10-31 18:10:55 +00:00
|
|
|
o->fd = -1;
|
2009-07-04 04:33:23 +00:00
|
|
|
o->isCheckedOut = FALSE;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fileIsCheckedOut( const struct tr_openfile * o )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
return fileIsOpen( o ) && o->isCheckedOut;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
/* returns an fd on success, or a -1 on failure and sets errno */
|
2007-10-30 18:35:06 +00:00
|
|
|
int
|
2009-06-21 07:36:51 +00:00
|
|
|
tr_fdFileCheckout( int torrentId,
|
|
|
|
const char * folder,
|
2009-01-16 16:38:16 +00:00
|
|
|
const char * torrentFile,
|
|
|
|
tr_bool doWrite,
|
|
|
|
tr_preallocation_mode preallocationMode,
|
|
|
|
uint64_t desiredFileSize )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-30 03:29:09 +00:00
|
|
|
int i, winner = -1;
|
2007-10-30 18:35:06 +00:00
|
|
|
struct tr_openfile * o;
|
2008-12-30 03:29:09 +00:00
|
|
|
char filename[MAX_PATH_LENGTH];
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2009-06-21 07:36:51 +00:00
|
|
|
assert( torrentId > 0 );
|
2008-01-18 19:13:32 +00:00
|
|
|
assert( folder && *folder );
|
|
|
|
assert( torrentFile && *torrentFile );
|
2009-07-04 04:33:23 +00:00
|
|
|
assert( tr_isBool( doWrite ) );
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2008-12-30 03:29:09 +00:00
|
|
|
tr_snprintf( filename, sizeof( filename ), "%s%c%s", folder, TR_PATH_DELIMITER, torrentFile );
|
|
|
|
dbgmsg( "looking for file '%s', writable %c", filename, doWrite ? 'y' : 'n' );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-06-21 07:36:51 +00:00
|
|
|
/* is it already open? */
|
|
|
|
for( i=0; i<gFd->openFileLimit; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-08 19:20:24 +00:00
|
|
|
o = &gFd->openFiles[i];
|
2007-10-30 18:35:06 +00:00
|
|
|
|
|
|
|
if( !fileIsOpen( o ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
continue;
|
2009-06-21 07:36:51 +00:00
|
|
|
if( torrentId != o->torrentId )
|
|
|
|
continue;
|
2007-10-30 18:35:06 +00:00
|
|
|
if( strcmp( filename, o->filename ) )
|
|
|
|
continue;
|
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
assert( !fileIsCheckedOut( o ) );
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
if( doWrite && !o->isWritable )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-06-21 07:36:51 +00:00
|
|
|
dbgmsg( "found it! it's open and available, but isn't writable. closing..." );
|
2009-07-04 04:33:23 +00:00
|
|
|
TrCloseFile( o );
|
2007-10-31 18:10:55 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
|
|
|
dbgmsg( "found it! it's ready for use!" );
|
2007-01-14 12:00:21 +00:00
|
|
|
winner = i;
|
2007-12-03 15:27:38 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2009-06-21 07:36:51 +00:00
|
|
|
dbgmsg( "it's not already open. looking for an open slot or an old file." );
|
2007-12-03 15:27:38 +00:00
|
|
|
while( winner < 0 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-11-01 13:47:32 +00:00
|
|
|
uint64_t date = tr_date( ) + 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-12-03 15:27:38 +00:00
|
|
|
/* look for the file that's been open longest */
|
2009-06-21 07:36:51 +00:00
|
|
|
for( i=0; i<gFd->openFileLimit; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-01-08 19:20:24 +00:00
|
|
|
o = &gFd->openFiles[i];
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !fileIsOpen( o ) )
|
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
winner = i;
|
|
|
|
dbgmsg( "found an empty slot in %d", winner );
|
2007-12-03 15:27:38 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( date > o->date )
|
|
|
|
{
|
2007-10-30 18:35:06 +00:00
|
|
|
date = o->date;
|
2007-10-31 18:10:55 +00:00
|
|
|
winner = i;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
assert( winner >= 0 );
|
|
|
|
|
|
|
|
if( fileIsOpen( &gFd->openFiles[winner] ) )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
dbgmsg( "closing file \"%s\"", gFd->openFiles[winner].filename );
|
|
|
|
TrCloseFile( &gFd->openFiles[winner] );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-03 15:27:38 +00:00
|
|
|
assert( winner >= 0 );
|
2009-01-08 19:20:24 +00:00
|
|
|
o = &gFd->openFiles[winner];
|
2007-10-30 18:35:06 +00:00
|
|
|
if( !fileIsOpen( o ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
const int err = TrOpenFile( winner, folder, torrentFile, doWrite,
|
|
|
|
preallocationMode, desiredFileSize );
|
2008-10-03 04:49:06 +00:00
|
|
|
if( err ) {
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
2008-11-05 05:56:06 +00:00
|
|
|
dbgmsg( "opened '%s' in slot %d, doWrite %c", filename, winner,
|
|
|
|
doWrite ? 'y' : 'n' );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_strlcpy( o->filename, filename, sizeof( o->filename ) );
|
2008-11-05 05:56:06 +00:00
|
|
|
o->isWritable = doWrite;
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
dbgmsg( "checking out '%s' in slot %d", filename, winner );
|
2009-06-21 07:36:51 +00:00
|
|
|
o->torrentId = torrentId;
|
2007-10-31 18:10:55 +00:00
|
|
|
o->isCheckedOut = 1;
|
2007-11-01 13:47:32 +00:00
|
|
|
o->date = tr_date( );
|
2007-10-31 18:10:55 +00:00
|
|
|
return o->fd;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
2007-11-21 16:16:59 +00:00
|
|
|
tr_fdFileReturn( int fd )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * o;
|
|
|
|
const struct tr_openfile * end;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
2007-11-21 16:16:59 +00:00
|
|
|
{
|
|
|
|
if( o->fd != fd )
|
|
|
|
continue;
|
2009-07-04 04:33:23 +00:00
|
|
|
dbgmsg( "releasing file \"%s\"", o->filename );
|
|
|
|
o->isCheckedOut = FALSE;
|
2007-11-21 16:16:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_fdFileClose( const char * filename )
|
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * o;
|
|
|
|
const struct tr_openfile * end;
|
2007-11-21 16:16:59 +00:00
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
2007-11-21 16:16:59 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !fileIsOpen( o ) || strcmp( filename, o->filename ) )
|
2007-11-21 16:16:59 +00:00
|
|
|
continue;
|
2009-07-04 04:33:23 +00:00
|
|
|
dbgmsg( "tr_fdFileClose closing \"%s\"", filename );
|
2009-06-21 07:36:51 +00:00
|
|
|
assert( !o->isCheckedOut && "this is a test assertion... I *think* this is always true now" );
|
2009-07-04 04:33:23 +00:00
|
|
|
TrCloseFile( o );
|
2009-06-21 07:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_fdTorrentClose( int torrentId )
|
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * o;
|
|
|
|
const struct tr_openfile * end;
|
2009-06-21 07:36:51 +00:00
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
2009-06-21 07:36:51 +00:00
|
|
|
if( fileIsOpen( o ) && o->torrentId == torrentId )
|
2009-07-04 04:33:23 +00:00
|
|
|
TrCloseFile( o );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Sockets
|
|
|
|
****
|
|
|
|
***/
|
2007-01-21 19:42:11 +00:00
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
static TR_INLINE int
|
2008-01-21 02:11:57 +00:00
|
|
|
getSocketMax( struct tr_fd_s * gFd )
|
|
|
|
{
|
2009-01-08 19:20:24 +00:00
|
|
|
return gFd->socketLimit;
|
2008-01-21 02:11:57 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
int
|
2008-12-15 00:17:08 +00:00
|
|
|
tr_fdSocketCreate( int domain, int type )
|
2007-01-21 19:42:11 +00:00
|
|
|
{
|
|
|
|
int s = -1;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-14 20:44:41 +00:00
|
|
|
if( gFd->socketCount < getSocketMax( gFd ) )
|
2008-12-15 00:17:08 +00:00
|
|
|
if( ( s = socket( domain, type, 0 ) ) < 0 )
|
|
|
|
{
|
2008-12-20 08:51:32 +00:00
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
if( sockerrno != EAFNOSUPPORT )
|
|
|
|
#endif
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_err( _( "Couldn't create socket: %s" ),
|
|
|
|
tr_strerror( sockerrno ) );
|
2008-12-15 00:17:08 +00:00
|
|
|
s = -sockerrno;
|
|
|
|
}
|
2007-07-21 17:35:47 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
if( s > -1 )
|
2008-10-14 20:44:41 +00:00
|
|
|
++gFd->socketCount;
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2008-10-14 20:44:41 +00:00
|
|
|
assert( gFd->socketCount >= 0 );
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
return s;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
int
|
2008-12-02 03:41:58 +00:00
|
|
|
tr_fdSocketAccept( int b,
|
|
|
|
tr_address * addr,
|
|
|
|
tr_port * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-06-06 16:19:34 +00:00
|
|
|
int s;
|
|
|
|
unsigned int len;
|
2008-12-02 03:41:58 +00:00
|
|
|
struct sockaddr_storage sock;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( addr );
|
|
|
|
assert( port );
|
2007-09-25 23:10:34 +00:00
|
|
|
|
2009-06-06 16:19:34 +00:00
|
|
|
len = sizeof( struct sockaddr_storage );
|
|
|
|
s = accept( b, (struct sockaddr *) &sock, &len );
|
|
|
|
|
2009-06-08 14:50:56 +00:00
|
|
|
if( ( s >= 0 ) && gFd->socketCount > getSocketMax( gFd ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2009-06-06 16:19:34 +00:00
|
|
|
EVUTIL_CLOSESOCKET( s );
|
|
|
|
s = -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2009-06-06 16:19:34 +00:00
|
|
|
|
|
|
|
if( s >= 0 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2008-12-02 03:41:58 +00:00
|
|
|
/* "The ss_family field of the sockaddr_storage structure will always
|
|
|
|
* align with the family field of any protocol-specific structure." */
|
|
|
|
if( sock.ss_family == AF_INET )
|
2009-06-15 05:16:36 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in *si;
|
|
|
|
union { struct sockaddr_storage dummy; struct sockaddr_in si; } s;
|
|
|
|
s.dummy = sock;
|
|
|
|
si = &s.si;
|
2008-12-02 03:41:58 +00:00
|
|
|
addr->type = TR_AF_INET;
|
2009-06-15 05:16:36 +00:00
|
|
|
addr->addr.addr4.s_addr = si->sin_addr.s_addr;
|
|
|
|
*port = si->sin_port;
|
2008-12-02 03:41:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-15 05:16:36 +00:00
|
|
|
struct sockaddr_in6 *si;
|
|
|
|
union { struct sockaddr_storage dummy; struct sockaddr_in6 si; } s;
|
|
|
|
s.dummy = sock;
|
|
|
|
si = &s.si;
|
2008-12-02 03:41:58 +00:00
|
|
|
addr->type = TR_AF_INET6;
|
2009-06-15 05:16:36 +00:00
|
|
|
addr->addr.addr6 = si->sin6_addr;
|
|
|
|
*port = si->sin6_port;
|
2008-12-02 03:41:58 +00:00
|
|
|
}
|
2008-10-14 20:44:41 +00:00
|
|
|
++gFd->socketCount;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 19:42:11 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
2009-07-04 04:33:23 +00:00
|
|
|
tr_fdSocketClose( int fd )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
if( fd >= 0 )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
EVUTIL_CLOSESOCKET( fd );
|
2008-10-14 20:44:41 +00:00
|
|
|
--gFd->socketCount;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2008-10-14 20:44:41 +00:00
|
|
|
assert( gFd->socketCount >= 0 );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Startup / Shutdown
|
|
|
|
****
|
|
|
|
***/
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
2009-01-08 19:20:24 +00:00
|
|
|
tr_fdInit( size_t openFileLimit, size_t socketLimit )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2008-01-03 16:36:20 +00:00
|
|
|
int i;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
assert( gFd == NULL );
|
|
|
|
gFd = tr_new0( struct tr_fd_s, 1 );
|
2009-01-08 19:20:24 +00:00
|
|
|
gFd->openFiles = tr_new0( struct tr_openfile, openFileLimit );
|
|
|
|
gFd->openFileLimit = openFileLimit;
|
2008-11-23 17:05:44 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_GETRLIMIT
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
getrlimit( RLIMIT_NOFILE, &rlim );
|
|
|
|
rlim.rlim_cur = MIN( rlim.rlim_max,
|
2009-01-08 19:20:24 +00:00
|
|
|
(rlim_t)( socketLimit + NOFILE_BUFFER ) );
|
2008-11-23 17:05:44 +00:00
|
|
|
setrlimit( RLIMIT_NOFILE, &rlim );
|
2009-01-08 19:20:24 +00:00
|
|
|
gFd->socketLimit = rlim.rlim_cur - NOFILE_BUFFER;
|
2008-11-23 17:05:44 +00:00
|
|
|
tr_dbg( "setrlimit( RLIMIT_NOFILE, %d )", (int)rlim.rlim_cur );
|
|
|
|
}
|
|
|
|
#else
|
2009-01-08 19:20:24 +00:00
|
|
|
gFd->socketLimit = socketLimit;
|
2008-11-23 17:05:44 +00:00
|
|
|
#endif
|
2009-01-08 19:20:24 +00:00
|
|
|
tr_dbg( "%zu usable file descriptors", socketLimit );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2009-01-08 19:20:24 +00:00
|
|
|
for( i = 0; i < gFd->openFileLimit; ++i )
|
|
|
|
gFd->openFiles[i].fd = -1;
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
|
|
|
tr_fdClose( void )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2009-07-04 04:33:23 +00:00
|
|
|
struct tr_openfile * o;
|
|
|
|
const struct tr_openfile * end;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2009-07-04 04:33:23 +00:00
|
|
|
for( o=gFd->openFiles, end=o+gFd->openFileLimit; o!=end; ++o )
|
|
|
|
if( fileIsOpen( o ) )
|
|
|
|
TrCloseFile( o );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
2009-01-18 15:15:10 +00:00
|
|
|
tr_free( gFd->openFiles );
|
2007-10-30 18:35:06 +00:00
|
|
|
tr_free( gFd );
|
2008-10-19 17:43:04 +00:00
|
|
|
gFd = NULL;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
2007-12-20 21:44:16 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_fdSetPeerLimit( uint16_t n )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
assert( gFd != NULL && "tr_fdInit() must be called first!" );
|
2009-01-08 19:20:24 +00:00
|
|
|
gFd->socketLimit = n;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
tr_fdGetPeerLimit( void )
|
|
|
|
{
|
2009-01-08 19:20:24 +00:00
|
|
|
return gFd ? gFd->socketLimit : -1;
|
2007-12-20 21:44:16 +00:00
|
|
|
}
|