From 4defa50a2782aaba0afbc76b3bef2055f1a746fe Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sun, 2 Nov 2008 10:57:49 +0000 Subject: [PATCH] Implement win32_chmod that uses wide characters, if possible, to get and set the file attributes. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@7963 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/win32/compat/compat.cpp | 96 ++++++++++++++++++++++++------ bacula/src/win32/compat/compat.h | 4 ++ bacula/src/win32/dll/bacula.def | 2 +- 3 files changed, 82 insertions(+), 20 deletions(-) diff --git a/bacula/src/win32/compat/compat.cpp b/bacula/src/win32/compat/compat.cpp index f88114938b..e99c212f44 100644 --- a/bacula/src/win32/compat/compat.cpp +++ b/bacula/src/win32/compat/compat.cpp @@ -532,11 +532,6 @@ int fcntl(int fd, int cmd) return 0; } -int chmod(const char *, mode_t) -{ - return 0; -} - int chown(const char *k, uid_t, gid_t) { return 0; @@ -1355,6 +1350,67 @@ WSA_Init(void) return 0; } +int win32_chmod(const char *path, mode_t mode) +{ + DWORD attr = (DWORD)-1; + + 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); + } 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); + } + } + + 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) @@ -1371,14 +1427,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; } @@ -1537,8 +1593,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) { @@ -2419,7 +2477,7 @@ file_dup2(int, int) * Emulation of mmap and unmmap for tokyo dbm */ void *mmap(void *start, size_t length, int prot, int flags, - int fd, off_t offset) + int fd, off_t offset) { DWORD fm_access = 0; DWORD mv_access = 0; @@ -2447,20 +2505,20 @@ void *mmap(void *start, size_t length, int prot, int flags, } h = CreateFileMapping((HANDLE)_get_osfhandle (fd), - NULL /* security */, - fm_access, - 0 /* MaximumSizeHigh */, - 0 /* MaximumSizeLow */, - NULL /* name of the file mapping object */); + 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); + 0 /* offset hi */, + 0 /* offset lo */, + length); CloseHandle(h); if (!mv || mv == INVALID_HANDLE_VALUE) { diff --git a/bacula/src/win32/compat/compat.h b/bacula/src/win32/compat/compat.h index 7f23c85e41..e79ef5419f 100644 --- a/bacula/src/win32/compat/compat.h +++ b/bacula/src/win32/compat/compat.h @@ -311,6 +311,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 @@ -337,12 +338,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 eb04b56934..5b0b89cfcd 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,6 @@ _Z4forkv _Z4killii _Z4pipePi _Z4statPKcP4stat -_Z5chmodPKct _Z5chownPKcjj _Z5fcntlii _Z5fcntliil -- 2.39.5