]> wimlib.net Git - wimlib/blobdiff - src/win32_apply.c
win32_apply.c: Work around deleting READONLY files
[wimlib] / src / win32_apply.c
index 312bd87605647b9b4ab5c3caf0ca48bedd1bb55d..e26455c4ae03de9b1484391f6644fadd96e15de0 100644 (file)
@@ -154,6 +154,33 @@ error:
        return WIMLIB_ERR_MKDIR;
 }
 
+/* Delete a non-directory file, working around Windows quirks.  */
+static BOOL
+win32_delete_file_wrapper(const wchar_t *path)
+{
+       DWORD attrib;
+       DWORD err;
+
+       if (DeleteFile(path))
+               return TRUE;
+
+       err = GetLastError();
+       attrib = GetFileAttributes(path);
+       if (attrib & FILE_ATTRIBUTE_READONLY) {
+               /* Try again with FILE_ATTRIBUTE_READONLY cleared.  */
+               attrib &= ~FILE_ATTRIBUTE_READONLY;
+               if (SetFileAttributes(path, attrib)) {
+                       if (DeleteFile(path))
+                               return TRUE;
+                       else
+                               err = GetLastError();
+               }
+       }
+
+       SetLastError(err);
+       return FALSE;
+}
+
 static int
 win32_create_hardlink(const wchar_t *oldpath, const wchar_t *newpath,
                      struct apply_ctx *ctx)
@@ -161,7 +188,7 @@ win32_create_hardlink(const wchar_t *oldpath, const wchar_t *newpath,
        if (!CreateHardLink(newpath, oldpath, NULL)) {
                if (GetLastError() != ERROR_ALREADY_EXISTS)
                        goto error;
-               if (!DeleteFile(newpath))
+               if (!win32_delete_file_wrapper(newpath))
                        goto error;
                if (!CreateHardLink(newpath, oldpath, NULL))
                        goto error;
@@ -180,7 +207,7 @@ win32_create_symlink(const wchar_t *oldpath, const wchar_t *newpath,
        if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0)) {
                if (GetLastError() != ERROR_ALREADY_EXISTS)
                        goto error;
-               if (!DeleteFile(newpath))
+               if (!win32_delete_file_wrapper(newpath))
                        goto error;
                if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0))
                        goto error;
@@ -540,8 +567,9 @@ win32_set_security_descriptor(const wchar_t *path, const u8 *desc,
        if (func_NtSetSecurityObject) {
                h = win32_open_existing_file(path, MAXIMUM_ALLOWED);
                if (h == INVALID_HANDLE_VALUE) {
-                       ERROR_WITH_ERRNO("Can't open %ls (%u)", path, GetLastError());
-                       goto error;
+                       set_errno_from_GetLastError();
+                       ERROR_WITH_ERRNO("Can't open %ls", path);
+                       return WIMLIB_ERR_SET_SECURITY;
                }
        }
 #endif
@@ -549,8 +577,10 @@ win32_set_security_descriptor(const wchar_t *path, const u8 *desc,
        for (;;) {
                err = do_win32_set_security_descriptor(h, path, info,
                                                       (PSECURITY_DESCRIPTOR)desc);
-               if (err == ERROR_SUCCESS)
+               if (err == ERROR_SUCCESS) {
+                       ret = 0;
                        break;
+               }
                if ((err == ERROR_PRIVILEGE_NOT_HELD ||
                     err == ERROR_ACCESS_DENIED) &&
                    !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
@@ -570,23 +600,18 @@ win32_set_security_descriptor(const wchar_t *path, const u8 *desc,
                        }
                        ctx->partial_security_descriptors--;
                        ctx->no_security_descriptors++;
+                       ret = 0;
                        break;
                }
-               SetLastError(err);
-               goto error;
+               set_errno_from_win32_error(err);
+               ret = WIMLIB_ERR_SET_SECURITY;
+               break;
        }
-       ret = 0;
-out_close:
 #ifdef WITH_NTDLL
        if (func_NtSetSecurityObject)
                CloseHandle(h);
 #endif
        return ret;
-
-error:
-       set_errno_from_GetLastError();
-       ret = WIMLIB_ERR_SET_SECURITY;
-       goto out_close;
 }
 
 static int