[Box Backup-commit] COMMIT r3576 - box/trunk/lib/common

subversion at boxbackup.org subversion at boxbackup.org
Wed Jun 10 20:46:09 BST 2015


Author: chris
Date: 2015-06-10 19:46:08 +0000 (Wed, 10 Jun 2015)
New Revision: 3576

Modified:
   box/trunk/lib/common/NamedLock.cpp
Log:
Fix logging of NamedLock acquisition for O_EXLOCK method.

Modified: box/trunk/lib/common/NamedLock.cpp
===================================================================
--- box/trunk/lib/common/NamedLock.cpp	2015-06-10 19:46:04 UTC (rev 3575)
+++ box/trunk/lib/common/NamedLock.cpp	2015-06-10 19:46:08 UTC (rev 3576)
@@ -77,99 +77,98 @@
 	mFileName = rFilename;
 
 	// See if the lock can be got
+	int flags = O_WRONLY | O_CREAT | O_TRUNC;
+
 #if HAVE_DECL_O_EXLOCK
+	flags |= O_NONBLOCK | O_EXLOCK;
 	BOX_TRACE("Trying to create lockfile " << rFilename << " using O_EXLOCK");
-	int fd = ::open(rFilename.c_str(),
-		O_WRONLY | O_NONBLOCK | O_CREAT | O_TRUNC | O_EXLOCK, mode);
-	if(fd != -1)
-	{
-		// Got a lock, lovely
-		mFileDescriptor = fd;
-		return true;
-	}
-	
-	// Failed. Why?
-	if(errno != EWOULDBLOCK)
-	{
-		// Not the expected error
-		THROW_SYS_FILE_ERROR("Failed to open lockfile", rFilename,
-			CommonException, OSFileError);
-	}
-
-	return false;
-#else
-	int flags = O_WRONLY | O_CREAT | O_TRUNC;
-
-# if !HAVE_DECL_F_SETLK && !defined HAVE_FLOCK
+#elif !HAVE_DECL_F_SETLK && !defined HAVE_FLOCK
 	// We have no other way to get a lock, so all we can do is fail if
 	// the file already exists, and take the risk of stale locks.
 	flags |= O_EXCL;
 	BOX_TRACE("Trying to create lockfile " << rFilename << " using O_EXCL");
-# else
+#else
 	BOX_TRACE("Trying to create lockfile " << rFilename << " without special flags");
-# endif
+#endif
 
 	int fd = ::open(rFilename.c_str(), flags, mode);
 	if(fd == -1)
-	{
-		if(errno == EEXIST && (flags & O_EXCL))
+#if HAVE_DECL_O_EXLOCK
+	{ // if()
+		if(errno == EWOULDBLOCK)
 		{
 			// Lockfile already exists, and we tried to open it
 			// exclusively, which means we failed to lock it.
-			BOX_NOTICE("Failed to lock lockfile with O_EXCL: " << rFilename
+			BOX_NOTICE("Failed to lock lockfile with O_EXLOCK: " << rFilename
 				<< ": already locked by another process?");
 			return false;
 		}
 		else
 		{
-			THROW_SYS_FILE_ERROR("Failed to open lockfile",
+			THROW_SYS_FILE_ERROR("Failed to open lockfile with O_EXLOCK",
 				rFilename, CommonException, OSFileError);
 		}
 	}
-
-	try
-	{
-#ifdef HAVE_FLOCK
-	BOX_TRACE("Trying to lock lockfile " << rFilename << " using flock()");
-	if(::flock(fd, LOCK_EX | LOCK_NB) != 0)
-	{
-		if(errno == EWOULDBLOCK)
+#else // !HAVE_DECL_O_EXLOCK
+	{ // if()
+		if(errno == EEXIST && (flags & O_EXCL))
 		{
-			::close(fd);
-			BOX_NOTICE("Failed to lock lockfile with flock(): " << rFilename
-				<< ": already locked by another process");
+			// Lockfile already exists, and we tried to open it
+			// exclusively, which means we failed to lock it.
+			BOX_NOTICE("Failed to lock lockfile with O_EXCL: " << rFilename
+				<< ": already locked by another process?");
 			return false;
 		}
 		else
 		{
-			THROW_SYS_FILE_ERROR("Failed to lock lockfile with flock()",
+			THROW_SYS_FILE_ERROR("Failed to open lockfile with O_EXCL",
 				rFilename, CommonException, OSFileError);
 		}
 	}
-#elif HAVE_DECL_F_SETLK
-	struct flock desc;
-	desc.l_type = F_WRLCK;
-	desc.l_whence = SEEK_SET;
-	desc.l_start = 0;
-	desc.l_len = 0;
-	BOX_TRACE("Trying to lock lockfile " << rFilename << " using fcntl()");
-	if(::fcntl(fd, F_SETLK, &desc) != 0)
+
+	try
 	{
-		if(errno == EAGAIN)
+# ifdef HAVE_FLOCK
+		BOX_TRACE("Trying to lock lockfile " << rFilename << " using flock()");
+		if(::flock(fd, LOCK_EX | LOCK_NB) != 0)
 		{
-			::close(fd);
-			BOX_NOTICE("Failed to lock lockfile with fcntl(): " << rFilename
-				<< ": already locked by another process");
-			return false;
+			if(errno == EWOULDBLOCK)
+			{
+				::close(fd);
+				BOX_NOTICE("Failed to lock lockfile with flock(): " << rFilename
+					<< ": already locked by another process");
+				return false;
+			}
+			else
+			{
+				THROW_SYS_FILE_ERROR("Failed to lock lockfile with flock()",
+					rFilename, CommonException, OSFileError);
+			}
 		}
-		else
+# elif HAVE_DECL_F_SETLK
+		struct flock desc;
+		desc.l_type = F_WRLCK;
+		desc.l_whence = SEEK_SET;
+		desc.l_start = 0;
+		desc.l_len = 0;
+		BOX_TRACE("Trying to lock lockfile " << rFilename << " using fcntl()");
+		if(::fcntl(fd, F_SETLK, &desc) != 0)
 		{
-			THROW_SYS_FILE_ERROR("Failed to lock lockfile with fcntl()",
-				rFilename, CommonException, OSFileError);
+			if(errno == EAGAIN)
+			{
+				::close(fd);
+				BOX_NOTICE("Failed to lock lockfile with fcntl(): " << rFilename
+					<< ": already locked by another process");
+				return false;
+			}
+			else
+			{
+				THROW_SYS_FILE_ERROR("Failed to lock lockfile with fcntl()",
+					rFilename, CommonException, OSFileError);
+			}
 		}
+# endif
 	}
-#endif
-	}
 	catch(BoxException &e)
 	{
 		::close(fd);
@@ -176,6 +175,7 @@
 		BOX_NOTICE("Failed to lock lockfile " << rFilename << ": " << e.what());
 		throw;
 	}
+#endif // HAVE_DECL_O_EXLOCK
 
 	if(!FileExists(rFilename))
 	{
@@ -190,7 +190,6 @@
 	BOX_TRACE("Successfully locked lockfile " << rFilename);
 
 	return true;
-#endif
 }
 
 // --------------------------------------------------------------------------




More information about the Boxbackup-commit mailing list