[Box Backup-commit] COMMIT r3546 - in box/trunk/lib: backupstore common win32

subversion at boxbackup.org subversion at boxbackup.org
Mon Apr 6 20:41:22 BST 2015


Author: chris
Date: 2015-04-06 19:41:22 +0000 (Mon, 06 Apr 2015)
New Revision: 3546

Modified:
   box/trunk/lib/backupstore/BackupStoreRefCountDatabase.cpp
   box/trunk/lib/common/Logging.h
   box/trunk/lib/win32/emu.cpp
Log:
Improve error logging in win32 emu library.

Consistently capture the last Windows error code in the global variable
winerrno. Add logging macros that report the Windows error message for
this error code.

Modified: box/trunk/lib/backupstore/BackupStoreRefCountDatabase.cpp
===================================================================
--- box/trunk/lib/backupstore/BackupStoreRefCountDatabase.cpp	2015-04-06 14:38:07 UTC (rev 3545)
+++ box/trunk/lib/backupstore/BackupStoreRefCountDatabase.cpp	2015-04-06 19:41:22 UTC (rev 3546)
@@ -70,14 +70,15 @@
 	#ifdef WIN32
 	if(FileExists(Final_Filename) && unlink(Final_Filename.c_str()) != 0)
 	{
-		THROW_SYS_FILE_ERROR("Failed to delete old permanent refcount "
-			"database file", mFilename, CommonException, OSFileError);
+		THROW_EMU_FILE_ERROR("Failed to delete old permanent refcount "
+			"database file", mFilename, CommonException,
+			OSFileError);
 	}
 	#endif
 
 	if(rename(mFilename.c_str(), Final_Filename.c_str()) != 0)
 	{
-		THROW_SYS_ERROR("Failed to rename temporary refcount database "
+		THROW_EMU_ERROR("Failed to rename temporary refcount database "
 			"file from " << mFilename << " to " <<
 			Final_Filename, CommonException, OSFileError);
 	}
@@ -106,8 +107,9 @@
 
 	if(unlink(mFilename.c_str()) != 0)
 	{
-		THROW_SYS_FILE_ERROR("Failed to delete temporary refcount database "
-			"file ", mFilename, CommonException, OSFileError);
+		THROW_EMU_FILE_ERROR("Failed to delete temporary refcount "
+			"database file", mFilename, CommonException,
+			OSFileError);
 	}
 
 	mIsModified = false;

Modified: box/trunk/lib/common/Logging.h
===================================================================
--- box/trunk/lib/common/Logging.h	2015-04-06 14:38:07 UTC (rev 3545)
+++ box/trunk/lib/common/Logging.h	2015-04-06 19:41:22 UTC (rev 3546)
@@ -98,6 +98,8 @@
 	#define BOX_WIN_ERRNO_MESSAGE(error_number, stuff) \
 		stuff << ": " << GetErrorMessage(error_number) << \
 		" (" << error_number << ")"
+	#define BOX_NATIVE_ERRNO_MESSAGE(error_number, stuff) \
+		BOX_WIN_ERRNO_MESSAGE(error_number, stuff)
 	#define BOX_LOG_WIN_ERROR(stuff) \
 		BOX_ERROR(BOX_WIN_ERRNO_MESSAGE(GetLastError(), stuff))
 	#define BOX_LOG_WIN_WARNING(stuff) \
@@ -117,11 +119,25 @@
 	#define THROW_WIN_FILE_ERROR(message, filename, exception, subtype) \
 		THROW_WIN_FILE_ERRNO(message, filename, GetLastError(), \
 			exception, subtype)
+	#define EMU_ERRNO winerrno
+	#define THROW_EMU_ERROR(message, exception, subtype) \
+		THROW_EXCEPTION_MESSAGE(exception, subtype, \
+			BOX_NATIVE_ERRNO_MESSAGE(EMU_ERRNO, message))
 #else
+	#define BOX_NATIVE_ERRNO_MESSAGE(error_number, stuff) \
+		BOX_SYS_ERRNO_MESSAGE(error_number, stuff)
 	#define BOX_LOG_NATIVE_ERROR(stuff)   BOX_LOG_SYS_ERROR(stuff)
 	#define BOX_LOG_NATIVE_WARNING(stuff) BOX_LOG_SYS_WARNING(stuff)
+	#define EMU_ERRNO errno
+	#define THROW_EMU_ERROR(message, exception, subtype) \
+		THROW_EXCEPTION_MESSAGE(exception, subtype, \
+			BOX_SYS_ERRNO_MESSAGE(EMU_ERRNO, message))
 #endif
 
+#define THROW_EMU_FILE_ERROR(message, filename, exception, subtype) \
+	THROW_EMU_ERROR(BOX_FILE_MESSAGE(filename, message), \
+		exception, subtype)
+
 #ifdef WIN32
 #	define BOX_LOG_SOCKET_ERROR(_type, _name, _port, stuff) \
 	BOX_LOG_WIN_ERROR_NUMBER(stuff << " (type " << _type << ", name " << \

Modified: box/trunk/lib/win32/emu.cpp
===================================================================
--- box/trunk/lib/win32/emu.cpp	2015-04-06 14:38:07 UTC (rev 3545)
+++ box/trunk/lib/win32/emu.cpp	2015-04-06 19:41:22 UTC (rev 3546)
@@ -32,8 +32,9 @@
 	if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, 
 		&hToken))
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to open process token: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		return false;
 	}
 
