(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:
Charles Kerr 2009-04-26 20:44:18 +00:00
parent 75520acd38
commit 698aa192aa
4 changed files with 31 additions and 37 deletions

View File

@ -201,7 +201,7 @@ preallocateFileFull( const char * filename, uint64_t length )
return success;
}
FILE*
int
tr_open_file_for_scanning( const char * filename )
{
int fd;
@ -224,33 +224,27 @@ tr_open_file_for_scanning( const char * filename )
/* open the file */
fd = open( filename, flags, 0666 );
if( fd < 0 )
return NULL;
if( fd >= 0 )
{
#ifdef HAVE_POSIX_FADVISE
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
posix_fadvise( fd, 0, 0, POSIX_FADV_SEQUENTIAL );
#endif
#ifdef SYS_DARWIN
fcntl( fd, F_NOCACHE, 1 );
fcntl( fd, F_RDAHEAD, 1 );
fcntl( fd, F_NOCACHE, 1 );
fcntl( fd, F_RDAHEAD, 1 );
#endif
}
return fdopen( fd, "r" );
return fd;
}
static void
flush_before_closing( int fd )
void
tr_close_file( int fd )
{
#if defined(HAVE_POSIX_FADVISE)
posix_fadvise( fd, 0, 0, POSIX_FADV_DONTNEED );
#endif
}
void
tr_close_file( FILE * fp )
{
flush_before_closing( fileno( fp ) );
fclose( fp );
close( fd );
}
/**
@ -346,7 +340,7 @@ TrCloseFile( int i )
assert( i < gFd->openFileLimit );
assert( fileIsOpen( o ) );
flush_before_closing( o->fd );
tr_close_file( o->fd );
close( o->fd );
o->fd = -1;
o->isCheckedOut = 0;

View File

@ -32,9 +32,9 @@
void tr_fdInit( size_t openFileLimit,
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 );

View File

@ -466,9 +466,9 @@ uint8_t *
tr_loadFile( const char * path,
size_t * size )
{
uint8_t * buf;
uint8_t * buf;
struct stat sb;
FILE * file;
int fd;
const char * err_fmt = _( "Couldn't read \"%1$s\": %2$s" );
/* try to stat the file */
@ -489,8 +489,8 @@ tr_loadFile( const char * path,
}
/* Load the torrent file into our buffer */
file = tr_open_file_for_scanning( path );
if( !file )
fd = tr_open_file_for_scanning( path );
if( fd < 0 )
{
const int err = errno;
tr_err( err_fmt, path, tr_strerror( errno ) );
@ -502,21 +502,21 @@ tr_loadFile( const char * path,
{
const int err = errno;
tr_err( err_fmt, path, _( "Memory allocation failed" ) );
tr_close_file( file );
tr_close_file( fd );
errno = err;
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;
tr_err( err_fmt, path, tr_strerror( errno ) );
tr_close_file( file );
tr_close_file( fd );
free( buf );
errno = err;
return NULL;
}
tr_close_file( file );
tr_close_file( fd );
*size = sb.st_size;
return buf;
}

View File

@ -42,7 +42,7 @@ static tr_bool
verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
{
SHA_CTX sha;
FILE * fp = NULL;
int fd = -1;
int64_t filePos = 0;
tr_bool changed = 0;
tr_bool hadPiece = 0;
@ -72,11 +72,11 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
}
/* if we're starting a new file... */
if( !filePos && !fp )
if( !filePos && (fd<0) )
{
char * filename = tr_buildPath( tor->downloadDir, file->name, NULL );
fp = tr_open_file_for_scanning( filename );
/* fprintf( stderr, "opening file #%d (%s) -- %p\n", fileIndex, filename, fp ); */
fd = tr_open_file_for_scanning( filename );
/* fprintf( stderr, "opening file #%d (%s) -- %d\n", fileIndex, filename, fd ); */
tr_free( filename );
}
@ -88,8 +88,8 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
/* fprintf( stderr, "reading this pass: %d\n", (int)bytesThisPass ); */
/* read a bit */
if( fp && tr_lseek( fileno(fp), filePos, SEEK_SET ) != -1 ) {
const int64_t numRead = fread( buffer, 1, bytesThisPass, fp );
if( (fd>=0) && tr_lseek( fd, filePos, SEEK_SET ) != -1 ) {
const int64_t numRead = read( fd, buffer, bytesThisPass );
if( numRead == bytesThisPass )
SHA1_Update( &sha, buffer, numRead );
}
@ -129,15 +129,15 @@ verifyTorrent( tr_torrent * tor, tr_bool * stopFlag )
if( leftInFile == 0 )
{
/* fprintf( stderr, "closing file\n" ); */
if( fp != NULL ) { tr_close_file( fp ); fp = NULL; }
if( fd >= 0 ) { tr_close_file( fd ); fd = -1; }
++fileIndex;
filePos = 0;
}
}
/* cleanup */
if( fp != NULL )
tr_close_file( fp );
if( fd >= 0 )
tr_close_file( fd );
tr_free( buffer );
#ifdef STOPWATCH