public static void FSHelperTest() { string dir = @"C:\Intel"; string outdir = @"C:\users\yabahman\desktop\"; Directory.SetCurrentDirectory(dir); //Setup const string longData = "asdjfahsdkjfhaksdjfhakjsdfhakjsdfhaskjdfhaskjdfhaksdjfhaskjdfhaskjdfhaskdjfhaskjdfhaskjdfhaskjdfhaskjdfhaskdjfhaskjfdh"; const string shortData = "a"; FileHelper.Touch("Shrunk.txt", longData); FileHelper.Touch("Grew.txt", shortData); FileHelper.Touch("Removed.txt"); File.Delete("Added.txt"); //Take snapshot 1 FSHelper fs1 = new FSHelper(dir); fs1.WriteToFile(outdir + @"outfile1.txt"); //Modify things FileHelper.Touch("Shrunk.txt", shortData); FileHelper.Touch("Grew.txt", longData); File.Delete("Removed.txt"); FileHelper.Touch("Added.txt"); //Take snapshot 2 FSHelper fs2 = new FSHelper(dir); fs2.WriteToFile(outdir + @"outfile2.txt"); //Compare data fs2.Diff(fs1); }
public static void FSHelperTest2() { //string dir = @"C:\Users\"; string dir = @"C:\"; string outdir = @"C:\users\yabahman\desktop\"; Directory.SetCurrentDirectory(dir); //Take snapshot 1 FSHelper fs1 = new FSHelper(dir); fs1.WriteToFile(outdir + @"outfile1.txt"); //Take snapshot 2 FSHelper fs2 = new FSHelper(dir); fs2.WriteToFile(outdir + @"outfile2.txt"); //Compare data fs2.Diff(fs1); }
public void Diff(FSHelper that) { int pad = 8; char c = ' '; Console.WriteLine(); //Get Intersection... var intersection = this.FileList.Intersect(that.FileList); double percent = (double)intersection.Count()/ (double)FileList.Count; Console.WriteLine("Files intersect:{0} ({1})", intersection.Count().ToString().PadLeft(pad, c), percent.ToString("P")); //Find files removed... (aka files that aren't in the intersection, but are in THAT) var removed = that.FileList.Except(intersection); foreach (var r in removed) { r.FileKind = FileEntry.Kind.Removal; r.Delta = -r.Size; } percent = (double)removed.Count() / (double)that.FileList.Count; Console.WriteLine("Files removed:\t{0} ({1})", removed.Count().ToString().PadLeft(pad, c), percent.ToString("P")); //Find files added... var added = this.FileList.Except(intersection); foreach (var a in added) { a.FileKind = FileEntry.Kind.Addition; a.Delta = a.Size; } percent = (double)added.Count() / (double)this.FileList.Count; Console.WriteLine("Files added:\t{0} ({1})", added.Count().ToString().PadLeft(pad, c), percent.ToString("P")); //Get list of files that grew. (Note: this time, we're using a comparer which compares the size as well as the filepath!) var grew = intersection.Except(that.FileList, new FileEntryComparer()); //Find out how much they grew by grew = grew.Select(entry => { var oldSize = that.FileList.Intersect(new List<FileEntry>() { entry }); FileEntry fe = new FileEntry(entry); fe.Delta = entry.Size - oldSize.First().Size; if (fe.Delta > 0) fe.FileKind = FileEntry.Kind.Growth; else fe.FileKind = FileEntry.Kind.Shrinkage; return fe; }); percent = (double)grew.Count()/ (double)this.FileList.Count; Console.WriteLine("Files grew:\t{0} ({1})",grew.Count().ToString().PadLeft(pad, c), percent.ToString("P")); //Find net growth Int64 netAdded = 0; foreach (var a in added) netAdded += a.Delta; netAdded /= 1024 * 1024; //convert from bytes -> mb percent = (double)netAdded / (double)this.GetTotalSize(); Console.WriteLine("Net added:\t{0} mb ({1})", netAdded.ToString().PadLeft(pad, c), percent.ToString("P")); //Find net growth Int64 netGrowth = 0; foreach (var g in grew) netGrowth += g.Delta; netGrowth /= 1024 * 1024; //convert from bytes -> mb percent = (double)netGrowth / (double)this.GetTotalSize(); Console.WriteLine("Net growth:\t{0} mb ({1})", netGrowth.ToString().PadLeft(pad, c), percent.ToString("P")); //Net overall percent = (double)(netAdded+netGrowth) / (double)this.GetTotalSize(); Console.WriteLine("Net overall:\t{0} mb ({1})", (netGrowth+netAdded).ToString().PadLeft(pad, c), percent.ToString("P")); //Print files which caused the most growth var overall = grew.Union(added).Union(removed).ToList(); overall.Sort(); overall.Reverse(); Console.WriteLine(); Console.WriteLine("{0} | {1} | path", "delta (mb)".PadLeft(15, ' '), "change".PadRight(9, ' ')); //Print out only different files from the mainOS partition //var mainOS = overall.Where(file => //{ // return file.Path; //}); var mainOS = overall.Where(file => true); var dpp = mainOS.Where(file => file.Path.StartsWith("dpp")); mainOS = mainOS.Except(dpp); var data = mainOS.Where(file => file.Path.StartsWith("data")); mainOS = mainOS.Except(data); var crashdump = mainOS.Where(file => file.Path.StartsWith("crashdump")); mainOS = mainOS.Except(crashdump); Console.WriteLine("-----------------------DPP partition\n"); foreach (var tmp in dpp) tmp.Print(); Console.WriteLine("-----------------------CrashDump Directory\n"); foreach (var tmp in crashdump) tmp.Print(); Console.WriteLine("-----------------------Main Directory \n"); foreach (var tmp in mainOS) tmp.Print(); Console.WriteLine("-----------------------Data Directory\n"); foreach (var tmp in data) tmp.Print(); Console.WriteLine("-----------------------Everything\n"); foreach (var tmp in overall) tmp.Print(); Console.WriteLine("-----------------------\n"); }
static void Main(string[] args) { if (args == null || args.Length <= 1) PrintUsage(); for (int i = 0; i < args.Length; i++) { switch (args[i]) { case "/dump": goto case "-dump"; case "-dump": if (i + 1 >= args.Length) { PrintUsage(); throw new ArgumentException("-dump requires an additional argument"); } string rootDir = args[++i]; if (!File.Exists(rootDir) && !Directory.Exists(rootDir)) { PrintUsage(); throw new ArgumentException("rootDir was not a file or directory"); } string outfile = "dump.txt"; FSHelper fs = new FSHelper(rootDir); //fs.WriteToFile(outfile); fs.Print(); break; case "/diff": goto case "-diff"; case "-diff": if (i + 2 >= args.Length) { PrintUsage(); throw new ArgumentException("-diff requires +2 additional arguments"); } string oldDirOrDumpFile = args[++i]; if (!File.Exists(oldDirOrDumpFile) && !Directory.Exists(oldDirOrDumpFile)) { PrintUsage(); throw new ArgumentException("oldDirOrDumpFile was not a file or directory"); } string newDirOrDumpFile = args[++i]; if (!File.Exists(newDirOrDumpFile) && !Directory.Exists(newDirOrDumpFile)) { PrintUsage(); throw new ArgumentException("newDirOrDumpFile was not a file or directory"); } FSHelper oldFs = new FSHelper(oldDirOrDumpFile); FSHelper newFs = new FSHelper(newDirOrDumpFile); newFs.Diff(oldFs); break; default: PrintUsage(); break; } } #if (DEBUG) Console.WriteLine("\nAll Done!"); Console.ReadLine(); #endif }