Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-1047) Harden Xml serialization and deserialization #1084

Conversation

RichiCoder1
Copy link
Contributor

@RichiCoder1 RichiCoder1 commented Dec 10, 2016

Rewrites the XML serialization service in a few specific ways:

  • Should update hash checks now builds the update file in memory rather than committing it the disk, and checks the hash in memory.
  • The ".update" file now using the slightly hardier File Replace to replace the undated file, and also generates a ".backup".
  • When deserializing, we now check for a ".backup" if serialization fails and attempt that too.
  • Generally use streams and dispose where possible.

Closes #1047

@RichiCoder1 RichiCoder1 force-pushed the feature/harden_config_serialization branch from c0badd5 to 922af62 Compare December 10, 2016 04:42
@RichiCoder1
Copy link
Contributor Author

I'm thinking I may add some unit tests around this too, but I have nothing hard group just yet

public byte[] ComputeHash(Stream stream)
{
return _algorithm.ComputeHash(stream);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -1,4 +1,4 @@
// Copyright � 2011 - Present RealDimensions Software, LLC
// Copyright � 2011 - Present RealDimensions Software, LLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you dropped this from UTF-8. Very much dislike when Visual Studio does this. 👎

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Darn't. Did even notice that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix both occurences

Copy link
Member

@ferventcoder ferventcoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great so far. I am concerned that the files did change from UTF8 (no BOM) to ANSI. If you could fix those files that were changed by VS and squash that back into this, that would be good.

@@ -1,4 +1,4 @@
// Copyright � 2011 - Present RealDimensions Software, LLC
// Copyright � 2011 - Present RealDimensions Software, LLC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here :/

}
throw;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is the meat - just compares the stream versus the file before it creates the old file? This would be great in low access environments so there is no error message when it can't create the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was exactly the idea! No more Failed to write chocolatey.config.update permission errors :D

@ferventcoder
Copy link
Member

ferventcoder commented Dec 16, 2016

Minor nitpicks to your git commit:

  • You don't need the colon in the commit summary (GH-1047): Harden Xml serialization and deserialization
  • misspelled deserializing in third bullet point - When desieralizing,

The other change I would request is to keep the same (GH-1047) to prepend, but move the changes to HashAlgorithm/HashProvider and FileSystem to their own commits. Those changes stand on their own. So I'd hope for three commits here instead of one.

memoryStream.CopyTo(updateFileStream);
}
_fileSystem.replace_file(tempUpdateFile, xmlFilePath, xmlFilePath + ".backup");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notes for me. We should probably delete the backup file when things are successful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly keep it because it allows us to recover if a future operation fails for some reason.

textWriter.Flush();
// Write the updated file to memory
using(var memoryStream = new MemoryStream())
using(var textWriter = new StreamWriter(memoryStream, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit - textWriter is probably now streamWriter

Copy link
Member

@ferventcoder ferventcoder Jan 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also instead of using, I actually prefer to be explicit when it comes to doing file writes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean with that second comment? Neither stream is personally involved in writing the file to disk.

@RichiCoder1 RichiCoder1 force-pushed the feature/harden_config_serialization branch from 63cdb82 to ab4af16 Compare February 8, 2017 19:33
@RichiCoder1 RichiCoder1 changed the title (GH-1047): Harden Xml serialization and deserialization (GH-1047) Harden Xml serialization and deserialization Feb 8, 2017
@RichiCoder1 RichiCoder1 force-pushed the feature/harden_config_serialization branch from ab4af16 to ce4181f Compare February 8, 2017 19:48
@RichiCoder1
Copy link
Contributor Author

Aside from your using request, which I couldn't parse, this should be good to go. Sorry for sitting on this for so long.

@ferventcoder
Copy link
Member

Merged into stable at 5134b46. Thanks for the contributions!! This is going to add quite a bit in what it provides!

@RichiCoder1 RichiCoder1 deleted the feature/harden_config_serialization branch March 23, 2017 15:29
@RichiCoder1 RichiCoder1 restored the feature/harden_config_serialization branch March 23, 2017 15:29
@RichiCoder1 RichiCoder1 deleted the feature/harden_config_serialization branch March 23, 2017 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants