2 * win32_apply.c - Windows-specific code for applying files from a WIM image.
6 * Copyright (C) 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/win32_common.h"
32 #include "wimlib/apply.h"
33 #include "wimlib/error.h"
34 #include "wimlib/lookup_table.h"
37 win32_start_extract(const wchar_t *path, struct apply_ctx *ctx)
41 bool supports_SetFileShortName;
43 ret = win32_get_vol_flags(path, &vol_flags, &supports_SetFileShortName);
47 ctx->supported_features.archive_files = 1;
48 ctx->supported_features.hidden_files = 1;
49 ctx->supported_features.system_files = 1;
51 if (vol_flags & FILE_FILE_COMPRESSION)
52 ctx->supported_features.compressed_files = 1;
54 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
55 ctx->supported_features.encrypted_files = 1;
56 ctx->supported_features.encrypted_directories = 1;
59 ctx->supported_features.not_context_indexed_files = 1;
61 if (vol_flags & FILE_SUPPORTS_SPARSE_FILES)
62 ctx->supported_features.sparse_files = 1;
64 if (vol_flags & FILE_NAMED_STREAMS)
65 ctx->supported_features.named_data_streams = 1;
67 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
68 ctx->supported_features.hard_links = 1;
70 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS) {
71 ctx->supported_features.reparse_points = 1;
72 if (win32func_CreateSymbolicLinkW)
73 ctx->supported_features.symlink_reparse_points = 1;
76 if (vol_flags & FILE_PERSISTENT_ACLS)
77 ctx->supported_features.security_descriptors = 1;
79 if (supports_SetFileShortName)
80 ctx->supported_features.short_names = 1;
84 /* Create a normal file, overwriting one already present. */
86 win32_create_file(const wchar_t *path, struct apply_ctx *ctx, u64 *cookie_ret)
89 unsigned retry_count = 0;
90 DWORD dwFlagsAndAttributes = FILE_FLAG_BACKUP_SEMANTICS;
93 /* WRITE_OWNER and WRITE_DAC privileges are required for some reason,
94 * even through we're creating a new file. */
95 h = CreateFile(path, WRITE_OWNER | WRITE_DAC, 0, NULL,
96 CREATE_ALWAYS, dwFlagsAndAttributes, NULL);
97 if (h == INVALID_HANDLE_VALUE) {
98 /* File couldn't be created. */
99 DWORD err = GetLastError();
100 if (err == ERROR_ACCESS_DENIED && retry_count == 0) {
102 /* Access denied error for the first time. Try
103 * adjusting file attributes. */
105 /* Get attributes of the existing file. */
106 DWORD attribs = GetFileAttributes(path);
107 if (attribs != INVALID_FILE_ATTRIBUTES &&
108 (attribs & (FILE_ATTRIBUTE_HIDDEN |
109 FILE_ATTRIBUTE_SYSTEM |
110 FILE_ATTRIBUTE_READONLY)))
112 /* If the existing file has
113 * FILE_ATTRIBUTE_HIDDEN and/or
114 * FILE_ATTRIBUTE_SYSTEM, they must be set in
115 * the call to CreateFile(). This is true even
116 * when FILE_ATTRIBUTE_NORMAL was not specified,
117 * contrary to the MS "documentation". */
118 dwFlagsAndAttributes |= (attribs &
119 (FILE_ATTRIBUTE_HIDDEN |
120 FILE_ATTRIBUTE_SYSTEM));
121 /* If the existing file has
122 * FILE_ATTRIBUTE_READONLY, it must be cleared
123 * before attempting to create a new file over
124 * it. This is true even when the process has
125 * the SE_RESTORE_NAME privilege and requested
126 * the FILE_FLAG_BACKUP_SEMANTICS flag to
128 if (attribs & FILE_ATTRIBUTE_READONLY) {
129 SetFileAttributes(path,
130 attribs & ~FILE_ATTRIBUTE_READONLY);
136 set_errno_from_win32_error(err);
137 return WIMLIB_ERR_OPEN;
144 win32_create_directory(const wchar_t *path, struct apply_ctx *ctx,
147 if (!CreateDirectory(path, NULL))
148 if (GetLastError() != ERROR_ALREADY_EXISTS)
153 set_errno_from_GetLastError();
154 return WIMLIB_ERR_MKDIR;
157 /* Delete a non-directory file, working around Windows quirks. */
159 win32_delete_file_wrapper(const wchar_t *path)
164 if (DeleteFile(path))
167 err = GetLastError();
168 attrib = GetFileAttributes(path);
169 if (attrib & FILE_ATTRIBUTE_READONLY) {
170 /* Try again with FILE_ATTRIBUTE_READONLY cleared. */
171 attrib &= ~FILE_ATTRIBUTE_READONLY;
172 if (SetFileAttributes(path, attrib)) {
173 if (DeleteFile(path))
176 err = GetLastError();
185 win32_create_hardlink(const wchar_t *oldpath, const wchar_t *newpath,
186 struct apply_ctx *ctx)
188 if (!CreateHardLink(newpath, oldpath, NULL)) {
189 if (GetLastError() != ERROR_ALREADY_EXISTS)
191 if (!win32_delete_file_wrapper(newpath))
193 if (!CreateHardLink(newpath, oldpath, NULL))
199 set_errno_from_GetLastError();
200 return WIMLIB_ERR_LINK;
204 win32_create_symlink(const wchar_t *oldpath, const wchar_t *newpath,
205 struct apply_ctx *ctx)
207 if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0)) {
208 if (GetLastError() != ERROR_ALREADY_EXISTS)
210 if (!win32_delete_file_wrapper(newpath))
212 if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0))
218 set_errno_from_GetLastError();
219 return WIMLIB_ERR_LINK;
223 win32_extract_wim_chunk(const void *buf, size_t len, void *arg)
225 HANDLE h = (HANDLE)arg;
226 DWORD nbytes_written;
228 if (unlikely(!WriteFile(h, buf, len, &nbytes_written, NULL)))
230 if (unlikely(nbytes_written != len))
235 set_errno_from_GetLastError();
236 return WIMLIB_ERR_WRITE;
240 win32_extract_stream(const wchar_t *path, const wchar_t *stream_name,
241 size_t stream_name_nchars,
242 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
244 DWORD creationDisposition = OPEN_EXISTING;
245 wchar_t *stream_path = (wchar_t*)path;
249 if (stream_name_nchars) {
250 creationDisposition = CREATE_ALWAYS;
251 stream_path = alloca(sizeof(wchar_t) *
253 wcslen(stream_name) + 1));
254 tsprintf(stream_path, L"%ls:%ls", path, stream_name);
257 h = CreateFile(stream_path, FILE_WRITE_DATA, 0, NULL,
258 creationDisposition, FILE_FLAG_BACKUP_SEMANTICS |
259 FILE_FLAG_OPEN_REPARSE_POINT,
261 if (h == INVALID_HANDLE_VALUE)
266 goto out_close_handle;
267 ret = extract_stream(lte, lte->size, win32_extract_wim_chunk, h);
276 set_errno_from_GetLastError();
277 return WIMLIB_ERR_WRITE;
281 win32_extract_unnamed_stream(file_spec_t file,
282 struct wim_lookup_table_entry *lte,
283 struct apply_ctx *ctx)
285 return win32_extract_stream(file.path, NULL, 0, lte, ctx);
289 win32_extract_named_stream(file_spec_t file, const wchar_t *stream_name,
290 size_t stream_name_nchars,
291 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
293 return win32_extract_stream(file.path, stream_name,
294 stream_name_nchars, lte, ctx);
297 struct win32_encrypted_extract_ctx {
298 const struct wim_lookup_table_entry *lte;
303 win32_encrypted_import_cb(unsigned char *data, void *_import_ctx,
304 unsigned long *len_p)
306 struct win32_encrypted_extract_ctx *import_ctx = _import_ctx;
307 unsigned long len = *len_p;
308 const struct wim_lookup_table_entry *lte = import_ctx->lte;
310 len = min(len, lte->size - import_ctx->offset);
312 if (read_partial_wim_stream_into_buf(lte, len, import_ctx->offset, data))
313 return ERROR_READ_FAULT;
315 import_ctx->offset += len;
317 return ERROR_SUCCESS;
321 win32_extract_encrypted_stream(const wchar_t *path,
322 struct wim_lookup_table_entry *lte,
323 struct apply_ctx *ctx)
328 struct win32_encrypted_extract_ctx extract_ctx;
330 err = OpenEncryptedFileRaw(path, CREATE_FOR_IMPORT, &file_ctx);
331 if (err != ERROR_SUCCESS) {
332 set_errno_from_win32_error(err);
333 ret = WIMLIB_ERR_OPEN;
337 extract_ctx.lte = lte;
338 extract_ctx.offset = 0;
339 err = WriteEncryptedFileRaw(win32_encrypted_import_cb, &extract_ctx,
341 if (err != ERROR_SUCCESS) {
342 set_errno_from_win32_error(err);
343 ret = WIMLIB_ERR_WRITE;
349 CloseEncryptedFileRaw(file_ctx);
355 win32_set_special_file_attributes(const wchar_t *path, u32 attributes)
359 USHORT compression_format = COMPRESSION_FORMAT_DEFAULT;
360 DWORD bytes_returned;
362 h = win32_open_existing_file(path, GENERIC_READ | GENERIC_WRITE);
363 if (h == INVALID_HANDLE_VALUE)
366 if (attributes & FILE_ATTRIBUTE_SPARSE_FILE)
367 if (!DeviceIoControl(h, FSCTL_SET_SPARSE,
370 &bytes_returned, NULL))
371 goto error_close_handle;
373 if (attributes & FILE_ATTRIBUTE_COMPRESSED)
374 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
375 &compression_format, sizeof(USHORT),
377 &bytes_returned, NULL))
378 goto error_close_handle;
383 if (attributes & FILE_ATTRIBUTE_ENCRYPTED)
384 if (!EncryptFile(path))
390 err = GetLastError();
398 win32_set_file_attributes(const wchar_t *path, u32 attributes,
399 struct apply_ctx *ctx, unsigned pass)
401 u32 special_attributes =
402 FILE_ATTRIBUTE_REPARSE_POINT |
403 FILE_ATTRIBUTE_DIRECTORY |
404 FILE_ATTRIBUTE_SPARSE_FILE |
405 FILE_ATTRIBUTE_COMPRESSED |
406 FILE_ATTRIBUTE_ENCRYPTED;
407 u32 actual_attributes;
409 /* Delay setting FILE_ATTRIBUTE_READONLY on the initial pass (when files
410 * are created, but data not extracted); otherwise the system will
411 * refuse access to the file even if the process has SeRestorePrivilege.
414 attributes &= ~FILE_ATTRIBUTE_READONLY;
416 if (!SetFileAttributes(path, attributes & ~special_attributes))
422 if (attributes & (FILE_ATTRIBUTE_SPARSE_FILE |
423 FILE_ATTRIBUTE_ENCRYPTED |
424 FILE_ATTRIBUTE_COMPRESSED))
425 if (!win32_set_special_file_attributes(path, attributes))
428 /* If file is not supposed to be encrypted or compressed, remove
429 * defaulted encrypted or compressed attributes (from creating file in
430 * encrypted or compressed directory). */
431 actual_attributes = GetFileAttributes(path);
432 if (actual_attributes == INVALID_FILE_ATTRIBUTES)
435 if ((actual_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
436 !(attributes & FILE_ATTRIBUTE_ENCRYPTED))
437 if (!DecryptFile(path, 0))
439 if ((actual_attributes & FILE_ATTRIBUTE_COMPRESSED) &&
440 !(attributes & FILE_ATTRIBUTE_COMPRESSED))
443 DWORD bytes_returned;
444 USHORT compression_format = COMPRESSION_FORMAT_NONE;
446 h = win32_open_existing_file(path, GENERIC_READ | GENERIC_WRITE);
447 if (h == INVALID_HANDLE_VALUE)
450 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
451 &compression_format, sizeof(USHORT),
453 &bytes_returned, NULL))
455 DWORD err = GetLastError();
468 set_errno_from_GetLastError();
469 return WIMLIB_ERR_SET_ATTRIBUTES;
473 win32_set_reparse_data(const wchar_t *path, const u8 *rpbuf, u16 rpbuflen,
474 struct apply_ctx *ctx)
478 DWORD bytes_returned;
480 h = win32_open_existing_file(path, GENERIC_WRITE);
481 if (h == INVALID_HANDLE_VALUE)
484 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
485 (void*)rpbuf, rpbuflen,
486 NULL, 0, &bytes_returned, NULL))
487 goto error_close_handle;
495 err = GetLastError();
499 set_errno_from_GetLastError();
500 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
504 win32_set_short_name(const wchar_t *path, const wchar_t *short_name,
505 size_t short_name_nchars, struct apply_ctx *ctx)
510 h = win32_open_existing_file(path, GENERIC_WRITE | DELETE);
511 if (h == INVALID_HANDLE_VALUE)
514 if (short_name_nchars) {
515 if (!SetFileShortName(h, short_name))
516 goto error_close_handle;
517 } else if (running_on_windows_7_or_later()) {
518 if (!SetFileShortName(h, L""))
519 goto error_close_handle;
528 err = GetLastError();
532 set_errno_from_GetLastError();
533 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
537 do_win32_set_security_descriptor(HANDLE h, const wchar_t *path,
538 SECURITY_INFORMATION info,
539 PSECURITY_DESCRIPTOR desc)
542 if (func_NtSetSecurityObject) {
543 return (*func_RtlNtStatusToDosError)(
544 (*func_NtSetSecurityObject)(h, info, desc));
547 if (SetFileSecurity(path, info, desc))
548 return ERROR_SUCCESS;
550 return GetLastError();
554 win32_set_security_descriptor(const wchar_t *path, const u8 *desc,
555 size_t desc_size, struct apply_ctx *ctx)
557 SECURITY_INFORMATION info;
562 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
563 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
564 h = INVALID_HANDLE_VALUE;
567 if (func_NtSetSecurityObject) {
568 h = win32_open_existing_file(path, MAXIMUM_ALLOWED);
569 if (h == INVALID_HANDLE_VALUE) {
570 set_errno_from_GetLastError();
571 ERROR_WITH_ERRNO("Can't open %ls", path);
572 return WIMLIB_ERR_SET_SECURITY;
578 err = do_win32_set_security_descriptor(h, path, info,
579 (PSECURITY_DESCRIPTOR)desc);
580 if (err == ERROR_SUCCESS) {
584 if ((err == ERROR_PRIVILEGE_NOT_HELD ||
585 err == ERROR_ACCESS_DENIED) &&
586 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
588 if (info & SACL_SECURITY_INFORMATION) {
589 info &= ~SACL_SECURITY_INFORMATION;
590 ctx->partial_security_descriptors++;
593 if (info & DACL_SECURITY_INFORMATION) {
594 info &= ~DACL_SECURITY_INFORMATION;
597 if (info & OWNER_SECURITY_INFORMATION) {
598 info &= ~OWNER_SECURITY_INFORMATION;
601 ctx->partial_security_descriptors--;
602 ctx->no_security_descriptors++;
606 set_errno_from_win32_error(err);
607 ret = WIMLIB_ERR_SET_SECURITY;
611 if (func_NtSetSecurityObject)
618 win32_set_timestamps(const wchar_t *path, u64 creation_time,
619 u64 last_write_time, u64 last_access_time,
620 struct apply_ctx *ctx)
624 FILETIME creationTime = {.dwLowDateTime = creation_time & 0xffffffff,
625 .dwHighDateTime = creation_time >> 32};
626 FILETIME lastAccessTime = {.dwLowDateTime = last_access_time & 0xffffffff,
627 .dwHighDateTime = last_access_time >> 32};
628 FILETIME lastWriteTime = {.dwLowDateTime = last_write_time & 0xffffffff,
629 .dwHighDateTime = last_write_time >> 32};
631 h = win32_open_existing_file(path, FILE_WRITE_ATTRIBUTES);
632 if (h == INVALID_HANDLE_VALUE)
635 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime))
636 goto error_close_handle;
644 err = GetLastError();
648 set_errno_from_GetLastError();
649 return WIMLIB_ERR_SET_TIMESTAMPS;
652 const struct apply_operations win32_apply_ops = {
655 .target_is_root = win32_path_is_root_of_drive,
656 .start_extract = win32_start_extract,
657 .create_file = win32_create_file,
658 .create_directory = win32_create_directory,
659 .create_hardlink = win32_create_hardlink,
660 .create_symlink = win32_create_symlink,
661 .extract_unnamed_stream = win32_extract_unnamed_stream,
662 .extract_named_stream = win32_extract_named_stream,
663 .extract_encrypted_stream = win32_extract_encrypted_stream,
664 .set_file_attributes = win32_set_file_attributes,
665 .set_reparse_data = win32_set_reparse_data,
666 .set_short_name = win32_set_short_name,
667 .set_security_descriptor = win32_set_security_descriptor,
668 .set_timestamps = win32_set_timestamps,
670 .path_prefix = L"\\\\?\\",
671 .path_prefix_nchars = 4,
672 .path_separator = L'\\',
675 .requires_realtarget_in_paths = 1,
676 .realpath_works_on_nonexisting_files = 1,
677 .root_directory_is_special = 1,
678 .requires_final_set_attributes_pass = 1,
679 .extract_encrypted_stream_creates_file = 1,
680 .requires_short_name_reordering = 1, /* TODO: check if this is really needed */
683 #endif /* __WIN32__ */