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

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sun, 7 Sep 2008 14:20:16 +0100 (BST)


Author: chris
Date: 2008-09-07 14:20:14 +0100 (Sun, 07 Sep 2008)
New Revision: 2275

Modified:
   box/trunk/lib/common/FileStream.cpp
   box/trunk/lib/common/FileStream.h
Log:
Fix test regression on Windows where a const char pointer was treated
as a file handle instead of as a string.


Modified: box/trunk/lib/common/FileStream.cpp
===================================================================
--- box/trunk/lib/common/FileStream.cpp	2008-09-06 10:58:49 UTC (rev 2274)
+++ box/trunk/lib/common/FileStream.cpp	2008-09-07 13:20:14 UTC (rev 2275)
@@ -33,7 +33,33 @@
 	  mIsEOF(false),
 	  mFileName(rFilename)
 {
+	AfterOpen();
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    FileStream::FileStream(const char *, int, int)
+//		Purpose: Alternative constructor, takes a const char *,
+//			 avoids const strings being interpreted as handles!
+//		Created: 2003/07/31
+//
+// --------------------------------------------------------------------------
+FileStream::FileStream(const char *pFilename, int flags, int mode)
 #ifdef WIN32
+	: mOSFileHandle(::openfile(pFilename, flags, mode)),
+#else
+	: mOSFileHandle(::open(pFilename, flags, mode)),
+#endif
+	  mIsEOF(false),
+	  mFileName(pFilename)
+{
+	AfterOpen();
+}
+
+void FileStream::AfterOpen()
+{
+#ifdef WIN32
 	if(mOSFileHandle == INVALID_HANDLE_VALUE)
 #else
 	if(mOSFileHandle < 0)
@@ -48,7 +74,7 @@
 		else
 		{
 			BOX_LOG_SYS_WARNING("Failed to open file: " <<
-				rFilename);
+				mFileName);
 			THROW_EXCEPTION(CommonException, OSFileOpenError)
 		}
 	}

Modified: box/trunk/lib/common/FileStream.h
===================================================================
--- box/trunk/lib/common/FileStream.h	2008-09-06 10:58:49 UTC (rev 2274)
+++ box/trunk/lib/common/FileStream.h	2008-09-07 13:20:14 UTC (rev 2275)
@@ -38,6 +38,18 @@
 		int flags = O_RDONLY,
 #endif
 		int mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
+
+	// Ensure that const char * name doesn't end up as a handle
+	// on Windows!
+
+	FileStream(const char *pFilename, 
+#ifdef WIN32
+		int flags = (O_RDONLY | O_BINARY),
+#else
+		int flags = O_RDONLY,
+#endif
+		int mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
+
 	FileStream(tOSFileHandle FileDescriptor);
 	
 	virtual ~FileStream();
@@ -56,6 +68,7 @@
 	tOSFileHandle mOSFileHandle;
 	bool mIsEOF;
 	FileStream(const FileStream &rToCopy) { /* do not call */ }
+	void AfterOpen();
 
 	// for debugging..
 	std::string mFileName;