diff options
author | Reinhard Tartler <siretart@tauware.de> | 2017-06-11 21:52:33 -0400 |
---|---|---|
committer | Reinhard Tartler <siretart@tauware.de> | 2017-06-11 21:52:33 -0400 |
commit | e0eb815b67734abd09ff41e2271630d4b2a6d760 (patch) | |
tree | 0df971c34f98d2a1dfd0921524a17d561a4a6536 /lib/win32 | |
parent | 676c9e1c9d4ac8eb8a440d7f11c4ac44f98f4a6a (diff) | |
parent | e19a5db232e1ef90e9a02159d2fbd9707ffe4373 (diff) |
merge upstream version 0.12
Diffstat (limited to 'lib/win32')
-rw-r--r-- | lib/win32/emu.cpp | 183 | ||||
-rw-r--r-- | lib/win32/emu.h | 79 | ||||
-rw-r--r-- | lib/win32/emu_winver.h | 37 | ||||
-rwxr-xr-x | lib/win32/getopt_long.cpp | 4 |
4 files changed, 303 insertions, 0 deletions
diff --git a/lib/win32/emu.cpp b/lib/win32/emu.cpp index db9974d2..9b5159cf 100644 --- a/lib/win32/emu.cpp +++ b/lib/win32/emu.cpp @@ -1,8 +1,11 @@ // Box Backup Win32 native port by Nick Knight +<<<<<<< HEAD // Need at least 0x0500 to use GetFileSizeEx on Cygwin/MinGW #define WINVER 0x0500 +======= +>>>>>>> 0.12 #include "emu.h" #ifdef WIN32 @@ -82,9 +85,12 @@ bool EnableBackupRights() return true; } +<<<<<<< HEAD // forward declaration char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage); +======= +>>>>>>> 0.12 // -------------------------------------------------------------------------- // // Function @@ -246,7 +252,11 @@ char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage) { ::syslog(LOG_WARNING, "Failed to convert wide string to narrow: " +<<<<<<< HEAD "error %d", GetLastError()); +======= + "%s", GetErrorMessage(GetLastError()).c_str()); +>>>>>>> 0.12 errno = EINVAL; return NULL; } @@ -278,7 +288,11 @@ char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage) { ::syslog(LOG_WARNING, "Failed to convert wide string to narrow: " +<<<<<<< HEAD "error %i", GetLastError()); +======= + "%s", GetErrorMessage(GetLastError()).c_str()); +>>>>>>> 0.12 errno = EACCES; delete [] buffer; return NULL; @@ -287,6 +301,72 @@ char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage) return buffer; } +<<<<<<< HEAD +======= +bool ConvertFromWideString(const std::wstring& rInput, + std::string* pOutput, unsigned int codepage) +{ + int len = WideCharToMultiByte + ( + codepage, // destination code page + 0, // character-type options + rInput.c_str(), // string to map + rInput.size(), // number of bytes in string - auto detect + NULL, // output buffer + 0, // size of buffer - work out + // how much space we need + NULL, // replace unknown chars with system default + NULL // don't tell us when that happened + ); + + if (len == 0) + { + ::syslog(LOG_WARNING, + "Failed to convert wide string to narrow: " + "%s", GetErrorMessage(GetLastError()).c_str()); + errno = EINVAL; + return false; + } + + char* buffer = new char[len]; + + if (buffer == NULL) + { + ::syslog(LOG_WARNING, + "Failed to convert wide string to narrow: " + "out of memory"); + errno = ENOMEM; + return false; + } + + len = WideCharToMultiByte + ( + codepage, // source code page + 0, // character-type options + rInput.c_str(), // string to map + rInput.size(), // number of bytes in string - auto detect + buffer, // output buffer + len, // size of buffer + NULL, // replace unknown chars with system default + NULL // don't tell us when that happened + ); + + if (len == 0) + { + ::syslog(LOG_WARNING, + "Failed to convert wide string to narrow: " + "%s", GetErrorMessage(GetLastError()).c_str()); + errno = EACCES; + delete [] buffer; + return false; + } + + *pOutput = std::string(buffer, len); + delete [] buffer; + return true; +} + +>>>>>>> 0.12 // -------------------------------------------------------------------------- // // Function @@ -391,7 +471,19 @@ std::string ConvertPathToAbsoluteUnicode(const char *pFileName) return tmpStr; } +<<<<<<< HEAD if (filename.length() > 2 && filename[0] == '\\' && +======= + if (filename.length() > 4 && filename[0] == '\\' && + filename[1] == '\\' && filename[2] == '?' && + filename[3] == '\\') + { + // File is already in absolute utf-8 format, e.g. + // \\?\GLOBALROOT\... + tmpStr = ""; + } + else if (filename.length() > 2 && filename[0] == '\\' && +>>>>>>> 0.12 filename[1] == '\\') { tmpStr += "UNC\\"; @@ -401,16 +493,27 @@ std::string ConvertPathToAbsoluteUnicode(const char *pFileName) } else if (filename.length() >= 1 && filename[0] == '\\') { +<<<<<<< HEAD // root directory of current drive. +======= + // starts with \, i.e. root directory of current drive. +>>>>>>> 0.12 tmpStr = wd; tmpStr.resize(2); // drive letter and colon } else if (filename.length() >= 2 && filename[1] != ':') { +<<<<<<< HEAD // Must be relative. We need to get the // current directory to make it absolute. tmpStr += wd; if (tmpStr[tmpStr.length()] != '\\') +======= + // Must be a relative path. We need to get the + // current directory to make it absolute. + tmpStr += wd; + if (tmpStr[tmpStr.length()-1] != '\\') +>>>>>>> 0.12 { tmpStr += '\\'; } @@ -439,7 +542,11 @@ std::string ConvertPathToAbsoluteUnicode(const char *pFileName) ""); } +<<<<<<< HEAD i = lastSlash; +======= + i = lastSlash - 1; +>>>>>>> 0.12 } } @@ -654,7 +761,11 @@ int emu_fstat(HANDLE hdir, struct emu_stat * st) { conv.HighPart = fi.nFileSizeHigh; conv.LowPart = fi.nFileSizeLow; +<<<<<<< HEAD st->st_size = (_off_t)conv.QuadPart; +======= + st->st_size = conv.QuadPart; +>>>>>>> 0.12 } // at the mo @@ -988,7 +1099,11 @@ DIR *opendir(const char *name) std::string dirName(name); //append a '\' win32 findfirst is sensitive to this +<<<<<<< HEAD if ( dirName[dirName.size()] != '\\' || dirName[dirName.size()] != '/' ) +======= + if (dirName[dirName.size()-1] != '\\' || dirName[dirName.size()-1] != '/') +>>>>>>> 0.12 { dirName += '\\'; } @@ -1012,15 +1127,26 @@ DIR *opendir(const char *name) return NULL; } +<<<<<<< HEAD pDir->fd = _wfindfirst((const wchar_t*)pDir->name, &(pDir->info)); if (pDir->fd == -1) +======= + pDir->fd = FindFirstFileW(pDir->name, &pDir->info); + DWORD tmp = GetLastError(); + + if (pDir->fd == INVALID_HANDLE_VALUE) +>>>>>>> 0.12 { delete [] pDir->name; delete pDir; return NULL; } +<<<<<<< HEAD +======= + +>>>>>>> 0.12 pDir->result.d_name = 0; return pDir; } @@ -1043,6 +1169,7 @@ struct dirent *readdir(DIR *dp) { struct dirent *den = NULL; +<<<<<<< HEAD if (dp && dp->fd != -1) { if (!dp->result.d_name || @@ -1052,10 +1179,25 @@ struct dirent *readdir(DIR *dp) std::wstring input(dp->info.name); memset(tempbuff, 0, sizeof(tempbuff)); WideCharToMultiByte(CP_UTF8, 0, dp->info.name, +======= + if (dp && dp->fd != INVALID_HANDLE_VALUE) + { + // first time around, when dp->result.d_name == NULL, use + // the values returned by FindFirstFile. After that, call + // FindNextFileW to return new ones. + if (!dp->result.d_name || + FindNextFileW(dp->fd, &dp->info) != 0) + { + den = &dp->result; + std::wstring input(dp->info.cFileName); + memset(tempbuff, 0, sizeof(tempbuff)); + WideCharToMultiByte(CP_UTF8, 0, dp->info.cFileName, +>>>>>>> 0.12 -1, &tempbuff[0], sizeof (tempbuff), NULL, NULL); //den->d_name = (char *)dp->info.name; den->d_name = &tempbuff[0]; +<<<<<<< HEAD if (dp->info.attrib & FILE_ATTRIBUTE_DIRECTORY) { den->d_type = S_IFDIR; @@ -1063,6 +1205,23 @@ struct dirent *readdir(DIR *dp) else { den->d_type = S_IFREG; +======= + den->d_type = dp->info.dwFileAttributes; + } + else // FindNextFileW failed + { + // Why did it fail? No more files? + winerrno = GetLastError(); + den = NULL; + + if (winerrno == ERROR_NO_MORE_FILES) + { + errno = 0; // no more files + } + else + { + errno = ENOSYS; +>>>>>>> 0.12 } } } @@ -1070,6 +1229,10 @@ struct dirent *readdir(DIR *dp) { errno = EBADF; } +<<<<<<< HEAD +======= + +>>>>>>> 0.12 return den; } catch (...) @@ -1091,24 +1254,44 @@ int closedir(DIR *dp) { try { +<<<<<<< HEAD int finres = -1; if (dp) { if(dp->fd != -1) { finres = _findclose(dp->fd); +======= + BOOL finres = false; + + if (dp) + { + if(dp->fd != INVALID_HANDLE_VALUE) + { + finres = FindClose(dp->fd); +>>>>>>> 0.12 } delete [] dp->name; delete dp; } +<<<<<<< HEAD if (finres == -1) // errors go to EBADF { errno = EBADF; } return finres; +======= + if (finres == FALSE) // errors go to EBADF + { + winerrno = GetLastError(); + errno = EBADF; + } + + return (finres == TRUE) ? 0 : -1; +>>>>>>> 0.12 } catch (...) { diff --git a/lib/win32/emu.h b/lib/win32/emu.h index f3389590..bc91e2ba 100644 --- a/lib/win32/emu.h +++ b/lib/win32/emu.h @@ -1,5 +1,10 @@ // emulates unix syscalls to win32 functions +<<<<<<< HEAD +======= +#include "emu_winver.h" + +>>>>>>> 0.12 #ifdef WIN32 #define EMU_STRUCT_STAT struct emu_stat #define EMU_STAT emu_stat @@ -15,6 +20,17 @@ #if ! defined EMU_INCLUDE && defined WIN32 #define EMU_INCLUDE +<<<<<<< HEAD +======= +// Need feature detection macros below +#include "../common/BoxConfig.h" + +// Shut up stupid new warnings. Thanks MinGW! Ever heard of "compatibility"? +#ifdef __MINGW32__ +# define __MINGW_FEATURES__ 0 +#endif + +>>>>>>> 0.12 // basic types, may be required by other headers since we // don't include sys/types.h @@ -42,6 +58,7 @@ typedef unsigned int pid_t; #endif +<<<<<<< HEAD // set up to include the necessary parts of Windows headers #define WIN32_LEAN_AND_MEAN @@ -50,6 +67,8 @@ #define __MSVCRT_VERSION__ 0x0601 #endif +======= +>>>>>>> 0.12 // Windows headers #include <winsock2.h> @@ -220,17 +239,28 @@ inline int strcasecmp(const char *s1, const char *s2) struct dirent { char *d_name; +<<<<<<< HEAD unsigned long d_type; +======= + DWORD d_type; // file attributes +>>>>>>> 0.12 }; struct DIR { +<<<<<<< HEAD intptr_t fd; // filedescriptor // struct _finddata_t info; struct _wfinddata_t info; // struct _finddata_t info; struct dirent result; // d_name (first time null) wchar_t *name; // null-terminated byte string +======= + HANDLE fd; // the HANDLE returned by FindFirstFile + WIN32_FIND_DATAW info; + struct dirent result; // d_name (first time null) + wchar_t* name; // null-terminated byte string +>>>>>>> 0.12 }; DIR *opendir(const char *name); @@ -286,9 +316,24 @@ inline unsigned int sleep(unsigned int secs) } #define INFTIM -1 +<<<<<<< HEAD #define POLLIN 0x1 #define POLLERR 0x8 #define POLLOUT 0x4 +======= + +#ifndef POLLIN +# define POLLIN 0x1 +#endif + +#ifndef POLLERR +# define POLLERR 0x8 +#endif + +#ifndef POLLOUT +# define POLLOUT 0x4 +#endif +>>>>>>> 0.12 #define SHUT_RDWR SD_BOTH #define SHUT_RD SD_RECEIVE @@ -307,11 +352,14 @@ inline int ioctl(SOCKET sock, int flag, int * something) return 0; } +<<<<<<< HEAD extern "C" inline int getpid() { return (int)GetCurrentProcessId(); } +======= +>>>>>>> 0.12 inline int waitpid(pid_t pid, int *status, int) { return 0; @@ -351,12 +399,21 @@ int emu_chmod (const char* pName, mode_t mode); char* emu_getcwd (char* pBuffer, int BufSize); int emu_rename (const char* pOldName, const char* pNewName); +<<<<<<< HEAD #define chdir(directory) emu_chdir (directory) #define mkdir(path, mode) emu_mkdir (path) #define unlink(file) emu_unlink (file) #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 chdir(directory) emu_chdir (directory) +#define mkdir(path, mode) emu_mkdir (path) +#define unlink(file) emu_unlink (file) +#define utimes(buffer, times) emu_utimes (buffer, times) +#define chmod(file, mode) emu_chmod (file, mode) +#define getcwd(buffer, size) emu_getcwd (buffer, size) +>>>>>>> 0.12 #define rename(oldname, newname) emu_rename (oldname, newname) // Not safe to replace stat/fstat/lstat on mingw at least, as struct stat @@ -396,6 +453,13 @@ bool ConvertFromUtf8 (const std::string& rSource, std::string& rDest, int destCodePage); bool ConvertUtf8ToConsole(const std::string& rSource, std::string& rDest); bool ConvertConsoleToUtf8(const std::string& rSource, std::string& rDest); +<<<<<<< HEAD +======= +char* ConvertFromWideString(const WCHAR* pString, unsigned int codepage); +bool ConvertFromWideString(const std::wstring& rInput, + std::string* pOutput, unsigned int codepage); +std::string ConvertPathToAbsoluteUnicode(const char *pFileName); +>>>>>>> 0.12 // Utility function which returns a default config file name, // based on the path of the current executable. @@ -409,6 +473,21 @@ std::string GetErrorMessage(DWORD errorCode); // relatively recent C runtime lib int console_read(char* pBuffer, size_t BufferSize); +<<<<<<< HEAD +======= +// Defined thus by MinGW, but missing from MSVC +// [http://curl.haxx.se/mail/lib-2004-11/0260.html] +// note: chsize() doesn't work over 2GB: +// [https://stat.ethz.ch/pipermail/r-devel/2005-May/033339.html] +#ifndef HAVE_FTRUNCATE + extern "C" int ftruncate(int, off_t); + inline int ftruncate(int __fd, off_t __length) + { + return _chsize(__fd, __length); + } +#endif + +>>>>>>> 0.12 #ifdef _MSC_VER /* disable certain compiler warnings to be able to actually see the show-stopper ones */ #pragma warning(disable:4101) // unreferenced local variable diff --git a/lib/win32/emu_winver.h b/lib/win32/emu_winver.h new file mode 100644 index 00000000..92060150 --- /dev/null +++ b/lib/win32/emu_winver.h @@ -0,0 +1,37 @@ +#ifndef _EMU_WINVER_H +#define _EMU_WINVER_H + +// set up to include the necessary parts of Windows headers + +#define WIN32_LEAN_AND_MEAN + +#ifndef __MSVCRT_VERSION__ +#define __MSVCRT_VERSION__ 0x0601 +#endif + +// We need WINVER at least 0x0500 to use GetFileSizeEx on Cygwin/MinGW, +// and 0x0501 for FindFirstFile(W) for opendir/readdir. +// +// WIN32_WINNT versions 0x0600 (Vista) and higher enable WSAPoll() in +// winsock2.h, whose struct pollfd conflicts with ours below, so for +// now we just set it lower than that, to Windows XP (0x0501). + +#ifdef WINVER +# if WINVER != 0x0501 +// provoke a redefinition warning to track down the offender +# define WINVER 0x0501 +# error Must include emu.h before setting WINVER +# endif +#endif +#define WINVER 0x0501 + +#ifdef _WIN32_WINNT +# if _WIN32_WINNT != 0x0501 +// provoke a redefinition warning to track down the offender +# define _WIN32_WINNT 0x0501 +# error Must include emu.h before setting _WIN32_WINNT +# endif +#endif +#define _WIN32_WINNT 0x0501 + +#endif // _EMU_WINVER_H diff --git a/lib/win32/getopt_long.cpp b/lib/win32/getopt_long.cpp index 5d910e1b..a508d22a 100755 --- a/lib/win32/getopt_long.cpp +++ b/lib/win32/getopt_long.cpp @@ -58,6 +58,10 @@ */
// #include "Box.h"
+<<<<<<< HEAD +======= +#include "emu.h"
+>>>>>>> 0.12 #include <errno.h>
#include <stdarg.h>
|