From: Kern Sibbald Date: Sat, 20 Dec 2008 13:22:14 +0000 (+0000) Subject: Backport most recent changes to compat.h and compat.cpp. X-Git-Tag: Release-2.4.4~16 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=d85ee86f0d8c81a1a5744d4ed501d54e3a83a62a;p=bacula%2Fbacula Backport most recent changes to compat.h and compat.cpp. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/branches/Branch-2.4@8202 91ce42f0-d328-0410-95d8-f526ca767f89 --- diff --git a/bacula/src/version.h b/bacula/src/version.h index 3eadafed6b..00810fa3a9 100644 --- a/bacula/src/version.h +++ b/bacula/src/version.h @@ -4,8 +4,8 @@ #undef VERSION #define VERSION "2.4.4-b2" -#define BDATE "13 December 2008" -#define LSMDATE "13Dec08" +#define BDATE "20 December 2008" +#define LSMDATE "20Dec08" #define PROG_COPYRIGHT "Copyright (C) %d-2008 Free Software Foundation Europe e.V.\n" #define BYEAR "2008" /* year for copyright messages in progs */ diff --git a/bacula/src/win32/compat/compat.cpp b/bacula/src/win32/compat/compat.cpp index c31e8465ab..e7a29472c6 100644 --- a/bacula/src/win32/compat/compat.cpp +++ b/bacula/src/win32/compat/compat.cpp @@ -20,7 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - Bacula® is a registered trademark of John Walker. + 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. @@ -40,6 +40,7 @@ #include "bacula.h" #include "compat.h" #include "jcr.h" +#include "findlib/find.h" #define b_errno_win32 (1<<29) @@ -58,6 +59,10 @@ 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; @@ -486,12 +491,44 @@ int umask(int) } #endif -int fcntl(int fd, int cmd) +#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; +} + +void *dlsym(void *handle, const char *name) +{ + void *symaddr; + symaddr = (void *)GetProcAddress((HMODULE)handle, name); + return symaddr; } -int chmod(const char *, mode_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; } @@ -548,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; @@ -604,6 +640,7 @@ statDir(const char *file, struct stat *sb) sb->st_ctime = now; sb->st_mtime = now; sb->st_atime = now; + sb->st_rdev = 0; return 0; } @@ -614,6 +651,7 @@ statDir(const char *file, struct stat *sb) POOLMEM* pwszBuf = get_pool_memory (PM_FNAME); make_win32_path_UTF8_2_wchar(&pwszBuf, file); + Dmsg1(100, "FindFirstFileW=%s\n", file); h = p_FindFirstFileW((LPCWSTR)pwszBuf, &info_w); free_pool_memory(pwszBuf); @@ -627,6 +665,7 @@ statDir(const char *file, struct stat *sb) // use ASCII } else if (p_FindFirstFileA) { + Dmsg1(100, "FindFirstFileA=%s\n", file); h = p_FindFirstFileA(file, &info_a); pdwFileAttributes = &info_a.dwFileAttributes; @@ -636,11 +675,17 @@ statDir(const char *file, struct stat *sb) 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(); - Dmsg2(99, "FindFirstFile(%s):%s\n", file, err); + /* + * 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; @@ -694,7 +739,7 @@ fstat(int fd, struct stat *sb) if (!GetFileInformationByHandle((HANDLE)fd, &info)) { const char *err = errorString(); - Dmsg1(99, "GetfileInformationByHandle: %s\n", err); + Dmsg1(2099, "GetfileInformationByHandle: %s\n", err); LocalFree((void *)err); errno = b_errno_win32; return -1; @@ -740,7 +785,7 @@ fstat(int fd, struct stat *sb) static int stat2(const char *file, struct stat *sb) { - HANDLE h; + HANDLE h = INVALID_HANDLE_VALUE; int rval = 0; char tmpbuf[5000]; conv_unix_to_win32_path(file, tmpbuf, 5000); @@ -752,26 +797,31 @@ stat2(const char *file, struct stat *sb) 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); + h = CreateFileA(tmpbuf, GENERIC_READ, + FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); } if (attr == (DWORD)-1) { const char *err = errorString(); - Dmsg2(99, "GetFileAttributes(%s): %s\n", tmpbuf, err); + Dmsg2(2099, "GetFileAttributes(%s): %s\n", tmpbuf, err); LocalFree((void *)err); + if (h != INVALID_HANDLE_VALUE) { + CloseHandle(h); + } errno = b_errno_win32; return -1; } - - h = CreateFileA(tmpbuf, GENERIC_READ, - FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); - if (h == INVALID_HANDLE_VALUE) { const char *err = errorString(); - Dmsg2(99, "Cannot open file for stat (%s):%s\n", tmpbuf, err); + Dmsg2(2099, "Cannot open file for stat (%s):%s\n", tmpbuf, err); LocalFree((void *)err); errno = b_errno_win32; return -1; @@ -782,9 +832,8 @@ stat2(const char *file, struct stat *sb) if (attr & FILE_ATTRIBUTE_DIRECTORY && file[1] == ':' && file[2] != 0) { - statDir(file, sb); + rval = statDir(file, sb); } - return rval; } @@ -801,7 +850,7 @@ stat(const char *file, struct stat *sb) 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) { @@ -1315,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) @@ -1331,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; } @@ -1346,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); } @@ -1497,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) { @@ -1807,7 +1928,7 @@ GetApplicationName(const char *cmdline, char **pexe, const char **pargs) } /** - * Create the process with UTF8 API + * Create the process with WCHAR API */ static BOOL CreateChildProcessW(const char *comspec, const char *cmdLine, @@ -1850,7 +1971,6 @@ CreateChildProcessW(const char *comspec, const char *cmdLine, NULL, // use parent's current directory &siStartInfo, // STARTUPINFO pointer hProcInfo); // receives PROCESS_INFORMATION - free_pool_memory(cmdLine_wchar); free_pool_memory(comspec_wchar); @@ -1942,6 +2062,7 @@ CreateChildProcess(const char *cmdline, HANDLE in, HANDLE out, HANDLE err) free(exeFile); + // New function disabled if (p_CreateProcessW && p_MultiByteToWideChar) { bFuncRetn = CreateChildProcessW(comspec, cmdLine.c_str(), &piProcInfo, in, out, err); @@ -2195,8 +2316,6 @@ close_wpipe(BPIPE *bpipe) return result; } -#include "findlib/find.h" - int utime(const char *fname, struct utimbuf *times) { @@ -2375,6 +2494,72 @@ file_dup2(int, int) } #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; + } + + 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; + } + + h = CreateFileMapping((HANDLE)_get_osfhandle (fd), + NULL /* security */, + fm_access, + 0 /* MaximumSizeHigh */, + 0 /* MaximumSizeLow */, + NULL /* name of the file mapping object */); + + 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; + } + + 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) {} diff --git a/bacula/src/win32/compat/compat.h b/bacula/src/win32/compat/compat.h index 7489caf117..a28ee6a437 100644 --- a/bacula/src/win32/compat/compat.h +++ b/bacula/src/win32/compat/compat.h @@ -1,7 +1,7 @@ /* Bacula® - The Network Backup Solution - Copyright (C) 2004-2007 Free Software Foundation Europe e.V. + 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. @@ -20,7 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - Bacula® is a registered trademark of John Walker. + 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. @@ -106,6 +106,7 @@ typedef UINT32 uid_t; typedef UINT32 gid_t; typedef UINT32 mode_t; typedef INT32 ssize_t; +typedef UINT32 size_t; #define HAVE_SSIZE_T 1 #endif /* HAVE_MINGW */ @@ -307,6 +308,7 @@ struct sigaction { #define mkdir(p, m) win32_mkdir(p) #define unlink win32_unlink #define chdir win32_chdir +#define chmod win32_chmod extern "C" void syslog(int type, const char *fmt, ...); #if !defined(LOG_DAEMON) #define LOG_DAEMON 0 @@ -333,12 +335,15 @@ extern "C" void * __cdecl _alloca(size_t); #define getcwd win32_getcwd #define chdir win32_chdir +#define chmod win32_chmod #define fputs win32_fputs char *win32_getcwd(char *buf, int maxlen); int win32_chdir(const char *buf); int win32_mkdir(const char *buf); int win32_fputs(const char *string, FILE *stream); int win32_unlink(const char *filename); +int win32_chmod(const char *, mode_t); + char* win32_cgets (char* buffer, int len); diff --git a/bacula/src/win32/dll/bacula.def b/bacula/src/win32/dll/bacula.def index 6892d96c64..4998b56876 100644 --- a/bacula/src/win32/dll/bacula.def +++ b/bacula/src/win32/dll/bacula.def @@ -8,6 +8,7 @@ _Z11close_wpipeP5BPIPE _Z11strncasecmpPKcS0_i _Z11win32_cgetsPci _Z11win32_chdirPKc +_Z11win32_chmodPKct _Z11win32_fputsPKcP6_iobuf _Z11win32_mkdirPKc _Z12UTF8_2_wcharPPcPKc @@ -33,7 +34,7 @@ _Z4forkv _Z4killii _Z4pipePi _Z4statPKcP4stat -_Z5chmodPKct +_Z11win32_chmodPKct _Z5chownPKcjj _Z5fcntlii _Z5fcntliil diff --git a/bacula/technotes-2.4 b/bacula/technotes-2.4 index cd6d6eb2ec..aec87958c1 100644 --- a/bacula/technotes-2.4 +++ b/bacula/technotes-2.4 @@ -1,6 +1,8 @@ Technical notes on version 2.4 General: +20Dec08 +kes Backport most recent changes to compat.h and compat.cpp. 13Dec08 kes Create and apply 2.4.3-migrate2.patch, which fixes two bugs - Bug #1206 -- sql error when there are no files to migrate.