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 # include <winternl.h>
38 # include <ntstatus.h>
40 NtSetSecurityObject(HANDLE Handle,
41 SECURITY_INFORMATION SecurityInformation,
42 PSECURITY_DESCRIPTOR SecurityDescriptor);
46 win32_start_extract(const wchar_t *path, struct apply_ctx *ctx)
50 bool supports_SetFileShortName;
52 ret = win32_get_vol_flags(path, &vol_flags, &supports_SetFileShortName);
56 ctx->supported_features.archive_files = 1;
57 ctx->supported_features.hidden_files = 1;
58 ctx->supported_features.system_files = 1;
60 if (vol_flags & FILE_FILE_COMPRESSION)
61 ctx->supported_features.compressed_files = 1;
63 if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
64 ctx->supported_features.encrypted_files = 1;
65 ctx->supported_features.encrypted_directories = 1;
68 ctx->supported_features.not_context_indexed_files = 1;
70 if (vol_flags & FILE_SUPPORTS_SPARSE_FILES)
71 ctx->supported_features.sparse_files = 1;
73 if (vol_flags & FILE_NAMED_STREAMS)
74 ctx->supported_features.named_data_streams = 1;
76 if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
77 ctx->supported_features.hard_links = 1;
79 if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS) {
80 ctx->supported_features.reparse_points = 1;
81 if (win32func_CreateSymbolicLinkW)
82 ctx->supported_features.symlink_reparse_points = 1;
85 if (vol_flags & FILE_PERSISTENT_ACLS)
86 ctx->supported_features.security_descriptors = 1;
88 if (supports_SetFileShortName)
89 ctx->supported_features.short_names = 1;
94 win32_create_file(const wchar_t *path, struct apply_ctx *ctx, u64 *cookie_ret)
98 h = CreateFile(path, 0, 0, NULL, CREATE_ALWAYS,
99 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
100 if (h == INVALID_HANDLE_VALUE)
106 set_errno_from_GetLastError();
107 return WIMLIB_ERR_OPEN;
111 win32_create_directory(const wchar_t *path, struct apply_ctx *ctx,
114 if (!CreateDirectory(path, NULL))
115 if (GetLastError() != ERROR_ALREADY_EXISTS)
120 set_errno_from_GetLastError();
121 return WIMLIB_ERR_MKDIR;
125 win32_create_hardlink(const wchar_t *oldpath, const wchar_t *newpath,
126 struct apply_ctx *ctx)
128 if (!CreateHardLink(newpath, oldpath, NULL)) {
129 if (GetLastError() != ERROR_ALREADY_EXISTS)
131 if (!DeleteFile(newpath))
133 if (!CreateHardLink(newpath, oldpath, NULL))
139 set_errno_from_GetLastError();
140 return WIMLIB_ERR_LINK;
144 win32_create_symlink(const wchar_t *oldpath, const wchar_t *newpath,
145 struct apply_ctx *ctx)
147 if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0)) {
148 if (GetLastError() != ERROR_ALREADY_EXISTS)
150 if (!DeleteFile(newpath))
152 if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0))
158 set_errno_from_GetLastError();
159 return WIMLIB_ERR_LINK;
163 win32_extract_wim_chunk(const void *buf, size_t len, void *arg)
165 HANDLE h = (HANDLE)arg;
166 DWORD nbytes_written;
168 if (unlikely(!WriteFile(h, buf, len, &nbytes_written, NULL)))
170 if (unlikely(nbytes_written != len))
175 set_errno_from_GetLastError();
176 return WIMLIB_ERR_WRITE;
180 win32_extract_stream(const wchar_t *path, const wchar_t *stream_name,
181 size_t stream_name_nchars,
182 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
184 DWORD creationDisposition = OPEN_EXISTING;
185 wchar_t *stream_path = (wchar_t*)path;
189 if (stream_name_nchars) {
190 creationDisposition = CREATE_ALWAYS;
191 stream_path = alloca(sizeof(wchar_t) *
193 wcslen(stream_name) + 1));
194 swprintf(stream_path, L"%ls:%ls", path, stream_name);
197 h = CreateFile(stream_path, FILE_WRITE_DATA, 0, NULL,
198 creationDisposition, FILE_FLAG_BACKUP_SEMANTICS |
199 FILE_FLAG_OPEN_REPARSE_POINT,
201 if (h == INVALID_HANDLE_VALUE)
206 goto out_close_handle;
207 ret = extract_wim_resource(lte, wim_resource_size(lte),
208 win32_extract_wim_chunk, h);
217 set_errno_from_GetLastError();
218 return WIMLIB_ERR_WRITE;
222 win32_extract_unnamed_stream(file_spec_t file,
223 struct wim_lookup_table_entry *lte,
224 struct apply_ctx *ctx)
226 return win32_extract_stream(file.path, NULL, 0, lte, ctx);
230 win32_extract_named_stream(file_spec_t file, const wchar_t *stream_name,
231 size_t stream_name_nchars,
232 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
234 return win32_extract_stream(file.path, stream_name,
235 stream_name_nchars, lte, ctx);
238 struct win32_encrypted_extract_ctx {
239 const struct wim_lookup_table_entry *lte;
244 win32_encrypted_import_cb(unsigned char *data, void *_import_ctx,
245 unsigned long *len_p)
247 struct win32_encrypted_extract_ctx *import_ctx = _import_ctx;
248 unsigned long len = *len_p;
249 const struct wim_lookup_table_entry *lte = import_ctx->lte;
251 len = min(len, wim_resource_size(lte) - import_ctx->offset);
253 if (read_partial_wim_resource_into_buf(lte, len, import_ctx->offset, data))
254 return ERROR_READ_FAULT;
256 import_ctx->offset += len;
258 return ERROR_SUCCESS;
262 win32_extract_encrypted_stream(const wchar_t *path,
263 struct wim_lookup_table_entry *lte,
264 struct apply_ctx *ctx)
269 struct win32_encrypted_extract_ctx extract_ctx;
271 err = OpenEncryptedFileRaw(path, CREATE_FOR_IMPORT, &file_ctx);
272 if (err != ERROR_SUCCESS) {
273 set_errno_from_win32_error(err);
274 ret = WIMLIB_ERR_OPEN;
278 extract_ctx.lte = lte;
279 extract_ctx.offset = 0;
280 err = WriteEncryptedFileRaw(win32_encrypted_import_cb, &extract_ctx,
282 if (err != ERROR_SUCCESS) {
283 set_errno_from_win32_error(err);
284 ret = WIMLIB_ERR_WRITE;
290 CloseEncryptedFileRaw(file_ctx);
296 win32_set_special_file_attributes(const wchar_t *path, u32 attributes)
300 USHORT compression_format = COMPRESSION_FORMAT_DEFAULT;
301 DWORD bytes_returned;
303 h = win32_open_existing_file(path, GENERIC_READ | GENERIC_WRITE);
304 if (h == INVALID_HANDLE_VALUE)
307 if (attributes & FILE_ATTRIBUTE_SPARSE_FILE)
308 if (!DeviceIoControl(h, FSCTL_SET_SPARSE,
311 &bytes_returned, NULL))
312 goto error_close_handle;
314 if (attributes & FILE_ATTRIBUTE_COMPRESSED)
315 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
316 &compression_format, sizeof(USHORT),
318 &bytes_returned, NULL))
319 goto error_close_handle;
324 if (attributes & FILE_ATTRIBUTE_ENCRYPTED)
325 if (!EncryptFile(path))
331 err = GetLastError();
339 win32_set_file_attributes(const wchar_t *path, u32 attributes,
340 struct apply_ctx *ctx, unsigned pass)
342 u32 special_attributes =
343 FILE_ATTRIBUTE_REPARSE_POINT |
344 FILE_ATTRIBUTE_DIRECTORY |
345 FILE_ATTRIBUTE_SPARSE_FILE |
346 FILE_ATTRIBUTE_COMPRESSED |
347 FILE_ATTRIBUTE_ENCRYPTED;
348 u32 actual_attributes;
350 /* Delay setting FILE_ATTRIBUTE_READONLY on the initial pass (when files
351 * are created, but data not extracted); otherwise the system will
352 * refuse access to the file even if the process has SeRestorePrivilege.
355 attributes &= ~FILE_ATTRIBUTE_READONLY;
357 if (!SetFileAttributes(path, attributes & ~special_attributes))
363 if (attributes & (FILE_ATTRIBUTE_SPARSE_FILE |
364 FILE_ATTRIBUTE_ENCRYPTED |
365 FILE_ATTRIBUTE_COMPRESSED))
366 if (!win32_set_special_file_attributes(path, attributes))
369 /* If file is not supposed to be encrypted or compressed, remove
370 * defaulted encrypted or compressed attributes (from creating file in
371 * encrypted or compressed directory). */
372 actual_attributes = GetFileAttributes(path);
373 if (actual_attributes == INVALID_FILE_ATTRIBUTES)
376 if ((actual_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
377 !(attributes & FILE_ATTRIBUTE_ENCRYPTED))
378 if (!DecryptFile(path, 0))
380 if ((actual_attributes & FILE_ATTRIBUTE_COMPRESSED) &&
381 !(attributes & FILE_ATTRIBUTE_COMPRESSED))
384 DWORD bytes_returned;
385 USHORT compression_format = COMPRESSION_FORMAT_NONE;
387 h = win32_open_existing_file(path, GENERIC_READ | GENERIC_WRITE);
388 if (h == INVALID_HANDLE_VALUE)
391 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
392 &compression_format, sizeof(USHORT),
394 &bytes_returned, NULL))
396 DWORD err = GetLastError();
409 set_errno_from_GetLastError();
410 return WIMLIB_ERR_SET_ATTRIBUTES;
414 win32_set_reparse_data(const wchar_t *path, const u8 *rpbuf, u16 rpbuflen,
415 struct apply_ctx *ctx)
419 DWORD bytes_returned;
421 h = win32_open_existing_file(path, GENERIC_WRITE);
422 if (h == INVALID_HANDLE_VALUE)
425 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
426 (void*)rpbuf, rpbuflen,
427 NULL, 0, &bytes_returned, NULL))
428 goto error_close_handle;
436 err = GetLastError();
440 set_errno_from_GetLastError();
441 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
445 win32_set_short_name(const wchar_t *path, const wchar_t *short_name,
446 size_t short_name_nchars, struct apply_ctx *ctx)
451 h = win32_open_existing_file(path, GENERIC_WRITE | DELETE);
452 if (h == INVALID_HANDLE_VALUE)
455 if (short_name_nchars) {
456 if (!SetFileShortName(h, short_name))
457 goto error_close_handle;
458 } else if (running_on_windows_7_or_later()) {
459 if (!SetFileShortName(h, L""))
460 goto error_close_handle;
469 err = GetLastError();
473 set_errno_from_GetLastError();
474 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
478 do_win32_set_security_descriptor(HANDLE h, const wchar_t *path,
479 SECURITY_INFORMATION info,
480 PSECURITY_DESCRIPTOR desc)
483 return RtlNtStatusToDosError(NtSetSecurityObject(h, info, desc));
485 if (SetFileSecurity(path, info, desc))
486 return ERROR_SUCCESS;
488 return GetLastError();
493 win32_set_security_descriptor(const wchar_t *path, const u8 *desc,
494 size_t desc_size, struct apply_ctx *ctx)
496 SECURITY_INFORMATION info;
501 info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
502 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
503 h = INVALID_HANDLE_VALUE;
506 h = win32_open_existing_file(path, MAXIMUM_ALLOWED);
507 if (h == INVALID_HANDLE_VALUE) {
508 ERROR_WITH_ERRNO("Can't open %ls (%u)", path, GetLastError());
514 err = do_win32_set_security_descriptor(h, path, info,
515 (PSECURITY_DESCRIPTOR)desc);
516 if (err == ERROR_SUCCESS)
518 if ((err == ERROR_PRIVILEGE_NOT_HELD ||
519 err == ERROR_ACCESS_DENIED) &&
520 !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
522 if (info & SACL_SECURITY_INFORMATION) {
523 info &= ~SACL_SECURITY_INFORMATION;
524 ctx->partial_security_descriptors++;
527 if (info & DACL_SECURITY_INFORMATION) {
528 info &= ~DACL_SECURITY_INFORMATION;
531 if (info & OWNER_SECURITY_INFORMATION) {
532 info &= ~OWNER_SECURITY_INFORMATION;
535 ctx->partial_security_descriptors--;
536 ctx->no_security_descriptors++;
549 set_errno_from_GetLastError();
550 ret = WIMLIB_ERR_SET_SECURITY;
555 win32_set_timestamps(const wchar_t *path, u64 creation_time,
556 u64 last_write_time, u64 last_access_time,
557 struct apply_ctx *ctx)
561 FILETIME creationTime = {.dwLowDateTime = creation_time & 0xffffffff,
562 .dwHighDateTime = creation_time >> 32};
563 FILETIME lastAccessTime = {.dwLowDateTime = last_access_time & 0xffffffff,
564 .dwHighDateTime = last_access_time >> 32};
565 FILETIME lastWriteTime = {.dwLowDateTime = last_write_time & 0xffffffff,
566 .dwHighDateTime = last_write_time >> 32};
568 h = win32_open_existing_file(path, FILE_WRITE_ATTRIBUTES);
569 if (h == INVALID_HANDLE_VALUE)
572 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime))
573 goto error_close_handle;
581 err = GetLastError();
585 set_errno_from_GetLastError();
586 return WIMLIB_ERR_SET_TIMESTAMPS;
589 const struct apply_operations win32_apply_ops = {
592 .target_is_root = win32_path_is_root_of_drive,
593 .start_extract = win32_start_extract,
594 .create_file = win32_create_file,
595 .create_directory = win32_create_directory,
596 .create_hardlink = win32_create_hardlink,
597 .create_symlink = win32_create_symlink,
598 .extract_unnamed_stream = win32_extract_unnamed_stream,
599 .extract_named_stream = win32_extract_named_stream,
600 .extract_encrypted_stream = win32_extract_encrypted_stream,
601 .set_file_attributes = win32_set_file_attributes,
602 .set_reparse_data = win32_set_reparse_data,
603 .set_short_name = win32_set_short_name,
604 .set_security_descriptor = win32_set_security_descriptor,
605 .set_timestamps = win32_set_timestamps,
607 .path_prefix = L"\\\\?\\",
608 .path_prefix_nchars = 4,
609 .path_separator = L'\\',
612 .requires_realtarget_in_paths = 1,
613 .realpath_works_on_nonexisting_files = 1,
614 .root_directory_is_special = 1,
615 .requires_final_set_attributes_pass = 1,
616 .extract_encrypted_stream_creates_file = 1,
617 .requires_short_name_reordering = 1, /* TODO: check if this is really needed */
620 #endif /* __WIN32__ */