You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.6 KiB

  1. using IllusionInjector.Utilities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace IllusionInjector.Updating.Backup
  9. {
  10. /// <summary>
  11. /// A unit for backup. WIP.
  12. /// </summary>
  13. internal class BackupUnit
  14. {
  15. public string Name { get; private set; }
  16. private DirectoryInfo _BackupPath;
  17. private List<string> _Files = new List<string>();
  18. public BackupUnit(string backupPath) : this(backupPath, DateTime.Now.ToString("yyyy-MM-dd_h-mm-ss"))
  19. {
  20. }
  21. public BackupUnit(string backupPath, string name)
  22. {
  23. Name = name;
  24. _BackupPath = new DirectoryInfo(Path.Combine(backupPath, Name));
  25. _BackupPath.Create();
  26. }
  27. public static BackupUnit FromDirectory(DirectoryInfo directory, string backupPath)
  28. {
  29. var unit = new BackupUnit(backupPath, directory.Name);
  30. // Parse directory
  31. foreach(var file in directory.GetFiles("*", SearchOption.AllDirectories)) {
  32. var relativePath = file.FullName.Substring(directory.FullName.Length + 1);
  33. unit._Files.Add(relativePath);
  34. }
  35. return unit;
  36. }
  37. public void Add(string file)
  38. {
  39. Add(new FileInfo(file));
  40. }
  41. internal void Delete()
  42. {
  43. _BackupPath.Delete(true);
  44. }
  45. /// <summary>
  46. /// Adds a file to the list of changed files and backups it.
  47. /// </summary>
  48. /// <param name="path"></param>
  49. public void Add(FileInfo file)
  50. {
  51. var relativePath = LoneFunctions.GetRelativePath(Environment.CurrentDirectory, file.FullName);
  52. var backupPath = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath));
  53. if(_Files.Contains(relativePath))
  54. {
  55. Console.WriteLine("Skipping backup of {0}", relativePath);
  56. return;
  57. }
  58. // Copy over
  59. backupPath.Directory.Create();
  60. if (file.Exists)
  61. {
  62. file.CopyTo(backupPath.FullName, true);
  63. } else
  64. {
  65. // Make empty file
  66. backupPath.Create().Close();
  67. }
  68. // Add to list
  69. _Files.Add(relativePath);
  70. }
  71. /// <summary>
  72. /// Reverts the changes made in this unit.
  73. /// </summary>
  74. public void Restore()
  75. {
  76. foreach(var relativePath in _Files)
  77. {
  78. //Console.WriteLine("Restoring {0}", relativePath);
  79. // Original version
  80. var backupFile = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath));
  81. var target = new FileInfo(Path.Combine(Environment.CurrentDirectory, relativePath));
  82. if (backupFile.Exists)
  83. {
  84. if (backupFile.Length > 0)
  85. {
  86. //Console.WriteLine(" {0} => {1}", backupFile.FullName, target.FullName);
  87. target.Directory.Create();
  88. backupFile.CopyTo(target.FullName, true);
  89. } else
  90. {
  91. //Console.WriteLine(" x {0}", target.FullName);
  92. if(target.Exists)
  93. {
  94. target.Delete();
  95. }
  96. }
  97. } else {
  98. //Console.Error.WriteLine("Backup not found!");
  99. }
  100. }
  101. }
  102. }
  103. }