]> git.sur5r.net Git - bacula/bacula/commitdiff
added unlink functionality for readonly files
authorThorsten Engel <thorsten.engel@matrix-computer.com>
Fri, 5 Aug 2005 16:13:47 +0000 (16:13 +0000)
committerThorsten Engel <thorsten.engel@matrix-computer.com>
Fri, 5 Aug 2005 16:13:47 +0000 (16:13 +0000)
git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@2297 91ce42f0-d328-0410-95d8-f526ca767f89

bacula/src/win32/compat/compat.cpp

index edd04b528b400233cec40f0cded43bdca05e60ae..9b0f3f046570b0ef9099f514b58cf02c29d1ff5b 100644 (file)
@@ -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;
 }