From 2f7c4252ceea10c674bd49ee423e4784a86e24da Mon Sep 17 00:00:00 2001 From: Marouane-elfadli Date: Fri, 30 Oct 2026 12:02:37 +0100 Subject: [PATCH 1/2] Fix: use incremental name instead of localized Copy suffix on rename and move conflicts Closes #19083 Signed-off-by: Marouane-elfadli --- .../Operations/FileOperationsHelpers.cs | 2326 +++++++++-------- 1 file changed, 1168 insertions(+), 1158 deletions(-) diff --git a/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs b/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs index 7ca8658d6713..6058c5264991 100644 --- a/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs +++ b/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs @@ -1,1158 +1,1168 @@ -// Copyright (c) Files Community -// Licensed under the MIT License. - -using Files.App.Utils.Storage.Operations; -using Files.Shared.Helpers; -using Microsoft.Extensions.Logging; -using System.Collections.Concurrent; -using System.IO; -using Windows.ApplicationModel.DataTransfer; -using Windows.Win32; -using Windows.Win32.UI.WindowsAndMessaging; -using FILEOPERATION_FLAGS = Windows.Win32.UI.Shell.FILEOPERATION_FLAGS; -using HRESULT = Windows.Win32.Foundation.HRESULT; -using HWND = Windows.Win32.Foundation.HWND; -using PROPERTYKEY = Windows.Win32.Foundation.PROPERTYKEY; -using SLR_FLAGS = Windows.Win32.UI.Shell.SLR_FLAGS; - -namespace Files.App.Utils.Storage -{ - public sealed partial class FileOperationsHelpers - { - private static readonly PROPERTYKEY PKEY_FilePlaceholderStatus = new() { fmtid = new("B2F9B9D6-FEC4-4DD5-94D7-8957488C807B"), pid = 2 }; - private const uint PS_CLOUDFILE_PLACEHOLDER = 8; - - private static ProgressHandler? progressHandler; // Warning: must be initialized from a MTA thread - - public static Task SetClipboard(string[] filesToCopy, DataPackageOperation operation) - { - return STATask.Run(() => - { - uint preferredDropEffect = (uint)(operation == DataPackageOperation.Copy - ? DataPackageOperation.Copy | DataPackageOperation.Link - : DataPackageOperation.Move); - ShellDataObject.SetClipboard(filesToCopy, preferredDropEffect); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> CreateItemAsync(string filePath, string fileOp, long ownerHwnd, bool asAdmin, string? template = null, byte[]? dataBytes = null) - { - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION | FILEOPERATION_FLAGS.FOF_NOERRORUI; - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - var shellOperationResult = new ShellOperationResult(); - var parentPath = Path.GetDirectoryName(filePath); - - if (parentPath is null || !SafetyExtensions.IgnoreExceptions(() => - { - using var shd = new ShellFolder(parentPath); - op.QueueNewItemOperation(shd, Path.GetFileName(filePath), - fileOp == "CreateFolder" ? FileAttributes.Directory : FileAttributes.Normal, template); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Destination = filePath, - HResult = -1 - }); - } - - var createTcs = new TaskCompletionSource(); - op.PostNewItem += (s, e) => - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Destination = e.DestItem.GetParsingPath(), - HResult = (int)e.Result - }); - }; - op.FinishOperations += (s, e) => createTcs.TrySetResult(e.Result.Succeeded); - - try - { - op.PerformOperations(); - } - catch - { - createTcs.TrySetResult(false); - } - - if (dataBytes is not null && - shellOperationResult.Items.SingleOrDefault() is { Succeeded: true, Destination: { } destination }) - { - SafetyExtensions.IgnoreExceptions(() => - { - using var fs = new FileStream(destination, FileMode.Open); - fs.Write(dataBytes, 0, dataBytes.Length); - fs.Flush(); - }, App.Logger); - } - - return (await createTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> TestRecycleAsync(string[] fileToDeletePath) - { - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; - op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE; - - var shellOperationResult = new ShellOperationResult(); - var tryDelete = false; - - for (var i = 0; i < fileToDeletePath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using var shi = new ShellItem(fileToDeletePath[i]); - using var file = SafetyExtensions.IgnoreExceptions(() => GetFirstFile(shi)) ?? shi; - if (file.Properties.TryGetValue(PKEY_FilePlaceholderStatus, out var value) && value == PS_CLOUDFILE_PLACEHOLDER) - { - // Online only files cannot be tried for deletion, so they are treated as to be permanently deleted. - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToDeletePath[i], - HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND - }); - } - else - { - op.QueueDeleteOperation(file); - tryDelete = true; - } - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToDeletePath[i], - HResult = -1 - }); - } - } - - if (!tryDelete) - return (true, shellOperationResult); - - var deleteTcs = new TaskCompletionSource(); - op.PreDeleteItem += [DebuggerHidden] (s, e) => - { - if ((e.Flags & Windows.Win32.UI.Shell._TRANSFER_SOURCE_FLAGS.TSF_DELETE_RECYCLE_IF_POSSIBLE) is 0) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = e.SourceItem.GetParsingPath(), - HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND - }); - throw new Win32Exception((int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND); // E_FAIL, stops operation - } - else - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = true, - Source = e.SourceItem.GetParsingPath(), - HResult = (int)HRESULT.COPYENGINE_E_USER_CANCELLED - }); - throw new Win32Exception((int)HRESULT.COPYENGINE_E_USER_CANCELLED); // E_FAIL, stops operation - } - }; - op.FinishOperations += (s, e) => deleteTcs.TrySetResult(e.Result.Succeeded); - - try - { - op.PerformOperations(); - } - catch - { - deleteTcs.TrySetResult(false); - } - - return (await deleteTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> DeleteItemAsync(string[] fileToDeletePath, bool permanently, long ownerHwnd, bool asAdmin, IProgress? progress, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - StatusCenterItemProgressModel fsProgress = new( - progress, - false, - FileSystemStatusCode.InProgress); - - var cts = new CancellationTokenSource(); - var sizeCalculator = new FileSizeCalculator(fileToDeletePath); - - // Track the count and update the progress - sizeCalculator.ItemsCountChanged += (newCount) => - { - fsProgress.ItemsCount = newCount; - fsProgress.Report(); - }; - - var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); - sizeTask.ContinueWith(_ => - { - fsProgress.TotalSize = 0; - fsProgress.ItemsCount = sizeCalculator.ItemsCount; - fsProgress.EnumerationCompleted = true; - fsProgress.Report(); - }); - - fsProgress.Report(); - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; - - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - if (!permanently) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE | FILEOPERATION_FLAGS.FOF_WANTNUKEWARNING; - } - - var shellOperationResult = new ShellOperationResult(); - - for (var i = 0; i < fileToDeletePath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using var shi = new ShellItem(fileToDeletePath[i]); - - op.QueueDeleteOperation(shi); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToDeletePath[i], - HResult = -1 - }); - } - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var deleteTcs = new TaskCompletionSource(); - - // Right before deleting item - op.PreDeleteItem += (s, e) => - { - if (e.SourceItem is not { } sourceItem) - return; - - if (sourceItem.GetParsingPath() is { } sourcePath) - sizeCalculator.ForceComputeFileSize(sourcePath); - fsProgress.FileName = sourceItem.Name ?? string.Empty; - fsProgress.Report(); - }; - - // Right after deleted item - op.PostDeleteItem += (s, e) => - { - var sourceItem = e.SourceItem; - var sourcePath = sourceItem?.GetParsingPath(); - if (sourceItem is { IsFolder: false } && sourcePath is not null) - { - if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) - fsProgress.AddProcessedItemsCount(1); - } - - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = e.DestItem?.GetParsingPath(), - HResult = (int)e.Result - }); - - UpdateFileTagsDb(e, "delete"); - }; - - op.FinishOperations += (s, e) - => deleteTcs.TrySetResult(e.Result.Succeeded); - - op.UpdateProgress += (s, e) => - { - // E_FAIL, stops operation - if (progressHandler.CheckCanceled(operationID)) - throw new Win32Exception(unchecked((int)0x80004005)); - - fsProgress.Report(e.ProgressPercentage); - progressHandler.UpdateOperation(operationID, e.ProgressPercentage); - }; - - try - { - op.PerformOperations(); - } - catch - { - deleteTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - cts.Cancel(); - - return (await deleteTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> RenameItemAsync(string fileToRenamePath, string newName, bool overwriteOnRename, long ownerHwnd, bool asAdmin, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - var shellOperationResult = new ShellOperationResult(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - op.OwnerWindow = (HWND)(nint)ownerHwnd; - op.Options |= !overwriteOnRename ? FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : 0; - - if (!SafetyExtensions.IgnoreExceptions(() => - { - using var shi = new ShellItem(fileToRenamePath); - op.QueueRenameOperation(shi, newName); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToRenamePath, - HResult = -1 - }); - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var renameTcs = new TaskCompletionSource(); - op.PostRenameItem += (s, e) => - { - var sourcePath = e.SourceItem.GetParsingPath(); - var sourceFolderPath = sourcePath is null ? null : Path.GetDirectoryName(sourcePath); - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, - HResult = (int)e.Result - }); - }; - op.PostRenameItem += (_, e) => UpdateFileTagsDb(e, "rename"); - op.FinishOperations += (s, e) => renameTcs.TrySetResult(e.Result.Succeeded); - - try - { - op.PerformOperations(); - } - catch - { - renameTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - return (await renameTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> MoveItemAsync(string[] fileToMovePath, string[] moveDestination, bool overwriteOnMove, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - StatusCenterItemProgressModel fsProgress = new( - progress, - false, - FileSystemStatusCode.InProgress); - - var cts = new CancellationTokenSource(); - var sizeCalculator = new FileSizeCalculator(fileToMovePath); - - // Track the count and update the progress - sizeCalculator.ItemsCountChanged += (newCount) => - { - fsProgress.ItemsCount = newCount; - fsProgress.Report(); - }; - - var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); - sizeTask.ContinueWith(_ => - { - fsProgress.TotalSize = sizeCalculator.Size; - fsProgress.ItemsCount = sizeCalculator.ItemsCount; - fsProgress.EnumerationCompleted = true; - fsProgress.Report(); - }); - - fsProgress.Report(); - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - var shellOperationResult = new ShellOperationResult(); - - op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; - - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - op.Options |= !overwriteOnMove ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; - - for (var i = 0; i < fileToMovePath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using ShellItem shi = new(fileToMovePath[i]); - var destinationFolderPath = Path.GetDirectoryName(moveDestination[i]) - ?? throw new ArgumentException("The move destination must include a parent folder.", nameof(moveDestination)); - using ShellFolder shd = new(destinationFolderPath); - - op.QueueMoveOperation(shi, shd, Path.GetFileName(moveDestination[i])); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToMovePath[i], - Destination = moveDestination[i], - // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead - HResult = MainStreamExists(fileToMovePath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC - }); - } - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var moveTcs = new TaskCompletionSource(); - - op.PreMoveItem += (s, e) => - { - if (e.SourceItem is not { } sourceItem) - return; - - if (sourceItem.GetParsingPath() is { } sourcePath) - sizeCalculator.ForceComputeFileSize(sourcePath); - fsProgress.FileName = sourceItem.Name ?? string.Empty; - fsProgress.Report(); - }; - - op.PostMoveItem += (s, e) => - { - var sourceItem = e.SourceItem; - var sourcePath = sourceItem?.GetParsingPath(); - if (sourceItem is { IsFolder: false } && sourcePath is not null) - { - if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) - fsProgress.AddProcessedItemsCount(1); - } - - var destinationFolderPath = e.DestFolder?.GetParsingPath(); - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, - HResult = (int)e.Result - }); - - UpdateFileTagsDb(e, "move"); - }; - - op.FinishOperations += (s, e) - => moveTcs.TrySetResult(e.Result.Succeeded); - - op.UpdateProgress += (s, e) => - { - // E_FAIL, stops operation - if (progressHandler.CheckCanceled(operationID)) - throw new Win32Exception(unchecked((int)0x80004005)); - - fsProgress.Report(e.ProgressPercentage); - progressHandler.UpdateOperation(operationID, e.ProgressPercentage); - }; - - try - { - op.PerformOperations(); - } - catch - { - moveTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - cts.Cancel(); - - return (await moveTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> CopyItemAsync(string[] fileToCopyPath, string[] copyDestination, bool overwriteOnCopy, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - StatusCenterItemProgressModel fsProgress = new( - progress, - false, - FileSystemStatusCode.InProgress); - - var cts = new CancellationTokenSource(); - var sizeCalculator = new FileSizeCalculator(fileToCopyPath); - - // Track the count and update the progress - sizeCalculator.ItemsCountChanged += (newCount) => - { - fsProgress.ItemsCount = newCount; - fsProgress.Report(); - }; - - var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); - sizeTask.ContinueWith(_ => - { - fsProgress.TotalSize = sizeCalculator.Size; - fsProgress.ItemsCount = sizeCalculator.ItemsCount; - fsProgress.EnumerationCompleted = true; - fsProgress.Report(); - }); - - fsProgress.Report(); - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - var shellOperationResult = new ShellOperationResult(); - - op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; - - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - op.Options |= !overwriteOnCopy ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; - - for (var i = 0; i < fileToCopyPath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using ShellItem shi = new(fileToCopyPath[i]); - var destinationFolderPath = Path.GetDirectoryName(copyDestination[i]) - ?? throw new ArgumentException("The copy destination must include a parent folder.", nameof(copyDestination)); - using ShellFolder shd = new(destinationFolderPath); - - var fileName = GetIncrementalName(overwriteOnCopy, copyDestination[i], fileToCopyPath[i]); - // Perform a copy operation - op.QueueCopyOperation(shi, shd, fileName); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToCopyPath[i], - Destination = copyDestination[i], - // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead - HResult = MainStreamExists(fileToCopyPath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC - }); - } - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var copyTcs = new TaskCompletionSource(); - - op.PreCopyItem += (s, e) => - { - if (e.SourceItem is not { } sourceItem) - return; - - if (sourceItem.GetParsingPath() is { } sourcePath) - sizeCalculator.ForceComputeFileSize(sourcePath); - fsProgress.FileName = sourceItem.Name ?? string.Empty; - fsProgress.Report(); - }; - - op.PostCopyItem += (s, e) => - { - var sourceItem = e.SourceItem; - var sourcePath = sourceItem?.GetParsingPath(); - if (sourceItem is { IsFolder: false } && sourcePath is not null) - { - if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) - fsProgress.AddProcessedItemsCount(1); - } - - var destinationFolderPath = e.DestFolder?.GetParsingPath(); - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, - HResult = (int)e.Result - }); - - UpdateFileTagsDb(e, "copy"); - }; - - op.FinishOperations += (s, e) - => copyTcs.TrySetResult(e.Result.Succeeded); - - op.UpdateProgress += (s, e) => - { - // E_FAIL, stops operation - if (progressHandler.CheckCanceled(operationID)) - throw new Win32Exception(unchecked((int)0x80004005)); - - fsProgress.Report(e.ProgressPercentage); - progressHandler.UpdateOperation(operationID, e.ProgressPercentage); - }; - - try - { - op.PerformOperations(); - } - catch - { - copyTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - cts.Cancel(); - - return (await copyTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static void TryCancelOperation(string operationId) - => progressHandler?.TryCancel(operationId); - - public static IEnumerable? CheckFileInUse(string[] fileToCheckPath) - { - var processes = SafetyExtensions.IgnoreExceptions(() => Win32Helper.WhoIsLocking(fileToCheckPath), App.Logger); - - if (processes is not null) - { - var win32proc = processes.Select(x => new Win32Process() - { - Name = x.ProcessName, - Pid = x.Id, - FileName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileName), - AppName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileVersionInfo?.FileDescription) - }).ToList(); - processes.ForEach(x => x.Dispose()); - - return win32proc; - } - else - { - return null; - } - } - - /// - /// Whether to run the shell's link resolution (which may search for moved targets and touch the - /// network). Pass when only the data stored in the link file is needed, - /// e.g. for listing items. - /// - public static async Task ParseLinkAsync(string linkPath, bool resolveTarget = true) - { - if (string.IsNullOrEmpty(linkPath)) - return null; - - string targetPath = string.Empty; - - try - { - if (FileExtensionHelpers.IsShortcutFile(linkPath)) - { - using var link = resolveTarget - ? new ShellLink(linkPath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)) - : new ShellLink(linkPath, resolve: false); - targetPath = link.TargetPath; - - // Broken shortcut (rooted target that's gone) keeps the delete prompt; app/shell targets aren't rooted - if (resolveTarget && Path.IsPathRooted(targetPath) && - !targetPath.StartsWith(@"\\", StringComparison.Ordinal) && !Path.Exists(targetPath)) - { - return new ShellLinkItem { TargetPath = targetPath, InvalidTarget = true }; - } - - return ShellFolderExtensions.GetShellLinkItem(link); - } - else if (FileExtensionHelpers.IsWebLinkFile(linkPath)) - { - targetPath = await STATask.Run(() => InternetShortcut.Load(linkPath), App.Logger); - return string.IsNullOrEmpty(targetPath) ? - new ShellLinkItem - { - TargetPath = string.Empty, - InvalidTarget = true - } : new ShellLinkItem { TargetPath = targetPath }; - } - return null; - } - catch (FileNotFoundException ex) // Could not parse shortcut - { - App.Logger?.LogWarning(ex, ex.Message); - // Return a item containing the invalid target path - return new ShellLinkItem - { - TargetPath = string.IsNullOrEmpty(targetPath) ? string.Empty : targetPath, - InvalidTarget = true - }; - } - catch (Exception ex) - { - // Could not parse shortcut - App.Logger.LogWarning(ex, ex.Message); - return null; - } - } - - public static Task CreateOrUpdateLinkAsync(string linkSavePath, string? targetPath, string? arguments = "", string? workingDirectory = "", bool runAsAdmin = false, SHOW_WINDOW_CMD showWindowCommand = SHOW_WINDOW_CMD.SW_NORMAL) - { - try - { - ArgumentNullException.ThrowIfNull(targetPath); - if (FileExtensionHelpers.IsShortcutFile(linkSavePath)) - { - using var newLink = new ShellLink(targetPath, arguments, workingDirectory); - - // Check if the target is a file - if (File.Exists(targetPath)) - newLink.RunAsAdministrator = runAsAdmin; - - newLink.SaveAs(linkSavePath); // Overwrite if exists - - // ShowState has to be set after SaveAs has been called, otherwise an UnauthorizedAccessException gets thrown in some cases - newLink.ShowState = showWindowCommand; - - return Task.FromResult(true); - } - else if (FileExtensionHelpers.IsWebLinkFile(linkSavePath)) - { - return STATask.Run(() => - { - InternetShortcut.Save(linkSavePath, targetPath); - return true; - }, App.Logger); - } - } - catch (UnauthorizedAccessException ex) - { - // Could not create shortcut - App.Logger.LogInformation(ex, "Failed to create shortcut"); - } - catch (Exception ex) - { - // Could not create shortcut - App.Logger.LogWarning(ex, ex.Message); - } - - return Task.FromResult(false); - } - - public static bool SetLinkIcon(string filePath, string? iconFile, int iconIndex) - { - iconFile ??= string.Empty; - var ext = Path.GetExtension(filePath).ToLowerInvariant(); - - try - { - return ext switch - { - ".lnk" => TrySetLnkShortcutIcon(filePath, iconFile, iconIndex), - ".url" => TrySetUrlShortcutIcon(filePath, iconFile, iconIndex), - _ => false, - }; - } - catch (UnauthorizedAccessException) - { - string psScript; - filePath = filePath.Replace("'", "''"); - iconFile = iconFile.Replace("'", "''"); - - if(ext == ".url") - { - psScript = $@" - $path = '{filePath}' - $iconFile = '{iconFile}' - $iconIndex = '{iconIndex}' - $content = Get-Content -LiteralPath $path - - $content = $content | Where-Object {{ $_ -notmatch '^IconFile=' -and $_ -notmatch '^IconIndex=' }} - - $newContent = foreach ($line in $content) {{ - $line - if ($line -eq '[InternetShortcut]') {{ - ""IconFile=$iconFile"" - ""IconIndex=$iconIndex"" - }} - }} - $newContent | Set-Content -LiteralPath $path -Encoding UTF8 - "; - } - else - { - psScript = $@" - $FilePath = '{filePath}' - $IconFile = '{iconFile}' - $IconIndex = '{iconIndex}' - - $Shell = New-Object -ComObject WScript.Shell - $Shortcut = $Shell.CreateShortcut($FilePath) - $Shortcut.IconLocation = ""$IconFile, $IconIndex"" - $Shortcut.Save() - "; - } - - var base64EncodedScript = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(psScript)); - - ProcessStartInfo startInfo = new ProcessStartInfo() - { - FileName = "powershell.exe", - Arguments = $"-NoProfile -EncodedCommand {base64EncodedScript}", - Verb = "runas", - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden, - UseShellExecute = true - }; - - // Start the process - Process process = new Process() { StartInfo = startInfo }; - process.Start(); - process.WaitForExit(); - - return true; - } - catch (Exception ex) - { - // Could not create shortcut - App.Logger.LogWarning(ex, ex.Message); - } - - return false; - } - - private static bool TrySetUrlShortcutIcon(string filePath, string iconFile, int iconIndex) - { - var fileExist = File.Exists(filePath); - if (!fileExist) - { - return false; - } - - var lines = File.ReadAllLines(filePath).ToList(); - var hasInternetShortcutHeader = lines.Any(l => l.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase)); - - if (!hasInternetShortcutHeader) - { - return false; - } - - lines.RemoveAll(l => - l.StartsWith("IconFile=", StringComparison.OrdinalIgnoreCase) || - l.StartsWith("IconIndex=", StringComparison.OrdinalIgnoreCase)); - - int index = 0; - int insertedIndex = 0; - foreach(var line in lines) - { - var isInternetShortcutHeader = line.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase); - if(isInternetShortcutHeader) - { - insertedIndex = index + 1; - break; - } - - index++; - } - - if (insertedIndex > 0 && !string.IsNullOrEmpty(iconFile)) - { - lines.Insert(insertedIndex, $"IconFile={iconFile}"); - lines.Insert(insertedIndex + 1, $"IconIndex={iconIndex}"); - } - - File.WriteAllLines(filePath, lines); - - return true; - } - - private static bool TrySetLnkShortcutIcon(string filePath, string iconFile, int iconIndex) - { - using var link = new ShellLink(filePath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)); - if (string.IsNullOrWhiteSpace(iconFile)) - { - link.IconLocation = new IconLocation(string.Empty, 0); - } - else - { - link.IconLocation = new IconLocation(iconFile, iconIndex); - } - link.SaveAs(filePath); // Overwrite if exists - - return true; - } - - public static Task OpenObjectPickerAsync(long hWnd) - => WindowsObjectPicker.OpenObjectPickerAsync((nint)hWnd, App.Logger); - - private static ShellItem? GetFirstFile(ShellItem shi) - { - if (!shi.IsFolder || shi.IsStream) - { - return shi; - } - using var shf = new ShellFolder(shi); - if (shf.FirstOrDefault(x => !x.IsFolder || x.IsStream) is ShellItem item) - { - return item; - } - foreach (var shsfi in shf.Where(x => x.IsFolder && !x.IsStream)) - { - using var shsf = new ShellFolder(shsfi); - if (GetFirstFile(shsf) is ShellItem item2) - { - return item2; - } - } - return null; - } - - private static void UpdateFileTagsDb(ShellFileOperations2.ShellFileOpEventArgs e, string operationType) - { - var dbInstance = FileTagsHelper.GetDbInstance(); - if (e.Result.Succeeded && e.SourceItem.GetParsingPath() is { } sourcePath) - { - var destPath = e.DestFolder.GetParsingPath(); - var sourceFolderPath = Path.GetDirectoryName(sourcePath); - var destination = operationType switch - { - "delete" => e.DestItem.GetParsingPath(), - "rename" => sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, - "copy" => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null, - _ => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null - }; - if (destination is null) - { - dbInstance.SetTags(sourcePath, null, []); // remove tag from deleted files - } - else - { - SafetyExtensions.IgnoreExceptions(() => - { - if (operationType == "copy") - { - var tag = dbInstance.GetTags(sourcePath, null); - - dbInstance.SetTags(destination, FileTagsHelper.GetFileFRN(destination), tag); // copy tag to new files - using var si = new ShellItem(destination); - if (si.IsFolder) // File tag is not copied automatically for folders - { - _ = FileTagsHelper.WriteFileTagAsync(destination, tag); - } - } - else - { - dbInstance.UpdateTag(sourcePath, FileTagsHelper.GetFileFRN(destination), destination); // move tag to new files - } - }, App.Logger); - } - if (e.Result == HRESULT.COPYENGINE_S_DONT_PROCESS_CHILDREN) // child items not processed, update manually - { - var tags = dbInstance.GetAllUnderPath(sourcePath).ToList(); - if (destination is null) // remove tag for items contained in the folder - { - tags.ForEach(t => dbInstance.SetTags(t.FilePath, null, [])); - } - else - { - if (operationType == "copy") // copy tag for items contained in the folder - { - tags.ForEach(t => - { - SafetyExtensions.IgnoreExceptions(() => - { - var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); - dbInstance.SetTags(subPath, FileTagsHelper.GetFileFRN(subPath), t.Tags ?? []); - }, App.Logger); - }); - } - else // move tag to new files - { - tags.ForEach(t => - { - SafetyExtensions.IgnoreExceptions(() => - { - var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); - dbInstance.UpdateTag(t.FilePath, FileTagsHelper.GetFileFRN(subPath), subPath); - }, App.Logger); - }); - } - } - } - } - } - - public static void WaitForCompletion() - => progressHandler?.WaitForCompletion(); - - private sealed partial class ProgressHandler : Disposable - { - private readonly ManualResetEvent operationsCompletedEvent; - - public sealed class OperationWithProgress - { - public double Progress { get; set; } - public bool Canceled { get; set; } - } - - private readonly ConcurrentDictionary operations; - - public HWND OwnerWindow { get; set; } - - public ProgressHandler() - { - operations = new ConcurrentDictionary(); - operationsCompletedEvent = new ManualResetEvent(true); - } - - public int Progress - { - get - { - var ongoing = operations.ToArray().Where(x => !x.Value.Canceled); - return ongoing.Any() ? (int)ongoing.Average(x => x.Value.Progress) : 0; - } - } - - public void AddOperation(string uid) - { - operations.TryAdd(uid, new OperationWithProgress()); - operationsCompletedEvent.Reset(); - } - - public void RemoveOperation(string uid) - { - operations.TryRemove(uid, out _); - if (!operations.Any()) - { - operationsCompletedEvent.Set(); - } - } - - public void UpdateOperation(string uid, double progress) - { - if (operations.TryGetValue(uid, out var op)) - { - op.Progress = progress; - } - } - - public bool CheckCanceled(string uid) - { - return !operations.TryGetValue(uid, out var op) || op.Canceled; - } - - public void TryCancel(string uid) - { - if (operations.TryGetValue(uid, out var op)) - { - op.Canceled = true; - } - } - - public void WaitForCompletion() - { - operationsCompletedEvent.WaitOne(); - } - - protected override void Dispose(bool disposing) - { - if (disposing) - { - operationsCompletedEvent?.Dispose(); - } - } - } - - private static string GetIncrementalName(bool overWriteOnCopy, string filePathToCheck, string filePathToCopy) - { - if ((!Path.Exists(filePathToCheck)) || overWriteOnCopy || filePathToCheck == filePathToCopy) - return Path.GetFileName(filePathToCheck); - - var index = 2; - var filePath = filePathToCheck; - if (Path.HasExtension(filePathToCheck)) - filePath = filePathToCheck.Substring(0, filePathToCheck.LastIndexOf('.')); - - Func genFilePath = x => string.Concat([filePath, " (", x.ToString(), ")", Path.GetExtension(filePathToCheck)]); - - while (Path.Exists(genFilePath(index))) - index++; - - return Path.GetFileName(genFilePath(index)); - } - - private static bool MainStreamExists(string path) - { - // Path.Exists is always false for ADS paths (file.txt:stream), so check the main stream's path - var fileName = Path.GetFileName(path); - var colonIndex = fileName.IndexOf(':'); - if (colonIndex is not -1) - path = path[..(path.Length - fileName.Length + colonIndex)]; - - return Path.Exists(path); - } - } -} +// Copyright (c) Files Community +// Licensed under the MIT License. + +using Files.App.Utils.Storage.Operations; +using Files.Shared.Helpers; +using Microsoft.Extensions.Logging; +using System.Collections.Concurrent; +using System.IO; +using Windows.ApplicationModel.DataTransfer; +using Windows.Win32; +using Windows.Win32.UI.WindowsAndMessaging; +using FILEOPERATION_FLAGS = Windows.Win32.UI.Shell.FILEOPERATION_FLAGS; +using HRESULT = Windows.Win32.Foundation.HRESULT; +using HWND = Windows.Win32.Foundation.HWND; +using PROPERTYKEY = Windows.Win32.Foundation.PROPERTYKEY; +using SLR_FLAGS = Windows.Win32.UI.Shell.SLR_FLAGS; + +namespace Files.App.Utils.Storage +{ + public sealed partial class FileOperationsHelpers + { + private static readonly PROPERTYKEY PKEY_FilePlaceholderStatus = new() { fmtid = new("B2F9B9D6-FEC4-4DD5-94D7-8957488C807B"), pid = 2 }; + private const uint PS_CLOUDFILE_PLACEHOLDER = 8; + + private static ProgressHandler? progressHandler; // Warning: must be initialized from a MTA thread + + public static Task SetClipboard(string[] filesToCopy, DataPackageOperation operation) + { + return STATask.Run(() => + { + uint preferredDropEffect = (uint)(operation == DataPackageOperation.Copy + ? DataPackageOperation.Copy | DataPackageOperation.Link + : DataPackageOperation.Move); + ShellDataObject.SetClipboard(filesToCopy, preferredDropEffect); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> CreateItemAsync(string filePath, string fileOp, long ownerHwnd, bool asAdmin, string? template = null, byte[]? dataBytes = null) + { + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION | FILEOPERATION_FLAGS.FOF_NOERRORUI; + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + var shellOperationResult = new ShellOperationResult(); + var parentPath = Path.GetDirectoryName(filePath); + + if (parentPath is null || !SafetyExtensions.IgnoreExceptions(() => + { + using var shd = new ShellFolder(parentPath); + op.QueueNewItemOperation(shd, Path.GetFileName(filePath), + fileOp == "CreateFolder" ? FileAttributes.Directory : FileAttributes.Normal, template); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Destination = filePath, + HResult = -1 + }); + } + + var createTcs = new TaskCompletionSource(); + op.PostNewItem += (s, e) => + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Destination = e.DestItem.GetParsingPath(), + HResult = (int)e.Result + }); + }; + op.FinishOperations += (s, e) => createTcs.TrySetResult(e.Result.Succeeded); + + try + { + op.PerformOperations(); + } + catch + { + createTcs.TrySetResult(false); + } + + if (dataBytes is not null && + shellOperationResult.Items.SingleOrDefault() is { Succeeded: true, Destination: { } destination }) + { + SafetyExtensions.IgnoreExceptions(() => + { + using var fs = new FileStream(destination, FileMode.Open); + fs.Write(dataBytes, 0, dataBytes.Length); + fs.Flush(); + }, App.Logger); + } + + return (await createTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> TestRecycleAsync(string[] fileToDeletePath) + { + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; + op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE; + + var shellOperationResult = new ShellOperationResult(); + var tryDelete = false; + + for (var i = 0; i < fileToDeletePath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using var shi = new ShellItem(fileToDeletePath[i]); + using var file = SafetyExtensions.IgnoreExceptions(() => GetFirstFile(shi)) ?? shi; + if (file.Properties.TryGetValue(PKEY_FilePlaceholderStatus, out var value) && value == PS_CLOUDFILE_PLACEHOLDER) + { + // Online only files cannot be tried for deletion, so they are treated as to be permanently deleted. + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToDeletePath[i], + HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND + }); + } + else + { + op.QueueDeleteOperation(file); + tryDelete = true; + } + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToDeletePath[i], + HResult = -1 + }); + } + } + + if (!tryDelete) + return (true, shellOperationResult); + + var deleteTcs = new TaskCompletionSource(); + op.PreDeleteItem += [DebuggerHidden] (s, e) => + { + if ((e.Flags & Windows.Win32.UI.Shell._TRANSFER_SOURCE_FLAGS.TSF_DELETE_RECYCLE_IF_POSSIBLE) is 0) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = e.SourceItem.GetParsingPath(), + HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND + }); + throw new Win32Exception((int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND); // E_FAIL, stops operation + } + else + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = true, + Source = e.SourceItem.GetParsingPath(), + HResult = (int)HRESULT.COPYENGINE_E_USER_CANCELLED + }); + throw new Win32Exception((int)HRESULT.COPYENGINE_E_USER_CANCELLED); // E_FAIL, stops operation + } + }; + op.FinishOperations += (s, e) => deleteTcs.TrySetResult(e.Result.Succeeded); + + try + { + op.PerformOperations(); + } + catch + { + deleteTcs.TrySetResult(false); + } + + return (await deleteTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> DeleteItemAsync(string[] fileToDeletePath, bool permanently, long ownerHwnd, bool asAdmin, IProgress? progress, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + StatusCenterItemProgressModel fsProgress = new( + progress, + false, + FileSystemStatusCode.InProgress); + + var cts = new CancellationTokenSource(); + var sizeCalculator = new FileSizeCalculator(fileToDeletePath); + + // Track the count and update the progress + sizeCalculator.ItemsCountChanged += (newCount) => + { + fsProgress.ItemsCount = newCount; + fsProgress.Report(); + }; + + var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); + sizeTask.ContinueWith(_ => + { + fsProgress.TotalSize = 0; + fsProgress.ItemsCount = sizeCalculator.ItemsCount; + fsProgress.EnumerationCompleted = true; + fsProgress.Report(); + }); + + fsProgress.Report(); + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; + + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + if (!permanently) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE | FILEOPERATION_FLAGS.FOF_WANTNUKEWARNING; + } + + var shellOperationResult = new ShellOperationResult(); + + for (var i = 0; i < fileToDeletePath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using var shi = new ShellItem(fileToDeletePath[i]); + + op.QueueDeleteOperation(shi); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToDeletePath[i], + HResult = -1 + }); + } + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var deleteTcs = new TaskCompletionSource(); + + // Right before deleting item + op.PreDeleteItem += (s, e) => + { + if (e.SourceItem is not { } sourceItem) + return; + + if (sourceItem.GetParsingPath() is { } sourcePath) + sizeCalculator.ForceComputeFileSize(sourcePath); + fsProgress.FileName = sourceItem.Name ?? string.Empty; + fsProgress.Report(); + }; + + // Right after deleted item + op.PostDeleteItem += (s, e) => + { + var sourceItem = e.SourceItem; + var sourcePath = sourceItem?.GetParsingPath(); + if (sourceItem is { IsFolder: false } && sourcePath is not null) + { + if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) + fsProgress.AddProcessedItemsCount(1); + } + + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = e.DestItem?.GetParsingPath(), + HResult = (int)e.Result + }); + + UpdateFileTagsDb(e, "delete"); + }; + + op.FinishOperations += (s, e) + => deleteTcs.TrySetResult(e.Result.Succeeded); + + op.UpdateProgress += (s, e) => + { + // E_FAIL, stops operation + if (progressHandler.CheckCanceled(operationID)) + throw new Win32Exception(unchecked((int)0x80004005)); + + fsProgress.Report(e.ProgressPercentage); + progressHandler.UpdateOperation(operationID, e.ProgressPercentage); + }; + + try + { + op.PerformOperations(); + } + catch + { + deleteTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + cts.Cancel(); + + return (await deleteTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> RenameItemAsync(string fileToRenamePath, string newName, bool overwriteOnRename, long ownerHwnd, bool asAdmin, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + var shellOperationResult = new ShellOperationResult(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + op.OwnerWindow = (HWND)(nint)ownerHwnd; + op.Options |= !overwriteOnRename ? FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : 0; + + if (!SafetyExtensions.IgnoreExceptions(() => + { + using var shi = new ShellItem(fileToRenamePath); + + // Generate an incremental name (e.g. "file (2).txt") instead of letting the shell append a localized "Copy" suffix. + // A case-only rename targets the same item, so it must be left untouched. + var renameTargetPath = Path.Combine(Path.GetDirectoryName(fileToRenamePath) ?? string.Empty, newName); + var finalName = !overwriteOnRename && !string.Equals(fileToRenamePath, renameTargetPath, StringComparison.OrdinalIgnoreCase) + ? GetIncrementalName(false, renameTargetPath, fileToRenamePath) + : newName; + + op.QueueRenameOperation(shi, finalName); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToRenamePath, + HResult = -1 + }); + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var renameTcs = new TaskCompletionSource(); + op.PostRenameItem += (s, e) => + { + var sourcePath = e.SourceItem.GetParsingPath(); + var sourceFolderPath = sourcePath is null ? null : Path.GetDirectoryName(sourcePath); + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, + HResult = (int)e.Result + }); + }; + op.PostRenameItem += (_, e) => UpdateFileTagsDb(e, "rename"); + op.FinishOperations += (s, e) => renameTcs.TrySetResult(e.Result.Succeeded); + + try + { + op.PerformOperations(); + } + catch + { + renameTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + return (await renameTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> MoveItemAsync(string[] fileToMovePath, string[] moveDestination, bool overwriteOnMove, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + StatusCenterItemProgressModel fsProgress = new( + progress, + false, + FileSystemStatusCode.InProgress); + + var cts = new CancellationTokenSource(); + var sizeCalculator = new FileSizeCalculator(fileToMovePath); + + // Track the count and update the progress + sizeCalculator.ItemsCountChanged += (newCount) => + { + fsProgress.ItemsCount = newCount; + fsProgress.Report(); + }; + + var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); + sizeTask.ContinueWith(_ => + { + fsProgress.TotalSize = sizeCalculator.Size; + fsProgress.ItemsCount = sizeCalculator.ItemsCount; + fsProgress.EnumerationCompleted = true; + fsProgress.Report(); + }); + + fsProgress.Report(); + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + var shellOperationResult = new ShellOperationResult(); + + op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; + + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + op.Options |= !overwriteOnMove ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; + + for (var i = 0; i < fileToMovePath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using ShellItem shi = new(fileToMovePath[i]); + var destinationFolderPath = Path.GetDirectoryName(moveDestination[i]) + ?? throw new ArgumentException("The move destination must include a parent folder.", nameof(moveDestination)); + using ShellFolder shd = new(destinationFolderPath); + + // Generate an incremental name (e.g. "file (2).txt") instead of letting the shell append a localized "Copy" suffix + var moveFileName = GetIncrementalName(overwriteOnMove, moveDestination[i], fileToMovePath[i]); + op.QueueMoveOperation(shi, shd, moveFileName); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToMovePath[i], + Destination = moveDestination[i], + // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead + HResult = MainStreamExists(fileToMovePath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC + }); + } + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var moveTcs = new TaskCompletionSource(); + + op.PreMoveItem += (s, e) => + { + if (e.SourceItem is not { } sourceItem) + return; + + if (sourceItem.GetParsingPath() is { } sourcePath) + sizeCalculator.ForceComputeFileSize(sourcePath); + fsProgress.FileName = sourceItem.Name ?? string.Empty; + fsProgress.Report(); + }; + + op.PostMoveItem += (s, e) => + { + var sourceItem = e.SourceItem; + var sourcePath = sourceItem?.GetParsingPath(); + if (sourceItem is { IsFolder: false } && sourcePath is not null) + { + if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) + fsProgress.AddProcessedItemsCount(1); + } + + var destinationFolderPath = e.DestFolder?.GetParsingPath(); + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, + HResult = (int)e.Result + }); + + UpdateFileTagsDb(e, "move"); + }; + + op.FinishOperations += (s, e) + => moveTcs.TrySetResult(e.Result.Succeeded); + + op.UpdateProgress += (s, e) => + { + // E_FAIL, stops operation + if (progressHandler.CheckCanceled(operationID)) + throw new Win32Exception(unchecked((int)0x80004005)); + + fsProgress.Report(e.ProgressPercentage); + progressHandler.UpdateOperation(operationID, e.ProgressPercentage); + }; + + try + { + op.PerformOperations(); + } + catch + { + moveTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + cts.Cancel(); + + return (await moveTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> CopyItemAsync(string[] fileToCopyPath, string[] copyDestination, bool overwriteOnCopy, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + StatusCenterItemProgressModel fsProgress = new( + progress, + false, + FileSystemStatusCode.InProgress); + + var cts = new CancellationTokenSource(); + var sizeCalculator = new FileSizeCalculator(fileToCopyPath); + + // Track the count and update the progress + sizeCalculator.ItemsCountChanged += (newCount) => + { + fsProgress.ItemsCount = newCount; + fsProgress.Report(); + }; + + var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); + sizeTask.ContinueWith(_ => + { + fsProgress.TotalSize = sizeCalculator.Size; + fsProgress.ItemsCount = sizeCalculator.ItemsCount; + fsProgress.EnumerationCompleted = true; + fsProgress.Report(); + }); + + fsProgress.Report(); + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + var shellOperationResult = new ShellOperationResult(); + + op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; + + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + op.Options |= !overwriteOnCopy ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; + + for (var i = 0; i < fileToCopyPath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using ShellItem shi = new(fileToCopyPath[i]); + var destinationFolderPath = Path.GetDirectoryName(copyDestination[i]) + ?? throw new ArgumentException("The copy destination must include a parent folder.", nameof(copyDestination)); + using ShellFolder shd = new(destinationFolderPath); + + var fileName = GetIncrementalName(overwriteOnCopy, copyDestination[i], fileToCopyPath[i]); + // Perform a copy operation + op.QueueCopyOperation(shi, shd, fileName); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToCopyPath[i], + Destination = copyDestination[i], + // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead + HResult = MainStreamExists(fileToCopyPath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC + }); + } + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var copyTcs = new TaskCompletionSource(); + + op.PreCopyItem += (s, e) => + { + if (e.SourceItem is not { } sourceItem) + return; + + if (sourceItem.GetParsingPath() is { } sourcePath) + sizeCalculator.ForceComputeFileSize(sourcePath); + fsProgress.FileName = sourceItem.Name ?? string.Empty; + fsProgress.Report(); + }; + + op.PostCopyItem += (s, e) => + { + var sourceItem = e.SourceItem; + var sourcePath = sourceItem?.GetParsingPath(); + if (sourceItem is { IsFolder: false } && sourcePath is not null) + { + if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) + fsProgress.AddProcessedItemsCount(1); + } + + var destinationFolderPath = e.DestFolder?.GetParsingPath(); + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, + HResult = (int)e.Result + }); + + UpdateFileTagsDb(e, "copy"); + }; + + op.FinishOperations += (s, e) + => copyTcs.TrySetResult(e.Result.Succeeded); + + op.UpdateProgress += (s, e) => + { + // E_FAIL, stops operation + if (progressHandler.CheckCanceled(operationID)) + throw new Win32Exception(unchecked((int)0x80004005)); + + fsProgress.Report(e.ProgressPercentage); + progressHandler.UpdateOperation(operationID, e.ProgressPercentage); + }; + + try + { + op.PerformOperations(); + } + catch + { + copyTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + cts.Cancel(); + + return (await copyTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static void TryCancelOperation(string operationId) + => progressHandler?.TryCancel(operationId); + + public static IEnumerable? CheckFileInUse(string[] fileToCheckPath) + { + var processes = SafetyExtensions.IgnoreExceptions(() => Win32Helper.WhoIsLocking(fileToCheckPath), App.Logger); + + if (processes is not null) + { + var win32proc = processes.Select(x => new Win32Process() + { + Name = x.ProcessName, + Pid = x.Id, + FileName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileName), + AppName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileVersionInfo?.FileDescription) + }).ToList(); + processes.ForEach(x => x.Dispose()); + + return win32proc; + } + else + { + return null; + } + } + + /// + /// Whether to run the shell's link resolution (which may search for moved targets and touch the + /// network). Pass when only the data stored in the link file is needed, + /// e.g. for listing items. + /// + public static async Task ParseLinkAsync(string linkPath, bool resolveTarget = true) + { + if (string.IsNullOrEmpty(linkPath)) + return null; + + string targetPath = string.Empty; + + try + { + if (FileExtensionHelpers.IsShortcutFile(linkPath)) + { + using var link = resolveTarget + ? new ShellLink(linkPath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)) + : new ShellLink(linkPath, resolve: false); + targetPath = link.TargetPath; + + // Broken shortcut (rooted target that's gone) keeps the delete prompt; app/shell targets aren't rooted + if (resolveTarget && Path.IsPathRooted(targetPath) && + !targetPath.StartsWith(@"\\", StringComparison.Ordinal) && !Path.Exists(targetPath)) + { + return new ShellLinkItem { TargetPath = targetPath, InvalidTarget = true }; + } + + return ShellFolderExtensions.GetShellLinkItem(link); + } + else if (FileExtensionHelpers.IsWebLinkFile(linkPath)) + { + targetPath = await STATask.Run(() => InternetShortcut.Load(linkPath), App.Logger); + return string.IsNullOrEmpty(targetPath) ? + new ShellLinkItem + { + TargetPath = string.Empty, + InvalidTarget = true + } : new ShellLinkItem { TargetPath = targetPath }; + } + return null; + } + catch (FileNotFoundException ex) // Could not parse shortcut + { + App.Logger?.LogWarning(ex, ex.Message); + // Return a item containing the invalid target path + return new ShellLinkItem + { + TargetPath = string.IsNullOrEmpty(targetPath) ? string.Empty : targetPath, + InvalidTarget = true + }; + } + catch (Exception ex) + { + // Could not parse shortcut + App.Logger.LogWarning(ex, ex.Message); + return null; + } + } + + public static Task CreateOrUpdateLinkAsync(string linkSavePath, string? targetPath, string? arguments = "", string? workingDirectory = "", bool runAsAdmin = false, SHOW_WINDOW_CMD showWindowCommand = SHOW_WINDOW_CMD.SW_NORMAL) + { + try + { + ArgumentNullException.ThrowIfNull(targetPath); + if (FileExtensionHelpers.IsShortcutFile(linkSavePath)) + { + using var newLink = new ShellLink(targetPath, arguments, workingDirectory); + + // Check if the target is a file + if (File.Exists(targetPath)) + newLink.RunAsAdministrator = runAsAdmin; + + newLink.SaveAs(linkSavePath); // Overwrite if exists + + // ShowState has to be set after SaveAs has been called, otherwise an UnauthorizedAccessException gets thrown in some cases + newLink.ShowState = showWindowCommand; + + return Task.FromResult(true); + } + else if (FileExtensionHelpers.IsWebLinkFile(linkSavePath)) + { + return STATask.Run(() => + { + InternetShortcut.Save(linkSavePath, targetPath); + return true; + }, App.Logger); + } + } + catch (UnauthorizedAccessException ex) + { + // Could not create shortcut + App.Logger.LogInformation(ex, "Failed to create shortcut"); + } + catch (Exception ex) + { + // Could not create shortcut + App.Logger.LogWarning(ex, ex.Message); + } + + return Task.FromResult(false); + } + + public static bool SetLinkIcon(string filePath, string? iconFile, int iconIndex) + { + iconFile ??= string.Empty; + var ext = Path.GetExtension(filePath).ToLowerInvariant(); + + try + { + return ext switch + { + ".lnk" => TrySetLnkShortcutIcon(filePath, iconFile, iconIndex), + ".url" => TrySetUrlShortcutIcon(filePath, iconFile, iconIndex), + _ => false, + }; + } + catch (UnauthorizedAccessException) + { + string psScript; + filePath = filePath.Replace("'", "''"); + iconFile = iconFile.Replace("'", "''"); + + if(ext == ".url") + { + psScript = $@" + $path = '{filePath}' + $iconFile = '{iconFile}' + $iconIndex = '{iconIndex}' + $content = Get-Content -LiteralPath $path + + $content = $content | Where-Object {{ $_ -notmatch '^IconFile=' -and $_ -notmatch '^IconIndex=' }} + + $newContent = foreach ($line in $content) {{ + $line + if ($line -eq '[InternetShortcut]') {{ + ""IconFile=$iconFile"" + ""IconIndex=$iconIndex"" + }} + }} + $newContent | Set-Content -LiteralPath $path -Encoding UTF8 + "; + } + else + { + psScript = $@" + $FilePath = '{filePath}' + $IconFile = '{iconFile}' + $IconIndex = '{iconIndex}' + + $Shell = New-Object -ComObject WScript.Shell + $Shortcut = $Shell.CreateShortcut($FilePath) + $Shortcut.IconLocation = ""$IconFile, $IconIndex"" + $Shortcut.Save() + "; + } + + var base64EncodedScript = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(psScript)); + + ProcessStartInfo startInfo = new ProcessStartInfo() + { + FileName = "powershell.exe", + Arguments = $"-NoProfile -EncodedCommand {base64EncodedScript}", + Verb = "runas", + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + UseShellExecute = true + }; + + // Start the process + Process process = new Process() { StartInfo = startInfo }; + process.Start(); + process.WaitForExit(); + + return true; + } + catch (Exception ex) + { + // Could not create shortcut + App.Logger.LogWarning(ex, ex.Message); + } + + return false; + } + + private static bool TrySetUrlShortcutIcon(string filePath, string iconFile, int iconIndex) + { + var fileExist = File.Exists(filePath); + if (!fileExist) + { + return false; + } + + var lines = File.ReadAllLines(filePath).ToList(); + var hasInternetShortcutHeader = lines.Any(l => l.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase)); + + if (!hasInternetShortcutHeader) + { + return false; + } + + lines.RemoveAll(l => + l.StartsWith("IconFile=", StringComparison.OrdinalIgnoreCase) || + l.StartsWith("IconIndex=", StringComparison.OrdinalIgnoreCase)); + + int index = 0; + int insertedIndex = 0; + foreach(var line in lines) + { + var isInternetShortcutHeader = line.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase); + if(isInternetShortcutHeader) + { + insertedIndex = index + 1; + break; + } + + index++; + } + + if (insertedIndex > 0 && !string.IsNullOrEmpty(iconFile)) + { + lines.Insert(insertedIndex, $"IconFile={iconFile}"); + lines.Insert(insertedIndex + 1, $"IconIndex={iconIndex}"); + } + + File.WriteAllLines(filePath, lines); + + return true; + } + + private static bool TrySetLnkShortcutIcon(string filePath, string iconFile, int iconIndex) + { + using var link = new ShellLink(filePath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)); + if (string.IsNullOrWhiteSpace(iconFile)) + { + link.IconLocation = new IconLocation(string.Empty, 0); + } + else + { + link.IconLocation = new IconLocation(iconFile, iconIndex); + } + link.SaveAs(filePath); // Overwrite if exists + + return true; + } + + public static Task OpenObjectPickerAsync(long hWnd) + => WindowsObjectPicker.OpenObjectPickerAsync((nint)hWnd, App.Logger); + + private static ShellItem? GetFirstFile(ShellItem shi) + { + if (!shi.IsFolder || shi.IsStream) + { + return shi; + } + using var shf = new ShellFolder(shi); + if (shf.FirstOrDefault(x => !x.IsFolder || x.IsStream) is ShellItem item) + { + return item; + } + foreach (var shsfi in shf.Where(x => x.IsFolder && !x.IsStream)) + { + using var shsf = new ShellFolder(shsfi); + if (GetFirstFile(shsf) is ShellItem item2) + { + return item2; + } + } + return null; + } + + private static void UpdateFileTagsDb(ShellFileOperations2.ShellFileOpEventArgs e, string operationType) + { + var dbInstance = FileTagsHelper.GetDbInstance(); + if (e.Result.Succeeded && e.SourceItem.GetParsingPath() is { } sourcePath) + { + var destPath = e.DestFolder.GetParsingPath(); + var sourceFolderPath = Path.GetDirectoryName(sourcePath); + var destination = operationType switch + { + "delete" => e.DestItem.GetParsingPath(), + "rename" => sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, + "copy" => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null, + _ => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null + }; + if (destination is null) + { + dbInstance.SetTags(sourcePath, null, []); // remove tag from deleted files + } + else + { + SafetyExtensions.IgnoreExceptions(() => + { + if (operationType == "copy") + { + var tag = dbInstance.GetTags(sourcePath, null); + + dbInstance.SetTags(destination, FileTagsHelper.GetFileFRN(destination), tag); // copy tag to new files + using var si = new ShellItem(destination); + if (si.IsFolder) // File tag is not copied automatically for folders + { + _ = FileTagsHelper.WriteFileTagAsync(destination, tag); + } + } + else + { + dbInstance.UpdateTag(sourcePath, FileTagsHelper.GetFileFRN(destination), destination); // move tag to new files + } + }, App.Logger); + } + if (e.Result == HRESULT.COPYENGINE_S_DONT_PROCESS_CHILDREN) // child items not processed, update manually + { + var tags = dbInstance.GetAllUnderPath(sourcePath).ToList(); + if (destination is null) // remove tag for items contained in the folder + { + tags.ForEach(t => dbInstance.SetTags(t.FilePath, null, [])); + } + else + { + if (operationType == "copy") // copy tag for items contained in the folder + { + tags.ForEach(t => + { + SafetyExtensions.IgnoreExceptions(() => + { + var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); + dbInstance.SetTags(subPath, FileTagsHelper.GetFileFRN(subPath), t.Tags ?? []); + }, App.Logger); + }); + } + else // move tag to new files + { + tags.ForEach(t => + { + SafetyExtensions.IgnoreExceptions(() => + { + var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); + dbInstance.UpdateTag(t.FilePath, FileTagsHelper.GetFileFRN(subPath), subPath); + }, App.Logger); + }); + } + } + } + } + } + + public static void WaitForCompletion() + => progressHandler?.WaitForCompletion(); + + private sealed partial class ProgressHandler : Disposable + { + private readonly ManualResetEvent operationsCompletedEvent; + + public sealed class OperationWithProgress + { + public double Progress { get; set; } + public bool Canceled { get; set; } + } + + private readonly ConcurrentDictionary operations; + + public HWND OwnerWindow { get; set; } + + public ProgressHandler() + { + operations = new ConcurrentDictionary(); + operationsCompletedEvent = new ManualResetEvent(true); + } + + public int Progress + { + get + { + var ongoing = operations.ToArray().Where(x => !x.Value.Canceled); + return ongoing.Any() ? (int)ongoing.Average(x => x.Value.Progress) : 0; + } + } + + public void AddOperation(string uid) + { + operations.TryAdd(uid, new OperationWithProgress()); + operationsCompletedEvent.Reset(); + } + + public void RemoveOperation(string uid) + { + operations.TryRemove(uid, out _); + if (!operations.Any()) + { + operationsCompletedEvent.Set(); + } + } + + public void UpdateOperation(string uid, double progress) + { + if (operations.TryGetValue(uid, out var op)) + { + op.Progress = progress; + } + } + + public bool CheckCanceled(string uid) + { + return !operations.TryGetValue(uid, out var op) || op.Canceled; + } + + public void TryCancel(string uid) + { + if (operations.TryGetValue(uid, out var op)) + { + op.Canceled = true; + } + } + + public void WaitForCompletion() + { + operationsCompletedEvent.WaitOne(); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + operationsCompletedEvent?.Dispose(); + } + } + } + + private static string GetIncrementalName(bool overWriteOnCopy, string filePathToCheck, string filePathToCopy) + { + if ((!Path.Exists(filePathToCheck)) || overWriteOnCopy || filePathToCheck == filePathToCopy) + return Path.GetFileName(filePathToCheck); + + var index = 2; + var filePath = filePathToCheck; + if (Path.HasExtension(filePathToCheck)) + filePath = filePathToCheck.Substring(0, filePathToCheck.LastIndexOf('.')); + + Func genFilePath = x => string.Concat([filePath, " (", x.ToString(), ")", Path.GetExtension(filePathToCheck)]); + + while (Path.Exists(genFilePath(index))) + index++; + + return Path.GetFileName(genFilePath(index)); + } + + private static bool MainStreamExists(string path) + { + // Path.Exists is always false for ADS paths (file.txt:stream), so check the main stream's path + var fileName = Path.GetFileName(path); + var colonIndex = fileName.IndexOf(':'); + if (colonIndex is not -1) + path = path[..(path.Length - fileName.Length + colonIndex)]; + + return Path.Exists(path); + } + } +} From 5ae7d9cff70c00e1c7cdd1aa8ee05587171081f8 Mon Sep 17 00:00:00 2001 From: Marouane-elfadli Date: Fri, 30 Oct 2026 12:15:20 +0100 Subject: [PATCH 2/2] Fix: normalize line endings in FileOperationsHelpers its a new update for file operations helpers Signed-off-by: Marouane-elfadli --- .../Operations/FileOperationsHelpers.cs | 2336 ++++++++--------- 1 file changed, 1168 insertions(+), 1168 deletions(-) diff --git a/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs b/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs index 6058c5264991..7fc5d8f797a0 100644 --- a/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs +++ b/src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs @@ -1,1168 +1,1168 @@ -// Copyright (c) Files Community -// Licensed under the MIT License. - -using Files.App.Utils.Storage.Operations; -using Files.Shared.Helpers; -using Microsoft.Extensions.Logging; -using System.Collections.Concurrent; -using System.IO; -using Windows.ApplicationModel.DataTransfer; -using Windows.Win32; -using Windows.Win32.UI.WindowsAndMessaging; -using FILEOPERATION_FLAGS = Windows.Win32.UI.Shell.FILEOPERATION_FLAGS; -using HRESULT = Windows.Win32.Foundation.HRESULT; -using HWND = Windows.Win32.Foundation.HWND; -using PROPERTYKEY = Windows.Win32.Foundation.PROPERTYKEY; -using SLR_FLAGS = Windows.Win32.UI.Shell.SLR_FLAGS; - -namespace Files.App.Utils.Storage -{ - public sealed partial class FileOperationsHelpers - { - private static readonly PROPERTYKEY PKEY_FilePlaceholderStatus = new() { fmtid = new("B2F9B9D6-FEC4-4DD5-94D7-8957488C807B"), pid = 2 }; - private const uint PS_CLOUDFILE_PLACEHOLDER = 8; - - private static ProgressHandler? progressHandler; // Warning: must be initialized from a MTA thread - - public static Task SetClipboard(string[] filesToCopy, DataPackageOperation operation) - { - return STATask.Run(() => - { - uint preferredDropEffect = (uint)(operation == DataPackageOperation.Copy - ? DataPackageOperation.Copy | DataPackageOperation.Link - : DataPackageOperation.Move); - ShellDataObject.SetClipboard(filesToCopy, preferredDropEffect); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> CreateItemAsync(string filePath, string fileOp, long ownerHwnd, bool asAdmin, string? template = null, byte[]? dataBytes = null) - { - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION | FILEOPERATION_FLAGS.FOF_NOERRORUI; - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - var shellOperationResult = new ShellOperationResult(); - var parentPath = Path.GetDirectoryName(filePath); - - if (parentPath is null || !SafetyExtensions.IgnoreExceptions(() => - { - using var shd = new ShellFolder(parentPath); - op.QueueNewItemOperation(shd, Path.GetFileName(filePath), - fileOp == "CreateFolder" ? FileAttributes.Directory : FileAttributes.Normal, template); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Destination = filePath, - HResult = -1 - }); - } - - var createTcs = new TaskCompletionSource(); - op.PostNewItem += (s, e) => - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Destination = e.DestItem.GetParsingPath(), - HResult = (int)e.Result - }); - }; - op.FinishOperations += (s, e) => createTcs.TrySetResult(e.Result.Succeeded); - - try - { - op.PerformOperations(); - } - catch - { - createTcs.TrySetResult(false); - } - - if (dataBytes is not null && - shellOperationResult.Items.SingleOrDefault() is { Succeeded: true, Destination: { } destination }) - { - SafetyExtensions.IgnoreExceptions(() => - { - using var fs = new FileStream(destination, FileMode.Open); - fs.Write(dataBytes, 0, dataBytes.Length); - fs.Flush(); - }, App.Logger); - } - - return (await createTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> TestRecycleAsync(string[] fileToDeletePath) - { - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; - op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE; - - var shellOperationResult = new ShellOperationResult(); - var tryDelete = false; - - for (var i = 0; i < fileToDeletePath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using var shi = new ShellItem(fileToDeletePath[i]); - using var file = SafetyExtensions.IgnoreExceptions(() => GetFirstFile(shi)) ?? shi; - if (file.Properties.TryGetValue(PKEY_FilePlaceholderStatus, out var value) && value == PS_CLOUDFILE_PLACEHOLDER) - { - // Online only files cannot be tried for deletion, so they are treated as to be permanently deleted. - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToDeletePath[i], - HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND - }); - } - else - { - op.QueueDeleteOperation(file); - tryDelete = true; - } - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToDeletePath[i], - HResult = -1 - }); - } - } - - if (!tryDelete) - return (true, shellOperationResult); - - var deleteTcs = new TaskCompletionSource(); - op.PreDeleteItem += [DebuggerHidden] (s, e) => - { - if ((e.Flags & Windows.Win32.UI.Shell._TRANSFER_SOURCE_FLAGS.TSF_DELETE_RECYCLE_IF_POSSIBLE) is 0) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = e.SourceItem.GetParsingPath(), - HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND - }); - throw new Win32Exception((int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND); // E_FAIL, stops operation - } - else - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = true, - Source = e.SourceItem.GetParsingPath(), - HResult = (int)HRESULT.COPYENGINE_E_USER_CANCELLED - }); - throw new Win32Exception((int)HRESULT.COPYENGINE_E_USER_CANCELLED); // E_FAIL, stops operation - } - }; - op.FinishOperations += (s, e) => deleteTcs.TrySetResult(e.Result.Succeeded); - - try - { - op.PerformOperations(); - } - catch - { - deleteTcs.TrySetResult(false); - } - - return (await deleteTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> DeleteItemAsync(string[] fileToDeletePath, bool permanently, long ownerHwnd, bool asAdmin, IProgress? progress, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - StatusCenterItemProgressModel fsProgress = new( - progress, - false, - FileSystemStatusCode.InProgress); - - var cts = new CancellationTokenSource(); - var sizeCalculator = new FileSizeCalculator(fileToDeletePath); - - // Track the count and update the progress - sizeCalculator.ItemsCountChanged += (newCount) => - { - fsProgress.ItemsCount = newCount; - fsProgress.Report(); - }; - - var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); - sizeTask.ContinueWith(_ => - { - fsProgress.TotalSize = 0; - fsProgress.ItemsCount = sizeCalculator.ItemsCount; - fsProgress.EnumerationCompleted = true; - fsProgress.Report(); - }); - - fsProgress.Report(); - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; - - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - if (!permanently) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE | FILEOPERATION_FLAGS.FOF_WANTNUKEWARNING; - } - - var shellOperationResult = new ShellOperationResult(); - - for (var i = 0; i < fileToDeletePath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using var shi = new ShellItem(fileToDeletePath[i]); - - op.QueueDeleteOperation(shi); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToDeletePath[i], - HResult = -1 - }); - } - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var deleteTcs = new TaskCompletionSource(); - - // Right before deleting item - op.PreDeleteItem += (s, e) => - { - if (e.SourceItem is not { } sourceItem) - return; - - if (sourceItem.GetParsingPath() is { } sourcePath) - sizeCalculator.ForceComputeFileSize(sourcePath); - fsProgress.FileName = sourceItem.Name ?? string.Empty; - fsProgress.Report(); - }; - - // Right after deleted item - op.PostDeleteItem += (s, e) => - { - var sourceItem = e.SourceItem; - var sourcePath = sourceItem?.GetParsingPath(); - if (sourceItem is { IsFolder: false } && sourcePath is not null) - { - if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) - fsProgress.AddProcessedItemsCount(1); - } - - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = e.DestItem?.GetParsingPath(), - HResult = (int)e.Result - }); - - UpdateFileTagsDb(e, "delete"); - }; - - op.FinishOperations += (s, e) - => deleteTcs.TrySetResult(e.Result.Succeeded); - - op.UpdateProgress += (s, e) => - { - // E_FAIL, stops operation - if (progressHandler.CheckCanceled(operationID)) - throw new Win32Exception(unchecked((int)0x80004005)); - - fsProgress.Report(e.ProgressPercentage); - progressHandler.UpdateOperation(operationID, e.ProgressPercentage); - }; - - try - { - op.PerformOperations(); - } - catch - { - deleteTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - cts.Cancel(); - - return (await deleteTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> RenameItemAsync(string fileToRenamePath, string newName, bool overwriteOnRename, long ownerHwnd, bool asAdmin, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - var shellOperationResult = new ShellOperationResult(); - - op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - op.OwnerWindow = (HWND)(nint)ownerHwnd; - op.Options |= !overwriteOnRename ? FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : 0; - - if (!SafetyExtensions.IgnoreExceptions(() => - { - using var shi = new ShellItem(fileToRenamePath); - - // Generate an incremental name (e.g. "file (2).txt") instead of letting the shell append a localized "Copy" suffix. - // A case-only rename targets the same item, so it must be left untouched. - var renameTargetPath = Path.Combine(Path.GetDirectoryName(fileToRenamePath) ?? string.Empty, newName); - var finalName = !overwriteOnRename && !string.Equals(fileToRenamePath, renameTargetPath, StringComparison.OrdinalIgnoreCase) - ? GetIncrementalName(false, renameTargetPath, fileToRenamePath) - : newName; - - op.QueueRenameOperation(shi, finalName); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToRenamePath, - HResult = -1 - }); - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var renameTcs = new TaskCompletionSource(); - op.PostRenameItem += (s, e) => - { - var sourcePath = e.SourceItem.GetParsingPath(); - var sourceFolderPath = sourcePath is null ? null : Path.GetDirectoryName(sourcePath); - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, - HResult = (int)e.Result - }); - }; - op.PostRenameItem += (_, e) => UpdateFileTagsDb(e, "rename"); - op.FinishOperations += (s, e) => renameTcs.TrySetResult(e.Result.Succeeded); - - try - { - op.PerformOperations(); - } - catch - { - renameTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - return (await renameTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> MoveItemAsync(string[] fileToMovePath, string[] moveDestination, bool overwriteOnMove, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - StatusCenterItemProgressModel fsProgress = new( - progress, - false, - FileSystemStatusCode.InProgress); - - var cts = new CancellationTokenSource(); - var sizeCalculator = new FileSizeCalculator(fileToMovePath); - - // Track the count and update the progress - sizeCalculator.ItemsCountChanged += (newCount) => - { - fsProgress.ItemsCount = newCount; - fsProgress.Report(); - }; - - var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); - sizeTask.ContinueWith(_ => - { - fsProgress.TotalSize = sizeCalculator.Size; - fsProgress.ItemsCount = sizeCalculator.ItemsCount; - fsProgress.EnumerationCompleted = true; - fsProgress.Report(); - }); - - fsProgress.Report(); - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - var shellOperationResult = new ShellOperationResult(); - - op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; - - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - op.Options |= !overwriteOnMove ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; - - for (var i = 0; i < fileToMovePath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using ShellItem shi = new(fileToMovePath[i]); - var destinationFolderPath = Path.GetDirectoryName(moveDestination[i]) - ?? throw new ArgumentException("The move destination must include a parent folder.", nameof(moveDestination)); - using ShellFolder shd = new(destinationFolderPath); - - // Generate an incremental name (e.g. "file (2).txt") instead of letting the shell append a localized "Copy" suffix - var moveFileName = GetIncrementalName(overwriteOnMove, moveDestination[i], fileToMovePath[i]); - op.QueueMoveOperation(shi, shd, moveFileName); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToMovePath[i], - Destination = moveDestination[i], - // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead - HResult = MainStreamExists(fileToMovePath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC - }); - } - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var moveTcs = new TaskCompletionSource(); - - op.PreMoveItem += (s, e) => - { - if (e.SourceItem is not { } sourceItem) - return; - - if (sourceItem.GetParsingPath() is { } sourcePath) - sizeCalculator.ForceComputeFileSize(sourcePath); - fsProgress.FileName = sourceItem.Name ?? string.Empty; - fsProgress.Report(); - }; - - op.PostMoveItem += (s, e) => - { - var sourceItem = e.SourceItem; - var sourcePath = sourceItem?.GetParsingPath(); - if (sourceItem is { IsFolder: false } && sourcePath is not null) - { - if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) - fsProgress.AddProcessedItemsCount(1); - } - - var destinationFolderPath = e.DestFolder?.GetParsingPath(); - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, - HResult = (int)e.Result - }); - - UpdateFileTagsDb(e, "move"); - }; - - op.FinishOperations += (s, e) - => moveTcs.TrySetResult(e.Result.Succeeded); - - op.UpdateProgress += (s, e) => - { - // E_FAIL, stops operation - if (progressHandler.CheckCanceled(operationID)) - throw new Win32Exception(unchecked((int)0x80004005)); - - fsProgress.Report(e.ProgressPercentage); - progressHandler.UpdateOperation(operationID, e.ProgressPercentage); - }; - - try - { - op.PerformOperations(); - } - catch - { - moveTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - cts.Cancel(); - - return (await moveTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static Task<(bool, ShellOperationResult)> CopyItemAsync(string[] fileToCopyPath, string[] copyDestination, bool overwriteOnCopy, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") - { - operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; - - StatusCenterItemProgressModel fsProgress = new( - progress, - false, - FileSystemStatusCode.InProgress); - - var cts = new CancellationTokenSource(); - var sizeCalculator = new FileSizeCalculator(fileToCopyPath); - - // Track the count and update the progress - sizeCalculator.ItemsCountChanged += (newCount) => - { - fsProgress.ItemsCount = newCount; - fsProgress.Report(); - }; - - var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); - sizeTask.ContinueWith(_ => - { - fsProgress.TotalSize = sizeCalculator.Size; - fsProgress.ItemsCount = sizeCalculator.ItemsCount; - fsProgress.EnumerationCompleted = true; - fsProgress.Report(); - }); - - fsProgress.Report(); - progressHandler ??= new(); - - return STATask.Run(async () => - { - using var op = new ShellFileOperations2(); - - var shellOperationResult = new ShellOperationResult(); - - op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; - - if (asAdmin) - { - op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; - } - - op.OwnerWindow = (HWND)(nint)ownerHwnd; - - op.Options |= !overwriteOnCopy ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; - - for (var i = 0; i < fileToCopyPath.Length; i++) - { - if (!SafetyExtensions.IgnoreExceptions(() => - { - using ShellItem shi = new(fileToCopyPath[i]); - var destinationFolderPath = Path.GetDirectoryName(copyDestination[i]) - ?? throw new ArgumentException("The copy destination must include a parent folder.", nameof(copyDestination)); - using ShellFolder shd = new(destinationFolderPath); - - var fileName = GetIncrementalName(overwriteOnCopy, copyDestination[i], fileToCopyPath[i]); - // Perform a copy operation - op.QueueCopyOperation(shi, shd, fileName); - })) - { - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = false, - Source = fileToCopyPath[i], - Destination = copyDestination[i], - // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead - HResult = MainStreamExists(fileToCopyPath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC - }); - } - } - - progressHandler.OwnerWindow = op.OwnerWindow; - progressHandler.AddOperation(operationID); - - var copyTcs = new TaskCompletionSource(); - - op.PreCopyItem += (s, e) => - { - if (e.SourceItem is not { } sourceItem) - return; - - if (sourceItem.GetParsingPath() is { } sourcePath) - sizeCalculator.ForceComputeFileSize(sourcePath); - fsProgress.FileName = sourceItem.Name ?? string.Empty; - fsProgress.Report(); - }; - - op.PostCopyItem += (s, e) => - { - var sourceItem = e.SourceItem; - var sourcePath = sourceItem?.GetParsingPath(); - if (sourceItem is { IsFolder: false } && sourcePath is not null) - { - if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) - fsProgress.AddProcessedItemsCount(1); - } - - var destinationFolderPath = e.DestFolder?.GetParsingPath(); - shellOperationResult.Items.Add(new ShellOperationItemResult() - { - Succeeded = e.Result.Succeeded, - Source = sourcePath, - Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, - HResult = (int)e.Result - }); - - UpdateFileTagsDb(e, "copy"); - }; - - op.FinishOperations += (s, e) - => copyTcs.TrySetResult(e.Result.Succeeded); - - op.UpdateProgress += (s, e) => - { - // E_FAIL, stops operation - if (progressHandler.CheckCanceled(operationID)) - throw new Win32Exception(unchecked((int)0x80004005)); - - fsProgress.Report(e.ProgressPercentage); - progressHandler.UpdateOperation(operationID, e.ProgressPercentage); - }; - - try - { - op.PerformOperations(); - } - catch - { - copyTcs.TrySetResult(false); - } - - progressHandler.RemoveOperation(operationID); - - cts.Cancel(); - - return (await copyTcs.Task, shellOperationResult); - }, App.Logger); - } - - public static void TryCancelOperation(string operationId) - => progressHandler?.TryCancel(operationId); - - public static IEnumerable? CheckFileInUse(string[] fileToCheckPath) - { - var processes = SafetyExtensions.IgnoreExceptions(() => Win32Helper.WhoIsLocking(fileToCheckPath), App.Logger); - - if (processes is not null) - { - var win32proc = processes.Select(x => new Win32Process() - { - Name = x.ProcessName, - Pid = x.Id, - FileName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileName), - AppName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileVersionInfo?.FileDescription) - }).ToList(); - processes.ForEach(x => x.Dispose()); - - return win32proc; - } - else - { - return null; - } - } - - /// - /// Whether to run the shell's link resolution (which may search for moved targets and touch the - /// network). Pass when only the data stored in the link file is needed, - /// e.g. for listing items. - /// - public static async Task ParseLinkAsync(string linkPath, bool resolveTarget = true) - { - if (string.IsNullOrEmpty(linkPath)) - return null; - - string targetPath = string.Empty; - - try - { - if (FileExtensionHelpers.IsShortcutFile(linkPath)) - { - using var link = resolveTarget - ? new ShellLink(linkPath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)) - : new ShellLink(linkPath, resolve: false); - targetPath = link.TargetPath; - - // Broken shortcut (rooted target that's gone) keeps the delete prompt; app/shell targets aren't rooted - if (resolveTarget && Path.IsPathRooted(targetPath) && - !targetPath.StartsWith(@"\\", StringComparison.Ordinal) && !Path.Exists(targetPath)) - { - return new ShellLinkItem { TargetPath = targetPath, InvalidTarget = true }; - } - - return ShellFolderExtensions.GetShellLinkItem(link); - } - else if (FileExtensionHelpers.IsWebLinkFile(linkPath)) - { - targetPath = await STATask.Run(() => InternetShortcut.Load(linkPath), App.Logger); - return string.IsNullOrEmpty(targetPath) ? - new ShellLinkItem - { - TargetPath = string.Empty, - InvalidTarget = true - } : new ShellLinkItem { TargetPath = targetPath }; - } - return null; - } - catch (FileNotFoundException ex) // Could not parse shortcut - { - App.Logger?.LogWarning(ex, ex.Message); - // Return a item containing the invalid target path - return new ShellLinkItem - { - TargetPath = string.IsNullOrEmpty(targetPath) ? string.Empty : targetPath, - InvalidTarget = true - }; - } - catch (Exception ex) - { - // Could not parse shortcut - App.Logger.LogWarning(ex, ex.Message); - return null; - } - } - - public static Task CreateOrUpdateLinkAsync(string linkSavePath, string? targetPath, string? arguments = "", string? workingDirectory = "", bool runAsAdmin = false, SHOW_WINDOW_CMD showWindowCommand = SHOW_WINDOW_CMD.SW_NORMAL) - { - try - { - ArgumentNullException.ThrowIfNull(targetPath); - if (FileExtensionHelpers.IsShortcutFile(linkSavePath)) - { - using var newLink = new ShellLink(targetPath, arguments, workingDirectory); - - // Check if the target is a file - if (File.Exists(targetPath)) - newLink.RunAsAdministrator = runAsAdmin; - - newLink.SaveAs(linkSavePath); // Overwrite if exists - - // ShowState has to be set after SaveAs has been called, otherwise an UnauthorizedAccessException gets thrown in some cases - newLink.ShowState = showWindowCommand; - - return Task.FromResult(true); - } - else if (FileExtensionHelpers.IsWebLinkFile(linkSavePath)) - { - return STATask.Run(() => - { - InternetShortcut.Save(linkSavePath, targetPath); - return true; - }, App.Logger); - } - } - catch (UnauthorizedAccessException ex) - { - // Could not create shortcut - App.Logger.LogInformation(ex, "Failed to create shortcut"); - } - catch (Exception ex) - { - // Could not create shortcut - App.Logger.LogWarning(ex, ex.Message); - } - - return Task.FromResult(false); - } - - public static bool SetLinkIcon(string filePath, string? iconFile, int iconIndex) - { - iconFile ??= string.Empty; - var ext = Path.GetExtension(filePath).ToLowerInvariant(); - - try - { - return ext switch - { - ".lnk" => TrySetLnkShortcutIcon(filePath, iconFile, iconIndex), - ".url" => TrySetUrlShortcutIcon(filePath, iconFile, iconIndex), - _ => false, - }; - } - catch (UnauthorizedAccessException) - { - string psScript; - filePath = filePath.Replace("'", "''"); - iconFile = iconFile.Replace("'", "''"); - - if(ext == ".url") - { - psScript = $@" - $path = '{filePath}' - $iconFile = '{iconFile}' - $iconIndex = '{iconIndex}' - $content = Get-Content -LiteralPath $path - - $content = $content | Where-Object {{ $_ -notmatch '^IconFile=' -and $_ -notmatch '^IconIndex=' }} - - $newContent = foreach ($line in $content) {{ - $line - if ($line -eq '[InternetShortcut]') {{ - ""IconFile=$iconFile"" - ""IconIndex=$iconIndex"" - }} - }} - $newContent | Set-Content -LiteralPath $path -Encoding UTF8 - "; - } - else - { - psScript = $@" - $FilePath = '{filePath}' - $IconFile = '{iconFile}' - $IconIndex = '{iconIndex}' - - $Shell = New-Object -ComObject WScript.Shell - $Shortcut = $Shell.CreateShortcut($FilePath) - $Shortcut.IconLocation = ""$IconFile, $IconIndex"" - $Shortcut.Save() - "; - } - - var base64EncodedScript = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(psScript)); - - ProcessStartInfo startInfo = new ProcessStartInfo() - { - FileName = "powershell.exe", - Arguments = $"-NoProfile -EncodedCommand {base64EncodedScript}", - Verb = "runas", - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden, - UseShellExecute = true - }; - - // Start the process - Process process = new Process() { StartInfo = startInfo }; - process.Start(); - process.WaitForExit(); - - return true; - } - catch (Exception ex) - { - // Could not create shortcut - App.Logger.LogWarning(ex, ex.Message); - } - - return false; - } - - private static bool TrySetUrlShortcutIcon(string filePath, string iconFile, int iconIndex) - { - var fileExist = File.Exists(filePath); - if (!fileExist) - { - return false; - } - - var lines = File.ReadAllLines(filePath).ToList(); - var hasInternetShortcutHeader = lines.Any(l => l.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase)); - - if (!hasInternetShortcutHeader) - { - return false; - } - - lines.RemoveAll(l => - l.StartsWith("IconFile=", StringComparison.OrdinalIgnoreCase) || - l.StartsWith("IconIndex=", StringComparison.OrdinalIgnoreCase)); - - int index = 0; - int insertedIndex = 0; - foreach(var line in lines) - { - var isInternetShortcutHeader = line.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase); - if(isInternetShortcutHeader) - { - insertedIndex = index + 1; - break; - } - - index++; - } - - if (insertedIndex > 0 && !string.IsNullOrEmpty(iconFile)) - { - lines.Insert(insertedIndex, $"IconFile={iconFile}"); - lines.Insert(insertedIndex + 1, $"IconIndex={iconIndex}"); - } - - File.WriteAllLines(filePath, lines); - - return true; - } - - private static bool TrySetLnkShortcutIcon(string filePath, string iconFile, int iconIndex) - { - using var link = new ShellLink(filePath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)); - if (string.IsNullOrWhiteSpace(iconFile)) - { - link.IconLocation = new IconLocation(string.Empty, 0); - } - else - { - link.IconLocation = new IconLocation(iconFile, iconIndex); - } - link.SaveAs(filePath); // Overwrite if exists - - return true; - } - - public static Task OpenObjectPickerAsync(long hWnd) - => WindowsObjectPicker.OpenObjectPickerAsync((nint)hWnd, App.Logger); - - private static ShellItem? GetFirstFile(ShellItem shi) - { - if (!shi.IsFolder || shi.IsStream) - { - return shi; - } - using var shf = new ShellFolder(shi); - if (shf.FirstOrDefault(x => !x.IsFolder || x.IsStream) is ShellItem item) - { - return item; - } - foreach (var shsfi in shf.Where(x => x.IsFolder && !x.IsStream)) - { - using var shsf = new ShellFolder(shsfi); - if (GetFirstFile(shsf) is ShellItem item2) - { - return item2; - } - } - return null; - } - - private static void UpdateFileTagsDb(ShellFileOperations2.ShellFileOpEventArgs e, string operationType) - { - var dbInstance = FileTagsHelper.GetDbInstance(); - if (e.Result.Succeeded && e.SourceItem.GetParsingPath() is { } sourcePath) - { - var destPath = e.DestFolder.GetParsingPath(); - var sourceFolderPath = Path.GetDirectoryName(sourcePath); - var destination = operationType switch - { - "delete" => e.DestItem.GetParsingPath(), - "rename" => sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, - "copy" => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null, - _ => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null - }; - if (destination is null) - { - dbInstance.SetTags(sourcePath, null, []); // remove tag from deleted files - } - else - { - SafetyExtensions.IgnoreExceptions(() => - { - if (operationType == "copy") - { - var tag = dbInstance.GetTags(sourcePath, null); - - dbInstance.SetTags(destination, FileTagsHelper.GetFileFRN(destination), tag); // copy tag to new files - using var si = new ShellItem(destination); - if (si.IsFolder) // File tag is not copied automatically for folders - { - _ = FileTagsHelper.WriteFileTagAsync(destination, tag); - } - } - else - { - dbInstance.UpdateTag(sourcePath, FileTagsHelper.GetFileFRN(destination), destination); // move tag to new files - } - }, App.Logger); - } - if (e.Result == HRESULT.COPYENGINE_S_DONT_PROCESS_CHILDREN) // child items not processed, update manually - { - var tags = dbInstance.GetAllUnderPath(sourcePath).ToList(); - if (destination is null) // remove tag for items contained in the folder - { - tags.ForEach(t => dbInstance.SetTags(t.FilePath, null, [])); - } - else - { - if (operationType == "copy") // copy tag for items contained in the folder - { - tags.ForEach(t => - { - SafetyExtensions.IgnoreExceptions(() => - { - var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); - dbInstance.SetTags(subPath, FileTagsHelper.GetFileFRN(subPath), t.Tags ?? []); - }, App.Logger); - }); - } - else // move tag to new files - { - tags.ForEach(t => - { - SafetyExtensions.IgnoreExceptions(() => - { - var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); - dbInstance.UpdateTag(t.FilePath, FileTagsHelper.GetFileFRN(subPath), subPath); - }, App.Logger); - }); - } - } - } - } - } - - public static void WaitForCompletion() - => progressHandler?.WaitForCompletion(); - - private sealed partial class ProgressHandler : Disposable - { - private readonly ManualResetEvent operationsCompletedEvent; - - public sealed class OperationWithProgress - { - public double Progress { get; set; } - public bool Canceled { get; set; } - } - - private readonly ConcurrentDictionary operations; - - public HWND OwnerWindow { get; set; } - - public ProgressHandler() - { - operations = new ConcurrentDictionary(); - operationsCompletedEvent = new ManualResetEvent(true); - } - - public int Progress - { - get - { - var ongoing = operations.ToArray().Where(x => !x.Value.Canceled); - return ongoing.Any() ? (int)ongoing.Average(x => x.Value.Progress) : 0; - } - } - - public void AddOperation(string uid) - { - operations.TryAdd(uid, new OperationWithProgress()); - operationsCompletedEvent.Reset(); - } - - public void RemoveOperation(string uid) - { - operations.TryRemove(uid, out _); - if (!operations.Any()) - { - operationsCompletedEvent.Set(); - } - } - - public void UpdateOperation(string uid, double progress) - { - if (operations.TryGetValue(uid, out var op)) - { - op.Progress = progress; - } - } - - public bool CheckCanceled(string uid) - { - return !operations.TryGetValue(uid, out var op) || op.Canceled; - } - - public void TryCancel(string uid) - { - if (operations.TryGetValue(uid, out var op)) - { - op.Canceled = true; - } - } - - public void WaitForCompletion() - { - operationsCompletedEvent.WaitOne(); - } - - protected override void Dispose(bool disposing) - { - if (disposing) - { - operationsCompletedEvent?.Dispose(); - } - } - } - - private static string GetIncrementalName(bool overWriteOnCopy, string filePathToCheck, string filePathToCopy) - { - if ((!Path.Exists(filePathToCheck)) || overWriteOnCopy || filePathToCheck == filePathToCopy) - return Path.GetFileName(filePathToCheck); - - var index = 2; - var filePath = filePathToCheck; - if (Path.HasExtension(filePathToCheck)) - filePath = filePathToCheck.Substring(0, filePathToCheck.LastIndexOf('.')); - - Func genFilePath = x => string.Concat([filePath, " (", x.ToString(), ")", Path.GetExtension(filePathToCheck)]); - - while (Path.Exists(genFilePath(index))) - index++; - - return Path.GetFileName(genFilePath(index)); - } - - private static bool MainStreamExists(string path) - { - // Path.Exists is always false for ADS paths (file.txt:stream), so check the main stream's path - var fileName = Path.GetFileName(path); - var colonIndex = fileName.IndexOf(':'); - if (colonIndex is not -1) - path = path[..(path.Length - fileName.Length + colonIndex)]; - - return Path.Exists(path); - } - } -} +// Copyright (c) Files Community +// Licensed under the MIT License. + +using Files.App.Utils.Storage.Operations; +using Files.Shared.Helpers; +using Microsoft.Extensions.Logging; +using System.Collections.Concurrent; +using System.IO; +using Windows.ApplicationModel.DataTransfer; +using Windows.Win32; +using Windows.Win32.UI.WindowsAndMessaging; +using FILEOPERATION_FLAGS = Windows.Win32.UI.Shell.FILEOPERATION_FLAGS; +using HRESULT = Windows.Win32.Foundation.HRESULT; +using HWND = Windows.Win32.Foundation.HWND; +using PROPERTYKEY = Windows.Win32.Foundation.PROPERTYKEY; +using SLR_FLAGS = Windows.Win32.UI.Shell.SLR_FLAGS; + +namespace Files.App.Utils.Storage +{ + public sealed partial class FileOperationsHelpers + { + private static readonly PROPERTYKEY PKEY_FilePlaceholderStatus = new() { fmtid = new("B2F9B9D6-FEC4-4DD5-94D7-8957488C807B"), pid = 2 }; + private const uint PS_CLOUDFILE_PLACEHOLDER = 8; + + private static ProgressHandler? progressHandler; // Warning: must be initialized from a MTA thread + + public static Task SetClipboard(string[] filesToCopy, DataPackageOperation operation) + { + return STATask.Run(() => + { + uint preferredDropEffect = (uint)(operation == DataPackageOperation.Copy + ? DataPackageOperation.Copy | DataPackageOperation.Link + : DataPackageOperation.Move); + ShellDataObject.SetClipboard(filesToCopy, preferredDropEffect); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> CreateItemAsync(string filePath, string fileOp, long ownerHwnd, bool asAdmin, string? template = null, byte[]? dataBytes = null) + { + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION | FILEOPERATION_FLAGS.FOF_NOERRORUI; + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + var shellOperationResult = new ShellOperationResult(); + var parentPath = Path.GetDirectoryName(filePath); + + if (parentPath is null || !SafetyExtensions.IgnoreExceptions(() => + { + using var shd = new ShellFolder(parentPath); + op.QueueNewItemOperation(shd, Path.GetFileName(filePath), + fileOp == "CreateFolder" ? FileAttributes.Directory : FileAttributes.Normal, template); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Destination = filePath, + HResult = -1 + }); + } + + var createTcs = new TaskCompletionSource(); + op.PostNewItem += (s, e) => + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Destination = e.DestItem.GetParsingPath(), + HResult = (int)e.Result + }); + }; + op.FinishOperations += (s, e) => createTcs.TrySetResult(e.Result.Succeeded); + + try + { + op.PerformOperations(); + } + catch + { + createTcs.TrySetResult(false); + } + + if (dataBytes is not null && + shellOperationResult.Items.SingleOrDefault() is { Succeeded: true, Destination: { } destination }) + { + SafetyExtensions.IgnoreExceptions(() => + { + using var fs = new FileStream(destination, FileMode.Open); + fs.Write(dataBytes, 0, dataBytes.Length); + fs.Flush(); + }, App.Logger); + } + + return (await createTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> TestRecycleAsync(string[] fileToDeletePath) + { + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; + op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE; + + var shellOperationResult = new ShellOperationResult(); + var tryDelete = false; + + for (var i = 0; i < fileToDeletePath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using var shi = new ShellItem(fileToDeletePath[i]); + using var file = SafetyExtensions.IgnoreExceptions(() => GetFirstFile(shi)) ?? shi; + if (file.Properties.TryGetValue(PKEY_FilePlaceholderStatus, out var value) && value == PS_CLOUDFILE_PLACEHOLDER) + { + // Online only files cannot be tried for deletion, so they are treated as to be permanently deleted. + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToDeletePath[i], + HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND + }); + } + else + { + op.QueueDeleteOperation(file); + tryDelete = true; + } + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToDeletePath[i], + HResult = -1 + }); + } + } + + if (!tryDelete) + return (true, shellOperationResult); + + var deleteTcs = new TaskCompletionSource(); + op.PreDeleteItem += [DebuggerHidden] (s, e) => + { + if ((e.Flags & Windows.Win32.UI.Shell._TRANSFER_SOURCE_FLAGS.TSF_DELETE_RECYCLE_IF_POSSIBLE) is 0) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = e.SourceItem.GetParsingPath(), + HResult = (int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND + }); + throw new Win32Exception((int)HRESULT.COPYENGINE_E_RECYCLE_BIN_NOT_FOUND); // E_FAIL, stops operation + } + else + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = true, + Source = e.SourceItem.GetParsingPath(), + HResult = (int)HRESULT.COPYENGINE_E_USER_CANCELLED + }); + throw new Win32Exception((int)HRESULT.COPYENGINE_E_USER_CANCELLED); // E_FAIL, stops operation + } + }; + op.FinishOperations += (s, e) => deleteTcs.TrySetResult(e.Result.Succeeded); + + try + { + op.PerformOperations(); + } + catch + { + deleteTcs.TrySetResult(false); + } + + return (await deleteTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> DeleteItemAsync(string[] fileToDeletePath, bool permanently, long ownerHwnd, bool asAdmin, IProgress? progress, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + StatusCenterItemProgressModel fsProgress = new( + progress, + false, + FileSystemStatusCode.InProgress); + + var cts = new CancellationTokenSource(); + var sizeCalculator = new FileSizeCalculator(fileToDeletePath); + + // Track the count and update the progress + sizeCalculator.ItemsCountChanged += (newCount) => + { + fsProgress.ItemsCount = newCount; + fsProgress.Report(); + }; + + var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); + sizeTask.ContinueWith(_ => + { + fsProgress.TotalSize = 0; + fsProgress.ItemsCount = sizeCalculator.ItemsCount; + fsProgress.EnumerationCompleted = true; + fsProgress.Report(); + }); + + fsProgress.Report(); + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOCONFIRMATION | FILEOPERATION_FLAGS.FOF_NOERRORUI; + + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + if (!permanently) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_RECYCLEONDELETE | FILEOPERATION_FLAGS.FOF_WANTNUKEWARNING; + } + + var shellOperationResult = new ShellOperationResult(); + + for (var i = 0; i < fileToDeletePath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using var shi = new ShellItem(fileToDeletePath[i]); + + op.QueueDeleteOperation(shi); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToDeletePath[i], + HResult = -1 + }); + } + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var deleteTcs = new TaskCompletionSource(); + + // Right before deleting item + op.PreDeleteItem += (s, e) => + { + if (e.SourceItem is not { } sourceItem) + return; + + if (sourceItem.GetParsingPath() is { } sourcePath) + sizeCalculator.ForceComputeFileSize(sourcePath); + fsProgress.FileName = sourceItem.Name ?? string.Empty; + fsProgress.Report(); + }; + + // Right after deleted item + op.PostDeleteItem += (s, e) => + { + var sourceItem = e.SourceItem; + var sourcePath = sourceItem?.GetParsingPath(); + if (sourceItem is { IsFolder: false } && sourcePath is not null) + { + if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) + fsProgress.AddProcessedItemsCount(1); + } + + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = e.DestItem?.GetParsingPath(), + HResult = (int)e.Result + }); + + UpdateFileTagsDb(e, "delete"); + }; + + op.FinishOperations += (s, e) + => deleteTcs.TrySetResult(e.Result.Succeeded); + + op.UpdateProgress += (s, e) => + { + // E_FAIL, stops operation + if (progressHandler.CheckCanceled(operationID)) + throw new Win32Exception(unchecked((int)0x80004005)); + + fsProgress.Report(e.ProgressPercentage); + progressHandler.UpdateOperation(operationID, e.ProgressPercentage); + }; + + try + { + op.PerformOperations(); + } + catch + { + deleteTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + cts.Cancel(); + + return (await deleteTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> RenameItemAsync(string fileToRenamePath, string newName, bool overwriteOnRename, long ownerHwnd, bool asAdmin, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + var shellOperationResult = new ShellOperationResult(); + + op.Options = FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + op.OwnerWindow = (HWND)(nint)ownerHwnd; + op.Options |= !overwriteOnRename ? FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : 0; + + if (!SafetyExtensions.IgnoreExceptions(() => + { + using var shi = new ShellItem(fileToRenamePath); + + // Generate an incremental name (e.g. "file (2).txt") instead of letting the shell append a localized "Copy" suffix. + // A case-only rename targets the same item, so it must be left untouched. + var renameTargetPath = Path.Combine(Path.GetDirectoryName(fileToRenamePath) ?? string.Empty, newName); + var finalName = !overwriteOnRename && !string.Equals(fileToRenamePath, renameTargetPath, StringComparison.OrdinalIgnoreCase) + ? GetIncrementalName(false, renameTargetPath, fileToRenamePath) + : newName; + + op.QueueRenameOperation(shi, finalName); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToRenamePath, + HResult = -1 + }); + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var renameTcs = new TaskCompletionSource(); + op.PostRenameItem += (s, e) => + { + var sourcePath = e.SourceItem.GetParsingPath(); + var sourceFolderPath = sourcePath is null ? null : Path.GetDirectoryName(sourcePath); + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, + HResult = (int)e.Result + }); + }; + op.PostRenameItem += (_, e) => UpdateFileTagsDb(e, "rename"); + op.FinishOperations += (s, e) => renameTcs.TrySetResult(e.Result.Succeeded); + + try + { + op.PerformOperations(); + } + catch + { + renameTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + return (await renameTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> MoveItemAsync(string[] fileToMovePath, string[] moveDestination, bool overwriteOnMove, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + StatusCenterItemProgressModel fsProgress = new( + progress, + false, + FileSystemStatusCode.InProgress); + + var cts = new CancellationTokenSource(); + var sizeCalculator = new FileSizeCalculator(fileToMovePath); + + // Track the count and update the progress + sizeCalculator.ItemsCountChanged += (newCount) => + { + fsProgress.ItemsCount = newCount; + fsProgress.Report(); + }; + + var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); + sizeTask.ContinueWith(_ => + { + fsProgress.TotalSize = sizeCalculator.Size; + fsProgress.ItemsCount = sizeCalculator.ItemsCount; + fsProgress.EnumerationCompleted = true; + fsProgress.Report(); + }); + + fsProgress.Report(); + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + var shellOperationResult = new ShellOperationResult(); + + op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; + + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + op.Options |= !overwriteOnMove ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; + + for (var i = 0; i < fileToMovePath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using ShellItem shi = new(fileToMovePath[i]); + var destinationFolderPath = Path.GetDirectoryName(moveDestination[i]) + ?? throw new ArgumentException("The move destination must include a parent folder.", nameof(moveDestination)); + using ShellFolder shd = new(destinationFolderPath); + + // Generate an incremental name (e.g. "file (2).txt") instead of letting the shell append a localized "Copy" suffix + var moveFileName = GetIncrementalName(overwriteOnMove, moveDestination[i], fileToMovePath[i]); + op.QueueMoveOperation(shi, shd, moveFileName); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToMovePath[i], + Destination = moveDestination[i], + // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead + HResult = MainStreamExists(fileToMovePath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC + }); + } + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var moveTcs = new TaskCompletionSource(); + + op.PreMoveItem += (s, e) => + { + if (e.SourceItem is not { } sourceItem) + return; + + if (sourceItem.GetParsingPath() is { } sourcePath) + sizeCalculator.ForceComputeFileSize(sourcePath); + fsProgress.FileName = sourceItem.Name ?? string.Empty; + fsProgress.Report(); + }; + + op.PostMoveItem += (s, e) => + { + var sourceItem = e.SourceItem; + var sourcePath = sourceItem?.GetParsingPath(); + if (sourceItem is { IsFolder: false } && sourcePath is not null) + { + if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) + fsProgress.AddProcessedItemsCount(1); + } + + var destinationFolderPath = e.DestFolder?.GetParsingPath(); + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, + HResult = (int)e.Result + }); + + UpdateFileTagsDb(e, "move"); + }; + + op.FinishOperations += (s, e) + => moveTcs.TrySetResult(e.Result.Succeeded); + + op.UpdateProgress += (s, e) => + { + // E_FAIL, stops operation + if (progressHandler.CheckCanceled(operationID)) + throw new Win32Exception(unchecked((int)0x80004005)); + + fsProgress.Report(e.ProgressPercentage); + progressHandler.UpdateOperation(operationID, e.ProgressPercentage); + }; + + try + { + op.PerformOperations(); + } + catch + { + moveTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + cts.Cancel(); + + return (await moveTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static Task<(bool, ShellOperationResult)> CopyItemAsync(string[] fileToCopyPath, string[] copyDestination, bool overwriteOnCopy, long ownerHwnd, bool asAdmin, IProgress progress, string operationID = "") + { + operationID = string.IsNullOrEmpty(operationID) ? Guid.NewGuid().ToString() : operationID; + + StatusCenterItemProgressModel fsProgress = new( + progress, + false, + FileSystemStatusCode.InProgress); + + var cts = new CancellationTokenSource(); + var sizeCalculator = new FileSizeCalculator(fileToCopyPath); + + // Track the count and update the progress + sizeCalculator.ItemsCountChanged += (newCount) => + { + fsProgress.ItemsCount = newCount; + fsProgress.Report(); + }; + + var sizeTask = sizeCalculator.ComputeSizeAsync(cts.Token); + sizeTask.ContinueWith(_ => + { + fsProgress.TotalSize = sizeCalculator.Size; + fsProgress.ItemsCount = sizeCalculator.ItemsCount; + fsProgress.EnumerationCompleted = true; + fsProgress.Report(); + }); + + fsProgress.Report(); + progressHandler ??= new(); + + return STATask.Run(async () => + { + using var op = new ShellFileOperations2(); + + var shellOperationResult = new ShellOperationResult(); + + op.Options = FILEOPERATION_FLAGS.FOF_NOCONFIRMMKDIR | FILEOPERATION_FLAGS.FOF_SILENT | FILEOPERATION_FLAGS.FOF_NOERRORUI; + + if (asAdmin) + { + op.Options |= FILEOPERATION_FLAGS.FOFX_SHOWELEVATIONPROMPT | FILEOPERATION_FLAGS.FOFX_REQUIREELEVATION; + } + + op.OwnerWindow = (HWND)(nint)ownerHwnd; + + op.Options |= !overwriteOnCopy ? FILEOPERATION_FLAGS.FOFX_PRESERVEFILEEXTENSIONS | FILEOPERATION_FLAGS.FOF_RENAMEONCOLLISION : FILEOPERATION_FLAGS.FOF_NOCONFIRMATION; + + for (var i = 0; i < fileToCopyPath.Length; i++) + { + if (!SafetyExtensions.IgnoreExceptions(() => + { + using ShellItem shi = new(fileToCopyPath[i]); + var destinationFolderPath = Path.GetDirectoryName(copyDestination[i]) + ?? throw new ArgumentException("The copy destination must include a parent folder.", nameof(copyDestination)); + using ShellFolder shd = new(destinationFolderPath); + + var fileName = GetIncrementalName(overwriteOnCopy, copyDestination[i], fileToCopyPath[i]); + // Perform a copy operation + op.QueueCopyOperation(shi, shd, fileName); + })) + { + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = false, + Source = fileToCopyPath[i], + Destination = copyDestination[i], + // HResult -1 makes the caller retry with the StorageFile API (ADS); a missing source must map to NotFound instead + HResult = MainStreamExists(fileToCopyPath[i]) ? -1 : CopyEngineResult.COPYENGINE_E_PATH_NOT_FOUND_SRC + }); + } + } + + progressHandler.OwnerWindow = op.OwnerWindow; + progressHandler.AddOperation(operationID); + + var copyTcs = new TaskCompletionSource(); + + op.PreCopyItem += (s, e) => + { + if (e.SourceItem is not { } sourceItem) + return; + + if (sourceItem.GetParsingPath() is { } sourcePath) + sizeCalculator.ForceComputeFileSize(sourcePath); + fsProgress.FileName = sourceItem.Name ?? string.Empty; + fsProgress.Report(); + }; + + op.PostCopyItem += (s, e) => + { + var sourceItem = e.SourceItem; + var sourcePath = sourceItem?.GetParsingPath(); + if (sourceItem is { IsFolder: false } && sourcePath is not null) + { + if (sizeCalculator.TryGetComputedFileSize(sourcePath, out _)) + fsProgress.AddProcessedItemsCount(1); + } + + var destinationFolderPath = e.DestFolder?.GetParsingPath(); + shellOperationResult.Items.Add(new ShellOperationItemResult() + { + Succeeded = e.Result.Succeeded, + Source = sourcePath, + Destination = destinationFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destinationFolderPath, e.Name) : null, + HResult = (int)e.Result + }); + + UpdateFileTagsDb(e, "copy"); + }; + + op.FinishOperations += (s, e) + => copyTcs.TrySetResult(e.Result.Succeeded); + + op.UpdateProgress += (s, e) => + { + // E_FAIL, stops operation + if (progressHandler.CheckCanceled(operationID)) + throw new Win32Exception(unchecked((int)0x80004005)); + + fsProgress.Report(e.ProgressPercentage); + progressHandler.UpdateOperation(operationID, e.ProgressPercentage); + }; + + try + { + op.PerformOperations(); + } + catch + { + copyTcs.TrySetResult(false); + } + + progressHandler.RemoveOperation(operationID); + + cts.Cancel(); + + return (await copyTcs.Task, shellOperationResult); + }, App.Logger); + } + + public static void TryCancelOperation(string operationId) + => progressHandler?.TryCancel(operationId); + + public static IEnumerable? CheckFileInUse(string[] fileToCheckPath) + { + var processes = SafetyExtensions.IgnoreExceptions(() => Win32Helper.WhoIsLocking(fileToCheckPath), App.Logger); + + if (processes is not null) + { + var win32proc = processes.Select(x => new Win32Process() + { + Name = x.ProcessName, + Pid = x.Id, + FileName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileName), + AppName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileVersionInfo?.FileDescription) + }).ToList(); + processes.ForEach(x => x.Dispose()); + + return win32proc; + } + else + { + return null; + } + } + + /// + /// Whether to run the shell's link resolution (which may search for moved targets and touch the + /// network). Pass when only the data stored in the link file is needed, + /// e.g. for listing items. + /// + public static async Task ParseLinkAsync(string linkPath, bool resolveTarget = true) + { + if (string.IsNullOrEmpty(linkPath)) + return null; + + string targetPath = string.Empty; + + try + { + if (FileExtensionHelpers.IsShortcutFile(linkPath)) + { + using var link = resolveTarget + ? new ShellLink(linkPath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)) + : new ShellLink(linkPath, resolve: false); + targetPath = link.TargetPath; + + // Broken shortcut (rooted target that's gone) keeps the delete prompt; app/shell targets aren't rooted + if (resolveTarget && Path.IsPathRooted(targetPath) && + !targetPath.StartsWith(@"\\", StringComparison.Ordinal) && !Path.Exists(targetPath)) + { + return new ShellLinkItem { TargetPath = targetPath, InvalidTarget = true }; + } + + return ShellFolderExtensions.GetShellLinkItem(link); + } + else if (FileExtensionHelpers.IsWebLinkFile(linkPath)) + { + targetPath = await STATask.Run(() => InternetShortcut.Load(linkPath), App.Logger); + return string.IsNullOrEmpty(targetPath) ? + new ShellLinkItem + { + TargetPath = string.Empty, + InvalidTarget = true + } : new ShellLinkItem { TargetPath = targetPath }; + } + return null; + } + catch (FileNotFoundException ex) // Could not parse shortcut + { + App.Logger?.LogWarning(ex, ex.Message); + // Return a item containing the invalid target path + return new ShellLinkItem + { + TargetPath = string.IsNullOrEmpty(targetPath) ? string.Empty : targetPath, + InvalidTarget = true + }; + } + catch (Exception ex) + { + // Could not parse shortcut + App.Logger.LogWarning(ex, ex.Message); + return null; + } + } + + public static Task CreateOrUpdateLinkAsync(string linkSavePath, string? targetPath, string? arguments = "", string? workingDirectory = "", bool runAsAdmin = false, SHOW_WINDOW_CMD showWindowCommand = SHOW_WINDOW_CMD.SW_NORMAL) + { + try + { + ArgumentNullException.ThrowIfNull(targetPath); + if (FileExtensionHelpers.IsShortcutFile(linkSavePath)) + { + using var newLink = new ShellLink(targetPath, arguments, workingDirectory); + + // Check if the target is a file + if (File.Exists(targetPath)) + newLink.RunAsAdministrator = runAsAdmin; + + newLink.SaveAs(linkSavePath); // Overwrite if exists + + // ShowState has to be set after SaveAs has been called, otherwise an UnauthorizedAccessException gets thrown in some cases + newLink.ShowState = showWindowCommand; + + return Task.FromResult(true); + } + else if (FileExtensionHelpers.IsWebLinkFile(linkSavePath)) + { + return STATask.Run(() => + { + InternetShortcut.Save(linkSavePath, targetPath); + return true; + }, App.Logger); + } + } + catch (UnauthorizedAccessException ex) + { + // Could not create shortcut + App.Logger.LogInformation(ex, "Failed to create shortcut"); + } + catch (Exception ex) + { + // Could not create shortcut + App.Logger.LogWarning(ex, ex.Message); + } + + return Task.FromResult(false); + } + + public static bool SetLinkIcon(string filePath, string? iconFile, int iconIndex) + { + iconFile ??= string.Empty; + var ext = Path.GetExtension(filePath).ToLowerInvariant(); + + try + { + return ext switch + { + ".lnk" => TrySetLnkShortcutIcon(filePath, iconFile, iconIndex), + ".url" => TrySetUrlShortcutIcon(filePath, iconFile, iconIndex), + _ => false, + }; + } + catch (UnauthorizedAccessException) + { + string psScript; + filePath = filePath.Replace("'", "''"); + iconFile = iconFile.Replace("'", "''"); + + if(ext == ".url") + { + psScript = $@" + $path = '{filePath}' + $iconFile = '{iconFile}' + $iconIndex = '{iconIndex}' + $content = Get-Content -LiteralPath $path + + $content = $content | Where-Object {{ $_ -notmatch '^IconFile=' -and $_ -notmatch '^IconIndex=' }} + + $newContent = foreach ($line in $content) {{ + $line + if ($line -eq '[InternetShortcut]') {{ + ""IconFile=$iconFile"" + ""IconIndex=$iconIndex"" + }} + }} + $newContent | Set-Content -LiteralPath $path -Encoding UTF8 + "; + } + else + { + psScript = $@" + $FilePath = '{filePath}' + $IconFile = '{iconFile}' + $IconIndex = '{iconIndex}' + + $Shell = New-Object -ComObject WScript.Shell + $Shortcut = $Shell.CreateShortcut($FilePath) + $Shortcut.IconLocation = ""$IconFile, $IconIndex"" + $Shortcut.Save() + "; + } + + var base64EncodedScript = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(psScript)); + + ProcessStartInfo startInfo = new ProcessStartInfo() + { + FileName = "powershell.exe", + Arguments = $"-NoProfile -EncodedCommand {base64EncodedScript}", + Verb = "runas", + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + UseShellExecute = true + }; + + // Start the process + Process process = new Process() { StartInfo = startInfo }; + process.Start(); + process.WaitForExit(); + + return true; + } + catch (Exception ex) + { + // Could not create shortcut + App.Logger.LogWarning(ex, ex.Message); + } + + return false; + } + + private static bool TrySetUrlShortcutIcon(string filePath, string iconFile, int iconIndex) + { + var fileExist = File.Exists(filePath); + if (!fileExist) + { + return false; + } + + var lines = File.ReadAllLines(filePath).ToList(); + var hasInternetShortcutHeader = lines.Any(l => l.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase)); + + if (!hasInternetShortcutHeader) + { + return false; + } + + lines.RemoveAll(l => + l.StartsWith("IconFile=", StringComparison.OrdinalIgnoreCase) || + l.StartsWith("IconIndex=", StringComparison.OrdinalIgnoreCase)); + + int index = 0; + int insertedIndex = 0; + foreach(var line in lines) + { + var isInternetShortcutHeader = line.Trim().Equals("[InternetShortcut]", StringComparison.OrdinalIgnoreCase); + if(isInternetShortcutHeader) + { + insertedIndex = index + 1; + break; + } + + index++; + } + + if (insertedIndex > 0 && !string.IsNullOrEmpty(iconFile)) + { + lines.Insert(insertedIndex, $"IconFile={iconFile}"); + lines.Insert(insertedIndex + 1, $"IconIndex={iconIndex}"); + } + + File.WriteAllLines(filePath, lines); + + return true; + } + + private static bool TrySetLnkShortcutIcon(string filePath, string iconFile, int iconIndex) + { + using var link = new ShellLink(filePath, SLR_FLAGS.SLR_NO_UI_WITH_MSG_PUMP, timeout: TimeSpan.FromMilliseconds(100)); + if (string.IsNullOrWhiteSpace(iconFile)) + { + link.IconLocation = new IconLocation(string.Empty, 0); + } + else + { + link.IconLocation = new IconLocation(iconFile, iconIndex); + } + link.SaveAs(filePath); // Overwrite if exists + + return true; + } + + public static Task OpenObjectPickerAsync(long hWnd) + => WindowsObjectPicker.OpenObjectPickerAsync((nint)hWnd, App.Logger); + + private static ShellItem? GetFirstFile(ShellItem shi) + { + if (!shi.IsFolder || shi.IsStream) + { + return shi; + } + using var shf = new ShellFolder(shi); + if (shf.FirstOrDefault(x => !x.IsFolder || x.IsStream) is ShellItem item) + { + return item; + } + foreach (var shsfi in shf.Where(x => x.IsFolder && !x.IsStream)) + { + using var shsf = new ShellFolder(shsfi); + if (GetFirstFile(shsf) is ShellItem item2) + { + return item2; + } + } + return null; + } + + private static void UpdateFileTagsDb(ShellFileOperations2.ShellFileOpEventArgs e, string operationType) + { + var dbInstance = FileTagsHelper.GetDbInstance(); + if (e.Result.Succeeded && e.SourceItem.GetParsingPath() is { } sourcePath) + { + var destPath = e.DestFolder.GetParsingPath(); + var sourceFolderPath = Path.GetDirectoryName(sourcePath); + var destination = operationType switch + { + "delete" => e.DestItem.GetParsingPath(), + "rename" => sourceFolderPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(sourceFolderPath, e.Name) : null, + "copy" => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null, + _ => destPath is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(destPath, e.Name) : null + }; + if (destination is null) + { + dbInstance.SetTags(sourcePath, null, []); // remove tag from deleted files + } + else + { + SafetyExtensions.IgnoreExceptions(() => + { + if (operationType == "copy") + { + var tag = dbInstance.GetTags(sourcePath, null); + + dbInstance.SetTags(destination, FileTagsHelper.GetFileFRN(destination), tag); // copy tag to new files + using var si = new ShellItem(destination); + if (si.IsFolder) // File tag is not copied automatically for folders + { + _ = FileTagsHelper.WriteFileTagAsync(destination, tag); + } + } + else + { + dbInstance.UpdateTag(sourcePath, FileTagsHelper.GetFileFRN(destination), destination); // move tag to new files + } + }, App.Logger); + } + if (e.Result == HRESULT.COPYENGINE_S_DONT_PROCESS_CHILDREN) // child items not processed, update manually + { + var tags = dbInstance.GetAllUnderPath(sourcePath).ToList(); + if (destination is null) // remove tag for items contained in the folder + { + tags.ForEach(t => dbInstance.SetTags(t.FilePath, null, [])); + } + else + { + if (operationType == "copy") // copy tag for items contained in the folder + { + tags.ForEach(t => + { + SafetyExtensions.IgnoreExceptions(() => + { + var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); + dbInstance.SetTags(subPath, FileTagsHelper.GetFileFRN(subPath), t.Tags ?? []); + }, App.Logger); + }); + } + else // move tag to new files + { + tags.ForEach(t => + { + SafetyExtensions.IgnoreExceptions(() => + { + var subPath = t.FilePath.Replace(sourcePath, destination, StringComparison.Ordinal); + dbInstance.UpdateTag(t.FilePath, FileTagsHelper.GetFileFRN(subPath), subPath); + }, App.Logger); + }); + } + } + } + } + } + + public static void WaitForCompletion() + => progressHandler?.WaitForCompletion(); + + private sealed partial class ProgressHandler : Disposable + { + private readonly ManualResetEvent operationsCompletedEvent; + + public sealed class OperationWithProgress + { + public double Progress { get; set; } + public bool Canceled { get; set; } + } + + private readonly ConcurrentDictionary operations; + + public HWND OwnerWindow { get; set; } + + public ProgressHandler() + { + operations = new ConcurrentDictionary(); + operationsCompletedEvent = new ManualResetEvent(true); + } + + public int Progress + { + get + { + var ongoing = operations.ToArray().Where(x => !x.Value.Canceled); + return ongoing.Any() ? (int)ongoing.Average(x => x.Value.Progress) : 0; + } + } + + public void AddOperation(string uid) + { + operations.TryAdd(uid, new OperationWithProgress()); + operationsCompletedEvent.Reset(); + } + + public void RemoveOperation(string uid) + { + operations.TryRemove(uid, out _); + if (!operations.Any()) + { + operationsCompletedEvent.Set(); + } + } + + public void UpdateOperation(string uid, double progress) + { + if (operations.TryGetValue(uid, out var op)) + { + op.Progress = progress; + } + } + + public bool CheckCanceled(string uid) + { + return !operations.TryGetValue(uid, out var op) || op.Canceled; + } + + public void TryCancel(string uid) + { + if (operations.TryGetValue(uid, out var op)) + { + op.Canceled = true; + } + } + + public void WaitForCompletion() + { + operationsCompletedEvent.WaitOne(); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + operationsCompletedEvent?.Dispose(); + } + } + } + + private static string GetIncrementalName(bool overWriteOnCopy, string filePathToCheck, string filePathToCopy) + { + if ((!Path.Exists(filePathToCheck)) || overWriteOnCopy || filePathToCheck == filePathToCopy) + return Path.GetFileName(filePathToCheck); + + var index = 2; + var filePath = filePathToCheck; + if (Path.HasExtension(filePathToCheck)) + filePath = filePathToCheck.Substring(0, filePathToCheck.LastIndexOf('.')); + + Func genFilePath = x => string.Concat([filePath, " (", x.ToString(), ")", Path.GetExtension(filePathToCheck)]); + + while (Path.Exists(genFilePath(index))) + index++; + + return Path.GetFileName(genFilePath(index)); + } + + private static bool MainStreamExists(string path) + { + // Path.Exists is always false for ADS paths (file.txt:stream), so check the main stream's path + var fileName = Path.GetFileName(path); + var colonIndex = fileName.IndexOf(':'); + if (colonIndex is not -1) + path = path[..(path.Length - fileName.Length + colonIndex)]; + + return Path.Exists(path); + } + } +}