2010-05-17 16:41:59 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2010-05-17 16:41:59 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2010-05-17 16:41:59 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
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 <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
|
|
|
|
2010-02-02 03:01:25 +00:00
|
|
|
#ifdef HAVE_FALLOCATE64
|
|
|
|
/* FIXME can't find the right #include voodoo to pick up the declaration.. */
|
|
|
|
extern int fallocate64( int fd, int mode, uint64_t offset, uint64_t len );
|
|
|
|
#endif
|
|
|
|
|
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>
|
2011-07-11 20:48:06 +00:00
|
|
|
#include <sys/time.h> /* getrlimit */
|
|
|
|
#include <sys/resource.h> /* getrlimit */
|
2009-01-25 23:47:07 +00:00
|
|
|
#include <fcntl.h> /* O_LARGEFILE posix_fadvise */
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <unistd.h> /* lseek(), write(), ftruncate(), pread(), pwrite(), etc */
|
2009-01-25 23:40:08 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2008-08-11 19:05:02 +00:00
|
|
|
#include "fdlimit.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "net.h"
|
2009-10-23 03:41:36 +00:00
|
|
|
#include "session.h"
|
|
|
|
#include "torrent.h" /* tr_isTorrent() */
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** 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
|
|
|
|
|
2011-01-07 00:17:13 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef O_SEQUENTIAL
|
|
|
|
#define O_SEQUENTIAL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2010-12-28 08:38:55 +00:00
|
|
|
preallocate_file_sparse( int fd, uint64_t length )
|
2009-01-16 16:38:16 +00:00
|
|
|
{
|
|
|
|
const char zero = '\0';
|
2011-03-22 15:19:54 +00:00
|
|
|
bool success = 0;
|
2010-03-09 16:19:59 +00:00
|
|
|
|
|
|
|
if( !length )
|
2011-03-22 15:19:54 +00:00
|
|
|
success = true;
|
2009-01-16 16:38:16 +00:00
|
|
|
|
2010-03-09 16:19:59 +00:00
|
|
|
#ifdef HAVE_FALLOCATE64
|
|
|
|
if( !success ) /* fallocate64 is always preferred, so try it first */
|
|
|
|
success = !fallocate64( fd, 0, 0, length );
|
|
|
|
#endif
|
2009-01-16 16:38:16 +00:00
|
|
|
|
2010-03-09 16:19:59 +00:00
|
|
|
if( !success ) /* fallback: the old-style seek-and-write */
|
|
|
|
success = ( lseek( fd, length-1, SEEK_SET ) != -1 )
|
|
|
|
&& ( write( fd, &zero, 1 ) != -1 )
|
|
|
|
&& ( ftruncate( fd, length ) != -1 );
|
2009-01-16 16:38:16 +00:00
|
|
|
|
2010-03-09 16:19:59 +00:00
|
|
|
return success;
|
2009-01-16 16:38:16 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2010-12-28 08:38:55 +00:00
|
|
|
preallocate_file_full( const char * filename, uint64_t length )
|
2008-11-05 05:56:06 +00:00
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool success = 0;
|
2008-11-15 19:59:18 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2008-11-05 05:56:06 +00:00
|
|
|
|
2010-06-21 16:44:35 +00:00
|
|
|
HANDLE hFile = CreateFile( filename, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0 );
|
2008-11-15 19:59:18 +00:00
|
|
|
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 )
|
|
|
|
{
|
2010-02-02 03:01:25 +00:00
|
|
|
# ifdef HAVE_FALLOCATE64
|
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
success = !fallocate64( fd, 0, 0, length );
|
|
|
|
}
|
|
|
|
# endif
|
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 */
|
|
|
|
{
|
2009-08-10 20:04:08 +00:00
|
|
|
uint8_t buf[ 4096 ];
|
|
|
|
memset( buf, 0, sizeof( buf ) );
|
2011-03-22 15:19:54 +00:00
|
|
|
success = true;
|
2009-08-10 20:04:08 +00:00
|
|
|
while ( success && ( length > 0 ) )
|
|
|
|
{
|
|
|
|
const int thisPass = MIN( length, sizeof( buf ) );
|
|
|
|
success = write( fd, buf, thisPass ) == thisPass;
|
|
|
|
length -= thisPass;
|
|
|
|
}
|
2009-06-16 17:10:47 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-02-02 23:19:54 +00:00
|
|
|
|
|
|
|
/* portability wrapper for fsync(). */
|
|
|
|
int
|
|
|
|
tr_fsync( int fd )
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
return _commit( fd );
|
|
|
|
#else
|
|
|
|
return fsync( fd );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-08 23:43:38 +00:00
|
|
|
/* Like pread and pwrite, except that the position is undefined afterwards.
|
|
|
|
And of course they are not thread-safe. */
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
/* don't use pread/pwrite on old versions of uClibc because they're buggy.
|
|
|
|
* https://trac.transmissionbt.com/ticket/3826 */
|
2010-12-14 05:07:06 +00:00
|
|
|
#ifdef __UCLIBC__
|
2010-12-14 14:41:23 +00:00
|
|
|
#define TR_UCLIBC_CHECK_VERSION(major,minor,micro) \
|
|
|
|
(__UCLIBC_MAJOR__ > (major) || \
|
|
|
|
(__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ > (minor)) || \
|
|
|
|
(__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ == (minor) && \
|
|
|
|
__UCLIBC_SUBLEVEL__ >= (micro)))
|
|
|
|
#if !TR_UCLIBC_CHECK_VERSION(0,9,28)
|
2010-12-14 05:07:06 +00:00
|
|
|
#undef HAVE_PREAD
|
|
|
|
#undef HAVE_PWRITE
|
|
|
|
#endif
|
2010-12-14 14:41:23 +00:00
|
|
|
#endif
|
2010-12-14 05:07:06 +00:00
|
|
|
|
2009-11-08 23:43:38 +00:00
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
#define HAVE_PREAD
|
|
|
|
#define HAVE_PWRITE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
tr_pread( int fd, void *buf, size_t count, off_t offset )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_PREAD
|
|
|
|
return pread( fd, buf, count, offset );
|
|
|
|
#else
|
|
|
|
const off_t lrc = lseek( fd, offset, SEEK_SET );
|
|
|
|
if( lrc < 0 )
|
|
|
|
return -1;
|
|
|
|
return read( fd, buf, count );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2010-05-22 00:09:36 +00:00
|
|
|
tr_pwrite( int fd, const void *buf, size_t count, off_t offset )
|
2009-11-08 23:43:38 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_PWRITE
|
|
|
|
return pwrite( fd, buf, count, offset );
|
|
|
|
#else
|
|
|
|
const off_t lrc = lseek( fd, offset, SEEK_SET );
|
|
|
|
if( lrc < 0 )
|
|
|
|
return -1;
|
|
|
|
return write( fd, buf, count );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-10-11 21:44:46 +00:00
|
|
|
tr_prefetch( int fd UNUSED, off_t offset UNUSED, size_t count UNUSED )
|
2009-11-08 23:43:38 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
return posix_fadvise( fd, offset, count, POSIX_FADV_WILLNEED );
|
|
|
|
#elif defined(SYS_DARWIN)
|
|
|
|
struct radvisory radv;
|
|
|
|
radv.ra_offset = offset;
|
|
|
|
radv.ra_count = count;
|
|
|
|
return fcntl( fd, F_RDADVISE, &radv );
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-12-17 15:36:31 +00:00
|
|
|
void
|
|
|
|
tr_set_file_for_single_pass( int fd )
|
|
|
|
{
|
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
#ifdef HAVE_POSIX_FADVISE
|
|
|
|
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
|
|
|
|
#endif
|
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
fcntl( fd, F_RDAHEAD, 1 );
|
|
|
|
fcntl( fd, F_NOCACHE, 1 );
|
|
|
|
#endif
|
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 00:17:13 +00:00
|
|
|
static int
|
|
|
|
open_local_file( const char * filename, int flags )
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2011-01-07 00:17:13 +00:00
|
|
|
const int fd = open( filename, flags, 0666 );
|
2010-12-17 15:36:31 +00:00
|
|
|
tr_set_file_for_single_pass( fd );
|
|
|
|
return fd;
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
2011-01-07 00:17:13 +00:00
|
|
|
int
|
|
|
|
tr_open_file_for_writing( const char * filename )
|
|
|
|
{
|
|
|
|
return open_local_file( filename, O_LARGEFILE|O_BINARY|O_CREAT|O_WRONLY );
|
|
|
|
}
|
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
|
|
|
{
|
2011-01-07 00:17:13 +00:00
|
|
|
return open_local_file( filename, O_LARGEFILE|O_BINARY|O_SEQUENTIAL|O_RDONLY );
|
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;
|
2010-06-21 13:08:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
/* it's unclear to me from the man pages if this actually flushes out the cache,
|
|
|
|
* but it couldn't hurt... */
|
|
|
|
fcntl( fd, F_NOCACHE, 1 );
|
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
|
|
|
}
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
/*****
|
|
|
|
******
|
|
|
|
******
|
|
|
|
******
|
|
|
|
*****/
|
|
|
|
|
|
|
|
struct tr_cached_file
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool is_writable;
|
2010-12-28 07:24:10 +00:00
|
|
|
int fd;
|
|
|
|
int torrent_id;
|
|
|
|
tr_file_index_t file_index;
|
|
|
|
time_t used_at;
|
|
|
|
};
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static inline bool
|
2010-12-28 07:24:10 +00:00
|
|
|
cached_file_is_open( const struct tr_cached_file * o )
|
|
|
|
{
|
|
|
|
assert( o != NULL );
|
|
|
|
|
|
|
|
return o->fd >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cached_file_close( struct tr_cached_file * o )
|
|
|
|
{
|
|
|
|
assert( cached_file_is_open( o ) );
|
|
|
|
|
|
|
|
tr_close_file( o->fd );
|
|
|
|
o->fd = -1;
|
|
|
|
}
|
|
|
|
|
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
|
2010-12-28 07:24:10 +00:00
|
|
|
cached_file_open( struct tr_cached_file * o,
|
|
|
|
const char * filename,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool writable,
|
2010-12-28 07:24:10 +00:00
|
|
|
tr_preallocation_mode allocation,
|
|
|
|
uint64_t file_size )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
int flags;
|
|
|
|
struct stat sb;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool alreadyExisted;
|
2008-01-18 19:13:32 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
/* create subfolders, if any */
|
2010-12-28 07:24:10 +00:00
|
|
|
if( writable )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-10-21 19:33:37 +00:00
|
|
|
char * dir = tr_dirname( filename );
|
|
|
|
const int err = tr_mkdirp( dir, 0777 ) ? errno : 0;
|
2008-10-14 04:51:42 +00:00
|
|
|
if( err ) {
|
2009-10-21 19:33:37 +00:00
|
|
|
tr_err( _( "Couldn't create \"%1$s\": %2$s" ), dir, tr_strerror( err ) );
|
|
|
|
tr_free( dir );
|
2008-10-03 04:49:06 +00:00
|
|
|
return err;
|
2008-10-14 04:51:42 +00:00
|
|
|
}
|
2009-10-21 19:33:37 +00:00
|
|
|
tr_free( dir );
|
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
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( writable && !alreadyExisted && ( allocation == TR_PREALLOCATE_FULL ) )
|
2010-12-28 08:38:55 +00:00
|
|
|
if( preallocate_file_full( filename, file_size ) )
|
2010-12-28 07:24:10 +00:00
|
|
|
tr_dbg( "Preallocated file \"%s\"", filename );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2007-10-31 18:10:55 +00:00
|
|
|
/* open the file */
|
2010-12-28 07:24:10 +00:00
|
|
|
flags = writable ? ( O_RDWR | O_CREAT ) : O_RDONLY;
|
2011-01-07 00:17:13 +00:00
|
|
|
flags |= O_LARGEFILE | O_BINARY | O_SEQUENTIAL;
|
2010-12-28 07:24:10 +00:00
|
|
|
o->fd = open( filename, flags, 0666 );
|
|
|
|
|
|
|
|
if( o->fd == -1 )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-01-18 19:13:32 +00:00
|
|
|
const int err = errno;
|
2011-01-06 01:05:13 +00:00
|
|
|
tr_err( _( "Couldn't open \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
|
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
|
|
|
|
*/
|
2010-12-28 07:24:10 +00:00
|
|
|
if( alreadyExisted && ( file_size < (uint64_t)sb.st_size ) )
|
2011-02-09 05:34:23 +00:00
|
|
|
{
|
|
|
|
if( ftruncate( o->fd, file_size ) == -1 )
|
|
|
|
{
|
|
|
|
const int err = errno;
|
|
|
|
tr_err( _( "Couldn't truncate \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2009-06-21 07:53:51 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( writable && !alreadyExisted && ( allocation == TR_PREALLOCATE_SPARSE ) )
|
2010-12-28 08:38:55 +00:00
|
|
|
preallocate_file_sparse( o->fd, file_size );
|
2009-01-16 16:38:16 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
/* Many (most?) clients request blocks in ascending order,
|
|
|
|
* so increase the readahead buffer.
|
|
|
|
* Also, disable OS-level caching because "inactive memory" angers users. */
|
|
|
|
tr_set_file_for_single_pass( o->fd );
|
2010-06-27 17:44:34 +00:00
|
|
|
|
2008-10-03 04:49:06 +00:00
|
|
|
return 0;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct tr_fileset
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2010-12-28 07:24:10 +00:00
|
|
|
struct tr_cached_file * begin;
|
2010-12-28 08:38:55 +00:00
|
|
|
const struct tr_cached_file * end;
|
2010-12-28 07:24:10 +00:00
|
|
|
};
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
static void
|
2010-12-28 07:24:10 +00:00
|
|
|
fileset_construct( struct tr_fileset * set, int n )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2010-12-28 07:24:10 +00:00
|
|
|
struct tr_cached_file * o;
|
|
|
|
const struct tr_cached_file TR_CACHED_FILE_INIT = { 0, -1, 0, 0, 0 };
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
set->begin = tr_new( struct tr_cached_file, n );
|
|
|
|
set->end = set->begin + n;
|
|
|
|
|
|
|
|
for( o=set->begin; o!=set->end; ++o )
|
|
|
|
*o = TR_CACHED_FILE_INIT;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
static void
|
|
|
|
fileset_close_all( struct tr_fileset * set )
|
2009-10-19 05:05:00 +00:00
|
|
|
{
|
2010-12-28 07:24:10 +00:00
|
|
|
struct tr_cached_file * o;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( set != NULL )
|
|
|
|
for( o=set->begin; o!=set->end; ++o )
|
|
|
|
if( cached_file_is_open( o ) )
|
|
|
|
cached_file_close( o );
|
|
|
|
}
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
static void
|
|
|
|
fileset_destruct( struct tr_fileset * set )
|
|
|
|
{
|
|
|
|
fileset_close_all( set );
|
|
|
|
tr_free( set->begin );
|
2010-12-28 08:38:55 +00:00
|
|
|
set->end = set->begin = NULL;
|
2010-12-28 07:24:10 +00:00
|
|
|
}
|
2009-10-23 03:41:36 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
static void
|
|
|
|
fileset_close_torrent( struct tr_fileset * set, int torrent_id )
|
|
|
|
{
|
|
|
|
struct tr_cached_file * o;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( set != NULL )
|
|
|
|
for( o=set->begin; o!=set->end; ++o )
|
|
|
|
if( ( o->torrent_id == torrent_id ) && cached_file_is_open( o ) )
|
|
|
|
cached_file_close( o );
|
|
|
|
}
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
static struct tr_cached_file *
|
|
|
|
fileset_lookup( struct tr_fileset * set, int torrent_id, tr_file_index_t i )
|
|
|
|
{
|
|
|
|
struct tr_cached_file * o;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( set != NULL )
|
|
|
|
for( o=set->begin; o!=set->end; ++o )
|
|
|
|
if( ( torrent_id == o->torrent_id ) && ( i == o->file_index ) && cached_file_is_open( o ) )
|
|
|
|
return o;
|
2009-10-19 05:05:00 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
return NULL;
|
2009-10-19 05:05:00 +00:00
|
|
|
}
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
static struct tr_cached_file *
|
|
|
|
fileset_get_empty_slot( struct tr_fileset * set )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2011-12-14 05:42:15 +00:00
|
|
|
struct tr_cached_file * cull = NULL;
|
|
|
|
|
|
|
|
if( set->begin != NULL )
|
|
|
|
{
|
|
|
|
struct tr_cached_file * o;
|
|
|
|
|
|
|
|
/* try to find an unused slot */
|
|
|
|
for( o=set->begin; o!=set->end; ++o )
|
|
|
|
if( !cached_file_is_open( o ) )
|
|
|
|
return o;
|
|
|
|
|
|
|
|
/* all slots are full... recycle the least recently used */
|
|
|
|
for( cull=NULL, o=set->begin; o!=set->end; ++o )
|
|
|
|
if( !cull || o->used_at < cull->used_at )
|
|
|
|
cull = o;
|
|
|
|
|
|
|
|
cached_file_close( cull );
|
|
|
|
}
|
2010-12-28 07:24:10 +00:00
|
|
|
|
|
|
|
return cull;
|
|
|
|
}
|
2009-10-23 03:41:36 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
/***
|
|
|
|
****
|
2011-06-24 22:39:20 +00:00
|
|
|
**** Startup / Shutdown
|
|
|
|
****
|
2010-12-28 07:24:10 +00:00
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
struct tr_fdInfo
|
|
|
|
{
|
2011-06-24 22:39:20 +00:00
|
|
|
int peerCount;
|
2010-12-28 07:24:10 +00:00
|
|
|
struct tr_fileset fileset;
|
|
|
|
};
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2011-06-24 22:39:20 +00:00
|
|
|
static void
|
|
|
|
ensureSessionFdInfoExists( tr_session * session )
|
|
|
|
{
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
if( session->fdInfo == NULL )
|
|
|
|
{
|
2011-07-11 20:48:06 +00:00
|
|
|
struct rlimit limit;
|
|
|
|
struct tr_fdInfo * i;
|
|
|
|
const int FILE_CACHE_SIZE = 32;
|
|
|
|
|
|
|
|
/* Create the local file cache */
|
|
|
|
i = tr_new0( struct tr_fdInfo, 1 );
|
|
|
|
fileset_construct( &i->fileset, FILE_CACHE_SIZE );
|
2011-06-24 22:39:20 +00:00
|
|
|
session->fdInfo = i;
|
2011-07-11 20:48:06 +00:00
|
|
|
|
|
|
|
/* set the open-file limit to the largest safe size wrt FD_SETSIZE */
|
|
|
|
if( !getrlimit( RLIMIT_NOFILE, &limit ) )
|
|
|
|
{
|
|
|
|
const int old_limit = (int) limit.rlim_cur;
|
|
|
|
const int new_limit = MIN( limit.rlim_max, FD_SETSIZE );
|
|
|
|
if( new_limit != old_limit )
|
|
|
|
{
|
|
|
|
limit.rlim_cur = new_limit;
|
|
|
|
setrlimit( RLIMIT_NOFILE, &limit );
|
|
|
|
getrlimit( RLIMIT_NOFILE, &limit );
|
|
|
|
tr_inf( "Changed open file limit from %d to %d", old_limit, (int)limit.rlim_cur );
|
|
|
|
}
|
|
|
|
}
|
2011-06-24 22:39:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_fdClose( tr_session * session )
|
|
|
|
{
|
|
|
|
if( session && session->fdInfo )
|
|
|
|
{
|
|
|
|
struct tr_fdInfo * i = session->fdInfo;
|
|
|
|
fileset_destruct( &i->fileset );
|
|
|
|
tr_free( i );
|
|
|
|
session->fdInfo = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
static struct tr_fileset*
|
|
|
|
get_fileset( tr_session * session )
|
|
|
|
{
|
2011-06-24 22:39:20 +00:00
|
|
|
if( !session )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ensureSessionFdInfoExists( session );
|
|
|
|
return &session->fdInfo->fileset;
|
2010-12-28 07:24:10 +00:00
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
void
|
|
|
|
tr_fdFileClose( tr_session * s, const tr_torrent * tor, tr_file_index_t i )
|
|
|
|
{
|
|
|
|
struct tr_cached_file * o;
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if(( o = fileset_lookup( get_fileset( s ), tr_torrentId( tor ), i )))
|
2011-02-02 23:29:17 +00:00
|
|
|
{
|
|
|
|
/* flush writable files so that their mtimes will be
|
|
|
|
* up-to-date when this function returns to the caller... */
|
|
|
|
if( o->is_writable )
|
|
|
|
tr_fsync( o->fd );
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
cached_file_close( o );
|
2011-02-02 23:29:17 +00:00
|
|
|
}
|
2010-12-28 07:24:10 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
int
|
2011-03-22 15:19:54 +00:00
|
|
|
tr_fdFileGetCached( tr_session * s, int torrent_id, tr_file_index_t i, bool writable )
|
2010-12-28 07:24:10 +00:00
|
|
|
{
|
|
|
|
struct tr_cached_file * o = fileset_lookup( get_fileset( s ), torrent_id, i );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( !o || ( writable && !o->is_writable ) )
|
|
|
|
return -1;
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
o->used_at = tr_time( );
|
|
|
|
return o->fd;
|
|
|
|
}
|
2007-10-30 18:35:06 +00:00
|
|
|
|
2011-04-28 18:40:46 +00:00
|
|
|
#ifdef SYS_DARWIN
|
|
|
|
#define TR_STAT_MTIME(sb) ((sb).st_mtimespec.tv_sec)
|
|
|
|
#else
|
|
|
|
#define TR_STAT_MTIME(sb) ((sb).st_mtime)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool
|
|
|
|
tr_fdFileGetCachedMTime( tr_session * s, int torrent_id, tr_file_index_t i, time_t * mtime )
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
struct stat sb;
|
|
|
|
struct tr_cached_file * o = fileset_lookup( get_fileset( s ), torrent_id, i );
|
|
|
|
|
|
|
|
if(( success = ( o != NULL ) && !fstat( o->fd, &sb )))
|
|
|
|
*mtime = TR_STAT_MTIME( sb );
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
void
|
|
|
|
tr_fdTorrentClose( tr_session * session, int torrent_id )
|
|
|
|
{
|
|
|
|
fileset_close_torrent( get_fileset( session ), torrent_id );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
/* returns an fd on success, or a -1 on failure and sets errno */
|
|
|
|
int
|
|
|
|
tr_fdFileCheckout( tr_session * session,
|
|
|
|
int torrent_id,
|
|
|
|
tr_file_index_t i,
|
|
|
|
const char * filename,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool writable,
|
2010-12-28 08:38:55 +00:00
|
|
|
tr_preallocation_mode allocation,
|
2010-12-28 07:24:10 +00:00
|
|
|
uint64_t file_size )
|
|
|
|
{
|
|
|
|
struct tr_fileset * set = get_fileset( session );
|
|
|
|
struct tr_cached_file * o = fileset_lookup( set, torrent_id, i );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( o && writable && !o->is_writable )
|
|
|
|
cached_file_close( o ); /* close it so we can reopen in rw mode */
|
|
|
|
else if( !o )
|
|
|
|
o = fileset_get_empty_slot( set );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( !cached_file_is_open( o ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2011-07-25 17:48:14 +00:00
|
|
|
const int err = cached_file_open( o, filename, writable, allocation, file_size );
|
2008-10-03 04:49:06 +00:00
|
|
|
if( err ) {
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
2007-10-30 18:35:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
dbgmsg( "opened '%s' writable %c", filename, writable?'y':'n' );
|
|
|
|
o->is_writable = writable;
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
dbgmsg( "checking out '%s'", filename );
|
|
|
|
o->torrent_id = torrent_id;
|
|
|
|
o->file_index = i;
|
|
|
|
o->used_at = tr_time( );
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Sockets
|
|
|
|
****
|
|
|
|
***/
|
2007-01-21 19:42:11 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
int
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSocketCreate( tr_session * session, int domain, int type )
|
2007-01-21 19:42:11 +00:00
|
|
|
{
|
|
|
|
int s = -1;
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
|
|
|
assert( tr_isSession( session ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2011-06-24 22:39:20 +00:00
|
|
|
ensureSessionFdInfoExists( session );
|
2009-10-23 03:41:36 +00:00
|
|
|
gFd = session->fdInfo;
|
|
|
|
|
2011-06-24 22:39:20 +00:00
|
|
|
if( gFd->peerCount < session->peerLimit )
|
2010-12-28 07:24:10 +00:00
|
|
|
if(( s = socket( domain, type, 0 )) < 0 )
|
2008-12-20 08:51:32 +00:00
|
|
|
if( sockerrno != EAFNOSUPPORT )
|
2010-12-28 07:24:10 +00:00
|
|
|
tr_err( _( "Couldn't create socket: %s" ), tr_strerror( sockerrno ) );
|
2007-07-21 17:35:47 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
if( s > -1 )
|
2011-06-24 22:39:20 +00:00
|
|
|
++gFd->peerCount;
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2011-06-24 22:39:20 +00:00
|
|
|
assert( gFd->peerCount >= 0 );
|
2009-11-29 08:53:14 +00:00
|
|
|
|
|
|
|
if( s >= 0 )
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool buf_logged = false;
|
2009-11-29 08:53:14 +00:00
|
|
|
if( !buf_logged )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
socklen_t size = sizeof( int );
|
2011-03-22 15:19:54 +00:00
|
|
|
buf_logged = true;
|
2009-11-29 08:53:14 +00:00
|
|
|
getsockopt( s, SOL_SOCKET, SO_SNDBUF, &i, &size );
|
2010-02-08 04:43:56 +00:00
|
|
|
tr_dbg( "SO_SNDBUF size is %d", i );
|
2009-11-29 08:53:14 +00:00
|
|
|
getsockopt( s, SOL_SOCKET, SO_RCVBUF, &i, &size );
|
2010-02-08 04:43:56 +00:00
|
|
|
tr_dbg( "SO_RCVBUF size is %d", i );
|
2009-11-29 08:53:14 +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
|
2010-12-28 07:24:10 +00:00
|
|
|
tr_fdSocketAccept( tr_session * s, int sockfd, tr_address * addr, tr_port * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2010-12-28 07:24:10 +00:00
|
|
|
int fd;
|
2009-06-06 16:19:34 +00:00
|
|
|
unsigned int len;
|
2009-10-23 03:41:36 +00:00
|
|
|
struct tr_fdInfo * gFd;
|
2008-12-02 03:41:58 +00:00
|
|
|
struct sockaddr_storage sock;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
assert( tr_isSession( s ) );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( addr );
|
|
|
|
assert( port );
|
2007-09-25 23:10:34 +00:00
|
|
|
|
2011-06-24 22:39:20 +00:00
|
|
|
ensureSessionFdInfoExists( s );
|
2010-12-28 07:24:10 +00:00
|
|
|
gFd = s->fdInfo;
|
2009-10-23 03:41:36 +00:00
|
|
|
|
2009-06-06 16:19:34 +00:00
|
|
|
len = sizeof( struct sockaddr_storage );
|
2010-12-28 07:24:10 +00:00
|
|
|
fd = accept( sockfd, (struct sockaddr *) &sock, &len );
|
2009-06-06 16:19:34 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
if( fd >= 0 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2011-06-24 22:39:20 +00:00
|
|
|
if( ( gFd->peerCount < s->peerLimit )
|
2011-03-25 05:34:26 +00:00
|
|
|
&& tr_address_from_sockaddr_storage( addr, port, &sock ) )
|
2011-03-05 16:29:19 +00:00
|
|
|
{
|
2011-06-24 22:39:20 +00:00
|
|
|
++gFd->peerCount;
|
2011-03-05 16:29:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_netCloseSocket( fd );
|
|
|
|
fd = -1;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 19:42:11 +00:00
|
|
|
|
2010-12-28 07:24:10 +00:00
|
|
|
return fd;
|
2007-01-21 19:42:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-30 18:35:06 +00:00
|
|
|
void
|
2009-10-23 03:41:36 +00:00
|
|
|
tr_fdSocketClose( tr_session * session, int fd )
|
2007-10-30 18:35:06 +00:00
|
|
|
{
|
2009-10-23 03:41:36 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
2009-12-26 23:52:59 +00:00
|
|
|
if( session->fdInfo != NULL )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-12-26 23:52:59 +00:00
|
|
|
struct tr_fdInfo * gFd = session->fdInfo;
|
2007-10-31 18:10:55 +00:00
|
|
|
|
2009-12-26 23:52:59 +00:00
|
|
|
if( fd >= 0 )
|
|
|
|
{
|
|
|
|
tr_netCloseSocket( fd );
|
2011-06-24 22:39:20 +00:00
|
|
|
--gFd->peerCount;
|
2009-12-26 23:52:59 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 22:39:20 +00:00
|
|
|
assert( gFd->peerCount >= 0 );
|
2009-12-26 23:52:59 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|