@@ -45,8 +46,9 @@
 		SE_BACKUP_NAME, //the name of the privilege
 		&( token_priv.Privileges[0].Luid ))) //result
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to lookup backup privilege: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		CloseHandle(hToken);
 		return false;
 	}
@@ -68,8 +70,9 @@
 		//this function is a little tricky - if we were adjusting
 		//more than one privilege, it could return success but not
 		//adjust them all - in the general case, you need to trap this
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to enable backup privilege: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		CloseHandle(hToken);
 		return false;
 
@@ -238,9 +241,10 @@
 
 	if (len == 0)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_WARNING, 
 			"Failed to convert wide string to narrow: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		errno = EINVAL;
 		return NULL;
 	}
@@ -270,9 +274,10 @@
 
 	if (len == 0)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_WARNING, 
 			"Failed to convert wide string to narrow: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		errno = EACCES;
 		delete [] buffer;
 		return NULL;
@@ -299,9 +304,10 @@
 
 	if (len == 0)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_WARNING, 
 			"Failed to convert wide string to narrow: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		errno = EINVAL;
 		return false;
 	}
@@ -331,9 +337,10 @@
 
 	if (len == 0)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_WARNING, 
 			"Failed to convert wide string to narrow: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		errno = EACCES;
 		delete [] buffer;
 		return false;
@@ -363,10 +370,11 @@
 		true);
 	if (pWide == NULL)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to convert string '%s' from "
 			"current code page %d to wide string: %s",
 			rSource.c_str(), sourceCodePage,
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		return false;
 	}
 
@@ -641,7 +649,7 @@
 
 		::syslog(LOG_WARNING, "Failed to open file '%s': "
 			"%s", pFileName, 
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 
 		return INVALID_HANDLE_VALUE;
 	}
@@ -684,8 +692,9 @@
 	BY_HANDLE_FILE_INFORMATION fi;
 	if (!GetFileInformationByHandle(hdir, &fi))
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_WARNING, "Failed to read file information: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		errno = EACCES;
 		return -1;
 	}
@@ -692,8 +701,9 @@
 
 	if (INVALID_FILE_ATTRIBUTES == fi.dwFileAttributes)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_WARNING, "Failed to get file attributes: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		errno = EACCES;
 		return -1;
 	}
@@ -826,10 +836,10 @@
 
 	if (handle == INVALID_HANDLE_VALUE)
 	{
-		DWORD err = GetLastError();
+		winerrno = GetLastError();
 
-		if (err == ERROR_FILE_NOT_FOUND ||
-			err == ERROR_PATH_NOT_FOUND)
+		if (winerrno == ERROR_FILE_NOT_FOUND ||
+			winerrno == ERROR_PATH_NOT_FOUND)
 		{
 			errno = ENOENT;
 		}
@@ -837,7 +847,7 @@
 		{
 			::syslog(LOG_WARNING, "Failed to open '%s': "
 				"%s", pFileName, 
-				GetErrorMessage(err).c_str());
+				GetErrorMessage(winerrno).c_str());
 			errno = EACCES;
 		}
 
