- DEBUG("Opening \"%ls\"", stream_path);
- requestedAccess = GENERIC_READ | GENERIC_WRITE |
- ACCESS_SYSTEM_SECURITY;
- /* DELETE access is needed for SetFileShortNameW(), for some reason.
- * But don't request it for the extraction root, for the following
- * reasons:
- *
- * - Requesting DELETE access on the extraction root will cause a
- * sharing violation if the extraction root is the current working
- * directory (".").
- * - The extraction root may be extracted to a different name than given
- * in the WIM file, in which case the DOS name, if given, would not be
- * meaningful.
- * - For full-image extractions, the root dentry is supposed to be
- * unnamed anyway.
- * - Microsoft's ImageX does not extract the root directory.
- */
- if (dentry != args->extract_root)
- requestedAccess |= DELETE;
-try_open_again:
- /* Open the stream to be extracted. Depending on what we have set
- * creationDisposition to, we may be creating this for the first time,
- * or we may be opening on existing stream we already created using
- * CreateDirectoryW() or OpenEncryptedFileRawW(). */
- h = CreateFileW(stream_path,
- requestedAccess,
- FILE_SHARE_READ,
- NULL,
- creationDisposition,
- win32_get_create_flags_and_attributes(inode->i_attributes),
- NULL);
- if (h == INVALID_HANDLE_VALUE) {
- err = GetLastError();
- if (err == ERROR_ACCESS_DENIED &&
- win32_path_is_root_of_drive(stream_path))
- {
- ret = 0;
- goto out;
- }
- if ((err == ERROR_PRIVILEGE_NOT_HELD ||
- err == ERROR_ACCESS_DENIED) &&
- (requestedAccess & ACCESS_SYSTEM_SECURITY))
- {
- /* Try opening the file again without privilege to
- * modify SACL. */
- requestedAccess &= ~ACCESS_SYSTEM_SECURITY;
- goto try_open_again;
- }
- if (err == ERROR_SHARING_VIOLATION &&
- (inode->i_attributes & (FILE_ATTRIBUTE_ENCRYPTED |
- FILE_ATTRIBUTE_DIRECTORY)) ==
- (FILE_ATTRIBUTE_ENCRYPTED | FILE_ATTRIBUTE_DIRECTORY))
- {
- if (remaining_sharing_violations) {
- --remaining_sharing_violations;
- /* This can happen when restoring encrypted directories
- * for some reason. Probably a bug in EncryptFile(). */
- WARNING("Couldn't open \"%ls\" due to sharing violation; "
- "re-trying after 100ms", stream_path);
- Sleep(100);
- goto try_open_again;
- } else {
- ERROR("Too many sharing violations; giving up...");
- }
- }
- if (creationDisposition == OPEN_EXISTING)
- ERROR("Failed to open \"%ls\"", stream_path);
- else
- ERROR("Failed to create \"%ls\"", stream_path);
- win32_error(err);
- ret = WIMLIB_ERR_OPEN;
- goto fail;
- }