mirror of
https://github.com/transmission/transmission
synced 2025-03-09 21:54:09 +00:00
(trunk libT) convert tr_open_file_for_scanning() and tr_close_file() to use file descriptors instead of file pointers so that we don't have to mix and match between them
This commit is contained in:
parent
75520acd38
commit
698aa192aa
4 changed files with 31 additions and 37 deletions
|
@ -201,7 +201,7 @@ preallocateFileFull( const char * filename, uint64_t length )
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE*
|
int
|
||||||
tr_open_file_for_scanning( const char * filename )
|
tr_open_file_for_scanning( const char * filename )
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -224,33 +224,27 @@ tr_open_file_for_scanning( const char * filename )
|
||||||
|
|
||||||
/* open the file */
|
/* open the file */
|
||||||
fd = open( filename, flags, 0666 );
|
fd = open( filename, flags, 0666 );
|
||||||
if( fd < 0 )
|
if( fd >= 0 )
|
||||||
return NULL;
|
{
|
||||||
|
|
||||||
#ifdef HAVE_POSIX_FADVISE
|
#ifdef HAVE_POSIX_FADVISE
|
||||||
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
|
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
|
||||||
#endif
|
#endif
|
||||||
#ifdef SYS_DARWIN
|
#ifdef SYS_DARWIN
|
||||||
fcntl( fd, F_NOCACHE, 1 );
|
fcntl( fd, F_NOCACHE, 1 );
|
||||||
fcntl( fd, F_RDAHEAD, 1 );
|
fcntl( fd, F_RDAHEAD, 1 );
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
return fdopen( fd, "r" );
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void
|
||||||
flush_before_closing( int fd )
|
tr_close_file( int fd )
|
||||||
{
|
{
|
||||||
#if defined(HAVE_POSIX_FADVISE)
|
#if defined(HAVE_POSIX_FADVISE)
|
||||||
posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
|
posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
|
||||||
#endif
|
#endif
|
||||||
}
|
close( fd );
|
||||||
|
|
||||||
void
|
|
||||||
tr_close_file( FILE * fp )
|
|
||||||
{
|
|
||||||
flush_before_closing( fileno( fp ) );
|
|
||||||
fclose( fp );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -346,7 +340,7 @@ TrCloseFile( int i )
|
||||||
assert( i < gFd->openFileLimit );
|
assert( i < gFd->openFileLimit );
|
||||||
assert( fileIsOpen( o ) );
|
assert( fileIsOpen( o ) );
|
||||||
|
|
||||||
flush_before_closing( o->fd );
|
tr_close_file( o->fd );
|
||||||
close( o->fd );
|
close( o->fd );
|
||||||
o->fd = -1;
|
o->fd = -1;
|
||||||
o->isCheckedOut = 0;
|
o->isCheckedOut = 0;
|
||||||
|
|
|
@ -32,9 +32,9 @@
|
||||||
void tr_fdInit( size_t openFileLimit,
|
void tr_fdInit( size_t openFileLimit,
|
||||||
size_t globalPeerLimit );
|
size_t globalPeerLimit );
|
||||||
|
|
||||||
FILE* tr_open_file_for_scanning( const char * filename );
|
int tr_open_file_for_scanning( const char * filename );
|
||||||
|
|
||||||
void tr_close_file( FILE * fp );
|
void tr_close_file( int fd );
|
||||||
|
|
||||||
int64_t tr_lseek( int fd, int64_t offset, int whence );
|
int64_t tr_lseek( int fd, int64_t offset, int whence );
|
||||||
|
|
||||||
|
|
|
@ -466,9 +466,9 @@ uint8_t *
|
||||||
tr_loadFile( const char * path,
|
tr_loadFile( const char * path,
|
||||||
size_t * size )
|
size_t * size )
|
||||||
{
|
{
|
||||||
uint8_t * buf;
|
uint8_t * buf;
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
FILE * file;
|
int fd;
|
||||||
const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
|
const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
|
||||||
|
|
||||||
/* try to stat the file */
|
/* try to stat the file */
|
||||||
|
@ -489,8 +489,8 @@ tr_loadFile( const char * path,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Load the torrent file into our buffer */
|
/* Load the torrent file into our buffer */
|
||||||
file = tr_open_file_for_scanning( path );
|
fd = tr_open_file_for_scanning( path );
|
||||||
if( !file )
|
if( fd < 0 )
|
||||||
{
|
{
|
||||||
const int err = errno;
|
const int err = errno;
|
||||||
tr_err( err_fmt, path, tr_strerror( errno ) );
|
tr_err( err_fmt, path, tr_strerror( errno ) );
|
||||||
|
@ -502,21 +502,21 @@ tr_loadFile( const char * path,
|
||||||
{
|
{
|
||||||
const int err = errno;
|
const int err = errno;
|
||||||
tr_err( err_fmt, path, _( "Memory allocation failed" ) );
|
tr_err( err_fmt, path, _( "Memory allocation failed" ) );
|
||||||
tr_close_file( file );
|
tr_close_file( fd );
|
||||||
errno = err;
|
errno = err;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if( fread( buf, sb.st_size, 1, file ) != 1 )
|
if( read( fd, buf, sb.st_size ) != sb.st_size )
|
||||||
{
|
{
|
||||||
const int err = errno;
|
const int err = errno;
|
||||||
tr_err( err_fmt, path, tr_strerror( errno ) );
|
tr_err( err_fmt, path, tr_strerror( errno ) );
|
||||||
tr_close_file( file );
|
tr_close_file( fd );
|
||||||
free( buf );
|
free( buf );
|
||||||
errno = err;
|
errno = err;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
tr_close_file( file );
|
tr_close_file( fd );
|
||||||
*size = sb.st_size;
|
*size = sb.st_size;
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ static tr_bool
|
||||||
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
{
|
{
|
||||||
SHA_CTX sha;
|
SHA_CTX sha;
|
||||||
FILE * fp = NULL;
|
int fd = -1;
|
||||||
int64_t filePos = 0;
|
int64_t filePos = 0;
|
||||||
tr_bool changed = 0;
|
tr_bool changed = 0;
|
||||||
tr_bool hadPiece = 0;
|
tr_bool hadPiece = 0;
|
||||||
|
@ -72,11 +72,11 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if we're starting a new file... */
|
/* if we're starting a new file... */
|
||||||
if( !filePos && !fp )
|
if( !filePos && (fd<0) )
|
||||||
{
|
{
|
||||||
char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
|
char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
|
||||||
fp = tr_open_file_for_scanning( filename );
|
fd = tr_open_file_for_scanning( filename );
|
||||||
/* fprintf( stderr, "opening file #%d (%s) -- %p\n", fileIndex, filename, fp ); */
|
/* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
|
||||||
tr_free( filename );
|
tr_free( filename );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,8 +88,8 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
/* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
|
/* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
|
||||||
|
|
||||||
/* read a bit */
|
/* read a bit */
|
||||||
if( fp && tr_lseek( fileno(fp), filePos, SEEK_SET ) != -1 ) {
|
if( (fd>=0) && tr_lseek( fd, filePos, SEEK_SET ) != -1 ) {
|
||||||
const int64_t numRead = fread( buffer, 1, bytesThisPass, fp );
|
const int64_t numRead = read( fd, buffer, bytesThisPass );
|
||||||
if( numRead == bytesThisPass )
|
if( numRead == bytesThisPass )
|
||||||
SHA1_Update( &sha, buffer, numRead );
|
SHA1_Update( &sha, buffer, numRead );
|
||||||
}
|
}
|
||||||
|
@ -129,15 +129,15 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
|
||||||
if( leftInFile == 0 )
|
if( leftInFile == 0 )
|
||||||
{
|
{
|
||||||
/* fprintf( stderr, "closing file\n" ); */
|
/* fprintf( stderr, "closing file\n" ); */
|
||||||
if( fp != NULL ) { tr_close_file( fp ); fp = NULL; }
|
if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
|
||||||
++fileIndex;
|
++fileIndex;
|
||||||
filePos = 0;
|
filePos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* cleanup */
|
/* cleanup */
|
||||||
if( fp != NULL )
|
if( fd >= 0 )
|
||||||
tr_close_file( fp );
|
tr_close_file( fd );
|
||||||
tr_free( buffer );
|
tr_free( buffer );
|
||||||
|
|
||||||
#ifdef STOPWATCH
|
#ifdef STOPWATCH
|
||||||
|
|
Loading…
Add table
Reference in a new issue