FileSystem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Utilities
  6. {
  7. public abstract class FileSystem : IFileSystem
  8. {
  9. public abstract FileSystemEntry GetEntry(string path);
  10. public abstract FileSystemEntry CreateFile(string path);
  11. public abstract FileSystemEntry CreateDirectory(string path);
  12. public abstract void Move(string source, string destination);
  13. public abstract void Delete(string path);
  14. public abstract List<FileSystemEntry> ListEntriesInDirectory(string path);
  15. public abstract Stream OpenFile(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options);
  16. public abstract void SetAttributes(string path, bool? isHidden, bool? isReadonly, bool? isArchived);
  17. public abstract void SetDates(string path, DateTime? creationDT, DateTime? lastWriteDT, DateTime? lastAccessDT);
  18. public List<FileSystemEntry> ListEntriesInRootDirectory()
  19. {
  20. return ListEntriesInDirectory(@"\");
  21. }
  22. public virtual List<KeyValuePair<string, ulong>> ListDataStreams(string path)
  23. {
  24. FileSystemEntry entry = GetEntry(path);
  25. List<KeyValuePair<string, ulong>> result = new List<KeyValuePair<string, ulong>>();
  26. if (!entry.IsDirectory)
  27. {
  28. result.Add(new KeyValuePair<string, ulong>("::$DATA", entry.Size));
  29. }
  30. return result;
  31. }
  32. public Stream OpenFile(string path, FileMode mode, FileAccess access, FileShare share)
  33. {
  34. return OpenFile(path, mode, access, share, FileOptions.None);
  35. }
  36. public void CopyFile(string sourcePath, string destinationPath)
  37. {
  38. const int bufferLength = 1024 * 1024;
  39. FileSystemEntry sourceFile = GetEntry(sourcePath);
  40. FileSystemEntry destinationFile = GetEntry(destinationPath);
  41. if (sourceFile == null | sourceFile.IsDirectory)
  42. {
  43. throw new FileNotFoundException();
  44. }
  45. if (destinationFile != null && destinationFile.IsDirectory)
  46. {
  47. throw new ArgumentException("Destination cannot be a directory");
  48. }
  49. if (destinationFile == null)
  50. {
  51. destinationFile = CreateFile(destinationPath);
  52. }
  53. Stream sourceStream = OpenFile(sourcePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, FileOptions.SequentialScan);
  54. Stream destinationStream = OpenFile(destinationPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, FileOptions.None);
  55. while (sourceStream.Position < sourceStream.Length)
  56. {
  57. int readSize = (int)Math.Max(bufferLength, sourceStream.Length - sourceStream.Position);
  58. byte[] buffer = new byte[readSize];
  59. sourceStream.Read(buffer, 0, buffer.Length);
  60. destinationStream.Write(buffer, 0, buffer.Length);
  61. }
  62. sourceStream.Close();
  63. destinationStream.Close();
  64. }
  65. public virtual bool Exists(string path)
  66. {
  67. try
  68. {
  69. GetEntry(path);
  70. }
  71. catch (FileNotFoundException)
  72. {
  73. return false;
  74. }
  75. catch (DirectoryNotFoundException)
  76. {
  77. return false;
  78. }
  79. return true;
  80. }
  81. public abstract string Name
  82. {
  83. get;
  84. }
  85. public abstract long Size
  86. {
  87. get;
  88. }
  89. public abstract long FreeSpace
  90. {
  91. get;
  92. }
  93. public abstract bool SupportsNamedStreams
  94. {
  95. get;
  96. }
  97. public static string GetParentDirectory(string path)
  98. {
  99. if (path == String.Empty)
  100. {
  101. path = @"\";
  102. }
  103. if (!path.StartsWith(@"\"))
  104. {
  105. throw new ArgumentException("Invalid path");
  106. }
  107. if (path.Length > 1 && path.EndsWith(@"\"))
  108. {
  109. path = path.Substring(0, path.Length - 1);
  110. }
  111. int separatorIndex = path.LastIndexOf(@"\");
  112. return path.Substring(0, separatorIndex + 1);
  113. }
  114. /// <summary>
  115. /// Will append a trailing slash to a directory path if not already present
  116. /// </summary>
  117. /// <param name="path"></param>
  118. /// <returns></returns>
  119. public static string GetDirectoryPath(string path)
  120. {
  121. if (path.EndsWith(@"\"))
  122. {
  123. return path;
  124. }
  125. else
  126. {
  127. return path + @"\";
  128. }
  129. }
  130. }
  131. }