Skip to content

Commit

Permalink
(chocolateyGH-1047) Account for cases where there's no original file …
Browse files Browse the repository at this point in the history
…on disk.
  • Loading branch information
RichiCoder1 committed Feb 8, 2017
1 parent 9d6a366 commit ab4af16
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/chocolatey/infrastructure/services/XmlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,32 @@ public void serialize<XmlType>(XmlType xmlType, string xmlFilePath, bool isSilen
() =>
{
var xmlSerializer = new XmlSerializer(typeof(XmlType));

// Write the updated file to memory
using(var memoryStream = new MemoryStream())
using(var textWriter = new StreamWriter(memoryStream, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)))
{
xmlSerializer.Serialize(textWriter, xmlType);
textWriter.Flush();

memoryStream.Position = 0;
if (!_hashProvider.hash_file(xmlFilePath).is_equal_to(_hashProvider.hash_stream(memoryStream)))

// Grab the hash of both files and compare them.
var originalFileHash = _hashProvider.hash_file(xmlFilePath);
if (!originalFileHash.is_equal_to(_hashProvider.hash_stream(memoryStream)))
{
// If there wasn't a file there in the first place, just write the new one out directly.
if(string.IsNullOrEmpty(originalFileHash))
{
using(var updateFileStream = _fileSystem.create_file(xmlFilePath))
{
memoryStream.Position = 0;
memoryStream.CopyTo(updateFileStream);
return;
}
}

// Otherwise, create an update file, and resiliently move it into place.
var tempUpdateFile = xmlFilePath + ".update";
using(var updateFileStream = _fileSystem.create_file(tempUpdateFile))
{
Expand Down

0 comments on commit ab4af16

Please sign in to comment.