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.

126 lines
3.9 KiB

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