Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (maint) formatting
  (GH-1581) Log error that caused .registry.bad file
  (maint)(log) update message about number of tries
  (GH-1581) try deserialization at least twice
  (GH-1581) Fix possible null exception
  • Loading branch information
ferventcoder committed Mar 13, 2019
2 parents 4db2e04 + e30e9b9 commit 23da326
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2018 Chocolatey Software, Inc
// Copyright © 2017 - 2019 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,6 +16,7 @@

namespace chocolatey.infrastructure.app.domain
{
using System.Security;
using Microsoft.Win32;

public static class RegistryValueExtensions
Expand All @@ -26,9 +27,11 @@ public static string get_value_as_string(this RegistryKey key, string name)

// Since it is possible that registry keys contain characters that are not valid
// in XML files, ensure that all content is escaped, prior to serialization
var escapedXml = System.Security.SecurityElement.Escape(key.GetValue(name).to_string()).Replace(""","\"").Replace("'","'");

return escapedXml == null ? string.Empty : escapedXml.Replace("\0", string.Empty);
// https://docs.microsoft.com/en-us/dotnet/api/system.security.securityelement.escape?view=netframework-4.0
return SecurityElement.Escape(key.GetValue(name).to_string()).to_string()
.Replace(""", "\"")
.Replace("'", "'")
.Replace("\0", string.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ focus exclusively in the string values not surrounded by CData. Once
Instead, you can use the following PowerShell command:
Move-Item .\.registry.bad .\.registry
".format_with(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE));

try
{
if (_fileSystem.file_exists(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE)))
Expand All @@ -98,8 +99,14 @@ focus exclusively in the string values not surrounded by CData. Once
packageInformation.RegistrySnapshot = _registryService.read_from_file(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE));
}
}
catch (Exception)
catch (Exception e)
{
if (_config.RegularOutput) this.Log().Warn(@"A .registry file at '{0}'
has errored attempting to read it. This file will be renamed to
'{1}' The error:
{2}
".format_with(_fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_FILE), _fileSystem.combine_paths(pkgStorePath, REGISTRY_SNAPSHOT_BAD_FILE), e.ToString()));

FaultTolerance.try_catch_with_logging_exception(
() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public Registry read_from_file(string filePath)
return null;
}

return _xmlService.deserialize<Registry>(filePath, 1);
return _xmlService.deserialize<Registry>(filePath, 2);
}

private void get_values(RegistryKey key, string subKeyName, IList<GenericRegistryValue> values, bool expandValues)
Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey/infrastructure/tolerance/FaultTolerance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void retry(int numberOfTries, Action action, int waitDurationMilli
public static T retry<T>(int numberOfTries, Func<T> function, int waitDurationMilliseconds = 100, int increaseRetryByMilliseconds = 0, bool isSilent = false)
{
if (function == null) return default(T);
if (numberOfTries == 0) throw new ApplicationException("You must specify a number of retries greater than zero.");
if (numberOfTries == 0) throw new ApplicationException("You must specify a number of tries greater than zero.");
var returnValue = default(T);

var debugging = log_is_in_debug_mode();
Expand Down

0 comments on commit 23da326

Please sign in to comment.