@@ -906,9 +916,10 @@
 	BY_HANDLE_FILE_INFORMATION fi;
 	if (!GetFileInformationByHandle(handle, &fi))
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_WARNING, "Failed to get file information "
 			"for '%s': %s", pName,
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		CloseHandle(handle);
 		errno = EACCES;
 		return -1;
@@ -961,8 +972,9 @@
 
 	if (!SetFileTime(handle, &creationTime, NULL, &modificationTime))
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to set times on '%s': %s", pName,
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		CloseHandle(handle);
 		return 1;
 	}
@@ -1004,8 +1016,9 @@
 	DWORD attribs = GetFileAttributesW(pBuffer);
 	if (attribs == INVALID_FILE_ATTRIBUTES)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to get file attributes of '%s': %s",
-			pName, GetErrorMessage(GetLastError()).c_str());
+			pName, GetErrorMessage(winerrno).c_str());
 		errno = EACCES;
 		free(pBuffer);
 		return -1;
@@ -1022,8 +1035,9 @@
 
 	if (!SetFileAttributesW(pBuffer, attribs))
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to set file attributes of '%s': %s",
-			pName, GetErrorMessage(GetLastError()).c_str());
+			pName, GetErrorMessage(winerrno).c_str());
 		errno = EACCES;
 		free(pBuffer);
 		return -1;
@@ -1308,8 +1322,9 @@
 
 	if (len == 0)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to get the program file name: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		return FALSE;
 	}
 
@@ -1326,8 +1341,9 @@
 			 0, NULL, REG_OPTION_NON_VOLATILE,
 			 KEY_WRITE, NULL, &hk, &dwDisp)) 
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to create the registry key: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		return FALSE;
 	}
 
@@ -1340,8 +1356,9 @@
 			   (LPBYTE)cmd,        // pointer to value data 
 			   len*sizeof(WCHAR))) // data size
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to set the event message file: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1358,8 +1375,9 @@
 			  (LPBYTE) &dwData, // pointer to value data 
 			  sizeof(DWORD)))   // length of value data 
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to set the supported types: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1373,8 +1391,9 @@
 			   (LPBYTE)cmd,           // pointer to value data 
 			   len*sizeof(WCHAR)))    // data size
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to set the category message file: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1386,8 +1405,9 @@
 			  (LPBYTE) &dwNum, // pointer to value data 
 			  sizeof(DWORD)))  // length of value data 
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to set the category count: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1427,8 +1447,9 @@
 	HANDLE newSyslogH = RegisterEventSource(NULL, nameStr.c_str());
 	if (newSyslogH == NULL)
 	{
+		winerrno = GetLastError();
 		::syslog(LOG_ERR, "Failed to register our own event source: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		return;
 	}
 
@@ -1540,8 +1561,8 @@
 		
 	if (result == 0)
 	{
-		DWORD err = GetLastError();
-		if (err == ERROR_LOG_FILE_FULL)
+		winerrno = GetLastError();
+		if (winerrno == ERROR_LOG_FILE_FULL)
 		{
 			if (!sHaveWarnedEventLogFull)
 			{
@@ -1554,7 +1575,7 @@
 		else
 		{
 			printf("Unable to send message to Event Log: %s:\r\n",
-				GetErrorMessage(err).c_str());
+				GetErrorMessage(winerrno).c_str());
 			fflush(stdout);
 		}
 	}
@@ -1588,8 +1609,9 @@
 	if (result != 0) return 0;
 
 	errno = EACCES;
+	winerrno = GetLastError();
 	fprintf(stderr, "Failed to change directory to '%s': %s\n",
-		pDirName, GetErrorMessage(GetLastError()).c_str());
+		pDirName, GetErrorMessage(winerrno).c_str());
 	return -1;
 }
 
@@ -1701,21 +1723,22 @@
 	}
 
 	BOOL result = CreateHardLinkW(pNewBuffer, pOldBuffer, NULL);
