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.

101 lines
3.2 KiB

  1. using IPA.Utilities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace IPA.Injector.Backups
  6. {
  7. /// <summary>
  8. /// A unit for backup. WIP.
  9. /// </summary>
  10. internal class BackupUnit
  11. {
  12. public string Name { get; private set; }
  13. private readonly DirectoryInfo _backupPath;
  14. private readonly HashSet<string> _files = new HashSet<string>();
  15. private readonly FileInfo _manifestFile;
  16. private const string ManifestFileName = "$manifest$.txt";
  17. public BackupUnit(string dir) : this(dir, Utils.CurrentTime().ToString("yyyy-MM-dd_h-mm-ss"))
  18. {
  19. }
  20. private BackupUnit(string dir, string name)
  21. {
  22. Name = name;
  23. _backupPath = new DirectoryInfo(Path.Combine(dir, Name));
  24. _manifestFile = new FileInfo(Path.Combine(_backupPath.FullName, ManifestFileName));
  25. }
  26. public static BackupUnit FromDirectory(DirectoryInfo directory, string dir)
  27. {
  28. var unit = new BackupUnit(dir, directory.Name);
  29. // Read Manifest
  30. if (unit._manifestFile.Exists)
  31. {
  32. var manifest = File.ReadAllText(unit._manifestFile.FullName);
  33. foreach (var line in manifest.Split(new[] { Environment.NewLine, "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries))
  34. unit._files.Add(line);
  35. }
  36. else
  37. {
  38. foreach (var file in directory.GetFiles("*", SearchOption.AllDirectories))
  39. {
  40. if (file.Name == ManifestFileName) continue;
  41. var relativePath = file.FullName.Substring(directory.FullName.Length + 1);
  42. unit._files.Add(relativePath);
  43. }
  44. }
  45. return unit;
  46. }
  47. public void Add(string file)
  48. {
  49. Add(new FileInfo(file));
  50. }
  51. internal void Delete()
  52. {
  53. _backupPath.Delete(true);
  54. }
  55. /// <summary>
  56. /// Adds a file to the list of changed files and backups it.
  57. /// </summary>
  58. /// <param name="file"></param>
  59. public void Add(FileInfo file)
  60. {
  61. var relativePath = Utilities.Utils.GetRelativePath(file.FullName, Environment.CurrentDirectory);
  62. var backupPath = new FileInfo(Path.Combine(_backupPath.FullName, relativePath));
  63. // Copy over
  64. backupPath.Directory?.Create();
  65. if (file.Exists)
  66. {
  67. if (File.Exists(backupPath.FullName))
  68. File.Delete(backupPath.FullName);
  69. file.CopyTo(backupPath.FullName);
  70. }
  71. else
  72. {
  73. // Make empty file
  74. //backupPath.Create().Close();
  75. // do not do this because it can cause problems
  76. }
  77. if (_files.Contains(relativePath)) return;
  78. if (!File.Exists(_manifestFile.FullName))
  79. _manifestFile.Create().Close();
  80. var stream = _manifestFile.AppendText();
  81. stream.WriteLine(relativePath);
  82. stream.Close();
  83. // Add to list
  84. _files.Add(relativePath);
  85. }
  86. }
  87. }