return success;
}
-FILE*
+int
tr_open_file_for_scanning( const char * filename )
{
int fd;
/* 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 );
}
/**
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;
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 );
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 */
}
/* 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 ) );
{
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;
}
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;
}
/* 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 );
}
/* 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 );
}
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