[Box Backup-dev] COMMIT r398 - box/chris/win32/cygwin-compile-fixes/lib/win32
boxbackup-dev@fluffy.co.uk
boxbackup-dev@fluffy.co.uk
Tue, 7 Feb 2006 11:42:53 +0000 (GMT)
Author: chris
Date: 2006-02-07 11:42:50 +0000 (Tue, 07 Feb 2006)
New Revision: 398
Modified:
box/chris/win32/cygwin-compile-fixes/lib/win32/emu.cpp
box/chris/win32/cygwin-compile-fixes/lib/win32/emu.h
Log:
* emu.cpp, emu.h
- Enable some emulations on Cygwin
Modified: box/chris/win32/cygwin-compile-fixes/lib/win32/emu.cpp
===================================================================
--- box/chris/win32/cygwin-compile-fixes/lib/win32/emu.cpp 2006-02-07 11:41:42 UTC (rev 397)
+++ box/chris/win32/cygwin-compile-fixes/lib/win32/emu.cpp 2006-02-07 11:42:50 UTC (rev 398)
@@ -5,16 +5,24 @@
#include "Box.h"
+#if defined WIN32 || defined __CYGWIN__
+ #include <windows.h>
+ #ifdef __CYGWIN__
+ #undef WIN32
+ #endif
+ HANDLE OpenFileByNameUtf8(const char* pName);
+ #include <string>
+#endif
+
+#ifdef __CYGWIN__
+ #include <stdio.h>
+#endif
+
#ifdef WIN32
-// #include "emu.h"
-
-#include <windows.h>
#include <fcntl.h>
-// #include <atlenc.h>
#include <unistd.h>
-#include <string>
#include <list>
//our implimentation for a timer
@@ -393,131 +401,6 @@
// --------------------------------------------------------------------------
//
// Function
-// Name: OpenFileByNameUtf8
-// Purpose: Converts filename to Unicode and returns
-// a handle to it. In case of error, sets errno,
-// logs the error and returns NULL.
-// Created: 10th December 2004
-//
-// --------------------------------------------------------------------------
-HANDLE OpenFileByNameUtf8(const char* pName)
-{
- //some string thing - required by ms to indicate long/unicode filename
- std::string tmpStr("\\\\?\\");
-
- // is the path relative or otherwise
- std::string fileN(pName);
- if (fileN[1] != ':')
- {
- // we need to get the current directory
- char wd[PATH_MAX];
- if(::getcwd(wd, PATH_MAX) == 0)
- {
- ::syslog(LOG_WARNING,
- "Failed to open '%s': path too long", pName);
- errno = ENAMETOOLONG;
- return NULL;
- }
-
- tmpStr += wd;
- if (tmpStr[tmpStr.length()] != '\\')
- {
- tmpStr += '\\';
- }
- }
-
- tmpStr += fileN;
-
- int strlen = MultiByteToWideChar(
- CP_UTF8, // code page
- 0, // character-type options
- tmpStr.c_str(), // string to map
- (int)tmpStr.length(), // number of bytes in string
- NULL, // wide-character buffer
- 0 // size of buffer - work out
- // how much space we need
- );
-
- wchar_t* buffer = new wchar_t[strlen+1];
-
- if (buffer == NULL)
- {
- ::syslog(LOG_WARNING,
- "Failed to open '%s': out of memory", pName);
- errno = ENOMEM;
- return NULL;
- }
-
- strlen = MultiByteToWideChar(
- CP_UTF8, // code page
- 0, // character-type options
- tmpStr.c_str(), // string to map
- (int)tmpStr.length(), // number of bytes in string
- buffer, // wide-character buffer
- strlen // size of buffer
- );
-
- if (strlen == 0)
- {
- ::syslog(LOG_WARNING,
- "Failed to open '%s': could not convert "
- "file name to Unicode", pName);
- errno = EACCES;
- delete [] buffer;
- return NULL;
- }
-
- buffer[strlen] = L'\0';
-
- HANDLE handle = CreateFileW(buffer,
- FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_READ_EA,
- FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING,
- FILE_FLAG_BACKUP_SEMANTICS,
- NULL);
-
- if (handle == INVALID_HANDLE_VALUE)
- {
- // if our open fails we should always be able to
- // open in this mode - to get the inode information
- // at least one process must have the file open -
- // in this case someone else does.
- handle = CreateFileW(buffer,
- 0,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- FILE_FLAG_BACKUP_SEMANTICS,
- NULL);
- }
-
- delete [] buffer;
-
- if (handle == INVALID_HANDLE_VALUE)
- {
- DWORD err = GetLastError();
-
- if (err == ERROR_FILE_NOT_FOUND)
- {
- errno = ENOENT;
- }
- else
- {
- ::syslog(LOG_WARNING,
- "Failed to open '%s': error %d", pName);
- errno = EACCES;
- }
-
- return NULL;
- }
-
- return handle;
-}
-
-// --------------------------------------------------------------------------
-//
-// Function
// Name: ourstat
// Purpose: replacement for the lstat and stat functions,
// works with unicode filenames supplied in utf8 format
@@ -553,50 +436,6 @@
return retVal;
}
-// --------------------------------------------------------------------------
-//
-// Function
-// Name: statfs
-// Purpose: returns the mount point of where a file is located -
-// in this case the volume serial number
-// Created: 25th October 2004
-//
-// --------------------------------------------------------------------------
-int statfs(const char * pName, struct statfs * s)
-{
- HANDLE handle = OpenFileByNameUtf8(pName);
-
- if (handle == NULL)
- {
- // errno already set and error logged by OpenFileByNameUtf8()
- return -1;
- }
-
- BY_HANDLE_FILE_INFORMATION fi;
- if (!GetFileInformationByHandle(handle, &fi))
- {
- ::syslog(LOG_WARNING, "Failed to get file information "
- "for '%s': error %d", pName, GetLastError());
- CloseHandle(handle);
- errno = EACCES;
- return -1;
- }
-
- // convert volume serial number to a string
- _ui64toa(fi.dwVolumeSerialNumber, s->f_mntonname + 1, 16);
-
- // pseudo unix mount point
- s->f_mntonname[0] = DIRECTORY_SEPARATOR_ASCHAR;
-
- CloseHandle(handle); // close the handle
-
- return 0;
-}
-
-
-
-
-
// MinGW provides opendir(), etc. via <dirent.h>
// MSVC does not provide these, so emulation is needed
@@ -871,7 +710,10 @@
return -1;
}
+#endif // WIN32
+#if defined WIN32 || defined __CYGWIN__
+
HANDLE gSyslogH = 0;
void syslog(int loglevel, const char *frmt, ...)
@@ -917,7 +759,7 @@
va_list args;
va_start(args, frmt);
-#ifdef __MINGW32__
+#if defined __MINGW32__ || defined __CYGWIN__
// no _vscprintf, use a fixed size buffer
buffer = new char[1024];
int len = 1023;
@@ -960,7 +802,7 @@
{
DWORD err = GetLastError();
printf("Unable to send message to Event Log "
- "(error %i):\r\n", err);
+ "(error %i):\r\n", (int)err);
}
printf("%s\r\n", buffer);
@@ -973,4 +815,174 @@
}
}
-#endif // WIN32
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: OpenFileByNameUtf8
+// Purpose: Converts filename to Unicode and returns
+// a handle to it. In case of error, sets errno,
+// logs the error and returns NULL.
+// Created: 10th December 2004
+//
+// --------------------------------------------------------------------------
+HANDLE OpenFileByNameUtf8(const char* pName)
+{
+ //some string thing - required by ms to indicate long/unicode filename
+ std::string tmpStr("\\\\?\\");
+
+ // is the path relative or otherwise
+ std::string fileN(pName);
+ if (fileN[1] != ':')
+ {
+ // we need to get the current directory
+ char wd[PATH_MAX];
+ if(::getcwd(wd, PATH_MAX) == 0)
+ {
+ ::syslog(LOG_WARNING,
+ "Failed to open '%s': path too long", pName);
+ errno = ENAMETOOLONG;
+ return NULL;
+ }
+
+ tmpStr += wd;
+ if (tmpStr[tmpStr.length()] != '\\')
+ {
+ tmpStr += '\\';
+ }
+ }
+
+ tmpStr += fileN;
+
+ int strlen = MultiByteToWideChar(
+ CP_UTF8, // code page
+ 0, // character-type options
+ tmpStr.c_str(), // string to map
+ (int)tmpStr.length(), // number of bytes in string
+ NULL, // wide-character buffer
+ 0 // size of buffer - work out
+ // how much space we need
+ );
+
+ wchar_t* buffer = new wchar_t[strlen+1];
+
+ if (buffer == NULL)
+ {
+ ::syslog(LOG_WARNING,
+ "Failed to open '%s': out of memory", pName);
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ strlen = MultiByteToWideChar(
+ CP_UTF8, // code page
+ 0, // character-type options
+ tmpStr.c_str(), // string to map
+ (int)tmpStr.length(), // number of bytes in string
+ buffer, // wide-character buffer
+ strlen // size of buffer
+ );
+
+ if (strlen == 0)
+ {
+ ::syslog(LOG_WARNING,
+ "Failed to open '%s': could not convert "
+ "file name to Unicode", pName);
+ errno = EACCES;
+ delete [] buffer;
+ return NULL;
+ }
+
+ buffer[strlen] = L'\0';
+
+ HANDLE handle = CreateFileW(buffer,
+ FILE_READ_ATTRIBUTES | FILE_LIST_DIRECTORY | FILE_READ_EA,
+ FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
+ NULL,
+ OPEN_EXISTING,
+ FILE_FLAG_BACKUP_SEMANTICS,
+ NULL);
+
+ if (handle == INVALID_HANDLE_VALUE)
+ {
+ // if our open fails we should always be able to
+ // open in this mode - to get the inode information
+ // at least one process must have the file open -
+ // in this case someone else does.
+ handle = CreateFileW(buffer,
+ 0,
+ FILE_SHARE_READ,
+ NULL,
+ OPEN_EXISTING,
+ FILE_FLAG_BACKUP_SEMANTICS,
+ NULL);
+ }
+
+ delete [] buffer;
+
+ if (handle == INVALID_HANDLE_VALUE)
+ {
+ DWORD err = GetLastError();
+
+ if (err == ERROR_FILE_NOT_FOUND)
+ {
+ errno = ENOENT;
+ }
+ else
+ {
+ ::syslog(LOG_WARNING,
+ "Failed to open '%s': error %d", pName);
+ errno = EACCES;
+ }
+
+ return NULL;
+ }
+
+ return handle;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: statfs
+// Purpose: returns the mount point of where a file is located -
+// in this case the volume serial number
+// Created: 25th October 2004
+//
+// --------------------------------------------------------------------------
+int statfs(const char * pName, struct statfs * s)
+{
+ HANDLE handle = OpenFileByNameUtf8(pName);
+
+ if (handle == NULL)
+ {
+ // errno already set and error logged by OpenFileByNameUtf8()
+ return -1;
+ }
+
+ BY_HANDLE_FILE_INFORMATION fi;
+ if (!GetFileInformationByHandle(handle, &fi))
+ {
+ ::syslog(LOG_WARNING, "Failed to get file information "
+ "for '%s': error %d", pName, GetLastError());
+ CloseHandle(handle);
+ errno = EACCES;
+ return -1;
+ }
+
+ // convert volume serial number to a string
+
+#ifdef __CYGWIN__
+ snprintf(s->f_mntonname, sizeof(s->f_mntonname), "/%llx",
+ (long long)fi.dwVolumeSerialNumber);
+#else
+ _ui64toa(fi.dwVolumeSerialNumber, s->f_mntonname + 1, 16);
+
+ // pseudo unix mount point
+ s->f_mntonname[0] = DIRECTORY_SEPARATOR_ASCHAR;
+#endif
+
+ CloseHandle(handle); // close the handle
+
+ return 0;
+}
+#endif
Modified: box/chris/win32/cygwin-compile-fixes/lib/win32/emu.h
===================================================================
--- box/chris/win32/cygwin-compile-fixes/lib/win32/emu.h 2006-02-07 11:41:42 UTC (rev 397)
+++ box/chris/win32/cygwin-compile-fixes/lib/win32/emu.h 2006-02-07 11:42:50 UTC (rev 398)
@@ -1,8 +1,23 @@
// emulates unix syscalls to win32 functions
-#if ! defined EMU_INCLUDE && defined WIN32
+#ifndef EMU_INCLUDE
#define EMU_INCLUDE
+#if defined WIN32 || defined __CYGWIN__
+ #include <errno.h>
+ #define LOG_INFO 6
+ #define LOG_WARNING 4
+ #define LOG_ERR 3
+ #define LOG_PID 0
+ #define LOG_LOCAL6 0
+
+ #ifndef PATH_MAX
+ #define PATH_MAX MAX_PATH
+ #endif
+#endif
+
+#ifdef WIN32
+
#define _STAT_DEFINED
#define _INO_T_DEFINED
@@ -10,7 +25,6 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <direct.h>
-#include <errno.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>
@@ -142,10 +156,6 @@
return 0;
}
-#ifndef PATH_MAX
-#define PATH_MAX MAX_PATH
-#endif
-
// MinGW provides a getopt implementation
#ifndef __MINGW32__
@@ -290,12 +300,6 @@
HANDLE openfile(const char *filename, int flags, int mode);
-#define LOG_INFO 6
-#define LOG_WARNING 4
-#define LOG_ERR 3
-#define LOG_PID 0
-#define LOG_LOCAL6 0
-
extern HANDLE gSyslogH;
void MyReportEvent(LPCTSTR *szMsg, DWORD errinfo);
inline void openlog(const char * daemonName, int, int)
@@ -352,12 +356,6 @@
return 0;
}
-//this shouldn't be needed.
-struct statfs
-{
- TCHAR f_mntonname[MAX_PATH];
-};
-
// I think this should get us going
// Although there is a warning about
// mount points in win32 can now exists - which means inode number can be
@@ -384,7 +382,6 @@
int ourstat(const char * name, struct stat * st);
int ourfstat(HANDLE file, struct stat * st);
-int statfs(const char * name, struct statfs * s);
//need this for converstions
inline time_t ConvertFileTimeToTime_t(FILETIME *fileTime)
@@ -416,6 +413,14 @@
int poll (struct pollfd *ufds, unsigned long nfds, int timeout);
bool EnableBackupRights( void );
+#endif // WIN32
+
+#ifdef __CYGWIN__
+typedef char TCHAR;
+#define MAX_PATH 1024
+#endif
+
+#if defined WIN32 || defined __CYGWIN__
//
// MessageId: MSG_ERR_EXIST
// MessageText:
@@ -423,4 +428,13 @@
//
#define MSG_ERR_EXIST ((DWORD)0xC0000004L)
-#endif // !EMU_INCLUDE && WIN32
+// this shouldn't be needed.
+struct statfs
+{
+ TCHAR f_mntonname[MAX_PATH];
+};
+
+int statfs(const char * name, struct statfs * s);
+#endif // WIN32 || __CYGWIN__
+
+#endif // !EMU_INCLUDE