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;
85 win32_create_file(const wchar_t *path, struct apply_ctx *ctx, u64 *cookie_ret)
89 h = CreateFile(path, 0, 0, NULL, CREATE_ALWAYS,
90 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, NULL);
91 if (h == INVALID_HANDLE_VALUE)
97 set_errno_from_GetLastError();
98 return WIMLIB_ERR_OPEN;
102 win32_create_directory(const wchar_t *path, struct apply_ctx *ctx,
105 if (!CreateDirectory(path, NULL))
106 if (GetLastError() != ERROR_ALREADY_EXISTS)
111 set_errno_from_GetLastError();
112 return WIMLIB_ERR_MKDIR;
116 win32_create_hardlink(const wchar_t *oldpath, const wchar_t *newpath,
117 struct apply_ctx *ctx)
119 if (!CreateHardLink(newpath, oldpath, NULL)) {
120 if (GetLastError() != ERROR_ALREADY_EXISTS)
122 if (!DeleteFile(newpath))
124 if (!CreateHardLink(newpath, oldpath, NULL))
130 set_errno_from_GetLastError();
131 return WIMLIB_ERR_LINK;
135 win32_create_symlink(const wchar_t *oldpath, const wchar_t *newpath,
136 struct apply_ctx *ctx)
138 if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0)) {
139 if (GetLastError() != ERROR_ALREADY_EXISTS)
141 if (!DeleteFile(newpath))
143 if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0))
149 set_errno_from_GetLastError();
150 return WIMLIB_ERR_LINK;
154 win32_extract_wim_chunk(const void *buf, size_t len, void *arg)
156 HANDLE h = (HANDLE)arg;
157 DWORD nbytes_written;
159 if (unlikely(!WriteFile(h, buf, len, &nbytes_written, NULL)))
161 if (unlikely(nbytes_written != len))
166 set_errno_from_GetLastError();
167 return WIMLIB_ERR_WRITE;
171 win32_extract_stream(const wchar_t *path, const wchar_t *stream_name,
172 size_t stream_name_nchars,
173 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
175 DWORD creationDisposition = OPEN_EXISTING;
176 wchar_t *stream_path = (wchar_t*)path;
180 if (stream_name_nchars) {
181 creationDisposition = CREATE_ALWAYS;
182 stream_path = alloca(sizeof(wchar_t) *
184 wcslen(stream_name) + 1));
185 swprintf(stream_path, L"%ls:%ls", path, stream_name);
188 h = CreateFile(stream_path, FILE_WRITE_DATA, 0, NULL,
189 creationDisposition, FILE_FLAG_BACKUP_SEMANTICS |
190 FILE_FLAG_OPEN_REPARSE_POINT,
192 if (h == INVALID_HANDLE_VALUE)
197 goto out_close_handle;
198 ret = extract_wim_resource(lte, wim_resource_size(lte),
199 win32_extract_wim_chunk, h);
208 set_errno_from_GetLastError();
209 return WIMLIB_ERR_WRITE;
213 win32_extract_unnamed_stream(file_spec_t file,
214 struct wim_lookup_table_entry *lte,
215 struct apply_ctx *ctx)
217 return win32_extract_stream(file.path, NULL, 0, lte, ctx);
221 win32_extract_named_stream(file_spec_t file, const wchar_t *stream_name,
222 size_t stream_name_nchars,
223 struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
225 return win32_extract_stream(file.path, stream_name,
226 stream_name_nchars, lte, ctx);
229 struct win32_encrypted_extract_ctx {
230 const struct wim_lookup_table_entry *lte;
235 win32_encrypted_import_cb(unsigned char *data, void *_import_ctx,
236 unsigned long *len_p)
238 struct win32_encrypted_extract_ctx *import_ctx = _import_ctx;
239 unsigned long len = *len_p;
240 const struct wim_lookup_table_entry *lte = import_ctx->lte;
242 len = min(len, wim_resource_size(lte) - import_ctx->offset);
244 if (read_partial_wim_resource_into_buf(lte, len, import_ctx->offset, data))
245 return ERROR_READ_FAULT;
247 import_ctx->offset += len;
249 return ERROR_SUCCESS;
253 win32_extract_encrypted_stream(file_spec_t file,
254 struct wim_lookup_table_entry *lte,
255 struct apply_ctx *ctx)
257 const tchar *path = file.path;
261 struct win32_encrypted_extract_ctx extract_ctx;
263 err = OpenEncryptedFileRaw(path, CREATE_FOR_IMPORT, &file_ctx);
264 if (err != ERROR_SUCCESS) {
265 errno = win32_error_to_errno(err);
266 ret = WIMLIB_ERR_OPEN;
270 extract_ctx.lte = lte;
271 extract_ctx.offset = 0;
272 err = WriteEncryptedFileRaw(win32_encrypted_import_cb, &extract_ctx,
274 if (err != ERROR_SUCCESS) {
275 errno = win32_error_to_errno(err);
276 ret = WIMLIB_ERR_WRITE;
282 CloseEncryptedFileRaw(file_ctx);
288 win32_set_special_file_attributes(const wchar_t *path, u32 attributes)
292 USHORT compression_format = COMPRESSION_FORMAT_DEFAULT;
293 DWORD bytes_returned;
295 h = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
296 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
297 FILE_FLAG_OPEN_REPARSE_POINT,
299 if (h == INVALID_HANDLE_VALUE)
302 if (attributes & FILE_ATTRIBUTE_SPARSE_FILE)
303 if (!DeviceIoControl(h, FSCTL_SET_SPARSE,
306 &bytes_returned, NULL))
307 goto error_close_handle;
309 if (attributes & FILE_ATTRIBUTE_COMPRESSED)
310 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
311 &compression_format, sizeof(USHORT),
313 &bytes_returned, NULL))
314 goto error_close_handle;
319 if (attributes & FILE_ATTRIBUTE_ENCRYPTED)
320 if (!EncryptFile(path))
326 err = GetLastError();
334 win32_set_file_attributes(const wchar_t *path, u32 attributes,
335 struct apply_ctx *ctx)
337 u32 special_attributes =
338 FILE_ATTRIBUTE_REPARSE_POINT |
339 FILE_ATTRIBUTE_DIRECTORY |
340 FILE_ATTRIBUTE_SPARSE_FILE |
341 FILE_ATTRIBUTE_COMPRESSED |
342 FILE_ATTRIBUTE_ENCRYPTED;
343 u32 actual_attributes;
345 if (!SetFileAttributes(path, attributes & ~special_attributes))
348 if (attributes & (FILE_ATTRIBUTE_SPARSE_FILE |
349 FILE_ATTRIBUTE_ENCRYPTED |
350 FILE_ATTRIBUTE_COMPRESSED))
351 if (!win32_set_special_file_attributes(path, attributes))
354 /* If file is not supposed to be encrypted or compressed, remove
355 * defaulted encrypted or compressed attributes (from creating file in
356 * encrypted or compressed directory). */
357 actual_attributes = GetFileAttributes(path);
358 if (actual_attributes == INVALID_FILE_ATTRIBUTES)
361 if ((actual_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
362 !(attributes & FILE_ATTRIBUTE_ENCRYPTED))
363 if (!DecryptFile(path, 0))
365 if ((actual_attributes & FILE_ATTRIBUTE_COMPRESSED) &&
366 !(attributes & FILE_ATTRIBUTE_COMPRESSED))
369 DWORD bytes_returned;
370 USHORT compression_format = COMPRESSION_FORMAT_NONE;
372 h = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
373 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
374 FILE_FLAG_OPEN_REPARSE_POINT,
376 if (h == INVALID_HANDLE_VALUE)
379 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
380 &compression_format, sizeof(USHORT),
382 &bytes_returned, NULL))
384 DWORD err = GetLastError();
397 set_errno_from_GetLastError();
398 return WIMLIB_ERR_SET_ATTRIBUTES;
402 win32_set_reparse_data(const wchar_t *path, const u8 *rpbuf, u16 rpbuflen,
403 struct apply_ctx *ctx)
407 DWORD bytes_returned;
409 h = CreateFile(path, GENERIC_WRITE, 0, NULL,
410 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
411 FILE_FLAG_OPEN_REPARSE_POINT,
413 if (h == INVALID_HANDLE_VALUE)
416 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
417 (void*)rpbuf, rpbuflen,
418 NULL, 0, &bytes_returned, NULL))
419 goto error_close_handle;
427 err = GetLastError();
431 set_errno_from_GetLastError();
432 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
436 win32_set_short_name(const wchar_t *path, const wchar_t *short_name,
437 size_t short_name_nchars, struct apply_ctx *ctx)
442 h = CreateFile(path, GENERIC_WRITE | DELETE, 0, NULL,
443 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
444 FILE_FLAG_OPEN_REPARSE_POINT,
446 if (h == INVALID_HANDLE_VALUE)
449 if (short_name_nchars) {
450 if (!SetFileShortName(h, short_name))
451 goto error_close_handle;
452 } else if (running_on_windows_7_or_later()) {
453 if (!SetFileShortName(h, L""))
454 goto error_close_handle;
463 err = GetLastError();
467 set_errno_from_GetLastError();
468 return WIMLIB_ERR_WRITE; /* XXX: need better error code */
472 win32_set_security_descriptor(const wchar_t *path, const u8 *desc, size_t desc_size,
473 struct apply_ctx *ctx)
475 SECURITY_INFORMATION info;
477 info = OWNER_SECURITY_INFORMATION |
478 GROUP_SECURITY_INFORMATION |
479 DACL_SECURITY_INFORMATION |
480 SACL_SECURITY_INFORMATION;
482 if (!SetFileSecurity(path, info, (PSECURITY_DESCRIPTOR)desc)) {
483 if (!(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS) &&
484 GetLastError() == ERROR_PRIVILEGE_NOT_HELD &&
485 (info & SACL_SECURITY_INFORMATION))
487 info &= ~SACL_SECURITY_INFORMATION;
495 set_errno_from_GetLastError();
496 return WIMLIB_ERR_SET_SECURITY;
500 win32_set_timestamps(const wchar_t *path, u64 creation_time,
501 u64 last_write_time, u64 last_access_time,
502 struct apply_ctx *ctx)
506 FILETIME creationTime = {.dwLowDateTime = creation_time & 0xffffffff,
507 .dwHighDateTime = creation_time >> 32};
508 FILETIME lastAccessTime = {.dwLowDateTime = last_access_time & 0xffffffff,
509 .dwHighDateTime = last_access_time >> 32};
510 FILETIME lastWriteTime = {.dwLowDateTime = last_write_time & 0xffffffff,
511 .dwHighDateTime = last_write_time >> 32};
513 h = CreateFile(path, FILE_WRITE_ATTRIBUTES, 0, NULL,
514 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS |
515 FILE_FLAG_OPEN_REPARSE_POINT,
517 if (h == INVALID_HANDLE_VALUE)
520 if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime))
521 goto error_close_handle;
529 err = GetLastError();
533 set_errno_from_GetLastError();
534 return WIMLIB_ERR_SET_TIMESTAMPS;
537 const struct apply_operations win32_apply_ops = {
540 .target_is_root = win32_path_is_root_of_drive,
541 .start_extract = win32_start_extract,
542 .create_file = win32_create_file,
543 .create_directory = win32_create_directory,
544 .create_hardlink = win32_create_hardlink,
545 .create_symlink = win32_create_symlink,
546 .extract_unnamed_stream = win32_extract_unnamed_stream,
547 .extract_named_stream = win32_extract_named_stream,
548 .extract_encrypted_stream = win32_extract_encrypted_stream,
549 .set_file_attributes = win32_set_file_attributes,
550 .set_reparse_data = win32_set_reparse_data,
551 .set_short_name = win32_set_short_name,
552 .set_security_descriptor = win32_set_security_descriptor,
553 .set_timestamps = win32_set_timestamps,
555 .path_prefix = L"\\\\?\\",
556 .path_prefix_nchars = 4,
557 .path_separator = L'\\',
560 .requires_realtarget_in_paths = 1,
561 .realpath_works_on_nonexisting_files = 1,
562 .root_directory_is_special = 1,
565 #endif /* __WIN32__ */