Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into remove_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gave92 committed May 23, 2021
2 parents 03fe656 + de5dd1b commit a3ee462
Show file tree
Hide file tree
Showing 35 changed files with 823 additions and 416 deletions.
56 changes: 56 additions & 0 deletions Files.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,62 @@ await Win32API.StartSTATask(() =>
}
break;

case "RenameItem":
var fileToRenamePath = (string)message["filepath"];
var newName = (string)message["newName"];
var overwriteOnRename = (bool)message["overwrite"];
using (var op = new ShellFileOperations())
{
op.Options = ShellFileOperations.OperationFlags.NoUI;
op.Options |= !overwriteOnRename ? ShellFileOperations.OperationFlags.PreserveFileExtensions | ShellFileOperations.OperationFlags.RenameOnCollision : 0;
using var shi = new ShellItem(fileToRenamePath);
op.QueueRenameOperation(shi, newName);
var renameTcs = new TaskCompletionSource<bool>();
op.PostRenameItem += (s, e) => renameTcs.TrySetResult(e.Result.Succeeded);
op.PerformOperations();
var result = await renameTcs.Task;
await Win32API.SendMessageAsync(connection, new ValueSet() { { "Success", result } }, message.Get("RequestID", (string)null));
}
break;

case "MoveItem":
var fileToMovePath = (string)message["filepath"];
var moveDestination = (string)message["destpath"];
var overwriteOnMove = (bool)message["overwrite"];
using (var op = new ShellFileOperations())
{
op.Options = ShellFileOperations.OperationFlags.NoUI;
op.Options |= !overwriteOnMove ? ShellFileOperations.OperationFlags.PreserveFileExtensions | ShellFileOperations.OperationFlags.RenameOnCollision : 0;
using var shi = new ShellItem(fileToMovePath);
using var shd = new ShellFolder(Path.GetDirectoryName(moveDestination));
op.QueueMoveOperation(shi, shd, Path.GetFileName(moveDestination));
var moveTcs = new TaskCompletionSource<bool>();
op.PostMoveItem += (s, e) => moveTcs.TrySetResult(e.Result.Succeeded);
op.PerformOperations();
var result = await moveTcs.Task;
await Win32API.SendMessageAsync(connection, new ValueSet() { { "Success", result } }, message.Get("RequestID", (string)null));
}
break;

case "CopyItem":
var fileToCopyPath = (string)message["filepath"];
var copyDestination = (string)message["destpath"];
var overwriteOnCopy = (bool)message["overwrite"];
using (var op = new ShellFileOperations())
{
op.Options = ShellFileOperations.OperationFlags.NoUI;
op.Options |= !overwriteOnCopy ? ShellFileOperations.OperationFlags.PreserveFileExtensions | ShellFileOperations.OperationFlags.RenameOnCollision : 0;
using var shi = new ShellItem(fileToCopyPath);
using var shd = new ShellFolder(Path.GetDirectoryName(copyDestination));
op.QueueCopyOperation(shi, shd, Path.GetFileName(copyDestination));
var copyTcs = new TaskCompletionSource<bool>();
op.PostCopyItem += (s, e) => copyTcs.TrySetResult(e.Result.Succeeded);
op.PerformOperations();
var result = await copyTcs.Task;
await Win32API.SendMessageAsync(connection, new ValueSet() { { "Success", result } }, message.Get("RequestID", (string)null));
}
break;

