From fcb72613443a8dcbb5db8fcea209867738db728e Mon Sep 17 00:00:00 2001 From: Thorsten Engel Date: Fri, 5 Aug 2005 16:13:47 +0000 Subject: [PATCH] added unlink functionality for readonly files git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@2297 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/win32/compat/compat.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/bacula/src/win32/compat/compat.cpp b/bacula/src/win32/compat/compat.cpp index edd04b528b..9b0f3f0465 100644 --- a/bacula/src/win32/compat/compat.cpp +++ b/bacula/src/win32/compat/compat.cpp @@ -1077,10 +1077,38 @@ win32_unlink(const char *filename) POOLMEM* pwszBuf = get_pool_memory(PM_FNAME); UTF8_2_wchar(&pwszBuf, filename); nRetCode = _wunlink((LPCWSTR) pwszBuf); + + /* 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) { + if (p_SetFileAttributesW((LPCWSTR)pwszBuf, dwAttr & ~FILE_ATTRIBUTE_READONLY)) { + nRetCode = _wunlink((LPCWSTR) pwszBuf); + /* reset to original if it didn't help */ + if (nRetCode == -1) + p_SetFileAttributesW((LPCWSTR)pwszBuf, dwAttr); + } + } + } free_pool_memory(pwszBuf); } else { nRetCode = _unlink(filename); - } + + /* special case if file is readonly, + we retry but unset attribute before */ + if (nRetCode == -1 && errno == EACCES && p_SetFileAttributesA && p_GetFileAttributesA) { + DWORD dwAttr = p_GetFileAttributesA(filename); + if (dwAttr != INVALID_FILE_ATTRIBUTES) { + if (p_SetFileAttributesA(filename, dwAttr & ~FILE_ATTRIBUTE_READONLY)) { + nRetCode = _unlink(filename); + /* reset to original if it didn't help */ + if (nRetCode == -1) + p_SetFileAttributesA(filename, dwAttr); + } + } + } + } return nRetCode; } -- 2.39.5