[PTRun][Program]Fix bug when renaming url shortcut (#17184)

* [PTRun][Program]Fix bug when renaming url shortcut

* Try to use the old full path for old app removal

* Guard against links to nowhere

* Fix test to have link point to existing location

* Update src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/Win32Program.cs

Co-authored-by: Heiko <61519853+htcfreek@users.noreply.github.com>

* Fix nit.

Co-authored-by: Heiko <61519853+htcfreek@users.noreply.github.com>
This commit is contained in:
Jaime Bernardo 2022-03-25 10:08:10 +00:00 committed by GitHub
parent 559a0442d8
commit 80e9fc0c43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 11 deletions

View File

@ -352,10 +352,11 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
string oldFullPath = directory + "\\" + oldpath;
string fullPath = directory + "\\" + path;
string linkingTo = Directory.GetCurrentDirectory();
// ShellLinkHelper must be mocked for lnk applications
var mockShellLink = new Mock<IShellLinkHelper>();
mockShellLink.Setup(m => m.RetrieveTargetPath(It.IsAny<string>())).Returns(string.Empty);
mockShellLink.Setup(m => m.RetrieveTargetPath(It.IsAny<string>())).Returns(linkingTo);
Win32Program.ShellLinkHelper = mockShellLink.Object;
// old item and new item are the actual items when they are in existence
@ -363,14 +364,14 @@ namespace Microsoft.Plugin.Program.UnitTests.Storage
{
Name = "oldpath",
ExecutableName = oldpath,
FullPath = fullPath,
FullPath = linkingTo,
};
Win32Program newitem = new Win32Program
{
Name = "path",
ExecutableName = path,
FullPath = fullPath,
FullPath = linkingTo,
};
win32ProgramRepository.Add(olditem);

View File

@ -491,9 +491,19 @@ namespace Microsoft.Plugin.Program.Programs
}
}
}
else
{
// If the link points nowhere, consider it invalid.
return InvalidProgram;
}
return program;
}
catch (System.IO.FileLoadException e)
{
ProgramLogger.Warn($"Couldn't load the link file at {path}. This might be caused by a new link being created and locked by the OS.", e, MethodBase.GetCurrentMethod().DeclaringType, path);
return InvalidProgram;
}
// Only do a catch all in production. This is so make developer aware of any unhandled exception and add the exception handling in.
// Error caused likely due to trying to get the description of the program

View File

@ -99,7 +99,7 @@ namespace Microsoft.Plugin.Program.Storage
string newPath = e.FullPath;
string extension = Path.GetExtension(newPath);
Win32Program.ApplicationType appType = Win32Program.GetAppTypeFromPath(newPath);
Win32Program.ApplicationType oldAppType = Win32Program.GetAppTypeFromPath(oldPath);
Programs.Win32Program newApp = Win32Program.GetAppFromPath(newPath);
Programs.Win32Program oldApp = null;
@ -109,13 +109,9 @@ namespace Microsoft.Plugin.Program.Storage
// This situation is not encountered for other application types because the fullPath is the path itself, instead of being computed by using the path to the app.
try
{
if (appType == Win32Program.ApplicationType.ShortcutApplication)
if (oldAppType == Win32Program.ApplicationType.ShortcutApplication || oldAppType == Win32Program.ApplicationType.InternetShortcutApplication)
{
oldApp = new Win32Program() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp.FullPath };
}
else if (appType == Win32Program.ApplicationType.InternetShortcutApplication)
{
oldApp = new Win32Program() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp.FullPath };
oldApp = new Win32Program() { Name = Path.GetFileNameWithoutExtension(e.OldName), ExecutableName = Path.GetFileName(e.OldName), FullPath = newApp?.FullPath ?? oldPath };
}
else
{
@ -132,7 +128,7 @@ namespace Microsoft.Plugin.Program.Storage
{
if (string.IsNullOrWhiteSpace(oldApp.Name) || string.IsNullOrWhiteSpace(oldApp.ExecutableName) || string.IsNullOrWhiteSpace(oldApp.FullPath))
{
Log.Error($"Old app was not initialized properly. OldFullPath: {e.OldFullPath}; OldName: {e.OldName}; FullPath: {e.FullPath}", GetType());
Log.Warn($"Old app data was not initialized properly for removal after file renaming. This likely means it was not a valid app to begin with and removal is not needed. OldFullPath: {e.OldFullPath}; OldName: {e.OldName}; FullPath: {e.FullPath}", GetType());
}
else
{
@ -160,6 +156,11 @@ namespace Microsoft.Plugin.Program.Storage
if (extension.Equals(LnkExtension, StringComparison.OrdinalIgnoreCase))
{
app = GetAppWithSameLnkResolvedPath(path);
if (app == null)
{
// Cancelled links won't have a resolved path.
app = GetAppWithSameNameAndExecutable(Path.GetFileNameWithoutExtension(path), Path.GetFileName(path));
}
}
else if (extension.Equals(UrlExtension, StringComparison.OrdinalIgnoreCase))
{