Author: dmacvicar Date: Mon Apr 7 12:17:58 2008 New Revision: 9415 URL: http://svn.opensuse.org/viewcvs/zypp?rev=9415&view=rev Log: - use Fd class instead of a plain integer so I can remove all duplicated code to close the file on exceptions, by getting automatic out of scoping. Modified: trunk/libzypp/zypp/base/InterProcessMutex.cc trunk/libzypp/zypp/base/InterProcessMutex.h Modified: trunk/libzypp/zypp/base/InterProcessMutex.cc URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/InterProcessMutex.cc?rev=9415&r1=9414&r2=9415&view=diff ============================================================================== --- trunk/libzypp/zypp/base/InterProcessMutex.cc (original) +++ trunk/libzypp/zypp/base/InterProcessMutex.cc Mon Apr 7 12:17:58 2008 @@ -40,8 +40,7 @@ InterProcessMutex::InterProcessMutex( ConsumerType ctype, const std::string &name, int timeout ) - : _fd(0) - , _name(name) + : _name(name) , _timeout(timeout) , _type(ctype) { @@ -58,12 +57,14 @@ // try to create the lock file atomically, this will fail if // the lock exists - if ( ( _fd = open(lock_file.c_str(), O_RDWR | O_CREAT | O_EXCL) ) == -1 ) + _fd.reset( new Fd( lock_file, O_RDWR | O_CREAT | O_EXCL) ); + if ( !_fd->isOpen() ) { struct flock lock; // the file exists, lets see if someone has it locked exclusively - if ( (_fd = open(lock_file.c_str(), O_RDWR)) == -1 ) + _fd.reset( new Fd( lock_file, O_RDWR ) ); + if ( !_fd->isOpen() ) { ZYPP_THROW(Exception(str::form(_("It %d, Can't open lock file: %s"), k, strerror(errno)))); } @@ -78,9 +79,8 @@ // get lock information - if (fcntl(_fd, F_GETLK, &lock) < 0) + if (fcntl(_fd->fd(), F_GETLK, &lock) < 0) { - close(_fd); ZYPP_THROW(Exception(string("Error getting lock info: ") + strerror(errno))); } @@ -125,7 +125,6 @@ // timeout and therefore we never abort. if ( (totalslept >= _timeout) && (_timeout >= 0 ) ) { - close(_fd); ZYPP_THROW(ZYppLockedException( _("This action is being run by another program already."), _name, lock.l_pid)); @@ -133,7 +132,6 @@ // if not, let sleep one second and count it LMIL << "waiting 1 second..." << endl; - close(_fd); sleep(1); ++totalslept; continue; @@ -146,9 +144,8 @@ // try to get more lock info lock.l_type = F_WRLCK; - if (fcntl(_fd, F_GETLK, &lock) < 0) + if (fcntl(_fd->fd(), F_GETLK, &lock) < 0) { - close(_fd); ZYPP_THROW(Exception(string("Error getting lock info: ") + strerror(errno))); } @@ -167,13 +164,11 @@ lock.l_whence = SEEK_SET; lock.l_pid = getpid(); - if (fcntl(_fd, F_SETLK, &lock) < 0) + if (fcntl(_fd->fd(), F_SETLK, &lock) < 0) { - close(_fd); ZYPP_THROW (Exception( "Can't lock file to unlink it.")); } filesystem::unlink(lock_file.c_str()); - close(_fd); continue; } else if ( lock.l_type == F_RDLCK ) @@ -185,9 +180,8 @@ lock.l_whence = SEEK_SET; lock.l_pid = getpid(); - if (fcntl(_fd, F_SETLK, &lock) < 0) + if (fcntl(_fd->fd(), F_SETLK, &lock) < 0) { - close(_fd); ZYPP_THROW (Exception( "Can't lock file for reader")); } // and keep the lock open. @@ -216,13 +210,11 @@ lock.l_whence = SEEK_SET; lock.l_pid = getpid(); - if (fcntl(_fd, F_SETLK, &lock) < 0) + if (fcntl(_fd->fd(), F_SETLK, &lock) < 0) { - close(_fd); ZYPP_THROW (Exception( "Can't lock file to unlink it.")); } filesystem::unlink(lock_file.c_str()); - close(_fd); continue; } else @@ -249,15 +241,12 @@ lock.l_type = F_WRLCK; lock.l_pid = getpid(); - if (fcntl(_fd, F_SETLK, &lock) < 0) - { - close(_fd); + if (fcntl(_fd->fd(), F_SETLK, &lock) < 0) ZYPP_THROW (Exception( "Can't lock file to write pid.")); - } char buffer[100]; sprintf( buffer, "%d\n", curr_pid); - write( _fd, buffer, strlen(buffer)); + write( _fd->fd(), buffer, strlen(buffer)); // by now the pid is written and the file locked. // If we are a reader, just downgrade the lock to @@ -266,11 +255,8 @@ { lock.l_type = F_RDLCK; - if (fcntl(_fd, F_SETLK, &lock) < 0) - { - close(_fd); + if (fcntl(_fd->fd(), F_SETLK, &lock) < 0) ZYPP_THROW (Exception( "Can't set lock file to shared")); - } } break; @@ -303,7 +289,7 @@ } // and finally close the file and release the lock - close(_fd); + // (happens automatically) } catch(...) {} // let no exception escape. } Modified: trunk/libzypp/zypp/base/InterProcessMutex.h URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/InterProcessMutex.h?rev=9415&r1=9414&r2=9415&view=diff ============================================================================== --- trunk/libzypp/zypp/base/InterProcessMutex.h (original) +++ trunk/libzypp/zypp/base/InterProcessMutex.h Mon Apr 7 12:17:58 2008 @@ -3,6 +3,7 @@ #define ZYPP_BASE_INTER_PROCESS_MUTEX_H #include <string> +#include "zypp/base/Fd.h" #include "zypp/base/Exception.h" #include "zypp/Pathname.h" @@ -81,7 +82,7 @@ bool isProcessRunning(pid_t pid_r); Pathname lockFilePath() const; private: - int _fd; + shared_ptr<Fd> _fd; std::string _name; int _timeout; ConsumerType _type; -- To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org For additional commands, e-mail: zypp-commit+help@opensuse.org