[Box Backup-dev] COMMIT r688 - box/chris/general/lib/win32

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Tue Jul 25 23:48:51 BST 2006


Author: chris
Date: 2006-07-25 22:48:48 +0000 (Tue, 25 Jul 2006)
New Revision: 688

Modified:
   box/chris/general/lib/win32/emu.cpp
   box/chris/general/lib/win32/emu.h
Log:
* emu.cpp, emu.h
- Added a real emulated utimes() function (although it sets the file 
  creation time rather than the last access time).
- OpenFileByNameUtf8 takes the desired access rights as a parameter,
  so that emu_utimes() can use it
- Fixed some compiler warnings
- Added a ConvertTime_tToFileTime() function


Modified: box/chris/general/lib/win32/emu.cpp
===================================================================
--- box/chris/general/lib/win32/emu.cpp	2006-07-23 22:36:27 UTC (rev 687)
+++ box/chris/general/lib/win32/emu.cpp	2006-07-25 22:48:48 UTC (rev 688)
@@ -516,16 +516,16 @@
 
 	if (flags & O_WRONLY)
 	{
-		accessRights = FILE_WRITE_ATTRIBUTES 
-			| FILE_WRITE_DATA | FILE_WRITE_EA;
+		accessRights = FILE_WRITE_DATA;
 		shareMode = FILE_SHARE_WRITE;
 	}
-	if (flags & (O_RDWR | O_CREAT))
+	else if (flags & (O_RDWR | O_CREAT))
 	{
 		accessRights |= FILE_WRITE_ATTRIBUTES 
 			| FILE_WRITE_DATA | FILE_WRITE_EA;
 		shareMode |= FILE_SHARE_WRITE;
 	}
