[Box Backup-commit] COMMIT r1948 - box/chris/general/infrastructure

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sat, 8 Dec 2007 21:45:06 +0000 (UTC)


Author: chris
Date: 2007-12-08 21:45:06 +0000 (Sat, 08 Dec 2007)
New Revision: 1948

Modified:
   box/chris/general/infrastructure/buildenv-testmain-template.cpp
Log:
Log exactly which file descriptors have been left open and what they are.
(merges [1903], [1916])


Modified: box/chris/general/infrastructure/buildenv-testmain-template.cpp
===================================================================
--- box/chris/general/infrastructure/buildenv-testmain-template.cpp	2007-12-08 21:41:38 UTC (rev 1947)
+++ box/chris/general/infrastructure/buildenv-testmain-template.cpp	2007-12-08 21:45:06 UTC (rev 1948)
@@ -64,26 +64,78 @@
 #ifdef WIN32
 
 // any way to check for open file descriptors on Win32?
-inline int  count_filedes()      { return 0;     }
-inline bool checkfilesleftopen() { return false; }
+inline bool check_filedes(bool x) { return 0;     }
+inline bool checkfilesleftopen()  { return false; }
 
 #else // !WIN32
 
-int count_filedes()
+#define FILEDES_MAX 256
+
+bool filedes_open[FILEDES_MAX];
+
+bool check_filedes(bool report)
 {
-	int c = 0;
+	bool allOk = true;
 
 	// See how many file descriptors there are with values < 256
-	for(int d = 0; d < 256; ++d)
+	for(int d = 0; d < FILEDES_MAX; ++d)
 	{
 		if(::fcntl(d, F_GETFD) != -1)
 		{
 			// File descriptor obviously exists
-			++c;
+			if (report && !filedes_open[d])
+			{
+				struct stat st;
+				if (fstat(d, &st) == 0)
+				{
+					int m = st.st_mode;
+					#define flag(x) ((m & x) ? #x " " : "")
+					BOX_FATAL("File descriptor " << d << 
+						" left open (type == " <<
+						flag(S_IFIFO) <<
+						flag(S_IFCHR) <<
+						flag(S_IFDIR) <<
+						flag(S_IFBLK) <<
+						flag(S_IFREG) <<
+						flag(S_IFLNK) <<
+						flag(S_IFSOCK) <<
+						flag(S_IFWHT) << " plus " <<							m << ")");
+				}
+				else
+				{
+					BOX_FATAL("File descriptor " << d << 
+						" left open (and stat failed)");
+				}
+	
+				allOk = false;
+				
+			}
+			else if (!report)
+			{
+				filedes_open[d] = true;
+			}
 		}
+		else 
+		{
+			if (report && filedes_open[d])
+			{
+				BOX_FATAL("File descriptor " << d << 
+					" was open, now closed");
+				allOk = false;
+			}
+			else
+			{
+				filedes_open[d] = false;
+			}
+		}
 	}
+
+	if (!report && allOk)
+	{
+		filedes_open_at_beginning = 0;
+	}
 	
-	return c;
+	return !allOk;
 }
 
 bool checkfilesleftopen()
@@ -92,6 +144,7 @@
 	{
 		// Not used correctly, pretend that there were things 
 		// left open so this gets investigated
+		BOX_FATAL("File descriptor test was not initialised");
 		return true;
 	}
 
@@ -99,7 +152,7 @@
 	::closelog();
 
 	// Count the file descriptors open
-	return filedes_open_at_beginning != count_filedes();
+	return check_filedes(true);
 }
 
 #endif
@@ -181,7 +234,7 @@
 	if(fulltestmode)
 	{
 		// Count open file descriptors for a very crude "files left open" test
-		filedes_open_at_beginning = count_filedes();
+		check_filedes(false);
 
 		// banner
 		printf("Running test TEST_NAME in " MODE_TEXT " mode...\n");