FileSystem.cs 5.2 KB

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