-	DWORD err = GetLastError();
+	winerrno = GetLastError();
 	delete [] pOldBuffer;
 	delete [] pNewBuffer;
 
 	if (!result)
 	{
-		if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
+		if (winerrno == ERROR_FILE_NOT_FOUND ||
+			winerrno == ERROR_PATH_NOT_FOUND)
 		{
 			errno = ENOENT;
 		}
-		else if (err == ERROR_SHARING_VIOLATION)
+		else if (winerrno == ERROR_SHARING_VIOLATION)
 		{
 			errno = EBUSY;
 		}
-		else if (err == ERROR_ACCESS_DENIED)
+		else if (winerrno == ERROR_ACCESS_DENIED)
 		{
 			errno = EACCES;
 		}
@@ -1723,7 +1746,7 @@
 		{
 			::syslog(LOG_WARNING, "Failed to hardlink file "
 				"'%s' to '%s': %s", pOldPath, pNewPath,
-				GetErrorMessage(err).c_str());
+				GetErrorMessage(winerrno).c_str());
 			errno = ENOSYS;
 		}
 
@@ -1752,20 +1775,21 @@
 	}
 
 	BOOL result = DeleteFileW(pBuffer);
-	DWORD err = GetLastError();
+	winerrno  = GetLastError();
 	delete [] pBuffer;
 
 	if (!result)
 	{
-		if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
+		if (winerrno == ERROR_FILE_NOT_FOUND ||
+			winerrno == ERROR_PATH_NOT_FOUND)
 		{
 			errno = ENOENT;
 		}
-		else if (err == ERROR_SHARING_VIOLATION)
+		else if (winerrno == ERROR_SHARING_VIOLATION)
 		{
 			errno = EBUSY;
 		}
-		else if (err == ERROR_ACCESS_DENIED)
+		else if (winerrno == ERROR_ACCESS_DENIED)
 		{
 			errno = EACCES;
 		}
@@ -1773,7 +1797,7 @@
 		{
 			::syslog(LOG_WARNING, "Failed to delete file "
 				"'%s': %s", pFileName, 
-				GetErrorMessage(err).c_str());
+				GetErrorMessage(winerrno).c_str());
 			errno = ENOSYS;
 		}
 
@@ -1818,21 +1842,22 @@
 	}
 
 	BOOL result = MoveFileW(pOldBuffer, pNewBuffer);
-	DWORD err = GetLastError();
+	winerrno = GetLastError();
 	delete [] pOldBuffer;
 	delete [] pNewBuffer;
 
 	if (!result)
 	{
-		if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
+		if (winerrno == ERROR_FILE_NOT_FOUND ||
+			winerrno == ERROR_PATH_NOT_FOUND)
 		{
 			errno = ENOENT;
 		}
-		else if (err == ERROR_SHARING_VIOLATION)
+		else if (winerrno == ERROR_SHARING_VIOLATION)
 		{
 			errno = EBUSY;
 		}
-		else if (err == ERROR_ACCESS_DENIED)
+		else if (winerrno == ERROR_ACCESS_DENIED)
 		{
 			errno = EACCES;
 		}
@@ -1840,7 +1865,7 @@
 		{
 			::syslog(LOG_WARNING, "Failed to rename file "
 				"'%s' to '%s': %s", pOldFileName, pNewFileName,
-				GetErrorMessage(err).c_str());
+				GetErrorMessage(winerrno).c_str());
 			errno = ENOSYS;
 		}
 		return -1;
@@ -1855,8 +1880,9 @@
 
 	if (hConsole == INVALID_HANDLE_VALUE)
 	{
+		winerrno = GetLastError();
 		::fprintf(stderr, "Failed to get a handle on standard input: "
-			"%s", GetErrorMessage(GetLastError()).c_str());
+			"%s", GetErrorMessage(winerrno).c_str());
 		return -1;
 	}
 
@@ -1879,8 +1905,9 @@
 			NULL // reserved
 		)) 
 	{
+		winerrno = GetLastError();
 		::fprintf(stderr, "Failed to read from console: %s\n",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		return -1;
 	}
 
@@ -1977,8 +2004,9 @@
 	// Convert the last-write time to local time.
 	if (!SystemTimeToFileTime(&stUTC, pTo))
 	{
+		winerrno = GetLastError();
 		syslog(LOG_ERR, "Failed to convert between time formats: %s",
-			GetErrorMessage(GetLastError()).c_str());
+			GetErrorMessage(winerrno).c_str());
 		return false;
 	}
 




More information about the Boxbackup-commit mailing list