[Box Backup-dev] COMMIT r693 - in box/chris/general: lib/common lib/win32 test/backupdiff test/backupstore

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Thu Jul 27 00:11:45 BST 2006


Author: chris
Date: 2006-07-26 23:11:39 +0000 (Wed, 26 Jul 2006)
New Revision: 693

Modified:
   box/chris/general/lib/common/FileStream.cpp
   box/chris/general/lib/win32/emu.cpp
   box/chris/general/lib/win32/emu.h
   box/chris/general/test/backupdiff/testbackupdiff.cpp
   box/chris/general/test/backupstore/testbackupstore.cpp
Log:
* lib/win32/emu.cpp, lib/win32/emu.h
- Added a real emulated chmod() function, but it only sets the READ_ONLY
  attribute on win32
- Moved ConvertFileTime to/from time_t functions out of line and into emu.cpp
- Changed openfile() to return INVALID_HANDLE_VALUE instead of NULL
  when it fails to open a file, for consistency with Win32 API

* test/backupstore/testbackupstore.cpp
* test/backupdiff/testbackupdiff.cpp
* lib/common/FileStream.cpp
- Deal with openfile() returning INVALID_HANDLE_VALUE


Modified: box/chris/general/lib/common/FileStream.cpp
===================================================================
--- box/chris/general/lib/common/FileStream.cpp	2006-07-26 19:10:10 UTC (rev 692)
+++ box/chris/general/lib/common/FileStream.cpp	2006-07-26 23:11:39 UTC (rev 693)
@@ -30,7 +30,7 @@
 	  mIsEOF(false)
 {
 #ifdef WIN32
-	if(mOSFileHandle == 0)
+	if(mOSFileHandle == INVALID_HANDLE_VALUE)
 #else
 	if(mOSFileHandle < 0)
 #endif

Modified: box/chris/general/lib/win32/emu.cpp
===================================================================
--- box/chris/general/lib/win32/emu.cpp	2006-07-26 19:10:10 UTC (rev 692)
+++ box/chris/general/lib/win32/emu.cpp	2006-07-26 23:11:39 UTC (rev 693)
@@ -497,7 +497,7 @@
 	if (AbsPathWithUnicode.size() == 0)
 	{
 		// error already logged by ConvertPathToAbsoluteUnicode()
-		return NULL;
+		return INVALID_HANDLE_VALUE;
 	}
 	
 	WCHAR* pBuffer = ConvertUtf8ToWideString(AbsPathWithUnicode.c_str());
@@ -506,7 +506,7 @@
 	if (pBuffer == NULL)
 	{
 		// error already logged by ConvertUtf8ToWideString()
-		return NULL;
+		return INVALID_HANDLE_VALUE;
 	}
 
 	// flags could be O_WRONLY | O_CREAT | O_RDONLY
@@ -553,7 +553,7 @@
 	{
 		::syslog(LOG_WARNING, "Failed to open file %s: "
 			"error %i", pFileName, GetLastError());
-		return NULL;
+		return INVALID_HANDLE_VALUE;
 	}
 
 	return hdir;
@@ -641,11 +641,11 @@
 	// all objects get user read (0400)
 	// if it's a directory it gets user execute (0100)
 	// if it's not read-only it gets user write (0200)
-	st->st_mode = 0400;
+	st->st_mode = S_IREAD;
 
 	if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
 	{
-		st->st_mode |= S_IFDIR | 0100;
+		st->st_mode |= S_IFDIR | S_IEXEC;
 	}
 	else
 	{
@@ -654,7 +654,7 @@
 
 	if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
 	{
-		st->st_mode |= 0200;
+		st->st_mode |= S_IWRITE;
 	}
 
 	return 0;
@@ -815,9 +815,11 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    emu_fstat
-//		Purpose: replacement for fstat supply a windows handle
-//		Created: 25th October 2004
+//		Name:    emu_utimes
+//		Purpose: replacement for the POSIX utimes() function,
+//			works with unicode filenames supplied in utf8 format,
+//			sets creation time instead of last access time.
+//		Created: 25th July 2006
 //
 // --------------------------------------------------------------------------
 int emu_utimes(const char * pName, const struct timeval times[])
@@ -859,6 +861,69 @@
 // --------------------------------------------------------------------------
 //
 // Function
+//		Name:    emu_chmod
+//		Purpose: replacement for the POSIX chmod function,
+//			works with unicode filenames supplied in utf8 format
+//		Created: 26th July 2006
+//
+// --------------------------------------------------------------------------
+int emu_chmod(const char * pName, mode_t mode)
+{
+	std::string AbsPathWithUnicode = 
+		ConvertPathToAbsoluteUnicode(pName);
+	
+	if (AbsPathWithUnicode.size() == 0)
+	{
+		// error already logged by ConvertPathToAbsoluteUnicode()
+		return -1;
+	}
+	
+	WCHAR* pBuffer = ConvertUtf8ToWideString(AbsPathWithUnicode.c_str());
+	// We are responsible for freeing pBuffer
+	
+	if (pBuffer == NULL)
+	{
+		// error already logged by ConvertUtf8ToWideString()
+		free(pBuffer);
+		return -1;
+	}
+
+	DWORD attribs = GetFileAttributesW(pBuffer);
+	if (attribs == INVALID_FILE_ATTRIBUTES)
+	{
+		::syslog(LOG_ERR, "Failed to get file attributes of '%s': "
+			"error %d", pName, GetLastError());
+		errno = EACCES;
+		free(pBuffer);
+		return -1;
+	}
+
+	if (mode & S_IWRITE)
+	{
+		attribs &= ~FILE_ATTRIBUTE_READONLY;
+	}
+	else
+	{
+		attribs |= FILE_ATTRIBUTE_READONLY;
+	}
+
+	if (!SetFileAttributesW(pBuffer, attribs))
+	{
+		::syslog(LOG_ERR, "Failed to set file attributes of '%s': "
+			"error %d", pName, GetLastError());
+		errno = EACCES;
+		free(pBuffer);
+		return -1;
+	}
+
+	free(pBuffer);
+	return 0;
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Function
 //		Name:    opendir
 //		Purpose: replacement for unix function, uses win32 findfirst routines
 //		Created: 25th October 2004
@@ -1548,4 +1613,60 @@
 	return bytes;
 }
 
+// need this for conversions
+time_t ConvertFileTimeToTime_t(FILETIME *fileTime)
+{
+	SYSTEMTIME stUTC;
+	struct tm timeinfo;
+
+	// Convert the last-write time to local time.
+	FileTimeToSystemTime(fileTime, &stUTC);
+
+	memset(&timeinfo, 0, sizeof(timeinfo));	
+	timeinfo.tm_sec = stUTC.wSecond;
+	timeinfo.tm_min = stUTC.wMinute;
+	timeinfo.tm_hour = stUTC.wHour;
+	timeinfo.tm_mday = stUTC.wDay;
+	timeinfo.tm_wday = stUTC.wDayOfWeek;
+	timeinfo.tm_mon = stUTC.wMonth - 1;
+	// timeinfo.tm_yday = ...;
+	timeinfo.tm_year = stUTC.wYear - 1900;
+
+	time_t retVal = mktime(&timeinfo) - _timezone;
+	return retVal;
+}
+
+bool ConvertTime_tToFileTime(const time_t from, FILETIME *pTo)
+{
+	time_t adjusted = from + _timezone;
+	struct tm *time_breakdown = gmtime(&adjusted);
+	if (time_breakdown == NULL)
+	{
+		::syslog(LOG_ERR, "Error: failed to convert time format: "
+			"%d is not a valid time\n", from);
+		return false;
+	}
+
+	SYSTEMTIME stUTC;
+	stUTC.wSecond       = time_breakdown->tm_sec;
+	stUTC.wMinute       = time_breakdown->tm_min;
+	stUTC.wHour         = time_breakdown->tm_hour;
+	stUTC.wDay          = time_breakdown->tm_mday;
+	stUTC.wDayOfWeek    = time_breakdown->tm_wday;
+	stUTC.wMonth        = time_breakdown->tm_mon  + 1;
+	stUTC.wYear         = time_breakdown->tm_year + 1900;
+	stUTC.wMilliseconds = 0;
+
+	// Convert the last-write time to local time.
+	if (!SystemTimeToFileTime(&stUTC, pTo))
+	{
+		syslog(LOG_ERR, "Failed to convert between time formats: "
+			"error %d", GetLastError());
+		return false;
+	}
+
+	return true;
+}
+
+
 #endif // WIN32

Modified: box/chris/general/lib/win32/emu.h
===================================================================
--- box/chris/general/lib/win32/emu.h	2006-07-26 19:10:10 UTC (rev 692)
+++ box/chris/general/lib/win32/emu.h	2006-07-26 23:11:39 UTC (rev 693)
@@ -112,25 +112,19 @@
 int   emu_unlink(const char* pFileName);
 char* emu_getcwd(char* pBuffer, int BufSize);
 int   emu_utimes(const char* pName, const struct timeval[]);
+int   emu_chmod (const char* pName, mode_t mode);
 
 #define utimes(buffer, times) emu_utimes(buffer, times)
 
 #ifdef _MSC_VER
-	inline int emu_chmod(const char * Filename, int mode)
-	{
-		// indicate success
-		return 0;
-	}
-
 	#define chmod(file, mode)     emu_chmod(file, mode)
 	#define chdir(directory)      emu_chdir(directory)
 	#define unlink(file)          emu_unlink(file)
 	#define getcwd(buffer, size)  emu_getcwd(buffer, size)
 #else
-	inline int chmod(const char * Filename, int mode)
+	inline int chmod(const char * pName, mode_t mode)
 	{
-		// indicate success
-		return 0;
+		return emu_chmod(pName, mode);
 	}
 
 	inline int chdir(const char* pDirName)
@@ -325,60 +319,9 @@
 int statfs(const char * name, struct statfs * s);
 
 // need this for conversions
-inline time_t ConvertFileTimeToTime_t(FILETIME *fileTime)
-{
-	SYSTEMTIME stUTC;
-	struct tm timeinfo;
+time_t ConvertFileTimeToTime_t(FILETIME *fileTime);
+bool   ConvertTime_tToFileTime(const time_t from, FILETIME *pTo);
 
-	// Convert the last-write time to local time.
-	FileTimeToSystemTime(fileTime, &stUTC);
-
-	memset(&timeinfo, 0, sizeof(timeinfo));	
-	timeinfo.tm_sec = stUTC.wSecond;
-	timeinfo.tm_min = stUTC.wMinute;
-	timeinfo.tm_hour = stUTC.wHour;
-	timeinfo.tm_mday = stUTC.wDay;
-	timeinfo.tm_wday = stUTC.wDayOfWeek;
-	timeinfo.tm_mon = stUTC.wMonth - 1;
-	// timeinfo.tm_yday = ...;
-	timeinfo.tm_year = stUTC.wYear - 1900;
-
-	time_t retVal = mktime(&timeinfo) - _timezone;
-	return retVal;
-}
-
-inline bool ConvertTime_tToFileTime(const time_t from, FILETIME *pTo)
-{
-	time_t adjusted = from + _timezone;
-	struct tm *time_breakdown = gmtime(&adjusted);
-	if (time_breakdown == NULL)
-	{
-		::syslog(LOG_ERR, "Error: failed to convert time format: "
-			"%d is not a valid time\n", from);
-		return false;
-	}
-
-	SYSTEMTIME stUTC;
-	stUTC.wSecond       = time_breakdown->tm_sec;
-	stUTC.wMinute       = time_breakdown->tm_min;
-	stUTC.wHour         = time_breakdown->tm_hour;
-	stUTC.wDay          = time_breakdown->tm_mday;
-	stUTC.wDayOfWeek    = time_breakdown->tm_wday;
-	stUTC.wMonth        = time_breakdown->tm_mon  + 1;
-	stUTC.wYear         = time_breakdown->tm_year + 1900;
-	stUTC.wMilliseconds = 0;
-
-	// Convert the last-write time to local time.
-	if (!SystemTimeToFileTime(&stUTC, pTo))
-	{
-		syslog(LOG_ERR, "Failed to convert between time formats: "
-			"error %d", GetLastError());
-		return false;
-	}
-
-	return true;
-}
-
 #ifdef _MSC_VER
 	#define stat(filename,  struct) emu_stat (filename, struct)
 	#define lstat(filename, struct) emu_stat (filename, struct)

Modified: box/chris/general/test/backupdiff/testbackupdiff.cpp
===================================================================
--- box/chris/general/test/backupdiff/testbackupdiff.cpp	2006-07-26 19:10:10 UTC (rev 692)
+++ box/chris/general/test/backupdiff/testbackupdiff.cpp	2006-07-26 23:11:39 UTC (rev 693)
@@ -68,7 +68,7 @@
 {
 	#ifdef WIN32
 	HANDLE handle = openfile(filename, O_WRONLY | O_CREAT | O_EXCL, 0);
-	TEST_THAT(handle != NULL);
+	TEST_THAT(handle != INVALID_HANDLE_VALUE);
 	SetFilePointer(handle, size, NULL, FILE_BEGIN);
 	TEST_THAT(GetLastError() == NO_ERROR);
 	TEST_THAT(SetEndOfFile(handle) == true);

Modified: box/chris/general/test/backupstore/testbackupstore.cpp
===================================================================
--- box/chris/general/test/backupstore/testbackupstore.cpp	2006-07-26 19:10:10 UTC (rev 692)
+++ box/chris/general/test/backupstore/testbackupstore.cpp	2006-07-26 23:11:39 UTC (rev 693)
@@ -1966,11 +1966,9 @@
 	CloseHandle(h1);
 
 	h1 = openfile("foo", O_CREAT | O_RDWR, 0);
-	assert(h1);
-	TEST_THAT(h1);
+	TEST_THAT(h1 != INVALID_HANDLE_VALUE);
 	h2 = openfile("foo", O_RDWR, 0);
-	assert(h2);
-	TEST_THAT(h2);
+	TEST_THAT(h2 != INVALID_HANDLE_VALUE);
 	CloseHandle(h2);
 	CloseHandle(h1);
 #endif




More information about the Boxbackup-dev mailing list