case "ParseLink":
var linkPath = (string)message["filepath"];
try
Expand Down
3 changes: 2 additions & 1 deletion Files/Enums/FileSystemStatusCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum FileSystemStatusCode
AlreadyExists = 32,
NotAFolder = 64,
NotAFile = 128,
InProgress = 256
ReadOnly = 256,
InProgress = 512
}
}
175 changes: 119 additions & 56 deletions Files/Filesystem/FilesystemOperations/FilesystemOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ await DialogDisplayHelper.ShowDialogAsync(
}
fsResult = fsCopyResult;
}
if (fsResult == FileSystemStatusCode.Unauthorized)
{
fsResult = await PerformAdminOperation(new ValueSet()
{
{ "Arguments", "FileOperation" },
{ "fileop", "CopyItem" },
{ "filepath", source.Path },
{ "destpath", destination },
{ "overwrite", collision == NameCollisionOption.ReplaceExisting }
});
}
errorCode?.Report(fsResult.ErrorCode);
if (!fsResult)
{
Expand Down Expand Up @@ -234,6 +245,17 @@ await DialogDisplayHelper.ShowDialogAsync(
}
fsResult = fsResultCopy;
}
if (fsResult == FileSystemStatusCode.Unauthorized)
{
fsResult = await PerformAdminOperation(new ValueSet()
{
{ "Arguments", "FileOperation" },
{ "fileop", "CopyItem" },
{ "filepath", source.Path },
{ "destpath", destination },
{ "overwrite", collision == NameCollisionOption.ReplaceExisting }
});
}
}
errorCode?.Report(fsResult.ErrorCode);
if (!fsResult)
Expand Down Expand Up @@ -387,6 +409,17 @@ await DialogDisplayHelper.ShowDialogAsync(
}
fsResult = fsResultMove;
}
if (fsResult == FileSystemStatusCode.Unauthorized || fsResult == FileSystemStatusCode.ReadOnly)
{
fsResult = await PerformAdminOperation(new ValueSet()
{
{ "Arguments", "FileOperation" },
{ "fileop", "MoveItem" },
{ "filepath", source.Path },
{ "destpath", destination },
{ "overwrite", collision == NameCollisionOption.ReplaceExisting }
});
}
}
errorCode?.Report(fsResult.ErrorCode);
}
Expand All @@ -406,7 +439,7 @@ await DialogDisplayHelper.ShowDialogAsync(
if (fsResult)
{
var file = (StorageFile)sourceResult;
var fsResultMove = await FilesystemTasks.Wrap(() => file.MoveAsync(destinationResult.Result, Path.GetFileName(file.Name), NameCollisionOption.FailIfExists).AsTask());
var fsResultMove = await FilesystemTasks.Wrap(() => file.MoveAsync(destinationResult.Result, Path.GetFileName(file.Name), collision).AsTask());

if (fsResultMove == FileSystemStatusCode.AlreadyExists)
{
Expand All @@ -421,6 +454,17 @@ await DialogDisplayHelper.ShowDialogAsync(
}
fsResult = fsResultMove;
}
if (fsResult == FileSystemStatusCode.Unauthorized || fsResult == FileSystemStatusCode.ReadOnly)
{
fsResult = await PerformAdminOperation(new ValueSet()
{
{ "Arguments", "FileOperation" },
{ "fileop", "MoveItem" },
{ "filepath", source.Path },
{ "destpath", destination },
{ "overwrite", collision == NameCollisionOption.ReplaceExisting }
});
}
}
errorCode?.Report(fsResult.ErrorCode);
}
Expand Down Expand Up @@ -516,28 +560,13 @@ public async Task<IStorageHistory> DeleteAsync(IStorageItemWithPath source,
}
if (!fsResult)
{
var elevateConfirmDialog = new Files.Dialogs.ElevateConfirmDialog();
var elevateConfirmResult = await elevateConfirmDialog.ShowAsync();
if (elevateConfirmResult == ContentDialogResult.Primary)
fsResult = await PerformAdminOperation(new ValueSet()
{
if (associatedInstance.ServiceConnection != null
&& await associatedInstance.ServiceConnection.Elevate())
{
// Try again with fulltrust process (admin)
if (associatedInstance.ServiceConnection != null)
{
var (status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(new ValueSet()
{
{ "Arguments", "FileOperation" },
{ "fileop", "DeleteItem" },
{ "filepath", source.Path },
{ "permanently", permanently }
});
fsResult = (FilesystemResult)(status == AppServiceResponseStatus.Success
&& response.Get("Success", false));
}
}
}
{ "Arguments", "FileOperation" },
{ "fileop", "DeleteItem" },
{ "filepath", source.Path },
{ "permanently", permanently }
});
}
}
else if (fsResult == FileSystemStatusCode.InUse)
Expand Down Expand Up @@ -649,7 +678,19 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source,
}
else
{
Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
var fsResult = await PerformAdminOperation(new ValueSet()
{
{ "Arguments", "FileOperation" },
{ "fileop", "RenameItem" },
{ "filepath", source.Path },
{ "newName", newName },
{ "overwrite", collision == NameCollisionOption.ReplaceExisting }
});
if (fsResult)
{
errorCode?.Report(FileSystemStatusCode.Success);
return new StorageHistory(FileOperationType.Rename, source, StorageItemHelpers.FromPathAndType(destination, source.ItemType));
}
}
}
else if (renamed == FileSystemStatusCode.NotAFile || renamed == FileSystemStatusCode.NotAFolder)
Expand Down Expand Up @@ -711,50 +752,51 @@ public async Task<IStorageHistory> RestoreFromTrashAsync(IStorageItemWithPath so
FilesystemResult fsResult = FileSystemStatusCode.InProgress;
errorCode?.Report(fsResult);

if (source.ItemType == FilesystemItemType.Directory)
fsResult = (FilesystemResult)await Task.Run(() => NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination));

if (!fsResult)
{
FilesystemResult<StorageFolder> sourceFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(source.Path);
FilesystemResult<StorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
if (source.ItemType == FilesystemItemType.Directory)
{
FilesystemResult<StorageFolder> sourceFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(source.Path);
FilesystemResult<StorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));

fsResult = sourceFolder.ErrorCode | destinationFolder.ErrorCode;
errorCode?.Report(fsResult);
fsResult = sourceFolder.ErrorCode | destinationFolder.ErrorCode;
errorCode?.Report(fsResult);

