X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Fwin32%2Fcompat%2Fcompat.cpp;h=e93ce9c45b39d3beb4d2fc3f13f724f1892c5b7e;hb=a33d3671776f81795bb6bd649572d845a4bcea10;hp=a9f46a1173aa145577a0a10e18660e1fc20e576a;hpb=8e90e3ed517ae5095a56f05175c54ba1ee1860cb;p=bacula%2Fbacula diff --git a/bacula/src/win32/compat/compat.cpp b/bacula/src/win32/compat/compat.cpp index a9f46a1173..e93ce9c45b 100644 --- a/bacula/src/win32/compat/compat.cpp +++ b/bacula/src/win32/compat/compat.cpp @@ -1,3 +1,30 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2004-2008 Free Software Foundation Europe e.V. + + The main author of Bacula is Kern Sibbald, with contributions from + many others, a complete list can be found in the file AUTHORS. + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation and included + in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of Kern Sibbald. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ // -*- Mode: C++ -*- // compat.cpp -- compatibilty layer to make bacula-fd run // natively under windows @@ -5,51 +32,43 @@ // Copyright transferred from Raider Solutions, Inc to // Kern Sibbald and John Walker by express permission. // -// Copyright (C) 2004-2006 Kern Sibbald -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// version 2 as amended with additional clauses defined in the -// file LICENSE in the main source directory. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// the file LICENSE for additional details. -// // Author : Christopher S. Hull // Created On : Sat Jan 31 15:55:00 2004 // $Id$ -#ifdef __APCUPSD__ - -#include "apc.h" -#include "compat.h" -#include "winapi.h" - -#else #include "bacula.h" #include "compat.h" #include "jcr.h" -#include "../../lib/winapi.h" -#include "vss.h" - -#endif +#include "findlib/find.h" #define b_errno_win32 (1<<29) +#define MAX_PATHLENGTH 1024 /* UTF-8 to UCS2 path conversion is expensive, so we cache the conversion. During backup the conversion is called 3 times (lstat, attribs, open), by using the cache this is reduced to 1 time */ -static POOLMEM *g_pWin32ConvUTF8Cache = get_pool_memory (PM_FNAME); -static POOLMEM *g_pWin32ConvUCS2Cache = get_pool_memory (PM_FNAME); +static POOLMEM *g_pWin32ConvUTF8Cache = get_pool_memory(PM_FNAME); +static POOLMEM *g_pWin32ConvUCS2Cache = get_pool_memory(PM_FNAME); static DWORD g_dwWin32ConvUTF8strlen = 0; static pthread_mutex_t Win32Convmutex = PTHREAD_MUTEX_INITIALIZER; +static t_pVSSPathConvert g_pVSSPathConvert; +static t_pVSSPathConvertW g_pVSSPathConvertW; + +/* Forward referenced functions */ +static const char *errorString(void); + + +void SetVSSPathConvert(t_pVSSPathConvert pPathConvert, t_pVSSPathConvertW pPathConvertW) +{ + g_pVSSPathConvert = pPathConvert; + g_pVSSPathConvertW = pPathConvertW; +} + void Win32ConvCleanupCache() { if (g_pWin32ConvUTF8Cache) { @@ -70,12 +89,11 @@ void Win32ConvCleanupCache() #undef fputs -#define USE_WIN32_COMPAT_IO 1 +//#define USE_WIN32_COMPAT_IO 1 #define USE_WIN32_32KPATHCONVERSION 1 -extern void d_msg(const char *file, int line, int level, const char *fmt,...); extern DWORD g_platform_id; -extern int enable_vss; +extern DWORD g_MinorVersion; // from MicroSoft SDK (KES) is the diff between Jan 1 1601 and Jan 1 1970 #ifdef HAVE_MINGW @@ -90,6 +108,29 @@ void conv_unix_to_win32_path(const char *name, char *win32_name, DWORD dwSize) { const char *fname = name; char *tname = win32_name; + + Dmsg0(100, "Enter convert_unix_to_win32_path\n"); + + if (IsPathSeparator(name[0]) && + IsPathSeparator(name[1]) && + name[2] == '.' && + IsPathSeparator(name[3])) { + + *win32_name++ = '\\'; + *win32_name++ = '\\'; + *win32_name++ = '.'; + *win32_name++ = '\\'; + + name += 4; + } else if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS && + g_pVSSPathConvert == NULL) { + /* allow path to be 32767 bytes */ + *win32_name++ = '\\'; + *win32_name++ = '\\'; + *win32_name++ = '?'; + *win32_name++ = '\\'; + } + while (*name) { /* Check for Unix separator and convert to Win32 */ if (name[0] == '/' && name[1] == '/') { /* double slash? */ @@ -97,7 +138,7 @@ void conv_unix_to_win32_path(const char *name, char *win32_name, DWORD dwSize) } if (*name == '/') { *win32_name++ = '\\'; /* convert char */ - /* If Win32 separated that is "quoted", remove quote */ + /* If Win32 separator that is "quoted", remove quote */ } else if (*name == '\\' && name[1] == '\\') { *win32_name++ = '\\'; name++; /* skip first \ */ @@ -114,54 +155,72 @@ void conv_unix_to_win32_path(const char *name, char *win32_name, DWORD dwSize) *win32_name = 0; } -#ifdef WIN32_VSS /* here we convert to VSS specific file name which can get longer because VSS will make something like \\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\bacula\\uninstall.exe from c:\bacula\uninstall.exe */ - if (g_pVSSClient && enable_vss && g_pVSSClient->IsInitialized()) { + Dmsg1(100, "path=%s\n", tname); + if (g_pVSSPathConvert != NULL) { POOLMEM *pszBuf = get_pool_memory (PM_FNAME); pszBuf = check_pool_memory_size(pszBuf, dwSize); bstrncpy(pszBuf, tname, strlen(tname)+1); - g_pVSSClient->GetShadowPath(pszBuf, tname, dwSize); + g_pVSSPathConvert(pszBuf, tname, dwSize); free_pool_memory(pszBuf); } -#endif + + Dmsg1(100, "Leave cvt_u_to_win32_path path=%s\n", tname); +} + +/* Conversion of a Unix filename to a Win32 filename */ +void unix_name_to_win32(POOLMEM **win32_name, char *name) +{ + /* One extra byte should suffice, but we double it */ + /* add MAX_PATH bytes for VSS shadow copy name */ + DWORD dwSize = 2*strlen(name)+MAX_PATH; + *win32_name = check_pool_memory_size(*win32_name, dwSize); + conv_unix_to_win32_path(name, *win32_name, dwSize); } POOLMEM* make_wchar_win32_path(POOLMEM *pszUCSPath, BOOL *pBIsRawPath /*= NULL*/) { /* created 02/27/2006 Thorsten Engel - - This function expects an UCS-encoded standard wchar_t in pszUCSPath and - will complete the input path to an absolue path of the form \\?\c:\path\file - - With this trick, it is possible to have 32K characters long paths. - - Optionally one can use pBIsRawPath to determine id pszUCSPath contains a path - to a raw windows partition */ + * + * This function expects an UCS-encoded standard wchar_t in pszUCSPath and + * will complete the input path to an absolue path of the form \\?\c:\path\file + * + * With this trick, it is possible to have 32K characters long paths. + * + * Optionally one can use pBIsRawPath to determine id pszUCSPath contains a path + * to a raw windows partition. + */ - if (pBIsRawPath) - *pBIsRawPath = FALSE; + Dmsg0(100, "Enter wchar_win32_path\n"); + if (pBIsRawPath) { + *pBIsRawPath = FALSE; /* Initialize, set later */ + } - if (!p_GetCurrentDirectoryW) + if (!p_GetCurrentDirectoryW) { + Dmsg0(100, "Leave wchar_win32_path no change \n"); return pszUCSPath; + } - wchar_t *name = (wchar_t *) pszUCSPath; + wchar_t *name = (wchar_t *)pszUCSPath; /* if it has already the desired form, exit without changes */ - if (wcslen(name) > 3 && wcsncmp(name, L"\\\\?\\", 4) == 0) + if (wcslen(name) > 3 && wcsncmp(name, L"\\\\?\\", 4) == 0) { + Dmsg0(100, "Leave wchar_win32_path no change \n"); return pszUCSPath; + } - POOLMEM *pwszBuf = get_pool_memory(PM_FNAME); - POOLMEM *pwszCurDirBuf = get_pool_memory(PM_FNAME); + wchar_t *pwszBuf = (wchar_t *)get_pool_memory(PM_FNAME); + wchar_t *pwszCurDirBuf = (wchar_t *)get_pool_memory(PM_FNAME); DWORD dwCurDirPathSize = 0; /* get buffer with enough size (name+max 6. wchars+1 null terminator */ DWORD dwBufCharsNeeded = (wcslen(name)+7); - pwszBuf = check_pool_memory_size(pwszBuf, dwBufCharsNeeded*sizeof(wchar_t)); + pwszBuf = (wchar_t *)check_pool_memory_size((POOLMEM *)pwszBuf, dwBufCharsNeeded*sizeof(wchar_t)); /* add \\?\ to support 32K long filepaths it is important to make absolute paths, so we add drive and @@ -172,28 +231,31 @@ make_wchar_win32_path(POOLMEM *pszUCSPath, BOOL *pBIsRawPath /*= NULL*/) BOOL bAddPrefix = TRUE; /* does path begin with drive? if yes, it is absolute */ - if (wcslen(name) >= 3 && (iswalpha (*name) && *(name+1) == ':' - && (*(name+2) == '\\' || *(name+2) == '/'))) { + if (iswalpha(name[0]) && name[1] == ':' && IsPathSeparator(name[2])) { bAddDrive = FALSE; bAddCurrentPath = FALSE; } /* is path absolute? */ - if (*name == '/' || *name == '\\') + if (IsPathSeparator(name[0])) bAddCurrentPath = FALSE; /* is path relative to itself?, if yes, skip ./ */ - if (wcslen(name) > 2 && ((wcsncmp(name, L"./", 2) == 0) || (wcsncmp(name, L".\\", 2) == 0))) { - name+=2; + if (name[0] == '.' && IsPathSeparator(name[1])) { + name += 2; } - /* is path of form '//./'? */ - if (wcslen(name) > 3 && ((wcsncmp(name, L"//./", 4) == 0) || (wcsncmp(name, L"\\\\.\\", 4) == 0))) { + /* is path of form '//./'? */ + if (IsPathSeparator(name[0]) && + IsPathSeparator(name[1]) && + name[2] == '.' && + IsPathSeparator(name[3])) { bAddDrive = FALSE; bAddCurrentPath = FALSE; bAddPrefix = FALSE; - if (pBIsRawPath) + if (pBIsRawPath) { *pBIsRawPath = TRUE; + } } int nParseOffset = 0; @@ -201,7 +263,7 @@ make_wchar_win32_path(POOLMEM *pszUCSPath, BOOL *pBIsRawPath /*= NULL*/) /* add 4 bytes header */ if (bAddPrefix) { nParseOffset = 4; - wcscpy ((wchar_t *) pwszBuf,L"\\\\?\\"); + wcscpy(pwszBuf, L"\\\\?\\"); } /* get current path if needed */ @@ -209,11 +271,9 @@ make_wchar_win32_path(POOLMEM *pszUCSPath, BOOL *pBIsRawPath /*= NULL*/) dwCurDirPathSize = p_GetCurrentDirectoryW(0, NULL); if (dwCurDirPathSize > 0) { /* get directory into own buffer as it may either return c:\... or \\?\C:\.... */ - pwszCurDirBuf = check_pool_memory_size(pwszCurDirBuf, (dwCurDirPathSize+1)*sizeof(wchar_t)); - p_GetCurrentDirectoryW(dwCurDirPathSize,(wchar_t *)pwszCurDirBuf); - } - else - { + pwszCurDirBuf = (wchar_t *)check_pool_memory_size((POOLMEM *)pwszCurDirBuf, (dwCurDirPathSize+1)*sizeof(wchar_t)); + p_GetCurrentDirectoryW(dwCurDirPathSize, pwszCurDirBuf); + } else { /* we have no info for doing so */ bAddDrive = FALSE; bAddCurrentPath = FALSE; @@ -225,16 +285,21 @@ make_wchar_win32_path(POOLMEM *pszUCSPath, BOOL *pBIsRawPath /*= NULL*/) if (bAddDrive && !bAddCurrentPath) { wchar_t szDrive[3]; - if (dwCurDirPathSize > 3 && wcsncmp((LPCWSTR)pwszCurDirBuf, L"\\\\?\\", 4) == 0) + if (IsPathSeparator(pwszCurDirBuf[0]) && + IsPathSeparator(pwszCurDirBuf[1]) && + pwszCurDirBuf[2] == '?' && + IsPathSeparator(pwszCurDirBuf[3])) { /* copy drive character */ - wcsncpy((wchar_t *) szDrive, (LPCWSTR)pwszCurDirBuf+4,2); - else + szDrive[0] = pwszCurDirBuf[4]; + } else { /* copy drive character */ - wcsncpy((wchar_t *) szDrive, (LPCWSTR)pwszCurDirBuf,2); + szDrive[0] = pwszCurDirBuf[0]; + } + szDrive[1] = ':'; szDrive[2] = 0; - - wcscat((wchar_t *) pwszBuf, szDrive); + + wcscat(pwszBuf, szDrive); nParseOffset +=2; } @@ -242,74 +307,81 @@ make_wchar_win32_path(POOLMEM *pszUCSPath, BOOL *pBIsRawPath /*= NULL*/) if (bAddCurrentPath) { /* the 1 add. character is for the eventually added backslash */ dwBufCharsNeeded += dwCurDirPathSize+1; - pwszBuf = check_pool_memory_size(pwszBuf, dwBufCharsNeeded*sizeof(wchar_t)); + pwszBuf = (wchar_t *)check_pool_memory_size((POOLMEM *)pwszBuf, dwBufCharsNeeded*sizeof(wchar_t)); /* get directory into own buffer as it may either return c:\... or \\?\C:\.... */ - if (dwCurDirPathSize > 3 && wcsncmp((LPCWSTR)pwszCurDirBuf, L"\\\\?\\", 4) == 0) + if (IsPathSeparator(pwszCurDirBuf[0]) && + IsPathSeparator(pwszCurDirBuf[1]) && + pwszCurDirBuf[2] == '?' && + IsPathSeparator(pwszCurDirBuf[3])) { /* copy complete string */ - wcscpy((wchar_t *) pwszBuf, (LPCWSTR)pwszCurDirBuf); - else + wcscpy(pwszBuf, pwszCurDirBuf); + } else { /* append path */ - wcscat((wchar_t *) pwszBuf, (LPCWSTR)pwszCurDirBuf); + wcscat(pwszBuf, pwszCurDirBuf); + } - nParseOffset = wcslen ((LPCWSTR) pwszBuf); + nParseOffset = wcslen((LPCWSTR) pwszBuf); /* check if path ends with backslash, if not, add one */ - if (*((wchar_t *) pwszBuf+nParseOffset-1) != L'\\') { - wcscat((wchar_t *) pwszBuf, L"\\"); + if (!IsPathSeparator(pwszBuf[nParseOffset-1])) { + wcscat(pwszBuf, L"\\"); nParseOffset++; - } + } } - - wchar_t *win32_name = (wchar_t *)pwszBuf+nParseOffset; + wchar_t *win32_name = &pwszBuf[nParseOffset]; + wchar_t *name_start = name; while (*name) { - /* Check for Unix separator and convert to Win32 */ - if (*name == '/') { + /* Check for Unix separator and convert to Win32, eliminating + * duplicate separators. + */ + if (IsPathSeparator(*name)) { *win32_name++ = '\\'; /* convert char */ - /* If Win32 separated that is "quoted", remove quote */ -/* HELPME (Thorsten Engel): I don't understand the following part - and it removes a backslash from e.g. "\\.\c:" which I need for - RAW device access. So I took it out */ -/* } else if (*name == '\\' && name[1] == '\\') { - *win32_name++ = '\\'; - name++; */ /* skip first \ */ + + /* Eliminate consecutive slashes, but not at the start so that + * \\.\ still works. + */ + if (name_start != name && IsPathSeparator(name[1])) { + name++; + } } else { *win32_name++ = *name; /* copy character */ } name++; } - + /* null terminate string */ *win32_name = 0; -#ifdef WIN32_VSS /* here we convert to VSS specific file name which - can get longer because VSS will make something like - \\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\bacula\\uninstall.exe - from c:\bacula\uninstall.exe + * can get longer because VSS will make something like + * \\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy1\\bacula\\uninstall.exe + * from c:\bacula\uninstall.exe */ - if (g_pVSSClient && enable_vss && g_pVSSClient->IsInitialized()) { + if (g_pVSSPathConvertW != NULL) { /* is output buffer large enough? */ - pwszBuf = check_pool_memory_size(pwszBuf, (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t)); + pwszBuf = (wchar_t *)check_pool_memory_size((POOLMEM *)pwszBuf, + (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t)); /* create temp. buffer */ - POOLMEM* pszBuf = get_pool_memory(PM_FNAME); - pszBuf = check_pool_memory_size(pszBuf, (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t)); + wchar_t *pszBuf = (wchar_t *)get_pool_memory(PM_FNAME); + pszBuf = (wchar_t *)check_pool_memory_size((POOLMEM *)pszBuf, + (dwBufCharsNeeded+MAX_PATH)*sizeof(wchar_t)); if (bAddPrefix) nParseOffset = 4; else nParseOffset = 0; - wcsncpy((wchar_t *) pszBuf, (wchar_t *) pwszBuf+nParseOffset, wcslen((wchar_t *)pwszBuf)+1-nParseOffset); - g_pVSSClient->GetShadowPathW((wchar_t *)pszBuf,(wchar_t *)pwszBuf,dwBufCharsNeeded+MAX_PATH); - free_pool_memory(pszBuf); + wcsncpy(pszBuf, &pwszBuf[nParseOffset], wcslen(pwszBuf)+1-nParseOffset); + g_pVSSPathConvertW(pszBuf, pwszBuf, dwBufCharsNeeded+MAX_PATH); + free_pool_memory((POOLMEM *)pszBuf); } -#endif free_pool_memory(pszUCSPath); - free_pool_memory(pwszCurDirBuf); + free_pool_memory((POOLMEM *)pwszCurDirBuf); - return pwszBuf; + Dmsg1(100, "Leave wchar_win32_path=%s\n", pwszBuf); + return (POOLMEM *)pwszBuf; } int @@ -412,35 +484,64 @@ make_win32_path_UTF8_2_wchar(POOLMEM **pszUCS, const char *pszUTF, BOOL* pBIsRaw return nRet; } -#ifndef HAVE_VC8 +#if !defined(_MSC_VER) || (_MSC_VER < 1400) // VC8+ int umask(int) { return 0; } #endif -int chmod(const char *, mode_t) +#ifndef LOAD_WITH_ALTERED_SEARCH_PATH +#define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008 +#endif + +void *dlopen(const char *file, int mode) { - return 0; + void *handle; + + handle = LoadLibraryEx(file, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + return handle; } -int chown(const char *k, uid_t, gid_t) +void *dlsym(void *handle, const char *name) { - return 0; + void *symaddr; + symaddr = (void *)GetProcAddress((HMODULE)handle, name); + return symaddr; } -int lchown(const char *k, uid_t, gid_t) +int dlclose(void *handle) +{ + if (handle && !FreeLibrary((HMODULE)handle)) { + errno = b_errno_win32; + return 1; /* failed */ + } + return 0; /* OK */ +} + +char *dlerror(void) +{ + static char buf[200]; + const char *err = errorString(); + bstrncpy(buf, (char *)err, sizeof(buf)); + LocalFree((void *)err); + return buf; +} + +int fcntl(int fd, int cmd) { return 0; } -#ifdef needed -bool fstype(const char *fname, char *fs, int fslen) +int chown(const char *k, uid_t, gid_t) { - return true; /* accept anything */ + return 0; } -#endif +int lchown(const char *k, uid_t, gid_t) +{ + return 0; +} long int random(void) @@ -459,16 +560,16 @@ srandom(unsigned int seed) void cvt_utime_to_ftime(const time_t &time, FILETIME &wintime) { - uint64_t mstime = time; - mstime *= WIN32_FILETIME_SCALE; - mstime += WIN32_FILETIME_ADJUST; + uint64_t mstime = time; + mstime *= WIN32_FILETIME_SCALE; + mstime += WIN32_FILETIME_ADJUST; - #ifdef HAVE_MINGW - wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffUL); - #else - wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64); - #endif - wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL); +#if defined(_MSC_VER) + wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffI64); +#else + wintime.dwLowDateTime = (DWORD)(mstime & 0xffffffffUL); +#endif + wintime.dwHighDateTime = (DWORD) ((mstime>>32)& 0xffffffffUL); } time_t @@ -484,8 +585,7 @@ cvt_ftime_to_utime(const FILETIME &time) return (time_t) (mstime & 0xffffffff); } -static const char * -errorString(void) +static const char *errorString(void) { LPVOID lpMsgBuf; @@ -520,168 +620,221 @@ statDir(const char *file, struct stat *sb) WIN32_FIND_DATAA info_a; // window's file info // cache some common vars to make code more transparent - DWORD* pdwFileAttributes; - DWORD* pnFileSizeHigh; - DWORD* pnFileSizeLow; - FILETIME* pftLastAccessTime; - FILETIME* pftLastWriteTime; - FILETIME* pftCreationTime; - + DWORD *pdwFileAttributes; + DWORD *pnFileSizeHigh; + DWORD *pnFileSizeLow; + DWORD *pdwReserved0; + FILETIME *pftLastAccessTime; + FILETIME *pftLastWriteTime; + FILETIME *pftCreationTime; + + /* + * Oh, cool, another exception: Microsoft doesn't let us do + * FindFile operations on a Drive, so simply fake root attibutes. + */ if (file[1] == ':' && file[2] == 0) { - d_msg(__FILE__, __LINE__, 99, "faking ROOT attrs(%s).\n", file); - sb->st_mode = S_IFDIR; - sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE; - time(&sb->st_ctime); - time(&sb->st_mtime); - time(&sb->st_atime); - return 0; + time_t now = time(NULL); + Dmsg1(99, "faking ROOT attrs(%s).\n", file); + sb->st_mode = S_IFDIR; + sb->st_mode |= S_IREAD|S_IEXEC|S_IWRITE; + sb->st_ctime = now; + sb->st_mtime = now; + sb->st_atime = now; + sb->st_rdev = 0; + return 0; } HANDLE h = INVALID_HANDLE_VALUE; - // use unicode or ascii + // use unicode if (p_FindFirstFileW) { POOLMEM* pwszBuf = get_pool_memory (PM_FNAME); make_win32_path_UTF8_2_wchar(&pwszBuf, file); - h = p_FindFirstFileW((LPCWSTR) pwszBuf, &info_w); + Dmsg1(100, "FindFirstFileW=%s\n", file); + h = p_FindFirstFileW((LPCWSTR)pwszBuf, &info_w); free_pool_memory(pwszBuf); pdwFileAttributes = &info_w.dwFileAttributes; + pdwReserved0 = &info_w.dwReserved0; pnFileSizeHigh = &info_w.nFileSizeHigh; pnFileSizeLow = &info_w.nFileSizeLow; pftLastAccessTime = &info_w.ftLastAccessTime; pftLastWriteTime = &info_w.ftLastWriteTime; pftCreationTime = &info_w.ftCreationTime; - } - else if (p_FindFirstFileA) { + + // use ASCII + } else if (p_FindFirstFileA) { + Dmsg1(100, "FindFirstFileA=%s\n", file); h = p_FindFirstFileA(file, &info_a); pdwFileAttributes = &info_a.dwFileAttributes; + pdwReserved0 = &info_a.dwReserved0; pnFileSizeHigh = &info_a.nFileSizeHigh; pnFileSizeLow = &info_a.nFileSizeLow; pftLastAccessTime = &info_a.ftLastAccessTime; pftLastWriteTime = &info_a.ftLastWriteTime; pftCreationTime = &info_a.ftCreationTime; + } else { + Dmsg0(100, "No findFirstFile A or W found\n"); } - if (h == INVALID_HANDLE_VALUE) { - const char *err = errorString(); - d_msg(__FILE__, __LINE__, 99, "FindFirstFile(%s):%s\n", file, err); - LocalFree((void *)err); - errno = b_errno_win32; - return -1; - } + if (h == INVALID_HANDLE_VALUE) { + const char *err = errorString(); + /* + * Note, in creating leading paths, it is normal that + * the file does not exist. + */ + Dmsg2(2099, "FindFirstFile(%s):%s\n", file, err); + LocalFree((void *)err); + errno = b_errno_win32; + return -1; + } else { + FindClose(h); + } - sb->st_mode = 0777; /* start with everything */ - if (*pdwFileAttributes & FILE_ATTRIBUTE_READONLY) - sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH); - if (*pdwFileAttributes & FILE_ATTRIBUTE_SYSTEM) - sb->st_mode &= ~S_IRWXO; /* remove everything for other */ - if (*pdwFileAttributes & FILE_ATTRIBUTE_HIDDEN) - sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */ - sb->st_mode |= S_IFDIR; - - sb->st_size = *pnFileSizeHigh; - sb->st_size <<= 32; - sb->st_size |= *pnFileSizeLow; - sb->st_blksize = 4096; - sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096; - - sb->st_atime = cvt_ftime_to_utime(*pftLastAccessTime); - sb->st_mtime = cvt_ftime_to_utime(*pftLastWriteTime); - sb->st_ctime = cvt_ftime_to_utime(*pftCreationTime); - FindClose(h); + sb->st_mode = 0777; /* start with everything */ + if (*pdwFileAttributes & FILE_ATTRIBUTE_READONLY) + sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH); + if (*pdwFileAttributes & FILE_ATTRIBUTE_SYSTEM) + sb->st_mode &= ~S_IRWXO; /* remove everything for other */ + if (*pdwFileAttributes & FILE_ATTRIBUTE_HIDDEN) + sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */ + sb->st_mode |= S_IFDIR; + + /* + * Store reparse/mount point info in st_rdev. Note a + * Win32 reparse point (junction point) is like a link + * though it can have many properties (directory link, + * soft link, hard link, HSM, ... + * A mount point is a reparse point where another volume + * is mounted, so it is like a Unix mount point (change of + * filesystem). + */ + if (*pdwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { + if (*pdwReserved0 & IO_REPARSE_TAG_MOUNT_POINT) { + sb->st_rdev = WIN32_MOUNT_POINT; /* mount point */ + } else { + sb->st_rdev = WIN32_REPARSE_POINT; /* reparse point */ + } + } + Dmsg2(100, "st_rdev=%d file=%s\n", sb->st_rdev, file); + sb->st_size = *pnFileSizeHigh; + sb->st_size <<= 32; + sb->st_size |= *pnFileSizeLow; + sb->st_blksize = 4096; + sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096; - return 0; + sb->st_atime = cvt_ftime_to_utime(*pftLastAccessTime); + sb->st_mtime = cvt_ftime_to_utime(*pftLastWriteTime); + sb->st_ctime = cvt_ftime_to_utime(*pftCreationTime); + + return 0; +} + +int +fstat(intptr_t fd, struct stat *sb) +{ + BY_HANDLE_FILE_INFORMATION info; + + if (!GetFileInformationByHandle((HANDLE)_get_osfhandle(fd), &info)) { + const char *err = errorString(); + Dmsg1(2099, "GetfileInformationByHandle: %s\n", err); + LocalFree((void *)err); + errno = b_errno_win32; + return -1; + } + + sb->st_dev = info.dwVolumeSerialNumber; + sb->st_ino = info.nFileIndexHigh; + sb->st_ino <<= 32; + sb->st_ino |= info.nFileIndexLow; + sb->st_nlink = (short)info.nNumberOfLinks; + if (sb->st_nlink > 1) { + Dmsg1(99, "st_nlink=%d\n", sb->st_nlink); + } + + sb->st_mode = 0777; /* start with everything */ + if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) + sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH); + if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) + sb->st_mode &= ~S_IRWXO; /* remove everything for other */ + if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) + sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */ + sb->st_mode |= S_IFREG; + + /* Use st_rdev to store reparse attribute */ + if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { + sb->st_rdev = WIN32_REPARSE_POINT; + } + Dmsg3(100, "st_rdev=%d sizino=%d ino=%lld\n", sb->st_rdev, sizeof(sb->st_ino), + (long long)sb->st_ino); + + sb->st_size = info.nFileSizeHigh; + sb->st_size <<= 32; + sb->st_size |= info.nFileSizeLow; + sb->st_blksize = 4096; + sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096; + sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime); + sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime); + sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime); + + return 0; } static int stat2(const char *file, struct stat *sb) { - BY_HANDLE_FILE_INFORMATION info; - HANDLE h; - int rval = 0; - char tmpbuf[1024]; - conv_unix_to_win32_path(file, tmpbuf, 1024); + HANDLE h = INVALID_HANDLE_VALUE; + int rval = 0; + char tmpbuf[5000]; + conv_unix_to_win32_path(file, tmpbuf, 5000); - DWORD attr = (DWORD)-1; + DWORD attr = (DWORD)-1; - if (p_GetFileAttributesW) { + if (p_GetFileAttributesW) { POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); make_win32_path_UTF8_2_wchar(&pwszBuf, tmpbuf); attr = p_GetFileAttributesW((LPCWSTR) pwszBuf); + if (p_CreateFileW) { + h = CreateFileW((LPCWSTR)pwszBuf, GENERIC_READ, + FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + } free_pool_memory(pwszBuf); - } else if (p_GetFileAttributesA) { - attr = p_GetFileAttributesA(tmpbuf); - } - - if (attr == -1) { - const char *err = errorString(); - d_msg(__FILE__, __LINE__, 99, - "GetFileAttributes(%s): %s\n", tmpbuf, err); - LocalFree((void *)err); - errno = b_errno_win32; - return -1; - } - - if (attr & FILE_ATTRIBUTE_DIRECTORY) - return statDir(tmpbuf, sb); - - h = CreateFileA(tmpbuf, GENERIC_READ, - FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + } else if (p_GetFileAttributesA) { + attr = p_GetFileAttributesA(tmpbuf); + h = CreateFileA(tmpbuf, GENERIC_READ, + FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + } - if (h == INVALID_HANDLE_VALUE) { - const char *err = errorString(); - d_msg(__FILE__, __LINE__, 99, - "Cannot open file for stat (%s):%s\n", tmpbuf, err); - LocalFree((void *)err); - rval = -1; - errno = b_errno_win32; - goto error; - } + if (attr == (DWORD)-1) { + const char *err = errorString(); + Dmsg2(2099, "GetFileAttributes(%s): %s\n", tmpbuf, err); + LocalFree((void *)err); + if (h != INVALID_HANDLE_VALUE) { + CloseHandle(h); + } + errno = b_errno_win32; + return -1; + } - if (!GetFileInformationByHandle(h, &info)) { - const char *err = errorString(); - d_msg(__FILE__, __LINE__, 99, - "GetfileInformationByHandle(%s): %s\n", tmpbuf, err); - LocalFree((void *)err); - rval = -1; - errno = b_errno_win32; - goto error; - } + if (h == INVALID_HANDLE_VALUE) { + const char *err = errorString(); + Dmsg2(2099, "Cannot open file for stat (%s):%s\n", tmpbuf, err); + LocalFree((void *)err); + errno = b_errno_win32; + return -1; + } - sb->st_dev = info.dwVolumeSerialNumber; - sb->st_ino = info.nFileIndexHigh; - sb->st_ino <<= 32; - sb->st_ino |= info.nFileIndexLow; - sb->st_nlink = (short)info.nNumberOfLinks; - if (sb->st_nlink > 1) { - d_msg(__FILE__, __LINE__, 99, "st_nlink=%d\n", sb->st_nlink); - } + rval = fstat((intptr_t)h, sb); + CloseHandle(h); - sb->st_mode = 0777; /* start with everything */ - if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) - sb->st_mode &= ~(S_IRUSR|S_IRGRP|S_IROTH); - if (info.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) - sb->st_mode &= ~S_IRWXO; /* remove everything for other */ - if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) - sb->st_mode |= S_ISVTX; /* use sticky bit -> hidden */ - sb->st_mode |= S_IFREG; - - sb->st_size = info.nFileSizeHigh; - sb->st_size <<= 32; - sb->st_size |= info.nFileSizeLow; - sb->st_blksize = 4096; - sb->st_blocks = (uint32_t)(sb->st_size + 4095)/4096; - sb->st_atime = cvt_ftime_to_utime(info.ftLastAccessTime); - sb->st_mtime = cvt_ftime_to_utime(info.ftLastWriteTime); - sb->st_ctime = cvt_ftime_to_utime(info.ftCreationTime); - -error: - CloseHandle(h); - return rval; + if (attr & FILE_ATTRIBUTE_DIRECTORY && + file[1] == ':' && file[2] != 0) { + rval = statDir(file, sb); + } + return rval; } int @@ -690,27 +843,20 @@ stat(const char *file, struct stat *sb) WIN32_FILE_ATTRIBUTE_DATA data; errno = 0; - memset(sb, 0, sizeof(*sb)); - /* why not allow win 95 to use p_GetFileAttributesExA ? - * this function allows _some_ open files to be stat'ed - * if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) { - * return stat2(file, sb); - * } - */ - if (p_GetFileAttributesExW) { /* dynamically allocate enough space for UCS2 filename */ - POOLMEM* pwszBuf = get_pool_memory (PM_FNAME); + POOLMEM *pwszBuf = get_pool_memory(PM_FNAME); make_win32_path_UTF8_2_wchar(&pwszBuf, file); - BOOL b = p_GetFileAttributesExW((LPCWSTR) pwszBuf, GetFileExInfoStandard, &data); + BOOL b = p_GetFileAttributesExW((LPCWSTR)pwszBuf, GetFileExInfoStandard, &data); free_pool_memory(pwszBuf); if (!b) { return stat2(file, sb); } + } else if (p_GetFileAttributesExA) { if (!p_GetFileAttributesExA(file, GetFileExInfoStandard, &data)) { return stat2(file, sb); @@ -735,6 +881,9 @@ stat(const char *file, struct stat *sb) sb->st_mode |= S_IFREG; } + /* Use st_rdev to store reparse attribute */ + sb->st_rdev = (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? 1 : 0; + sb->st_nlink = 1; sb->st_size = data.nFileSizeHigh; sb->st_size <<= 32; @@ -744,9 +893,71 @@ stat(const char *file, struct stat *sb) sb->st_atime = cvt_ftime_to_utime(data.ftLastAccessTime); sb->st_mtime = cvt_ftime_to_utime(data.ftLastWriteTime); sb->st_ctime = cvt_ftime_to_utime(data.ftCreationTime); + + /* + * If we are not at the root, then to distinguish a reparse + * point from a mount point, we must call FindFirstFile() to + * get the WIN32_FIND_DATA, which has the bit that indicates + * that this directory is a mount point -- aren't Win32 APIs + * wonderful? (sarcasm). The code exists in the statDir + * subroutine. + */ + if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && + file[1] == ':' && file[2] != 0) { + statDir(file, sb); + } + Dmsg3(100, "sizino=%d ino=%lld file=%s\n", sizeof(sb->st_ino), + (long long)sb->st_ino, file); return 0; } +/* + * We write our own ftruncate because the one in the + * Microsoft library mrcrt.dll does not truncate + * files greater than 2GB. + * KES - May 2007 + */ +int win32_ftruncate(int fd, int64_t length) +{ + /* Set point we want to truncate file */ + __int64 pos = _lseeki64(fd, (__int64)length, SEEK_SET); + + if (pos != (__int64)length) { + errno = EACCES; /* truncation failed, get out */ + return -1; + } + + /* Truncate file */ + if (SetEndOfFile((HANDLE)_get_osfhandle(fd)) == 0) { + errno = b_errno_win32; + return -1; + } + errno = 0; + return 0; +} + +int fcntl(int fd, int cmd, long arg) +{ + int rval = 0; + + switch (cmd) { + case F_GETFL: + rval = O_NONBLOCK; + break; + + case F_SETFL: + rval = 0; + break; + + default: + errno = EINVAL; + rval = -1; + break; + } + + return rval; +} + int lstat(const char *file, struct stat *sb) { @@ -827,23 +1038,24 @@ strcasecmp(const char *s1, const char *s2) int strncasecmp(const char *s1, const char *s2, int len) { - register int ch1, ch2; + register int ch1 = 0, ch2 = 0; - if (s1==s2) - return 0; /* strings are equal if same object. */ - else if (!s1) - return -1; - else if (!s2) - return 1; - while (len--) { - ch1 = *s1; - ch2 = *s2; - s1++; - s2++; - if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break; - } + if (s1==s2) + return 0; /* strings are equal if same object. */ + else if (!s1) + return -1; + else if (!s2) + return 1; + + while (len--) { + ch1 = *s1; + ch2 = *s2; + s1++; + s2++; + if (ch1 == 0 || tolower(ch1) != tolower(ch2)) break; + } - return (ch1 - ch2); + return (ch1 - ch2); } int @@ -851,6 +1063,7 @@ gettimeofday(struct timeval *tv, struct timezone *) { SYSTEMTIME now; FILETIME tmp; + GetSystemTime(&now); if (tv == NULL) { @@ -867,21 +1080,24 @@ gettimeofday(struct timeval *tv, struct timezone *) _100nsec |= tmp.dwLowDateTime; _100nsec -= WIN32_FILETIME_ADJUST; - tv->tv_sec =(long) (_100nsec / 10000000); - tv->tv_usec = (long) ((_100nsec % 10000000)/10); + tv->tv_sec = (long)(_100nsec / 10000000); + tv->tv_usec = (long)((_100nsec % 10000000)/10); return 0; } /* For apcupsd this is in src/lib/wincompat.c */ -#ifndef __APCUPSD__ extern "C" void syslog(int type, const char *fmt, ...) { /*#ifndef HAVE_CONSOLE MessageBox(NULL, msg, "Bacula", MB_OK); #endif*/ } -#endif + +void +closelog() +{ +} struct passwd * getpwuid(uid_t) @@ -919,28 +1135,15 @@ opendir(const char *path) return NULL; } + Dmsg1(100, "Opendir path=%s\n", path); rval = (_dir *)malloc(sizeof(_dir)); memset (rval, 0, sizeof (_dir)); if (rval == NULL) return NULL; char *tspec = (char *)malloc(max_len); if (tspec == NULL) return NULL; - if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) { -#ifdef WIN32_VSS - /* will append \\?\ at front itself */ - conv_unix_to_win32_path(path, tspec, max_len-4); -#else - /* allow path to be 32767 bytes */ - tspec[0] = '\\'; - tspec[1] = '\\'; - tspec[2] = '?'; - tspec[3] = '\\'; - tspec[4] = 0; - conv_unix_to_win32_path(path, tspec+4, max_len-4); -#endif - } else { - conv_unix_to_win32_path(path, tspec, max_len); - } + conv_unix_to_win32_path(path, tspec, max_len); + Dmsg1(100, "win32 path=%s\n", tspec); // add backslash only if there is none yet (think of c:\) if (tspec[strlen(tspec)-1] != '\\') @@ -953,7 +1156,7 @@ opendir(const char *path) // convert to wchar_t if (p_FindFirstFileW) { POOLMEM* pwcBuf = get_pool_memory(PM_FNAME);; - make_win32_path_UTF8_2_wchar(&pwcBuf,rval->spec); + make_win32_path_UTF8_2_wchar(&pwcBuf, rval->spec); rval->dirh = p_FindFirstFileW((LPCWSTR)pwcBuf, &rval->data_w); @@ -969,21 +1172,20 @@ opendir(const char *path) } else goto err; - d_msg(__FILE__, __LINE__, - 99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n", + Dmsg3(99, "opendir(%s)\n\tspec=%s,\n\tFindFirstFile returns %d\n", path, rval->spec, rval->dirh); rval->offset = 0; if (rval->dirh == INVALID_HANDLE_VALUE) goto err; - if (rval->valid_w) - d_msg(__FILE__, __LINE__, - 99, "\tFirstFile=%s\n", rval->data_w.cFileName); + if (rval->valid_w) { + Dmsg1(99, "\tFirstFile=%s\n", rval->data_w.cFileName); + } - if (rval->valid_a) - d_msg(__FILE__, __LINE__, - 99, "\tFirstFile=%s\n", rval->data_a.cFileName); + if (rval->valid_a) { + Dmsg1(99, "\tFirstFile=%s\n", rval->data_a.cFileName); + } return (DIR *)rval; @@ -1050,11 +1252,10 @@ readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) } *result = entry; /* return entry address */ - d_msg(__FILE__, __LINE__, - 99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n", + Dmsg4(99, "readdir_r(%p, { d_name=\"%s\", d_reclen=%d, d_off=%d\n", dirp, entry->d_name, entry->d_reclen, entry->d_off); } else { -// d_msg(__FILE__, __LINE__, 99, "readdir_r !valid\n"); +// Dmsg0(99, "readdir_r !valid\n"); errno = b_errno_win32; return -1; } @@ -1163,6 +1364,73 @@ WSA_Init(void) return 0; } +int win32_chmod(const char *path, mode_t mode) +{ + DWORD attr = (DWORD)-1; + + Dmsg1(100, "Enter win32_chmod. path=%s\n", path); + if (p_GetFileAttributesW) { + POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); + make_win32_path_UTF8_2_wchar(&pwszBuf, path); + + attr = p_GetFileAttributesW((LPCWSTR) pwszBuf); + if (attr != INVALID_FILE_ATTRIBUTES) { + /* Use Bacula mappings define in stat() above */ + if (mode & (S_IRUSR|S_IRGRP|S_IROTH)) { + attr |= FILE_ATTRIBUTE_READONLY; + } else { + attr &= ~FILE_ATTRIBUTE_READONLY; + } + if (mode & S_ISVTX) { + attr |= FILE_ATTRIBUTE_HIDDEN; + } else { + attr &= ~FILE_ATTRIBUTE_HIDDEN; + } + if (mode & S_IRWXO) { + attr |= FILE_ATTRIBUTE_SYSTEM; + } else { + attr &= ~FILE_ATTRIBUTE_SYSTEM; + } + attr = p_SetFileAttributesW((LPCWSTR)pwszBuf, attr); + } + free_pool_memory(pwszBuf); + Dmsg0(100, "Leave win32_chmod. AttributesW\n"); + } else if (p_GetFileAttributesA) { + if (mode & (S_IRUSR|S_IRGRP|S_IROTH)) { + attr |= FILE_ATTRIBUTE_READONLY; + } else { + attr &= ~FILE_ATTRIBUTE_READONLY; + } + if (mode & S_ISVTX) { + attr |= FILE_ATTRIBUTE_HIDDEN; + } else { + attr &= ~FILE_ATTRIBUTE_HIDDEN; + } + if (mode & S_IRWXO) { + attr |= FILE_ATTRIBUTE_SYSTEM; + } else { + attr &= ~FILE_ATTRIBUTE_SYSTEM; + } + attr = p_GetFileAttributesA(path); + if (attr != INVALID_FILE_ATTRIBUTES) { + attr = p_SetFileAttributesA(path, attr); + } + Dmsg0(100, "Leave win32_chmod did AttributesA\n"); + } else { + Dmsg0(100, "Leave win32_chmod did nothing\n"); + } + + + if (attr == (DWORD)-1) { + const char *err = errorString(); + Dmsg2(99, "Get/SetFileAttributes(%s): %s\n", path, err); + LocalFree((void *)err); + errno = b_errno_win32; + return -1; + } + return 0; +} + int win32_chdir(const char *dir) @@ -1179,14 +1447,14 @@ win32_chdir(const char *dir) errno = b_errno_win32; return -1; } - } - else if (p_SetCurrentDirectoryA) { + } else if (p_SetCurrentDirectoryA) { if (0 == p_SetCurrentDirectoryA(dir)) { errno = b_errno_win32; return -1; } + } else { + return -1; } - else return -1; return 0; } @@ -1194,15 +1462,18 @@ win32_chdir(const char *dir) int win32_mkdir(const char *dir) { + Dmsg1(100, "enter win32_mkdir. dir=%s\n", dir); if (p_wmkdir){ POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); make_win32_path_UTF8_2_wchar(&pwszBuf, dir); int n = p_wmkdir((LPCWSTR)pwszBuf); free_pool_memory(pwszBuf); + Dmsg0(100, "Leave win32_mkdir did wmkdir\n"); return n; } + Dmsg0(100, "Leave win32_mkdir did _mkdir\n"); return _mkdir(dir); } @@ -1263,15 +1534,16 @@ win32_fputs(const char *string, FILE *stream) POOLMEM* pszBuf = get_pool_memory(PM_MESSAGE); pszBuf = check_pool_memory_size(pszBuf, dwChars+1); - dwChars = p_WideCharToMultiByte(GetConsoleOutputCP(),0,(LPCWSTR) pwszBuf,-1,pszBuf,dwChars,NULL,NULL); + dwChars = p_WideCharToMultiByte(GetConsoleOutputCP(),0,(LPCWSTR)pwszBuf,-1,pszBuf,dwChars,NULL,NULL); free_pool_memory(pwszBuf); if (WriteConsoleA (hOut, pszBuf, dwChars-1, &dwCharsWritten, NULL)) { free_pool_memory(pszBuf); return dwCharsWritten; } + free_pool_memory(pszBuf); } - + /* Fall back */ return fputs(string, stream); } @@ -1344,8 +1616,10 @@ win32_unlink(const char *filename) nRetCode = _wunlink((LPCWSTR) pwszBuf); - /* special case if file is readonly, - we retry but unset attribute before */ + /* + * special case if file is readonly, + * we retry but unset attribute before + */ if (nRetCode == -1 && errno == EACCES && p_SetFileAttributesW && p_GetFileAttributesW) { DWORD dwAttr = p_GetFileAttributesW((LPCWSTR)pwszBuf); if (dwAttr != INVALID_FILE_ATTRIBUTES) { @@ -1409,7 +1683,7 @@ winver::winver(void) osvinfo.dwMajorVersion, osvinfo.dwMinorVersion); snprintf(WIN_RAWVERSION, sizeof(WIN_RAWVERSION), "Windows %#08x", ver); - switch (ver) + switch (ver) { case MS_WINDOWS_95: (version = "Windows 95"); break; case MS_WINDOWS_98: (version = "Windows 98"); break; @@ -1422,7 +1696,7 @@ winver::winver(void) } bstrncpy(WIN_VERSION_LONG, version, sizeof(WIN_VERSION_LONG)); - snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %d.%d.%d", + snprintf(WIN_VERSION, sizeof(WIN_VERSION), "%s %lu.%lu.%lu", platform, osvinfo.dwMajorVersion, osvinfo.dwMinorVersion, osvinfo.dwBuildNumber); #if 0 @@ -1480,96 +1754,340 @@ getArgv0(const char *cmdline) return rval; } +/* + * Extracts the executable or script name from the first string in + * cmdline. + * + * If the name contains blanks then it must be quoted with double quotes, + * otherwise quotes are optional. If the name contains blanks then it + * will be converted to a short name. + * + * The optional quotes will be removed. The result is copied to a malloc'ed + * buffer and returned through the pexe argument. The pargs parameter is set + * to the address of the character in cmdline located after the name. + * + * The malloc'ed buffer returned in *pexe must be freed by the caller. + */ +bool +GetApplicationName(const char *cmdline, char **pexe, const char **pargs) +{ + const char *pExeStart = NULL; /* Start of executable name in cmdline */ + const char *pExeEnd = NULL; /* Character after executable name (separator) */ + + const char *pBasename = NULL; /* Character after last path separator */ + const char *pExtension = NULL; /* Period at start of extension */ + + const char *current = cmdline; + + bool bQuoted = false; + + /* Skip initial whitespace */ + + while (*current == ' ' || *current == '\t') + { + current++; + } + + /* Calculate start of name and determine if quoted */ + + if (*current == '"') { + pExeStart = ++current; + bQuoted = true; + } else { + pExeStart = current; + bQuoted = false; + } + + *pargs = NULL; + *pexe = NULL; + + /* + * Scan command line looking for path separators (/ and \\) and the + * terminator, either a quote or a blank. The location of the + * extension is also noted. + */ + + for ( ; *current != '\0'; current++) + { + if (*current == '.') { + pExtension = current; + } else if (IsPathSeparator(*current) && current[1] != '\0') { + pBasename = ¤t[1]; + pExtension = NULL; + } + + /* Check for terminator, either quote or blank */ + if (bQuoted) { + if (*current != '"') { + continue; + } + } else { + if (*current != ' ') { + continue; + } + } + + /* + * Hit terminator, remember end of name (address of terminator) and + * start of arguments + */ + pExeEnd = current; + + if (bQuoted && *current == '"') { + *pargs = ¤t[1]; + } else { + *pargs = current; + } + + break; + } + + if (pBasename == NULL) { + pBasename = pExeStart; + } + + if (pExeEnd == NULL) { + pExeEnd = current; + } + + if (*pargs == NULL) + { + *pargs = current; + } + + bool bHasPathSeparators = pExeStart != pBasename; + + /* We have pointers to all the useful parts of the name */ + + /* Default extensions in the order cmd.exe uses to search */ + + static const char ExtensionList[][5] = { ".com", ".exe", ".bat", ".cmd" }; + DWORD dwBasePathLength = pExeEnd - pExeStart; + + DWORD dwAltNameLength = 0; + char *pPathname = (char *)alloca(MAX_PATHLENGTH + 1); + char *pAltPathname = (char *)alloca(MAX_PATHLENGTH + 1); + + pPathname[MAX_PATHLENGTH] = '\0'; + pAltPathname[MAX_PATHLENGTH] = '\0'; + + memcpy(pPathname, pExeStart, dwBasePathLength); + pPathname[dwBasePathLength] = '\0'; + + if (pExtension == NULL) { + /* Try appending extensions */ + for (int index = 0; index < (int)(sizeof(ExtensionList) / sizeof(ExtensionList[0])); index++) { + + if (!bHasPathSeparators) { + /* There are no path separators, search in the standard locations */ + dwAltNameLength = SearchPath(NULL, pPathname, ExtensionList[index], MAX_PATHLENGTH, pAltPathname, NULL); + if (dwAltNameLength > 0 && dwAltNameLength <= MAX_PATHLENGTH) { + memcpy(pPathname, pAltPathname, dwAltNameLength); + pPathname[dwAltNameLength] = '\0'; + break; + } + } else { + bstrncpy(&pPathname[dwBasePathLength], ExtensionList[index], MAX_PATHLENGTH - dwBasePathLength); + if (GetFileAttributes(pPathname) != INVALID_FILE_ATTRIBUTES) { + break; + } + pPathname[dwBasePathLength] = '\0'; + } + } + } else if (!bHasPathSeparators) { + /* There are no path separators, search in the standard locations */ + dwAltNameLength = SearchPath(NULL, pPathname, NULL, MAX_PATHLENGTH, pAltPathname, NULL); + if (dwAltNameLength > 0 && dwAltNameLength < MAX_PATHLENGTH) { + memcpy(pPathname, pAltPathname, dwAltNameLength); + pPathname[dwAltNameLength] = '\0'; + } + } + + if (strchr(pPathname, ' ') != NULL) { + dwAltNameLength = GetShortPathName(pPathname, pAltPathname, MAX_PATHLENGTH); + + if (dwAltNameLength > 0 && dwAltNameLength <= MAX_PATHLENGTH) { + *pexe = (char *)malloc(dwAltNameLength + 1); + if (*pexe == NULL) { + return false; + } + memcpy(*pexe, pAltPathname, dwAltNameLength + 1); + } + } + + if (*pexe == NULL) { + DWORD dwPathnameLength = strlen(pPathname); + *pexe = (char *)malloc(dwPathnameLength + 1); + if (*pexe == NULL) { + return false; + } + memcpy(*pexe, pPathname, dwPathnameLength + 1); + } + + return true; +} + +/** + * Create the process with WCHAR API + */ +static BOOL +CreateChildProcessW(const char *comspec, const char *cmdLine, + PROCESS_INFORMATION *hProcInfo, + HANDLE in, HANDLE out, HANDLE err) +{ + STARTUPINFOW siStartInfo; + BOOL bFuncRetn = FALSE; + + // Set up members of the STARTUPINFO structure. + ZeroMemory( &siStartInfo, sizeof(siStartInfo) ); + siStartInfo.cb = sizeof(siStartInfo); + // setup new process to use supplied handles for stdin,stdout,stderr + + siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; + siStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; + + siStartInfo.hStdInput = in; + siStartInfo.hStdOutput = out; + siStartInfo.hStdError = err; + + // Convert argument to WCHAR + POOLMEM *cmdLine_wchar = get_pool_memory(PM_FNAME); + POOLMEM *comspec_wchar = get_pool_memory(PM_FNAME); + + UTF8_2_wchar(&cmdLine_wchar, cmdLine); + UTF8_2_wchar(&comspec_wchar, comspec); + + // Create the child process. + Dmsg2(150, "Calling CreateProcess(%s, %s, ...)\n", comspec_wchar, cmdLine_wchar); + + // try to execute program + bFuncRetn = p_CreateProcessW((WCHAR*)comspec_wchar, + (WCHAR*)cmdLine_wchar,// command line + NULL, // process security attributes + NULL, // primary thread security attributes + TRUE, // handles are inherited + 0, // creation flags + NULL, // use parent's environment + NULL, // use parent's current directory + &siStartInfo, // STARTUPINFO pointer + hProcInfo); // receives PROCESS_INFORMATION + free_pool_memory(cmdLine_wchar); + free_pool_memory(comspec_wchar); + + return bFuncRetn; +} + + +/** + * Create the process with ANSI API + */ +static BOOL +CreateChildProcessA(const char *comspec, char *cmdLine, + PROCESS_INFORMATION *hProcInfo, + HANDLE in, HANDLE out, HANDLE err) +{ + STARTUPINFOA siStartInfo; + BOOL bFuncRetn = FALSE; + + // Set up members of the STARTUPINFO structure. + ZeroMemory( &siStartInfo, sizeof(siStartInfo) ); + siStartInfo.cb = sizeof(siStartInfo); + // setup new process to use supplied handles for stdin,stdout,stderr + siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; + siStartInfo.wShowWindow = SW_SHOWMINNOACTIVE; + + siStartInfo.hStdInput = in; + siStartInfo.hStdOutput = out; + siStartInfo.hStdError = err; + + // Create the child process. + Dmsg2(150, "Calling CreateProcess(%s, %s, ...)\n", comspec, cmdLine); + + // try to execute program + bFuncRetn = p_CreateProcessA(comspec, + cmdLine, // command line + NULL, // process security attributes + NULL, // primary thread security attributes + TRUE, // handles are inherited + 0, // creation flags + NULL, // use parent's environment + NULL, // use parent's current directory + &siStartInfo,// STARTUPINFO pointer + hProcInfo);// receives PROCESS_INFORMATION + return bFuncRetn; +} /** * OK, so it would seem CreateProcess only handles true executables: - * .com or .exe files. - * So test to see whether we're getting a .bat file and if so grab - * $COMSPEC value and pass batch file to it. + * .com or .exe files. So grab $COMSPEC value and pass command line to it. */ HANDLE CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err) { - static const char *comspec = NULL; - PROCESS_INFORMATION piProcInfo; - STARTUPINFOA siStartInfo; - BOOL bFuncRetn = FALSE; + static const char *comspec = NULL; + PROCESS_INFORMATION piProcInfo; + BOOL bFuncRetn = FALSE; - if (comspec == NULL) { - comspec = getenv("COMSPEC"); - } - if (comspec == NULL) // should never happen - return INVALID_HANDLE_VALUE; + if (!p_CreateProcessA || !p_CreateProcessW) + return INVALID_HANDLE_VALUE; - // Set up members of the PROCESS_INFORMATION structure. - ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) ); + if (comspec == NULL) + comspec = getenv("COMSPEC"); + if (comspec == NULL) // should never happen + return INVALID_HANDLE_VALUE; - // Set up members of the STARTUPINFO structure. + // Set up members of the PROCESS_INFORMATION structure. + ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) ); - ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); - siStartInfo.cb = sizeof(STARTUPINFO); - // setup new process to use supplied handles for stdin,stdout,stderr - // if supplied handles are not used the send a copy of our STD_HANDLE - // as appropriate - siStartInfo.dwFlags = STARTF_USESTDHANDLES; + // if supplied handles are not used the send a copy of our STD_HANDLE + // as appropriate + if (in == INVALID_HANDLE_VALUE) + in = GetStdHandle(STD_INPUT_HANDLE); - if (in != INVALID_HANDLE_VALUE) - siStartInfo.hStdInput = in; - else - siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); + if (out == INVALID_HANDLE_VALUE) + out = GetStdHandle(STD_OUTPUT_HANDLE); - if (out != INVALID_HANDLE_VALUE) - siStartInfo.hStdOutput = out; - else - siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); - if (err != INVALID_HANDLE_VALUE) - siStartInfo.hStdError = err; - else - siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); - // Create the child process. - - char exeFile[256]; - int cmdLen = strlen(cmdline) + strlen(comspec) + 16; - - char *cmdLine = (char *)alloca(cmdLen); - - bstrncpy(exeFile, comspec, sizeof(exeFile)); - bstrncpy(cmdLine, comspec, cmdLen); - bstrncat(cmdLine, " /c ", cmdLen); - bstrncat(cmdLine, cmdline, cmdLen); - - // try to execute program - bFuncRetn = CreateProcessA(exeFile, - cmdLine, // command line - NULL, // process security attributes - NULL, // primary thread security attributes - TRUE, // handles are inherited - 0, // creation flags - NULL, // use parent's environment - NULL, // use parent's current directory - &siStartInfo, // STARTUPINFO pointer - &piProcInfo); // receives PROCESS_INFORMATION - - if (bFuncRetn == 0) { - ErrorExit("CreateProcess failed\n"); - const char *err = errorString(); - d_msg(__FILE__, __LINE__, 99, - "CreateProcess(%s, %s, ...)=%s\n", exeFile, cmdLine, err); - LocalFree((void *)err); - return INVALID_HANDLE_VALUE; - } - // we don't need a handle on the process primary thread so we close - // this now. - CloseHandle(piProcInfo.hThread); + if (err == INVALID_HANDLE_VALUE) + err = GetStdHandle(STD_ERROR_HANDLE); - return piProcInfo.hProcess; -} + char *exeFile; + const char *argStart; + + if (!GetApplicationName(cmdline, &exeFile, &argStart)) { + return INVALID_HANDLE_VALUE; + } + + POOL_MEM cmdLine(PM_FNAME); + Mmsg(cmdLine, "%s /c %s%s", comspec, exeFile, argStart); + + free(exeFile); + + // New function disabled + if (p_CreateProcessW && p_MultiByteToWideChar) { + bFuncRetn = CreateChildProcessW(comspec, cmdLine.c_str(), &piProcInfo, + in, out, err); + } else { + bFuncRetn = CreateChildProcessA(comspec, cmdLine.c_str(), &piProcInfo, + in, out, err); + } + if (bFuncRetn == 0) { + ErrorExit("CreateProcess failed\n"); + const char *err = errorString(); + Dmsg3(99, "CreateProcess(%s, %s, ...)=%s\n",comspec,cmdLine.c_str(),err); + LocalFree((void *)err); + return INVALID_HANDLE_VALUE; + } + // we don't need a handle on the process primary thread so we close + // this now. + CloseHandle(piProcInfo.hThread); + return piProcInfo.hProcess; +} void ErrorExit (LPCSTR lpszMessage) { - d_msg(__FILE__, __LINE__, 0, "%s", lpszMessage); + Dmsg1(0, "%s", lpszMessage); } @@ -1591,7 +2109,6 @@ CloseIfValid(HANDLE handle) CloseHandle(handle); } -#ifndef HAVE_MINGW BPIPE * open_bpipe(char *prog, int wait, const char *mode) { @@ -1684,23 +2201,23 @@ open_bpipe(char *prog, int wait, const char *mode) // process terminates we can // detect eof. // ugly but convert WIN32 HANDLE to FILE* - int rfd = _open_osfhandle((long)hChildStdoutRdDup, O_RDONLY); + int rfd = _open_osfhandle((intptr_t)hChildStdoutRdDup, O_RDONLY | O_BINARY); if (rfd >= 0) { - bpipe->rfd = _fdopen(rfd, "r"); + bpipe->rfd = _fdopen(rfd, "rb"); } } if (mode_write) { CloseHandle(hChildStdinRd); // close our read side so as not // to interfre with child's copy // ugly but convert WIN32 HANDLE to FILE* - int wfd = _open_osfhandle((long)hChildStdinWrDup, O_WRONLY); + int wfd = _open_osfhandle((intptr_t)hChildStdinWrDup, O_WRONLY | O_BINARY); if (wfd >= 0) { - bpipe->wfd = _fdopen(wfd, "w"); + bpipe->wfd = _fdopen(wfd, "wb"); } } if (wait > 0) { - bpipe->timer_id = start_child_timer(bpipe->worker_pid, wait); + bpipe->timer_id = start_child_timer(NULL, bpipe->worker_pid, wait); } return bpipe; @@ -1717,7 +2234,6 @@ cleanup: return NULL; } -#endif //HAVE_MINGW int kill(int pid, int signal) @@ -1731,7 +2247,6 @@ kill(int pid, int signal) return rval; } -#ifndef HAVE_MINGW int close_bpipe(BPIPE *bpipe) @@ -1739,6 +2254,16 @@ close_bpipe(BPIPE *bpipe) int rval = 0; int32_t remaining_wait = bpipe->wait; + /* Close pipes */ + if (bpipe->rfd) { + fclose(bpipe->rfd); + bpipe->rfd = NULL; + } + if (bpipe->wfd) { + fclose(bpipe->wfd); + bpipe->wfd = NULL; + } + if (remaining_wait == 0) { /* wait indefinitely */ remaining_wait = INT32_MAX; } @@ -1747,8 +2272,7 @@ close_bpipe(BPIPE *bpipe) if (!GetExitCodeProcess((HANDLE)bpipe->worker_pid, &exitCode)) { const char *err = errorString(); rval = b_errno_win32; - d_msg(__FILE__, __LINE__, 0, - "GetExitCode error %s\n", err); + Dmsg1(0, "GetExitCode error %s\n", err); LocalFree((void *)err); break; } @@ -1780,20 +2304,19 @@ close_bpipe(BPIPE *bpipe) int close_wpipe(BPIPE *bpipe) { - int stat = 1; + int result = 1; if (bpipe->wfd) { fflush(bpipe->wfd); if (fclose(bpipe->wfd) != 0) { - stat = 0; + result = 0; } bpipe->wfd = NULL; } - return stat; + return result; } -#include "findlib/find.h" - +#ifndef MINGW64 int utime(const char *fname, struct utimbuf *times) { @@ -1808,10 +2331,10 @@ utime(const char *fname, struct utimbuf *times) HANDLE h = INVALID_HANDLE_VALUE; if (p_CreateFileW) { - POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); - make_win32_path_UTF8_2_wchar(&pwszBuf, tmpbuf); + POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); + make_win32_path_UTF8_2_wchar(&pwszBuf, tmpbuf); - h = p_CreateFileW((LPCWSTR)pwszBuf, + h = p_CreateFileW((LPCWSTR)pwszBuf, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, @@ -1819,9 +2342,9 @@ utime(const char *fname, struct utimbuf *times) FILE_FLAG_BACKUP_SEMANTICS, // required for directories NULL); - free_pool_memory(pwszBuf); + free_pool_memory(pwszBuf); } else if (p_CreateFileA) { - h = p_CreateFileA(tmpbuf, + h = p_CreateFileA(tmpbuf, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, @@ -1831,12 +2354,11 @@ utime(const char *fname, struct utimbuf *times) } if (h == INVALID_HANDLE_VALUE) { - const char *err = errorString(); - d_msg(__FILE__, __LINE__, 99, - "Cannot open file \"%s\" for utime(): ERR=%s", tmpbuf, err); - LocalFree((void *)err); - errno = b_errno_win32; - return -1; + const char *err = errorString(); + Dmsg2(99, "Cannot open file \"%s\" for utime(): ERR=%s", tmpbuf, err); + LocalFree((void *)err); + errno = b_errno_win32; + return -1; } int rval = SetFileTime(h, NULL, &acc, &mod) ? 0 : -1; @@ -1846,113 +2368,61 @@ utime(const char *fname, struct utimbuf *times) } return rval; } +#endif -#if USE_WIN32_COMPAT_IO - +#if 0 int -open(const char *file, int flags, int mode) -{ - if (p_wopen) { +file_open(const char *file, int flags, int mode) +{ + DWORD access = 0; + DWORD shareMode = 0; + DWORD create = 0; + DWORD msflags = 0; + HANDLE foo = INVALID_HANDLE_VALUE; + const char *remap = file; + + if (flags & O_WRONLY) access = GENERIC_WRITE; + else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE; + else access = GENERIC_READ; + + if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) + create = CREATE_NEW; + else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) + create = CREATE_ALWAYS; + else if (flags & O_CREAT) + create = OPEN_ALWAYS; + else if (flags & O_TRUNC) + create = TRUNCATE_EXISTING; + else + create = OPEN_EXISTING; + + shareMode = 0; + + if (flags & O_APPEND) { + printf("open...APPEND not implemented yet."); + exit(-1); + } + + if (p_CreateFileW) { POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); make_win32_path_UTF8_2_wchar(&pwszBuf, file); - int nRet = p_wopen((LPCWSTR) pwszBuf, flags|_O_BINARY, mode); + foo = p_CreateFileW((LPCWSTR) pwszBuf, access, shareMode, NULL, create, msflags, NULL); free_pool_memory(pwszBuf); + } else if (p_CreateFileA) + foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL); - return nRet; + if (INVALID_HANDLE_VALUE == foo) { + errno = b_errno_win32; + return (int)-1; } + return (int)foo; - return _open(file, flags|_O_BINARY, mode); } -/* - * Note, this works only for a file. If you want - * to close a socket, use closesocket(). - * Bacula has been modified in src/lib/bnet.c - * to use closesocket(). - */ -#ifndef HAVE_VC8 -int -close(int fd) -{ - return _close(fd); -} - -#ifndef HAVE_WXCONSOLE -ssize_t -read(int fd, void *buf, ssize_t len) -{ - return _read(fd, buf, (size_t)len); -} - -ssize_t -write(int fd, const void *buf, ssize_t len) -{ - return _write(fd, buf, (size_t)len); -} -#endif - - -off_t -lseek(int fd, off_t offset, int whence) -{ - return (off_t)_lseeki64(fd, offset, whence); -} int -dup2(int fd1, int fd2) -{ - return _dup2(fd1, fd2); -} -#endif -#else -int -open(const char *file, int flags, int mode) -{ - DWORD access = 0; - DWORD shareMode = 0; - DWORD create = 0; - DWORD msflags = 0; - HANDLE foo = INVALID_HANDLE_VALUE; - const char *remap = file; - - if (flags & O_WRONLY) access = GENERIC_WRITE; - else if (flags & O_RDWR) access = GENERIC_READ|GENERIC_WRITE; - else access = GENERIC_READ; - - if (flags & O_CREAT) create = CREATE_NEW; - else create = OPEN_EXISTING; - - if (flags & O_TRUNC) create = TRUNCATE_EXISTING; - - if (!(flags & O_EXCL)) - shareMode = FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE; - - if (flags & O_APPEND) { - printf("open...APPEND not implemented yet."); - exit(-1); - } - - if (p_CreateFileW) { - POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); - make_win32_path_UTF8_2_wchar(pwszBuf, file); - - foo = p_CreateFileW((LPCWSTR) pwszBuf, access, shareMode, NULL, create, msflags, NULL); - free_pool_memory(pwszBuf); - } else if (p_CreateFileA) - foo = CreateFile(file, access, shareMode, NULL, create, msflags, NULL); - - if (INVALID_HANDLE_VALUE == foo) { - errno = b_errno_win32; - return(int) -1; - } - return (int)foo; - -} - - -int -close(int fd) +file_close(int fd) { if (!CloseHandle((HANDLE)fd)) { errno = b_errno_win32; @@ -1963,7 +2433,7 @@ close(int fd) } ssize_t -write(int fd, const void *data, ssize_t len) +file_write(int fd, const void *data, ssize_t len) { BOOL status; DWORD bwrite; @@ -1975,7 +2445,7 @@ write(int fd, const void *data, ssize_t len) ssize_t -read(int fd, void *data, ssize_t len) +file_read(int fd, void *data, ssize_t len) { BOOL status; DWORD bread; @@ -1986,11 +2456,14 @@ read(int fd, void *data, ssize_t len) return -1; } -off_t -lseek(int fd, off_t offset, int whence) +boffset_t +file_seek(int fd, boffset_t offset, int whence) { DWORD method = 0; DWORD val; + LONG offset_low = (LONG)offset; + LONG offset_high = (LONG)(offset >> 32); + switch (whence) { case SEEK_SET : method = FILE_BEGIN; @@ -2006,7 +2479,8 @@ lseek(int fd, off_t offset, int whence) return -1; } - if ((val=SetFilePointer((HANDLE)fd, (DWORD)offset, NULL, method)) == INVALID_SET_FILE_POINTER) { + + if ((val=SetFilePointer((HANDLE)fd, offset_low, &offset_high, method)) == INVALID_SET_FILE_POINTER) { errno = b_errno_win32; return -1; } @@ -2015,27 +2489,80 @@ lseek(int fd, off_t offset, int whence) } int -dup2(int, int) +file_dup2(int, int) { errno = ENOSYS; return -1; } +#endif + +#ifdef xxx +/* + * Emulation of mmap and unmmap for tokyo dbm + */ +void *mmap(void *start, size_t length, int prot, int flags, + int fd, off_t offset) +{ + DWORD fm_access = 0; + DWORD mv_access = 0; + HANDLE h; + HANDLE mv; + if (length == 0) { + return MAP_FAILED; + } + if (!fd) { + return MAP_FAILED; + } -#endif + if (flags & PROT_WRITE) { + fm_access |= PAGE_READWRITE; + } else if (flags & PROT_READ) { + fm_access |= PAGE_READONLY; + } + + if (flags & PROT_READ) { + mv_access |= FILE_MAP_READ; + } + if (flags & PROT_WRITE) { + mv_access |= FILE_MAP_WRITE; + } -#endif //HAVE_MINGW + h = CreateFileMapping((HANDLE)_get_osfhandle (fd), + NULL /* security */, + fm_access, + 0 /* MaximumSizeHigh */, + 0 /* MaximumSizeLow */, + NULL /* name of the file mapping object */); -#ifdef HAVE_MINGW -/* syslog function, added by Nicolas Boichat */ -void closelog() {} -void openlog(const char *ident, int option, int facility) {} -#endif //HAVE_MINGW + if (!h || h == INVALID_HANDLE_VALUE) { + return MAP_FAILED; + } + + mv = MapViewOfFile(h, mv_access, + 0 /* offset hi */, + 0 /* offset lo */, + length); + CloseHandle(h); + + if (!mv || mv == INVALID_HANDLE_VALUE) { + return MAP_FAILED; + } -/* Temp kludges ***FIXME**** */ -#ifdef __APCUPSD__ -unsigned int alarm(unsigned int seconds) + return (void *) mv; +} + +int munmap(void *start, size_t length) { + if (!start) { + return -1; + } + UnmapViewOfFile(start); return 0; } #endif + +#ifdef HAVE_MINGW +/* syslog function, added by Nicolas Boichat */ +void openlog(const char *ident, int option, int facility) {} +#endif //HAVE_MINGW