[Box Backup-commit] COMMIT r1436 - box/chris/general/lib/win32

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Thu Mar 22 23:57:14 GMT 2007


Author: chris
Date: 2007-03-22 23:57:13 +0000 (Thu, 22 Mar 2007)
New Revision: 1436

Modified:
   box/chris/general/lib/win32/emu.cpp
   box/chris/general/lib/win32/emu.h
Log:
Add emulated rename() with path conversion


Modified: box/chris/general/lib/win32/emu.cpp
===================================================================
--- box/chris/general/lib/win32/emu.cpp	2007-03-22 23:48:12 UTC (rev 1435)
+++ box/chris/general/lib/win32/emu.cpp	2007-03-22 23:57:13 UTC (rev 1436)
@@ -1609,6 +1609,72 @@
 	return 0;
 }
 
+int emu_rename(const char* pOldFileName, const char* pNewFileName)
+{
+	std::string OldFilePathWithUnicode = 
+		ConvertPathToAbsoluteUnicode(pOldFileName);
+
+	if (OldPathWithUnicode.size() == 0)
+	{
+		// error already logged by ConvertPathToAbsoluteUnicode()
+		return -1;
+	}
+
+	WCHAR* pOldBuffer = ConvertUtf8ToWideString(OldPathWithUnicode.c_str());
+	if (!pOldBuffer)
+	{
+		return -1;
+	}
+
+	std::string NewFilePathWithUnicode = 
+		ConvertPathToAbsoluteUnicode(pNewFileName);
+
+	if (NewPathWithUnicode.size() == 0)
+	{
+		// error already logged by ConvertPathToAbsoluteUnicode()
+		delete [] pOldBuffer;
+		return -1;
+	}
+
+	WCHAR* pNewBuffer = ConvertUtf8ToWideString(NewPathWithUnicode.c_str());
+	if (!pNewBuffer)
+	{
+		delete [] pOldBuffer;
+		return -1;
+	}
+
+	BOOL result = MoveFileW(pOldBuffer, pNewBuffer);
+	DWORD err = GetLastError();
+	delete [] pOldBuffer;
+	delete [] pNewBuffer;
+
+	if (!result)
+	{
+		if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
+		{
+			errno = ENOENT;
+		}
+		else if (err == ERROR_SHARING_VIOLATION)
+		{
+			errno = EBUSY;
+		}
+		else if (err == ERROR_ACCESS_DENIED)
+		{
+			errno = EACCES;
+		}
+		else
+		{
+			::syslog(LOG_WARNING, "Failed to rename file "
+				"'%s' to '%s': %s", pOldFileName, pNewFileName,
+				GetErrorMessage(err).c_str());
+			errno = ENOSYS;
+		}
+		return -1;
+	}
+
+	return 0;
+}
+
 int console_read(char* pBuffer, size_t BufferSize)
 {
 	HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);

Modified: box/chris/general/lib/win32/emu.h
===================================================================
--- box/chris/general/lib/win32/emu.h	2007-03-22 23:48:12 UTC (rev 1435)
+++ box/chris/general/lib/win32/emu.h	2007-03-22 23:57:13 UTC (rev 1436)
@@ -337,6 +337,7 @@
 int   emu_utimes (const char* pName, const struct timeval[]);
 int   emu_chmod  (const char* pName, mode_t mode);
 char* emu_getcwd (char* pBuffer,     int BufSize);
+char* emu_rename (const char* pOldName, const char* pNewName);
 
 #define chdir(directory)        emu_chdir  (directory)
 #define mkdir(path,     mode)   emu_mkdir  (path)
@@ -347,6 +348,7 @@
 #define utimes(buffer,  times)  emu_utimes (buffer,   times)
 #define chmod(file,     mode)   emu_chmod  (file,     mode)
 #define getcwd(buffer,  size)   emu_getcwd (buffer,   size)
+#define rename(oldname, newname) emu_rename (oldname, newname)
 
 int statfs(const char * name, struct statfs * s);
 




More information about the Boxbackup-commit mailing list