X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=src%2Fwin32_apply.c;h=e26455c4ae03de9b1484391f6644fadd96e15de0;hp=b7646651efd5d08040dda18cad4d0e4ed0064e1d;hb=956f44f1fa63c3dfb99b080d4961c7c44fac640d;hpb=a460c9e7653b2fde6f40c0a5336a695ba52ffa87 diff --git a/src/win32_apply.c b/src/win32_apply.c index b7646651..e26455c4 100644 --- a/src/win32_apply.c +++ b/src/win32_apply.c @@ -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;