From dd75a769c96024fc5fd716e0c50d7339863ad8f6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 18 Jan 2008 01:40:41 +0000 Subject: [PATCH] fix tr_mkdirp()'s errno/retvals. --- libtransmission/utils.c | 15 ++++++++------- libtransmission/utils.h | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/libtransmission/utils.c b/libtransmission/utils.c index eaa85d4e5..136057b7f 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -439,12 +439,12 @@ tr_mkdirp( const char * path_in, int permissions ) if( stat( path, &sb ) ) { /* Folder doesn't exist yet */ - if( tr_mkdir( path, permissions ) ) - { - tr_err( "Could not create directory %s (%s)", path, - strerror( errno ) ); + if( tr_mkdir( path, permissions ) ) { + const int err = errno; + tr_err( "Couldn't create directory %s (%s)", path, strerror( err ) ); tr_free( path ); - return 1; + errno = err; + return -1; } } else if( ( sb.st_mode & S_IFMT ) != S_IFDIR ) @@ -452,7 +452,8 @@ tr_mkdirp( const char * path_in, int permissions ) /* Node exists but isn't a folder */ tr_err( "Remove %s, it's in the way.", path ); tr_free( path ); - return 1; + errno = ENOTDIR; + return -1; } if( done ) @@ -507,7 +508,7 @@ tr_ioErrorFromErrno( void ) } } -char * +const char * tr_errorString( int code ) { switch( code ) diff --git a/libtransmission/utils.h b/libtransmission/utils.h index d717c9352..133cc8fe8 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -40,11 +40,26 @@ FILE* tr_getLog( void ); char* tr_getLogTimeStr( char * buf, int buflen ); -int tr_rand ( int ); +/** Returns a random number in the range of [0...n) */ +int tr_rand ( int n ); +/** + * a portability wrapper around mkdir(). + * On WIN32, the `permissions' argument is unused. + * + * @return zero on success, or -1 if an error occurred + * (in which case errno is set appropriately). + */ +int tr_mkdir( const char * path, int permissions ); + +/** + * Like mkdir, but makes parent directories as needed. + * + * @return zero on success, or -1 if an error occurred + * (in which case errno is set appropriately). + */ int tr_mkdirp( const char * path, int permissions ); -int tr_mkdir( const char * path, int permissions ); uint8_t* tr_loadFile( const char * filename, size_t * size ); @@ -63,7 +78,7 @@ struct timeval timevalMsec( uint64_t milliseconds ); int tr_ioErrorFromErrno( void ); -char * tr_errorString( int code ); +const char * tr_errorString( int code ); /* return the current date in milliseconds */ uint64_t tr_date( void );