if (fsResult)
{
fsResult = await FilesystemTasks.Wrap(() =>
if (fsResult)
{
return MoveDirectoryAsync(sourceFolder.Result,
destinationFolder.Result,
Path.GetFileName(destination),
CreationCollisionOption.FailIfExists,
true);
}); // TODO: we could use here FilesystemHelpers with registerHistory false?
fsResult = await FilesystemTasks.Wrap(() => MoveDirectoryAsync(sourceFolder.Result, destinationFolder.Result, Path.GetFileName(destination),
CreationCollisionOption.FailIfExists, true));
// TODO: we could use here FilesystemHelpers with registerHistory false?
}
errorCode?.Report(fsResult);
}
errorCode?.Report(fsResult);
}
else
{
FilesystemResult<StorageFile> sourceFile = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(source.Path);
FilesystemResult<StorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));
else
{
FilesystemResult<StorageFile> sourceFile = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(source.Path);
FilesystemResult<StorageFolder> destinationFolder = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(Path.GetDirectoryName(destination));

fsResult = sourceFile.ErrorCode | destinationFolder.ErrorCode;
errorCode?.Report(fsResult);
fsResult = sourceFile.ErrorCode | destinationFolder.ErrorCode;
errorCode?.Report(fsResult);

if (fsResult)
{
fsResult = await FilesystemTasks.Wrap(() =>
if (fsResult)
{
return sourceFile.Result.MoveAsync(destinationFolder.Result,
Path.GetFileName(destination),
NameCollisionOption.GenerateUniqueName).AsTask();
});
fsResult = await FilesystemTasks.Wrap(() => sourceFile.Result.MoveAsync(destinationFolder.Result, Path.GetFileName(destination), NameCollisionOption.GenerateUniqueName).AsTask());
}
errorCode?.Report(fsResult);
}
else if (fsResult == FileSystemStatusCode.Unauthorized)
if (fsResult == FileSystemStatusCode.Unauthorized || fsResult == FileSystemStatusCode.ReadOnly)
{
// Try again with MoveFileFromApp
fsResult = (FilesystemResult)NativeFileOperationsHelper.MoveFileFromApp(source.Path, destination);
fsResult = await PerformAdminOperation(new ValueSet()
{
{ "Arguments", "FileOperation" },
{ "fileop", "MoveItem" },
{ "filepath", source.Path },
{ "destpath", destination },
{ "overwrite", false }
});
}
errorCode?.Report(fsResult);
}

if (fsResult)
Expand Down Expand Up @@ -832,6 +874,27 @@ private static async Task<StorageFolder> MoveDirectoryAsync(IStorageFolder sourc
return createdRoot;
}

private async Task<FilesystemResult> PerformAdminOperation(ValueSet operation)
{
var elevateConfirmDialog = new Files.Dialogs.ElevateConfirmDialog();
var elevateConfirmResult = await elevateConfirmDialog.ShowAsync();
if (elevateConfirmResult == ContentDialogResult.Primary)
{
if (associatedInstance.ServiceConnection != null &&
await associatedInstance.ServiceConnection.Elevate())
{
// Try again with fulltrust process (admin)
if (associatedInstance.ServiceConnection != null)
{
var (status, response) = await associatedInstance.ServiceConnection.SendMessageForResponseAsync(operation);
return (FilesystemResult)(status == AppServiceResponseStatus.Success
&& response.Get("Success", false));
}
}
}
return (FilesystemResult)false;
}

#endregion Helpers

#region IDisposable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public async Task<ReturnResult> PerformOperationTypeAsync(DataPackageOperation o
var items = await packageView.GetStorageItemsAsync();
NavigationHelpers.OpenItemsWithExecutable(associatedInstance, items.ToList(), destination);
}

// TODO: Support link creation
return default;
}
Expand Down Expand Up @@ -861,13 +861,10 @@ public async Task<ReturnResult> RenameAsync(IStorageItemWithPath source, string
}

/* Only prompt user when extension has changed,
not when file name has changed
*/
not when file name has changed */
if (Path.GetExtension(source.Path) != Path.GetExtension(newName))
{
var renameDialogText = "RenameFileDialog/Text".GetLocalized();

var yesSelected = await DialogDisplayHelper.ShowDialogAsync("Rename", renameDialogText, "Yes", "No");
var yesSelected = await DialogDisplayHelper.ShowDialogAsync("RenameFileDialogTitle".GetLocalized(), "RenameFileDialog/Text".GetLocalized(), "ButtonYes/Content".GetLocalized(), "ButtonNo/Content".GetLocalized());
if (yesSelected)
{
history = await filesystemOperations.RenameAsync(source, newName, collision, errorCode, cancellationToken);
Expand Down
Loading

0 comments on commit a3ee462

Please sign in to comment.