+
 	if (flags & O_CREAT)
 	{
 		createDisposition = OPEN_ALWAYS;
@@ -595,7 +595,7 @@
 
 	memset(st, 0, sizeof(*st));
 
-	// This next example is how we get our INODE (equivalent) information
+	// This is how we get our INODE (equivalent) information
 	ULARGE_INTEGER conv;
 	conv.HighPart = fi.nFileIndexHigh;
 	conv.LowPart = fi.nFileIndexLow;
@@ -670,7 +670,7 @@
 //		Created: 10th December 2004
 //
 // --------------------------------------------------------------------------
-HANDLE OpenFileByNameUtf8(const char* pFileName)
+HANDLE OpenFileByNameUtf8(const char* pFileName, DWORD flags)
 {
 	std::string AbsPathWithUnicode = 
 		ConvertPathToAbsoluteUnicode(pFileName);
@@ -691,8 +691,7 @@
 	}
 
 	HANDLE handle = CreateFileW(pBuffer, 
-		FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_READ_EA |
-		READ_CONTROL, 
+		flags,
 		FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, 
 		NULL, 
 		OPEN_EXISTING, 
@@ -706,7 +705,7 @@
 		// at least one process must have the file open - 
 		// in this case someone else does.
 		handle = CreateFileW(pBuffer, 
-			READ_CONTROL, 
+			READ_CONTROL,
 			FILE_SHARE_READ, 
 			NULL, 
 			OPEN_EXISTING, 
@@ -749,7 +748,8 @@
 // --------------------------------------------------------------------------
 int emu_stat(const char * pName, struct stat * st)
 {
-	HANDLE handle = OpenFileByNameUtf8(pName);
+	HANDLE handle = OpenFileByNameUtf8(pName, 
+		FILE_READ_ATTRIBUTES | FILE_READ_EA);
 
 	if (handle == NULL)
 	{
@@ -782,7 +782,8 @@
 // --------------------------------------------------------------------------
 int statfs(const char * pName, struct statfs * s)
 {
-	HANDLE handle = OpenFileByNameUtf8(pName);
+	HANDLE handle = OpenFileByNameUtf8(pName,
+		FILE_READ_ATTRIBUTES | FILE_READ_EA);
 
 	if (handle == NULL)
 	{
@@ -814,6 +815,49 @@
 // --------------------------------------------------------------------------
 //
 // Function
+//		Name:    emu_fstat
+//		Purpose: replacement for fstat supply a windows handle
+//		Created: 25th October 2004
+//
+// --------------------------------------------------------------------------
+int emu_utimes(const char * pName, const struct timeval times[])
+{
+	FILETIME creationTime;
+	if (!ConvertTime_tToFileTime(times[0].tv_sec, &creationTime))
+	{
+		errno = EINVAL;
+		return -1;
+	}
+
+	FILETIME modificationTime;
+	if (!ConvertTime_tToFileTime(times[1].tv_sec, &modificationTime))
+	{
+		errno = EINVAL;
+		return -1;
+	}
+
+	HANDLE handle = OpenFileByNameUtf8(pName, FILE_WRITE_ATTRIBUTES);
+
+	if (handle == NULL)
+	{
+		// errno already set and error logged by OpenFileByNameUtf8()
+		return -1;
+	}
+
+	if (!SetFileTime(handle, &creationTime, NULL, &modificationTime))
+	{
+		::syslog(LOG_ERR, "Failed to set times on '%s': error %d",
+			pName, GetLastError());
+		CloseHandle(handle);
+		return 1;
+	}
+
+	return 0;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
 //		Name:    opendir
 //		Purpose: replacement for unix function, uses win32 findfirst routines
 //		Created: 25th October 2004
@@ -1227,7 +1271,14 @@
 	va_start(args, frmt);
 
 	int len = vsnprintf(buffer, sizeof(buffer)-1, sixfour.c_str(), args);
-	assert(len < sizeof(buffer));
+	assert(len >= 0);
+	if (len < 0) 
+	{
+		printf("%s\r\n", buffer);
+		return;
+	}
+	
+	assert((size_t)len < sizeof(buffer));
 	buffer[sizeof(buffer)-1] = 0;
 
 	va_end(args);
@@ -1305,7 +1356,7 @@
 		return NULL;
 	}
 
-	if (len > BufSize)
+	if ((int)len > BufSize)
 	{
 		errno = ENAMETOOLONG;
 		return NULL;

Modified: box/chris/general/lib/win32/emu.h
===================================================================
--- box/chris/general/lib/win32/emu.h	2006-07-23 22:36:27 UTC (rev 687)
+++ box/chris/general/lib/win32/emu.h	2006-07-25 22:48:48 UTC (rev 688)
@@ -91,14 +91,7 @@
 	#define S_ISDIR(x) (S_IFDIR & x)
 #endif
 
-inline int utimes(const char * Filename, timeval[])
-{
-	//again I am guessing this is quite important to
-	//be functioning, as large restores would be a problem
 
-	//indicate success
-	return 0;
-}
 inline int chown(const char * Filename, u_int32_t uid, u_int32_t gid)
 {
 	//important - this needs implementing
@@ -118,7 +111,10 @@
 int   emu_chdir (const char* pDirName);
 int   emu_unlink(const char* pFileName);
 char* emu_getcwd(char* pBuffer, int BufSize);
+int   emu_utimes(const char* pName, const struct timeval[]);
 
+#define utimes(buffer, times) emu_utimes(buffer, times)
+
 #ifdef _MSC_VER
 	inline int emu_chmod(const char * Filename, int mode)
 	{
@@ -126,10 +122,10 @@
 		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)
+	#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)
 	{
@@ -328,7 +324,7 @@
 int emu_fstat(HANDLE file, struct stat * st);
 int statfs(const char * name, struct statfs * s);
 
-//need this for converstions
+// need this for conversions
 inline time_t ConvertFileTimeToTime_t(FILETIME *fileTime)
 {
 	SYSTEMTIME stUTC;
@@ -336,7 +332,6 @@
 
 	// Convert the last-write time to local time.
 	FileTimeToSystemTime(fileTime, &stUTC);
-	// SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
 
 	memset(&timeinfo, 0, sizeof(timeinfo));	
 	timeinfo.tm_sec = stUTC.wSecond;
@@ -352,6 +347,38 @@
 	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)




More information about the Boxbackup-dev mailing list