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;
50 ctx->supported_features.compressed_files = !!(vol_flags & FILE_FILE_COMPRESSION);
51 ctx->supported_features.encrypted_files = !!(vol_flags & FILE_SUPPORTS_ENCRYPTION);
52 ctx->supported_features.not_context_indexed_files = 1;
53 ctx->supported_features.sparse_files = !!(vol_flags & FILE_SUPPORTS_SPARSE_FILES);
54 ctx->supported_features.named_data_streams = !!(vol_flags & FILE_NAMED_STREAMS);
55 ctx->supported_features.hard_links = !!(vol_flags & FILE_SUPPORTS_HARD_LINKS);
56 ctx->supported_features.reparse_points = !!(vol_flags & FILE_SUPPORTS_REPARSE_POINTS);
57 ctx->supported_features.security_descriptors = !!(vol_flags & FILE_PERSISTENT_ACLS);
58 ctx->supported_features.short_names = !!supports_SetFileShortName;
63 win32_create_file(const wchar_t *path, struct apply_ctx *ctx)
67 h = CreateFile(path, 0, 0, NULL, CREATE_ALWAYS,
68 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
69 if (h == INVALID_HANDLE_VALUE)
75 set_errno_from_GetLastError();
76 return WIMLIB_ERR_OPEN;
80 win32_create_directory(const wchar_t *path, struct apply_ctx *ctx)
82 if (!CreateDirectory(path, NULL))
83 if (GetLastError() != ERROR_ALREADY_EXISTS)
88 set_errno_from_GetLastError();
89 return WIMLIB_ERR_MKDIR;
93 win32_create_hardlink(const wchar_t *oldpath, const wchar_t *newpath,
94 struct apply_ctx *ctx)
96 if (!CreateHardLink(newpath, oldpath, NULL))
101 set_errno_from_GetLastError();
102 return WIMLIB_ERR_LINK;
106 win32_extract_wim_chunk(const void *buf, size_t len, void *arg)
108 HANDLE h = (HANDLE)arg;
109 DWORD nbytes_written;
111 if (unlikely(!WriteFile(h, buf, len, &nbytes_written, NULL)))
113 if (unlikely(nbytes_written != len))
118 set_errno_from_GetLastError();
119 return WIMLIB_ERR_WRITE;
123 win32_extract_stream(const wchar_t *path, const wchar_t *stream_name,
124 size_t stream_name_nchars,
125 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
127 DWORD creationDisposition = OPEN_EXISTING;
128 wchar_t *stream_path = (wchar_t*)path;
132 if (stream_name_nchars) {
133 creationDisposition = CREATE_ALWAYS;
134 stream_path = alloca(sizeof(wchar_t) *
136 wcslen(stream_name) + 1));
137 swprintf(stream_path, L"%ls:%ls", path, stream_name);
140 h = CreateFile(stream_path, FILE_WRITE_DATA, 0, NULL,
141 creationDisposition, FILE_FLAG_BACKUP_SEMANTICS |
142 FILE_FLAG_OPEN_REPARSE_POINT,
144 if (h == INVALID_HANDLE_VALUE)
149 goto out_close_handle;
150 ret = extract_wim_resource(lte, wim_resource_size(lte),
151 win32_extract_wim_chunk, h);
160 set_errno_from_GetLastError();
161 return WIMLIB_ERR_WRITE;
165 win32_extract_unnamed_stream(file_spec_t file,
166 struct wim_lookup_table_entry *lte,
167 struct apply_ctx *ctx)
169 return win32_extract_stream(file.path, NULL, 0, lte, ctx);
173 win32_extract_named_stream(file_spec_t file, const wchar_t *stream_name,
174 size_t stream_name_nchars,
175 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
177 return win32_extract_stream(file.path, stream_name,
178 stream_name_nchars, lte, ctx);
181 struct win32_encrypted_extract_ctx {
182 const struct wim_lookup_table_entry *lte;
187 win32_encrypted_import_cb(unsigned char *data, void *_import_ctx,
188 unsigned long *len_p)
190 struct win32_encrypted_extract_ctx *import_ctx = _import_ctx;
191 unsigned long len = *len_p;
192 const struct wim_lookup_table_entry *lte = import_ctx->lte;
194 len = min(len, wim_resource_size(lte) - import_ctx->offset);
196 if (read_partial_wim_resource_into_buf(lte, len, import_ctx->offset, data))
197 return ERROR_READ_FAULT;
199 import_ctx->offset += len;
201 return ERROR_SUCCESS;
205 win32_extract_encrypted_stream(const wchar_t *path,
206 struct wim_lookup_table_entry *lte,
207 struct apply_ctx *ctx)
212 struct win32_encrypted_extract_ctx extract_ctx;
214 err = OpenEncryptedFileRaw(path, CREATE_FOR_IMPORT, &file_ctx);
215 if (err != ERROR_SUCCESS) {
216 errno = win32_error_to_errno(err);
217 ret = WIMLIB_ERR_OPEN;
221 extract_ctx.lte = lte;
222 extract_ctx.offset = 0;
223 err = WriteEncryptedFileRaw(win32_encrypted_import_cb, &extract_ctx,
225 if (err != ERROR_SUCCESS) {
226 errno = win32_error_to_errno(err);
227 ret = WIMLIB_ERR_WRITE;
233 CloseEncryptedFileRaw(file_ctx);
239 win32_set_special_file_attributes(const wchar_t *path, u32 attributes)
243 USHORT compression_format = COMPRESSION_FORMAT_DEFAULT;
244 DWORD bytes_returned;
246 h = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
247 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
248 FILE_FLAG_OPEN_REPARSE_POINT,
250 if (h == INVALID_HANDLE_VALUE)
253 if (attributes & FILE_ATTRIBUTE_SPARSE_FILE)
254 if (!DeviceIoControl(h, FSCTL_SET_SPARSE,
257 &bytes_returned, NULL))
258 goto error_close_handle;
260 if (attributes & FILE_ATTRIBUTE_COMPRESSED)
261 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
262 &compression_format, sizeof(USHORT),
264 &bytes_returned, NULL))
265 goto error_close_handle;
270 if (attributes & FILE_ATTRIBUTE_ENCRYPTED)
271 if (!EncryptFile(path))
277 err = GetLastError();
285 win32_set_file_attributes(const wchar_t *path, u32 attributes,
286 struct apply_ctx *ctx)
288 u32 special_attributes =
289 FILE_ATTRIBUTE_REPARSE_POINT |
290 FILE_ATTRIBUTE_DIRECTORY |
291 FILE_ATTRIBUTE_SPARSE_FILE |
292 FILE_ATTRIBUTE_COMPRESSED |
293 FILE_ATTRIBUTE_ENCRYPTED;
294 u32 actual_attributes;
296 if (!SetFileAttributes(path, attributes & ~special_attributes))
299 if (attributes & (FILE_ATTRIBUTE_SPARSE_FILE |
300 FILE_ATTRIBUTE_ENCRYPTED |
301 FILE_ATTRIBUTE_COMPRESSED))
302 if (!win32_set_special_file_attributes(path, attributes))
305 /* If file is not supposed to be encrypted or compressed, remove
306 * defaulted encrypted or compressed attributes (from creating file in
307 * encrypted or compressed directory). */
308 actual_attributes = GetFileAttributes(path);
309 if (actual_attributes == INVALID_FILE_ATTRIBUTES)
312 if ((actual_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
313 !(attributes & FILE_ATTRIBUTE_ENCRYPTED))
314 if (!DecryptFile(path, 0))
316 if ((actual_attributes & FILE_ATTRIBUTE_COMPRESSED) &&
317 !(attributes & FILE_ATTRIBUTE_COMPRESSED))
320 DWORD bytes_returned;
321 USHORT compression_format = COMPRESSION_FORMAT_NONE;
323 h = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
324 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
325 FILE_FLAG_OPEN_REPARSE_POINT,
327 if (h == INVALID_HANDLE_VALUE)
330 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
331 &compression_format, sizeof(USHORT),
333 &bytes_returned, NULL))
335 DWORD err = GetLastError();
349 set_errno_from_GetLastError();
350 return WIMLIB_ERR_SET_ATTRIBUTES;
354 win32_set_reparse_data(const wchar_t *path, const u8 *rpbuf, u16 rpbuflen,
355 struct apply_ctx *ctx)
359 DWORD bytes_returned;
361 h = CreateFile(path, GENERIC_WRITE, 0, NULL,
362 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
363 FILE_FLAG_OPEN_REPARSE_POINT,
365 if (h == INVALID_HANDLE_VALUE)
368 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
369 (void*)rpbuf, rpbuflen,
370 NULL, 0, &bytes_returned, NULL))
371 goto error_close_handle;
379 err = GetLastError();
383 set_errno_from_GetLastError();
384 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
388 win32_set_short_name(const wchar_t *path, const wchar_t *short_name,
389 size_t short_name_nchars, struct apply_ctx *ctx)
394 h = CreateFile(path, GENERIC_WRITE | DELETE, 0, NULL,
395 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
396 FILE_FLAG_OPEN_REPARSE_POINT,
398 if (h == INVALID_HANDLE_VALUE)
401 if (short_name_nchars) {
402 if (!SetFileShortName(h, short_name))
403 goto error_close_handle;
404 } else if (running_on_windows_7_or_later()) {
405 if (!SetFileShortName(h, L""))
406 goto error_close_handle;
415 err = GetLastError();
419 set_errno_from_GetLastError();
420 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
424 win32_set_security_descriptor(const wchar_t *path, const u8 *desc, size_t desc_size,
425 struct apply_ctx *ctx)
427 SECURITY_INFORMATION info;
429 info = OWNER_SECURITY_INFORMATION |
430 GROUP_SECURITY_INFORMATION |
431 DACL_SECURITY_INFORMATION |
432 SACL_SECURITY_INFORMATION;
434 if (!SetFileSecurity(path, info, (PSECURITY_DESCRIPTOR)desc)) {
435 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
436 GetLastError() == ERROR_PRIVILEGE_NOT_HELD &&
437 (info & SACL_SECURITY_INFORMATION))
439 info &= ~SACL_SECURITY_INFORMATION;
447 set_errno_from_GetLastError();
448 return WIMLIB_ERR_SET_SECURITY;
452 win32_set_timestamps(const wchar_t *path, u64 creation_time,
453 u64 last_write_time, u64 last_access_time,
454 struct apply_ctx *ctx)
458 FILETIME creationTime = {.dwLowDateTime = creation_time & 0xffffffff,
459 .dwHighDateTime = creation_time >> 32};
460 FILETIME lastAccessTime = {.dwLowDateTime = last_access_time & 0xffffffff,
461 .dwHighDateTime = last_access_time >> 32};
462 FILETIME lastWriteTime = {.dwLowDateTime = last_write_time & 0xffffffff,
463 .dwHighDateTime = last_write_time >> 32};
465 h = CreateFile(path, FILE_WRITE_ATTRIBUTES, 0, NULL,
466 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
467 FILE_FLAG_OPEN_REPARSE_POINT,
469 if (h == INVALID_HANDLE_VALUE)
472 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime))
473 goto error_close_handle;
481 err = GetLastError();
485 set_errno_from_GetLastError();
486 return WIMLIB_ERR_SET_TIMESTAMPS;
489 const struct apply_operations win32_apply_ops = {
492 .target_is_root = win32_path_is_root_of_drive,
493 .start_extract = win32_start_extract,
494 .create_file = win32_create_file,
495 .create_directory = win32_create_directory,
496 .create_hardlink = win32_create_hardlink,
497 .extract_unnamed_stream = win32_extract_unnamed_stream,
498 .extract_named_stream = win32_extract_named_stream,
499 .extract_encrypted_stream = win32_extract_encrypted_stream,
500 .set_file_attributes = win32_set_file_attributes,
501 .set_reparse_data = win32_set_reparse_data,
502 .set_short_name = win32_set_short_name,
503 .set_security_descriptor = win32_set_security_descriptor,
504 .set_timestamps = win32_set_timestamps,
506 .path_prefix = L"\\\\?\\",
507 .path_prefix_nchars = 4,
508 .path_separator = L'\\',
511 .requires_realtarget_in_paths = 1,
512 .realpath_works_on_nonexisting_files = 1,
513 .root_directory_is_special = 1,
516 #endif /* __WIN32__ */