- DWORD creationDisposition = CREATE_ALWAYS;
- DWORD requestedAccess;
- BY_HANDLE_FILE_INFORMATION file_info;
- unsigned remaining_sharing_violations = 1000;
- const struct wim_inode *inode = dentry->d_inode;
-
- if (stream_name_utf16) {
- /* Named stream. Create a buffer that contains the UTF-16LE
- * string [./]path:stream_name_utf16. This is needed to
- * create and open the stream using CreateFileW(). I'm not
- * aware of any other APIs to do this. Note: the '$DATA' suffix
- * seems to be unneeded. Additional note: a "./" prefix needs
- * to be added when the path is not absolute to avoid ambiguity
- * with drive letters. */
- size_t stream_path_nchars;
- size_t path_nchars;
- size_t stream_name_nchars;
- const wchar_t *prefix;
-
- path_nchars = wcslen(path);
- stream_name_nchars = wcslen(stream_name_utf16);
- stream_path_nchars = path_nchars + 1 + stream_name_nchars;
- if (path[0] != cpu_to_le16(L'\0') &&
- path[0] != cpu_to_le16(L'/') &&
- path[0] != cpu_to_le16(L'\\') &&
- path[1] != cpu_to_le16(L':'))
- {
- prefix = L"./";
- stream_path_nchars += 2;
- } else {
- prefix = L"";
- }
- stream_path = alloca((stream_path_nchars + 1) * sizeof(wchar_t));
- swprintf(stream_path, L"%ls%ls:%ls",
- prefix, path, stream_name_utf16);
- } else {
- /* Unnamed stream; its path is just the path to the file itself.
- * */
- stream_path = (wchar_t*)path;
-
- ret = win32_begin_extract_unnamed_stream(inode, lte, path,
- &creationDisposition,
- args->vol_flags);
- if (ret)
- goto fail;
- }
-
- DEBUG("Opening \"%ls\"", stream_path);
- /* DELETE access is needed for SetFileShortNameW(), for some reason. */
- requestedAccess = GENERIC_READ | GENERIC_WRITE | DELETE |
- ACCESS_SYSTEM_SECURITY;
-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 &&
- 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) {
- 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...");
- }
- } else {
- 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;
- }