X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fwin32_capture.c;h=12212910f860500db4905a281fc7a971412b3daf;hb=51829aecdac415b417ab5b8ac897014bb780de10;hp=4bc0f7c2b89d6e564c2f7f0cb4a2d4dc671e664a;hpb=e3782beb9f112e4f4f3b41cf809f639e852932f5;p=wimlib diff --git a/src/win32_capture.c b/src/win32_capture.c index 4bc0f7c2..12212910 100644 --- a/src/win32_capture.c +++ b/src/win32_capture.c @@ -36,6 +36,7 @@ #include "wimlib/encoding.h" #include "wimlib/endianness.h" #include "wimlib/error.h" +#include "wimlib/object_id.h" #include "wimlib/paths.h" #include "wimlib/reparse.h" #include "wimlib/win32_vss.h" @@ -269,8 +270,8 @@ winnt_openat(HANDLE cur_dir, const wchar_t *path, size_t path_nchars, options |= FILE_SEQUENTIAL_ONLY; } retry: - status = (*func_NtOpenFile)(h_ret, perms, &attr, &iosb, - FILE_SHARE_VALID_FLAGS, options); + status = NtOpenFile(h_ret, perms, &attr, &iosb, + FILE_SHARE_VALID_FLAGS, options); if (!NT_SUCCESS(status)) { /* Try requesting fewer permissions */ if (status == STATUS_ACCESS_DENIED || @@ -332,14 +333,14 @@ read_winnt_stream_prefix(const struct windows_file *file, u64 bytes_remaining; int ret; - status = (*func_NtOpenFile)(&h, FILE_READ_DATA | SYNCHRONIZE, - &attr, &iosb, - FILE_SHARE_VALID_FLAGS, - FILE_OPEN_REPARSE_POINT | - FILE_OPEN_FOR_BACKUP_INTENT | - FILE_SYNCHRONOUS_IO_NONALERT | - FILE_SEQUENTIAL_ONLY | - (file->is_file_id ? FILE_OPEN_BY_FILE_ID : 0)); + status = NtOpenFile(&h, FILE_READ_DATA | SYNCHRONIZE, + &attr, &iosb, + FILE_SHARE_VALID_FLAGS, + FILE_OPEN_REPARSE_POINT | + FILE_OPEN_FOR_BACKUP_INTENT | + FILE_SYNCHRONOUS_IO_NONALERT | + FILE_SEQUENTIAL_ONLY | + (file->is_file_id ? FILE_OPEN_BY_FILE_ID : 0)); if (unlikely(!NT_SUCCESS(status))) { if (status == STATUS_SHARING_VIOLATION) { ERROR("Can't open %ls for reading:\n" @@ -365,8 +366,8 @@ read_winnt_stream_prefix(const struct windows_file *file, count = min(sizeof(buf), bytes_remaining); retry_read: - status = (*func_NtReadFile)(h, NULL, NULL, NULL, - &iosb, buf, count, NULL, NULL); + status = NtReadFile(h, NULL, NULL, NULL, + &iosb, buf, count, NULL, NULL); if (unlikely(!NT_SUCCESS(status))) { if (status == STATUS_END_OF_FILE) { ERROR("%ls: File was concurrently truncated", @@ -404,7 +405,7 @@ read_winnt_stream_prefix(const struct windows_file *file, if (ret) break; } - (*func_NtClose)(h); + NtClose(h); return ret; } @@ -512,8 +513,8 @@ winnt_get_short_name(HANDLE h, struct wim_dentry *dentry) u8 buf[128] _aligned_attribute(8); const FILE_NAME_INFORMATION *info; - status = (*func_NtQueryInformationFile)(h, &iosb, buf, sizeof(buf), - FileAlternateNameInformation); + status = NtQueryInformationFile(h, &iosb, buf, sizeof(buf), + FileAlternateNameInformation); info = (const FILE_NAME_INFORMATION *)buf; if (NT_SUCCESS(status) && info->FileNameLength != 0) { dentry->d_short_name = utf16le_dupz(info->FileName, @@ -594,11 +595,11 @@ winnt_load_security_descriptor(HANDLE h, struct wim_inode *inode, * ntdll function and therefore not officially part of the Win32 API. * Oh well. */ - while (!(NT_SUCCESS(status = (*func_NtQuerySecurityObject)(h, - requestedInformation, - (PSECURITY_DESCRIPTOR)buf, - bufsize, - &len_needed)))) + while (!NT_SUCCESS(status = NtQuerySecurityObject(h, + requestedInformation, + (PSECURITY_DESCRIPTOR)buf, + bufsize, + &len_needed))) { switch (status) { case STATUS_BUFFER_TOO_SMALL: @@ -655,6 +656,46 @@ out: return 0; } +/* Load a file's object ID into the corresponding WIM inode. */ +static noinline_for_stack int +winnt_load_object_id(HANDLE h, struct wim_inode *inode, + const wchar_t *full_path, struct winnt_scan_ctx *ctx) +{ + FILE_OBJECTID_BUFFER buffer; + NTSTATUS status; + u32 len; + + if (!(ctx->vol_flags & FILE_SUPPORTS_OBJECT_IDS)) + return 0; + + status = winnt_fsctl(h, FSCTL_GET_OBJECT_ID, NULL, 0, + &buffer, sizeof(buffer), &len); + + if (status == STATUS_OBJECTID_NOT_FOUND) /* No object ID */ + return 0; + + if (status == STATUS_INVALID_DEVICE_REQUEST) { + /* The filesystem claimed to support object IDs, but we can't + * actually read them. This happens with Samba. */ + ctx->vol_flags &= ~FILE_SUPPORTS_OBJECT_IDS; + return 0; + } + + if (!NT_SUCCESS(status)) { + winnt_error(status, L"\"%ls\": Can't read object ID", + printable_path(full_path)); + return WIMLIB_ERR_STAT; + } + + if (len == 0) /* No object ID (for directories) */ + return 0; + + if (!inode_set_object_id(inode, &buffer, len)) + return WIMLIB_ERR_NOMEM; + + return 0; +} + static int winnt_build_dentry_tree_recursive(struct wim_dentry **root_ret, HANDLE cur_dir, @@ -685,10 +726,10 @@ winnt_recurse_directory(HANDLE h, /* Using NtQueryDirectoryFile() we can re-use the same open handle, * which we opened with FILE_FLAG_BACKUP_SEMANTICS. */ - while (NT_SUCCESS(status = (*func_NtQueryDirectoryFile)(h, NULL, NULL, NULL, - &iosb, buf, bufsize, - FileNamesInformation, - FALSE, NULL, FALSE))) + while (NT_SUCCESS(status = NtQueryDirectoryFile(h, NULL, NULL, NULL, + &iosb, buf, bufsize, + FileNamesInformation, + FALSE, NULL, FALSE))) { const FILE_NAMES_INFORMATION *info = buf; for (;;) { @@ -756,18 +797,17 @@ file_has_ino_and_dev(HANDLE h, u64 ino, u64 dev) FILE_INTERNAL_INFORMATION int_info; FILE_FS_VOLUME_INFORMATION vol_info; - status = (*func_NtQueryInformationFile)(h, &iosb, - &int_info, sizeof(int_info), - FileInternalInformation); + status = NtQueryInformationFile(h, &iosb, &int_info, sizeof(int_info), + FileInternalInformation); if (!NT_SUCCESS(status)) return false; if (int_info.IndexNumber.QuadPart != ino) return false; - status = (*func_NtQueryVolumeInformationFile)(h, &iosb, - &vol_info, sizeof(vol_info), - FileFsVolumeInformation); + status = NtQueryVolumeInformationFile(h, &iosb, + &vol_info, sizeof(vol_info), + FileFsVolumeInformation); if (!(NT_SUCCESS(status) || status == STATUS_BUFFER_OVERFLOW)) return false; @@ -839,17 +879,17 @@ winnt_relativize_link_target(const wchar_t *target, size_t target_nbytes, name.MaximumLength = name.Length; /* Try opening the file */ - status = (*func_NtOpenFile) (&h, - FILE_READ_ATTRIBUTES | FILE_TRAVERSE, - &attr, - &iosb, - FILE_SHARE_VALID_FLAGS, - FILE_OPEN_FOR_BACKUP_INTENT); + status = NtOpenFile(&h, + FILE_READ_ATTRIBUTES | FILE_TRAVERSE, + &attr, + &iosb, + FILE_SHARE_VALID_FLAGS, + FILE_OPEN_FOR_BACKUP_INTENT); if (NT_SUCCESS(status)) { /* Reset root directory */ if (attr.RootDirectory) - (*func_NtClose)(attr.RootDirectory); + NtClose(attr.RootDirectory); attr.RootDirectory = h; name.Buffer = (wchar_t *)p; name.Length = 0; @@ -863,7 +903,7 @@ winnt_relativize_link_target(const wchar_t *target, size_t target_nbytes, out_close_root_dir: if (attr.RootDirectory) - (*func_NtClose)(attr.RootDirectory); + NtClose(attr.RootDirectory); while (p > target && *(p - 1) == L'\\') p--; return p; @@ -1197,11 +1237,11 @@ winnt_scan_data_streams(HANDLE h, const wchar_t *path, size_t path_nchars, goto unnamed_only; /* Get a buffer containing the stream information. */ - while (!NT_SUCCESS(status = (*func_NtQueryInformationFile)(h, - &iosb, - buf, - bufsize, - FileStreamInformation))) + while (!NT_SUCCESS(status = NtQueryInformationFile(h, + &iosb, + buf, + bufsize, + FileStreamInformation))) { switch (status) { @@ -1486,9 +1526,8 @@ get_file_info(HANDLE h, struct file_info *info) NTSTATUS status; FILE_ALL_INFORMATION all_info; - status = (*func_NtQueryInformationFile)(h, &iosb, &all_info, - sizeof(all_info), - FileAllInformation); + status = NtQueryInformationFile(h, &iosb, &all_info, sizeof(all_info), + FileAllInformation); if (unlikely(!NT_SUCCESS(status) && status != STATUS_BUFFER_OVERFLOW)) return status; @@ -1515,9 +1554,9 @@ get_volume_information(HANDLE h, const wchar_t *full_path, NTSTATUS status; /* Get volume flags */ - status = (*func_NtQueryVolumeInformationFile)(h, &iosb, attr_info, - sizeof(_attr_info), - FileFsAttributeInformation); + status = NtQueryVolumeInformationFile(h, &iosb, attr_info, + sizeof(_attr_info), + FileFsAttributeInformation); if (NT_SUCCESS(status)) { ctx->vol_flags = attr_info->FileSystemAttributes; ctx->is_ntfs = (attr_info->FileSystemNameLength == 4 * sizeof(wchar_t)) && @@ -1528,9 +1567,9 @@ get_volume_information(HANDLE h, const wchar_t *full_path, } /* Get volume ID. */ - status = (*func_NtQueryVolumeInformationFile)(h, &iosb, &vol_info, - sizeof(vol_info), - FileFsVolumeInformation); + status = NtQueryVolumeInformationFile(h, &iosb, &vol_info, + sizeof(vol_info), + FileFsVolumeInformation); if ((NT_SUCCESS(status) || status == STATUS_BUFFER_OVERFLOW) && (iosb.Information >= offsetof(FILE_FS_VOLUME_INFORMATION, VolumeSerialNumber) + @@ -1675,6 +1714,11 @@ winnt_build_dentry_tree_recursive(struct wim_dentry **root_ret, goto out; } + /* Get the file's object ID. */ + ret = winnt_load_object_id(h, inode, full_path, ctx); + if (ret) + goto out; + /* If this is a reparse point, load the reparse data. */ if (unlikely(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)) { ret = winnt_load_reparse_data(h, inode, full_path, ctx->params); @@ -1693,7 +1737,7 @@ winnt_build_dentry_tree_recursive(struct wim_dentry **root_ret, * ERROR_SHARING_VIOLATION if there are any open handles to the * file, we have to close the file and re-open it later if * needed. */ - (*func_NtClose)(h); + NtClose(h); h = NULL; ret = winnt_scan_efsrpc_raw_data(inode, full_path, full_path_nchars, ctx); @@ -1734,7 +1778,7 @@ winnt_build_dentry_tree_recursive(struct wim_dentry **root_ret, /* Re-open the directory with FILE_LIST_DIRECTORY access. */ if (h) { - (*func_NtClose)(h); + NtClose(h); h = NULL; } status = winnt_openat(cur_dir, relative_path, @@ -1763,7 +1807,7 @@ out_progress: ret = do_capture_progress(ctx->params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL); out: if (likely(h)) - (*func_NtClose)(h); + NtClose(h); if (unlikely(ret)) { free_dentry_tree(root, ctx->params->blob_table); root = NULL; @@ -1939,7 +1983,8 @@ struct ntfs_inode { u32 attributes; u32 security_id; u32 num_aliases; - u32 num_streams; + u32 num_streams : 31; + u32 have_object_id : 1; u32 first_stream_offset; struct ntfs_dentry *first_child; wchar_t short_name[13]; @@ -2110,6 +2155,13 @@ is_valid_stream_entry(const STREAM_LAYOUT_ENTRY *stream) stream->StreamIdentifierLength / 2); } +static bool +is_object_id_stream(const STREAM_LAYOUT_ENTRY *stream) +{ + return stream->StreamIdentifierLength == 24 && + !wmemcmp(stream->StreamIdentifier, L"::$OBJECT_ID", 12); +} + /* * If the specified STREAM_LAYOUT_ENTRY represents a DATA stream as opposed to * some other type of NTFS stream such as a STANDARD_INFORMATION stream, return @@ -2146,10 +2198,12 @@ use_stream(const FILE_LAYOUT_ENTRY *file, const STREAM_LAYOUT_ENTRY *stream, /* Validate the STREAM_LAYOUT_ENTRYs of the specified file and compute the total * length in bytes of the ntfs_stream structures needed to hold the stream - * information. */ + * information. In addition, set *have_object_id_ret=true if the file has an + * object ID stream. */ static int validate_streams_and_compute_total_length(const FILE_LAYOUT_ENTRY *file, - size_t *total_length_ret) + size_t *total_length_ret, + bool *have_object_id_ret) { const STREAM_LAYOUT_ENTRY *stream = (const void *)file + file->FirstStreamOffset; @@ -2173,6 +2227,8 @@ validate_streams_and_compute_total_length(const FILE_LAYOUT_ENTRY *file, if (use_stream(file, stream, &name, &name_nchars)) { total += ALIGN(sizeof(struct ntfs_stream) + (name_nchars + 1) * sizeof(wchar_t), 8); + } else if (is_object_id_stream(stream)) { + *have_object_id_ret = true; } if (stream->NextStreamOffset == 0) break; @@ -2276,6 +2332,7 @@ load_one_file(const FILE_LAYOUT_ENTRY *file, struct ntfs_inode_map *inode_map) size_t n; int ret; void *p; + bool have_object_id = false; inode_size = ALIGN(sizeof(struct ntfs_inode), 8); @@ -2292,7 +2349,8 @@ load_one_file(const FILE_LAYOUT_ENTRY *file, struct ntfs_inode_map *inode_map) } if (file_has_streams(file)) { - ret = validate_streams_and_compute_total_length(file, &n); + ret = validate_streams_and_compute_total_length(file, &n, + &have_object_id); if (ret) return ret; inode_size += n; @@ -2310,6 +2368,7 @@ load_one_file(const FILE_LAYOUT_ENTRY *file, struct ntfs_inode_map *inode_map) ni->last_write_time = info->BasicInformation.LastWriteTime; ni->last_access_time = info->BasicInformation.LastAccessTime; ni->security_id = info->SecurityId; + ni->have_object_id = have_object_id; p = FIRST_DENTRY(ni); @@ -2400,7 +2459,7 @@ load_files_from_mft(const wchar_t *path, struct ntfs_inode_map *inode_map) ret = 0; out: FREE(out); - (*func_NtClose)(h); + NtClose(h); return ret; } @@ -2552,7 +2611,8 @@ generate_wim_structures_recursive(struct wim_dentry **root_ret, * filter driver (WOF) hides reparse points from regular filesystem APIs * but not from FSCTL_QUERY_FILE_LAYOUT. */ if (ni->attributes & (FILE_ATTRIBUTE_REPARSE_POINT | - FILE_ATTRIBUTE_ENCRYPTED)) + FILE_ATTRIBUTE_ENCRYPTED) || + ni->have_object_id) { ret = winnt_build_dentry_tree_recursive(&root, NULL, @@ -2631,7 +2691,7 @@ generate_wim_structures_recursive(struct wim_dentry **root_ret, goto out; } ret = winnt_load_security_descriptor(h, inode, path, ctx); - (*func_NtClose)(h); + NtClose(h); if (ret) goto out; @@ -2848,7 +2908,7 @@ win32_build_dentry_tree(struct wim_dentry **root_ret, get_volume_information(h, path, &ctx); - (*func_NtClose)(h); + NtClose(h); #ifdef ENABLE_FAST_MFT_SCAN if (ctx.is_ntfs && !_wgetenv(L"WIMLIB_DISABLE_QUERY_FILE_LAYOUT")) {