FileSaveStorage.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace DevilLink2.Persistence
  6. {
  7. public interface ISaveStorage
  8. {
  9. object SyncRoot { get; }
  10. bool Exists(string name);
  11. string Read(string name);
  12. void WriteDurable(string name, string text);
  13. void Move(string source, string destination);
  14. void Delete(string name);
  15. bool TryAtomicReplace(string source, string destination);
  16. }
  17. public sealed class FileSaveStorage : ISaveStorage
  18. {
  19. private static readonly Dictionary<string, object> gates = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  20. private readonly string directory;
  21. private readonly bool preferAtomicReplace;
  22. private static readonly Encoding utf8 = new UTF8Encoding(false, true);
  23. public object SyncRoot { get; }
  24. public string DirectoryPath => directory;
  25. public FileSaveStorage(string directory, bool preferAtomicReplace = true)
  26. {
  27. if (string.IsNullOrWhiteSpace(directory)) throw new ArgumentException("Save directory is required.", nameof(directory));
  28. string fullPath = Path.GetFullPath(directory);
  29. this.directory = fullPath == Path.GetPathRoot(fullPath) ? fullPath : fullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
  30. this.preferAtomicReplace = preferAtomicReplace;
  31. lock (gates)
  32. {
  33. if (!gates.TryGetValue(this.directory, out var gate)) gates.Add(this.directory, gate = new object());
  34. SyncRoot = gate;
  35. }
  36. }
  37. private string Resolve(string name)
  38. {
  39. if (string.IsNullOrEmpty(name) || Path.GetFileName(name) != name || name == "." || name == "..")
  40. throw new ArgumentException("Expected a file name within the save directory.", nameof(name));
  41. return Path.Combine(directory, name);
  42. }
  43. public bool Exists(string name) => File.Exists(Resolve(name));
  44. public string Read(string name) => File.ReadAllText(Resolve(name), utf8);
  45. public void WriteDurable(string name, string text)
  46. {
  47. Directory.CreateDirectory(directory);
  48. byte[] bytes = utf8.GetBytes(text);
  49. using (var stream = new FileStream(Resolve(name), FileMode.Create, FileAccess.Write, FileShare.None))
  50. { stream.Write(bytes, 0, bytes.Length); stream.Flush(true); }
  51. }
  52. public void Move(string source, string destination) => File.Move(Resolve(source), Resolve(destination));
  53. public void Delete(string name) => File.Delete(Resolve(name));
  54. public bool TryAtomicReplace(string source, string destination)
  55. {
  56. if (!preferAtomicReplace) return false;
  57. try { File.Replace(Resolve(source), Resolve(destination), null); return true; }
  58. catch (NotSupportedException) { return false; }
  59. // Permission/sharing/disk errors propagate; do not turn them into destructive retries.
  60. }
  61. }
  62. }