mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
Port Tor to work on Windows CE
Most of the changes here are switches to use APIs available on Windows
CE. The most pervasive change is that Windows CE only provides the
wide-character ("FooW") variants of most of the windows function, and
doesn't support the older ASCII verions at all.
This patch will require use of the wcecompat library to get working
versions of the posix-style fd-based file IO functions.
[commit message by nickm]
This commit is contained in:
+33
-13
@@ -22,8 +22,10 @@
|
||||
#ifdef MS_WINDOWS
|
||||
#include <process.h>
|
||||
#include <windows.h>
|
||||
#if !defined (WINCE)
|
||||
#include <sys/locking.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_UNAME
|
||||
#include <sys/utsname.h>
|
||||
@@ -169,12 +171,13 @@ tor_munmap_file(tor_mmap_t *handle)
|
||||
tor_mmap_t *
|
||||
tor_mmap_file(const char *filename)
|
||||
{
|
||||
WCHAR wfilename[MAX_PATH]= {0};
|
||||
tor_mmap_t *res = tor_malloc_zero(sizeof(tor_mmap_t));
|
||||
int empty = 0;
|
||||
res->file_handle = INVALID_HANDLE_VALUE;
|
||||
res->mmap_handle = NULL;
|
||||
|
||||
res->file_handle = CreateFile(filename,
|
||||
mbstowcs(wfilename,filename,MAX_PATH);
|
||||
res->file_handle = CreateFileW(wfilename,
|
||||
GENERIC_READ, FILE_SHARE_READ,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
@@ -1697,10 +1700,15 @@ get_uname(void)
|
||||
#endif
|
||||
{
|
||||
#ifdef MS_WINDOWS
|
||||
OSVERSIONINFOEX info;
|
||||
#if defined (WINCE)
|
||||
OSVERSIONINFO info;
|
||||
#else
|
||||
OSVERSIONINFOEXW info;
|
||||
#endif
|
||||
int i;
|
||||
const char *plat = NULL;
|
||||
const char *extra = NULL;
|
||||
char acsd[MAX_PATH] = {0};
|
||||
static struct {
|
||||
unsigned major; unsigned minor; const char *version;
|
||||
} win_version_table[] = {
|
||||
@@ -1718,20 +1726,21 @@ get_uname(void)
|
||||
};
|
||||
memset(&info, 0, sizeof(info));
|
||||
info.dwOSVersionInfoSize = sizeof(info);
|
||||
if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
|
||||
if (! GetVersionExW((LPOSVERSIONINFO)&info)) {
|
||||
strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
|
||||
" doesn't work.", sizeof(uname_result));
|
||||
uname_result_is_set = 1;
|
||||
return uname_result;
|
||||
}
|
||||
wcstombs(acsd, info.szCSDVersion, MAX_PATH);
|
||||
if (info.dwMajorVersion == 4 && info.dwMinorVersion == 0) {
|
||||
if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
|
||||
plat = "Windows NT 4.0";
|
||||
else
|
||||
plat = "Windows 95";
|
||||
if (info.szCSDVersion[1] == 'B')
|
||||
if (acsd[1] == 'B')
|
||||
extra = "OSR2 (B)";
|
||||
else if (info.szCSDVersion[1] == 'C')
|
||||
else if (acsd[1] == 'C')
|
||||
extra = "OSR2 (C)";
|
||||
} else {
|
||||
for (i=0; win_version_table[i].major>0; ++i) {
|
||||
@@ -1743,14 +1752,14 @@ get_uname(void)
|
||||
}
|
||||
}
|
||||
if (plat && !strcmp(plat, "Windows 98")) {
|
||||
if (info.szCSDVersion[1] == 'A')
|
||||
if (acsd[1] == 'A')
|
||||
extra = "SE (A)";
|
||||
else if (info.szCSDVersion[1] == 'B')
|
||||
else if (acsd[1] == 'B')
|
||||
extra = "SE (B)";
|
||||
}
|
||||
if (plat) {
|
||||
if (!extra)
|
||||
extra = info.szCSDVersion;
|
||||
extra = acsd;
|
||||
tor_snprintf(uname_result, sizeof(uname_result), "%s %s",
|
||||
plat, extra);
|
||||
} else {
|
||||
@@ -1759,13 +1768,14 @@ get_uname(void)
|
||||
tor_snprintf(uname_result, sizeof(uname_result),
|
||||
"Very recent version of Windows [major=%d,minor=%d] %s",
|
||||
(int)info.dwMajorVersion,(int)info.dwMinorVersion,
|
||||
info.szCSDVersion);
|
||||
acsd);
|
||||
else
|
||||
tor_snprintf(uname_result, sizeof(uname_result),
|
||||
"Unrecognized version of Windows [major=%d,minor=%d] %s",
|
||||
(int)info.dwMajorVersion,(int)info.dwMinorVersion,
|
||||
info.szCSDVersion);
|
||||
acsd);
|
||||
}
|
||||
#if !defined (WINCE)
|
||||
#ifdef VER_SUITE_BACKOFFICE
|
||||
if (info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
|
||||
strlcat(uname_result, " [domain controller]", sizeof(uname_result));
|
||||
@@ -1775,6 +1785,7 @@ get_uname(void)
|
||||
strlcat(uname_result, " [workstation]", sizeof(uname_result));
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
|
||||
#endif
|
||||
@@ -1902,8 +1913,15 @@ tor_gettimeofday(struct timeval *timeval)
|
||||
uint64_t ft_64;
|
||||
FILETIME ft_ft;
|
||||
} ft;
|
||||
#if defined (WINCE)
|
||||
/* wince do not have GetSystemTimeAsFileTime */
|
||||
SYSTEMTIME stime;
|
||||
GetSystemTime(&stime);
|
||||
SystemTimeToFileTime(&stime,&ft.ft_ft);
|
||||
#else
|
||||
/* number of 100-nsec units since Jan 1, 1601 */
|
||||
GetSystemTimeAsFileTime(&ft.ft_ft);
|
||||
#endif
|
||||
if (ft.ft_64 < EPOCH_BIAS) {
|
||||
log_err(LD_GENERAL,"System time is before 1970; failing.");
|
||||
exit(1);
|
||||
@@ -2515,10 +2533,11 @@ char *
|
||||
format_win32_error(DWORD err)
|
||||
{
|
||||
LPVOID str = NULL;
|
||||
char abuf[1024] = {0};
|
||||
char *result;
|
||||
|
||||
/* Somebody once decided that this interface was better than strerror(). */
|
||||
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL, err,
|
||||
@@ -2527,7 +2546,8 @@ format_win32_error(DWORD err)
|
||||
0, NULL);
|
||||
|
||||
if (str) {
|
||||
result = tor_strdup((char*)str);
|
||||
wcstombs(abuf,str,1024);
|
||||
result = tor_strdup((char*)abuf);
|
||||
LocalFree(str); /* LocalFree != free() */
|
||||
} else {
|
||||
result = tor_strdup("<unformattable error>");
|
||||
|
||||
+2
-2
@@ -177,8 +177,8 @@ extern INLINE double U64_TO_DBL(uint64_t x) {
|
||||
/* ===== String compatibility */
|
||||
#ifdef MS_WINDOWS
|
||||
/* Windows names string functions differently from most other platforms. */
|
||||
#define strncasecmp strnicmp
|
||||
#define strcasecmp stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#define strcasecmp _stricmp
|
||||
#endif
|
||||
#ifndef HAVE_STRLCAT
|
||||
size_t strlcat(char *dst, const char *src, size_t siz) ATTR_NONNULL((1,2));
|
||||
|
||||
+13
-8
@@ -1707,7 +1707,7 @@ check_private_dir(const char *dirname, cpd_check_t check)
|
||||
return -1;
|
||||
} else if (check == CPD_CREATE) {
|
||||
log_info(LD_GENERAL, "Creating directory %s", dirname);
|
||||
#ifdef MS_WINDOWS
|
||||
#if defined (MS_WINDOWS) && !defined (WINCE)
|
||||
r = mkdir(dirname);
|
||||
#else
|
||||
r = mkdir(dirname, 0700);
|
||||
@@ -1843,7 +1843,8 @@ start_writing_to_file(const char *fname, int open_flags, int mode,
|
||||
if (open_flags & O_BINARY)
|
||||
new_file->binary = 1;
|
||||
|
||||
if ((new_file->fd = open(open_name, open_flags, mode)) < 0) {
|
||||
new_file->fd = open(open_name, open_flags, mode);
|
||||
if (new_file->fd < 0) {
|
||||
log_warn(LD_FS, "Couldn't open \"%s\" (%s) for writing: %s",
|
||||
open_name, fname, strerror(errno));
|
||||
goto err;
|
||||
@@ -2526,22 +2527,26 @@ tor_listdir(const char *dirname)
|
||||
smartlist_t *result;
|
||||
#ifdef MS_WINDOWS
|
||||
char *pattern;
|
||||
WCHAR wpattern[MAX_PATH] = {0};
|
||||
char name[MAX_PATH] = {0};
|
||||
HANDLE handle;
|
||||
WIN32_FIND_DATA findData;
|
||||
WIN32_FIND_DATAW findData;
|
||||
size_t pattern_len = strlen(dirname)+16;
|
||||
pattern = tor_malloc(pattern_len);
|
||||
tor_snprintf(pattern, pattern_len, "%s\\*", dirname);
|
||||
if (INVALID_HANDLE_VALUE == (handle = FindFirstFile(pattern, &findData))) {
|
||||
mbstowcs(wpattern,pattern,MAX_PATH);
|
||||
if (INVALID_HANDLE_VALUE == (handle = FindFirstFileW(wpattern, &findData))) {
|
||||
tor_free(pattern);
|
||||
return NULL;
|
||||
}
|
||||
wcstombs(name,findData.cFileName,MAX_PATH);
|
||||
result = smartlist_create();
|
||||
while (1) {
|
||||
if (strcmp(findData.cFileName, ".") &&
|
||||
strcmp(findData.cFileName, "..")) {
|
||||
smartlist_add(result, tor_strdup(findData.cFileName));
|
||||
if (strcmp(name, ".") &&
|
||||
strcmp(name, "..")) {
|
||||
smartlist_add(result, tor_strdup(name));
|
||||
}
|
||||
if (!FindNextFile(handle, &findData)) {
|
||||
if (!FindNextFileW(handle, &findData)) {
|
||||
DWORD err;
|
||||
if ((err = GetLastError()) != ERROR_NO_MORE_FILES) {
|
||||
char *errstr = format_win32_error(err);
|
||||
|
||||
Reference in New Issue
Block a user