Skip to content

Commit

Permalink
Show error on package parse (#5097)
Browse files Browse the repository at this point in the history
* If referenced file cannot be read, show error

Previously an exception would be thrown

* Resolve warnings
  • Loading branch information
bdukes authored Apr 26, 2022
1 parent fb8e626 commit c2615a7
Showing 1 changed file with 67 additions and 53 deletions.
120 changes: 67 additions & 53 deletions DNN Platform/Library/Services/Installer/Installers/PackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ namespace DotNetNuke.Services.Installer.Installers
using DotNetNuke.Services.Installer.Dependencies;
using DotNetNuke.Services.Installer.Packages;

using SchwabenCode.QuickIO;

/// -----------------------------------------------------------------------------
/// <summary>
/// The PackageInstaller class is an Installer for Packages.
/// </summary>
/// -----------------------------------------------------------------------------
public class PackageInstaller : ComponentInstallerBase
{
private readonly SortedList<int, ComponentInstallerBase> _componentInstallers = new SortedList<int, ComponentInstallerBase>();
private PackageInfo _installedPackage;
private EventMessage _eventMessage;
private readonly SortedList<int, ComponentInstallerBase> componentInstallers = new SortedList<int, ComponentInstallerBase>();
private PackageInfo installedPackage;
private EventMessage eventMessage;

/// -----------------------------------------------------------------------------
/// <summary>
Expand Down Expand Up @@ -54,7 +56,7 @@ public PackageInstaller(PackageInfo package)

// Set type
installer.Type = package.PackageType;
this._componentInstallers.Add(0, installer);
this.componentInstallers.Add(0, installer);
}
}
}
Expand All @@ -71,8 +73,7 @@ public PackageInstaller(string packageManifest, InstallerInfo info)
{
this.IsValid = true;
this.DeleteFiles = Null.NullBoolean;
this.Package = new PackageInfo(info);
this.Package.Manifest = packageManifest;
this.Package = new PackageInfo(info) { Manifest = packageManifest, };

if (!string.IsNullOrEmpty(packageManifest))
{
Expand Down Expand Up @@ -107,20 +108,20 @@ public PackageInstaller(string packageManifest, InstallerInfo info)
/// -----------------------------------------------------------------------------
public override void Commit()
{
for (int index = 0; index <= this._componentInstallers.Count - 1; index++)
for (int index = 0; index <= this.componentInstallers.Count - 1; index++)
{
ComponentInstallerBase compInstaller = this._componentInstallers.Values[index];
ComponentInstallerBase compInstaller = this.componentInstallers.Values[index];
if (compInstaller.Version >= this.Package.InstalledVersion && compInstaller.Completed)
{
compInstaller.Commit();
}
}

// Add Event Message
if (this._eventMessage != null && !string.IsNullOrEmpty(this._eventMessage.Attributes["UpgradeVersionsList"]))
if (this.eventMessage != null && !string.IsNullOrEmpty(this.eventMessage.Attributes["UpgradeVersionsList"]))
{
this._eventMessage.Attributes.Set("desktopModuleID", Null.NullInteger.ToString());
EventQueueController.SendMessage(this._eventMessage, "Application_Start");
this.eventMessage.Attributes.Set("desktopModuleID", Null.NullInteger.ToString());
EventQueueController.SendMessage(this.eventMessage, "Application_Start");
}

if (this.Log.Valid)
Expand All @@ -146,19 +147,19 @@ public override void Install()
try
{
// Save the Package Information
if (this._installedPackage != null)
if (this.installedPackage != null)
{
this.Package.PackageID = this._installedPackage.PackageID;
this.Package.PackageID = this.installedPackage.PackageID;
}

// Save Package
PackageController.Instance.SaveExtensionPackage(this.Package);

// Iterate through all the Components
for (int index = 0; index <= this._componentInstallers.Count - 1; index++)
for (int index = 0; index <= this.componentInstallers.Count - 1; index++)
{
ComponentInstallerBase compInstaller = this._componentInstallers.Values[index];
if ((this._installedPackage == null) || (compInstaller.Version > this.Package.InstalledVersion) || this.Package.InstallerInfo.RepairInstall)
ComponentInstallerBase compInstaller = this.componentInstallers.Values[index];
if ((this.installedPackage == null) || (compInstaller.Version > this.Package.InstalledVersion) || this.Package.InstallerInfo.RepairInstall)
{
this.Log.AddInfo(Util.INSTALL_Start + " - " + compInstaller.Type);
compInstaller.Install();
Expand Down Expand Up @@ -248,20 +249,20 @@ public override void ReadManifest(XPathNavigator manifestNav)

if (packageType.SupportsSideBySideInstallation)
{
this._installedPackage = PackageController.Instance.GetExtensionPackage(this.Package.PortalID, p => p.Name.Equals(this.Package.Name, StringComparison.OrdinalIgnoreCase)
this.installedPackage = PackageController.Instance.GetExtensionPackage(this.Package.PortalID, p => p.Name.Equals(this.Package.Name, StringComparison.OrdinalIgnoreCase)
&& p.PackageType.Equals(this.Package.PackageType, StringComparison.OrdinalIgnoreCase)
&& p.Version == this.Package.Version);
}
else
{
this._installedPackage = PackageController.Instance.GetExtensionPackage(this.Package.PortalID, p => p.Name.Equals(this.Package.Name, StringComparison.OrdinalIgnoreCase)
this.installedPackage = PackageController.Instance.GetExtensionPackage(this.Package.PortalID, p => p.Name.Equals(this.Package.Name, StringComparison.OrdinalIgnoreCase)
&& p.PackageType.Equals(this.Package.PackageType, StringComparison.OrdinalIgnoreCase));
}

if (this._installedPackage != null)
if (this.installedPackage != null)
{
this.Package.InstalledVersion = this._installedPackage.Version;
this.Package.InstallerInfo.PackageID = this._installedPackage.PackageID;
this.Package.InstalledVersion = this.installedPackage.Version;
this.Package.InstallerInfo.PackageID = this.installedPackage.PackageID;

if (this.Package.InstalledVersion > this.Package.Version)
{
Expand All @@ -271,20 +272,20 @@ public override void ReadManifest(XPathNavigator manifestNav)
else if (this.Package.InstalledVersion == this.Package.Version)
{
this.Package.InstallerInfo.Installed = true;
this.Package.InstallerInfo.PortalID = this._installedPackage.PortalID;
this.Package.InstallerInfo.PortalID = this.installedPackage.PortalID;
}
}

this.Log.AddInfo(Util.DNN_ReadingPackage + " - " + this.Package.PackageType + " - " + this.Package.Name);
this.Package.FriendlyName = Util.ReadElement(manifestNav, "friendlyName", this.Package.Name);
this.Package.Description = Util.ReadElement(manifestNav, "description");

XPathNavigator foldernameNav = null;
XPathNavigator folderNameNav = null;
this.Package.FolderName = string.Empty;
switch (this.Package.PackageType)
{
case "Module":
// In Dynamics moduels, a component:type=File can have a basePath pointing to the App_Conde folder. This is not a correct FolderName
// In Dynamics modules, a component:type=File can have a basePath pointing to the App_Conde folder. This is not a correct FolderName
// To ensure that FolderName is DesktopModules...
var folderNameValue = PackageController.GetSpecificFolderName(manifestNav, "components/component/files|components/component/resourceFiles", "basePath", "DesktopModules");
if (!string.IsNullOrEmpty(folderNameValue))
Expand All @@ -294,26 +295,26 @@ public override void ReadManifest(XPathNavigator manifestNav)

break;
case "Auth_System":
foldernameNav = manifestNav.SelectSingleNode("components/component/files");
if (foldernameNav != null)
folderNameNav = manifestNav.SelectSingleNode("components/component/files");
if (folderNameNav != null)
{
this.Package.FolderName = Util.ReadElement(foldernameNav, "basePath").Replace('\\', '/');
this.Package.FolderName = Util.ReadElement(folderNameNav, "basePath").Replace('\\', '/');
}

break;
case "Container":
foldernameNav = manifestNav.SelectSingleNode("components/component/containerFiles");
if (foldernameNav != null)
folderNameNav = manifestNav.SelectSingleNode("components/component/containerFiles");
if (folderNameNav != null)
{
this.Package.FolderName = Globals.glbContainersPath + Util.ReadElement(foldernameNav, "containerName").Replace('\\', '/');
this.Package.FolderName = Globals.glbContainersPath + Util.ReadElement(folderNameNav, "containerName").Replace('\\', '/');
}

break;
case "Skin":
foldernameNav = manifestNav.SelectSingleNode("components/component/skinFiles");
if (foldernameNav != null)
folderNameNav = manifestNav.SelectSingleNode("components/component/skinFiles");
if (folderNameNav != null)
{
this.Package.FolderName = Globals.glbSkinsPath + Util.ReadElement(foldernameNav, "skinName").Replace('\\', '/');
this.Package.FolderName = Globals.glbSkinsPath + Util.ReadElement(folderNameNav, "skinName").Replace('\\', '/');
}

break;
Expand All @@ -328,7 +329,7 @@ public override void ReadManifest(XPathNavigator manifestNav)
break;
}

this._eventMessage = this.ReadEventMessageNode(manifestNav);
this.eventMessage = this.ReadEventMessageNode(manifestNav);

// Get Icon
XPathNavigator iconFileNav = manifestNav.SelectSingleNode("iconFile");
Expand Down Expand Up @@ -375,6 +376,11 @@ public override void ReadManifest(XPathNavigator manifestNav)
else
{
this.Package.License = this.ReadTextFromFile(licenseSrc);
if (this.Package.License == null)
{
// failure reading file
return;
}
}
}

Expand All @@ -397,6 +403,11 @@ public override void ReadManifest(XPathNavigator manifestNav)
else
{
this.Package.ReleaseNotes = this.ReadTextFromFile(relNotesSrc);
if (this.Package.ReleaseNotes == null)
{
// failure reading file
return;
}
}
}

Expand All @@ -411,11 +422,9 @@ public override void ReadManifest(XPathNavigator manifestNav)
foreach (XPathNavigator dependencyNav in manifestNav.CreateNavigator().Select("dependencies/dependency"))
{
var dependency = DependencyFactory.GetDependency(dependencyNav);
var packageDependecy = dependency as IManagedPackageDependency;

if (packageDependecy != null)
if (dependency is IManagedPackageDependency packageDependency)
{
packageDependencies.Add(packageDependecy.PackageDependency);
packageDependencies.Add(packageDependency.PackageDependency);
}

if (!dependency.IsValid)
Expand All @@ -436,9 +445,9 @@ public override void ReadManifest(XPathNavigator manifestNav)
/// -----------------------------------------------------------------------------
public override void Rollback()
{
for (int index = 0; index <= this._componentInstallers.Count - 1; index++)
for (int index = 0; index <= this.componentInstallers.Count - 1; index++)
{
ComponentInstallerBase compInstaller = this._componentInstallers.Values[index];
ComponentInstallerBase compInstaller = this.componentInstallers.Values[index];
if (compInstaller.Version > this.Package.InstalledVersion && compInstaller.Completed)
{
this.Log.AddInfo(Util.COMPONENT_RollingBack + " - " + compInstaller.Type);
Expand All @@ -448,15 +457,15 @@ public override void Rollback()
}

// If Previously Installed Package exists then we need to update the DataStore with this
if (this._installedPackage == null)
if (this.installedPackage == null)
{
// No Previously Installed Package - Delete newly added Package
PackageController.Instance.DeleteExtensionPackage(this.Package);
}
else
{
// Previously Installed Package - Rollback to Previously Installed
PackageController.Instance.SaveExtensionPackage(this._installedPackage);
PackageController.Instance.SaveExtensionPackage(this.installedPackage);
}
}

Expand All @@ -468,11 +477,10 @@ public override void Rollback()
public override void UnInstall()
{
// Iterate through all the Components
for (int index = 0; index <= this._componentInstallers.Count - 1; index++)
for (int index = 0; index <= this.componentInstallers.Count - 1; index++)
{
ComponentInstallerBase compInstaller = this._componentInstallers.Values[index];
var fileInstaller = compInstaller as FileInstaller;
if (fileInstaller != null)
ComponentInstallerBase compInstaller = this.componentInstallers.Values[index];
if (compInstaller is FileInstaller fileInstaller)
{
fileInstaller.DeleteFiles = this.DeleteFiles;
}
Expand Down Expand Up @@ -531,7 +539,7 @@ private void ReadComponents(XPathNavigator manifestNav)
foreach (XPathNavigator componentNav in manifestNav.CreateNavigator().Select("components/component"))
{
// Set default order to next value (ie the same as the size of the collection)
int order = this._componentInstallers.Count;
int order = this.componentInstallers.Count;

string type = componentNav.GetAttribute("type", string.Empty);
if (this.InstallMode == InstallMode.Install)
Expand Down Expand Up @@ -563,22 +571,28 @@ private void ReadComponents(XPathNavigator manifestNav)
}
else
{
this._componentInstallers.Add(order, installer);
this.componentInstallers.Add(order, installer);
this.Package.InstallerInfo.AllowableFiles += ", " + installer.AllowableFiles;
}
}
}

private string ReadTextFromFile(string source)
{
string strText = Null.NullString;
if (this.Package.InstallerInfo.InstallMode != InstallMode.ManifestOnly)
if (this.Package.InstallerInfo.InstallMode == InstallMode.ManifestOnly)
{
// Load from file
strText = FileSystemUtils.ReadFile(this.Package.InstallerInfo.TempInstallFolder + "\\" + source);
return Null.NullString;
}

return strText;
try
{
return FileSystemUtils.ReadFile(this.Package.InstallerInfo.TempInstallFolder + "\\" + source);
}
catch (PathNotFoundException)
{
this.Log.AddFailure($"Unable to read {source}, file not found");
return null;
}
}
}
}

0 comments on commit c2615a7

Please sign in to comment.