Skip to content

Commit

Permalink
Added option to only have Corruptor corrupt body rather than header too
Browse files Browse the repository at this point in the history
  • Loading branch information
improvedk committed Mar 23, 2014
1 parent 29f9995 commit 63b4064
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/OrcaMDF.Framework/Corruptor.cs
Expand Up @@ -13,7 +13,7 @@ public static class Corruptor
/// <param name="path">The path of the file to corrupt</param>
/// <param name="corruptionPercentage">To percentage of the pages to corrupt. 0.1 = 10%</param>
/// <returns>A list of the page IDs that were corrupted</returns>
public static IEnumerable<int> CorruptFileUsingGarbage(string path, double corruptionPercentage)
public static IEnumerable<int> CorruptFileUsingGarbage(string path, double corruptionPercentage, bool onlyBody)
{
var rnd = new Random();

Expand All @@ -25,9 +25,6 @@ public static IEnumerable<int> CorruptFileUsingGarbage(string path, double corru

using (var file = File.OpenWrite(path))
{
byte[] garbage = new byte[8192];
rnd.NextBytes(garbage);

int pageCount = (int)(file.Length / 8192);
int pageCountToCorrupt = (int)(pageCount * corruptionPercentage);

Expand All @@ -39,8 +36,19 @@ public static IEnumerable<int> CorruptFileUsingGarbage(string path, double corru

foreach (int pageID in pageIDsToCorrupt)
{
file.Position = pageID * 8192;
file.Write(garbage, 0, 8192);
byte[] garbage = new byte[8192];
rnd.NextBytes(garbage);

if (onlyBody)
{
file.Position = pageID * 8192 + 96;
file.Write(garbage, 0, 8060);
}
else
{
file.Position = pageID * 8192;
file.Write(garbage, 0, 8192);
}
}

return pageIDsToCorrupt;
Expand Down

0 comments on commit 63b4064

Please sign in to comment.