NTFSFileSystem.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (C) 2014-2016 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. using DiskAccessLibrary;
  11. using Utilities;
  12. namespace DiskAccessLibrary.FileSystems.NTFS
  13. {
  14. /// <summary>
  15. /// Adapter providing FileSystem implementation for NTFS (using NTFSVolume).
  16. /// </summary>
  17. public class NTFSFileSystem : FileSystem, IExtendableFileSystem
  18. {
  19. NTFSVolume m_volume;
  20. public NTFSFileSystem(Volume volume)
  21. {
  22. m_volume = new NTFSVolume(volume);
  23. }
  24. public NTFSFileSystem(NTFSVolume volume)
  25. {
  26. m_volume = volume;
  27. }
  28. public override FileSystemEntry GetEntry(string path)
  29. {
  30. FileRecord record = m_volume.GetFileRecord(path);
  31. if (record != null)
  32. {
  33. return ToFileSystemEntry(path, record);
  34. }
  35. else
  36. {
  37. return null;
  38. }
  39. }
  40. public override FileSystemEntry CreateFile(string path)
  41. {
  42. throw new NotImplementedException("The method or operation is not implemented.");
  43. }
  44. public override FileSystemEntry CreateDirectory(string path)
  45. {
  46. throw new NotImplementedException("The method or operation is not implemented.");
  47. }
  48. public override void Move(string source, string destination)
  49. {
  50. throw new NotImplementedException("The method or operation is not implemented.");
  51. }
  52. public override void Delete(string path)
  53. {
  54. throw new NotImplementedException("The method or operation is not implemented.");
  55. }
  56. public override List<FileSystemEntry> ListEntriesInDirectory(string path)
  57. {
  58. FileRecord directoryRecord = m_volume.GetFileRecord(path);
  59. if (directoryRecord != null && directoryRecord.IsDirectory)
  60. {
  61. List<FileRecord> records = m_volume.GetFileRecordsInDirectory(directoryRecord.MftSegmentNumber);
  62. List<FileSystemEntry> result = new List<FileSystemEntry>();
  63. path = FileSystem.GetDirectoryPath(path);
  64. foreach (FileRecord record in records)
  65. {
  66. string fullPath = path + record.FileName;
  67. FileSystemEntry entry = ToFileSystemEntry(fullPath, record);
  68. result.Add(entry);
  69. }
  70. return result;
  71. }
  72. else
  73. {
  74. return null;
  75. }
  76. }
  77. public override Stream OpenFile(string path, FileMode mode, FileAccess access, FileShare share)
  78. {
  79. if (mode == FileMode.Open || mode == FileMode.Truncate)
  80. {
  81. FileRecord record = m_volume.GetFileRecord(path);
  82. if (record != null && !record.IsDirectory)
  83. {
  84. NTFSFile file = new NTFSFile(m_volume, record);
  85. NTFSFileStream stream = new NTFSFileStream(file);
  86. if (mode == FileMode.Truncate)
  87. {
  88. stream.SetLength(0);
  89. }
  90. return stream;
  91. }
  92. else
  93. {
  94. throw new FileNotFoundException();
  95. }
  96. }
  97. throw new NotImplementedException("The method or operation is not implemented.");
  98. }
  99. public override void SetAttributes(string path, bool? isHidden, bool? isReadonly, bool? isArchived)
  100. {
  101. FileRecord record = m_volume.GetFileRecord(path);
  102. if (isHidden.HasValue)
  103. {
  104. if (isHidden.Value)
  105. {
  106. record.StandardInformation.FileAttributes |= FileAttributes.Hidden;
  107. }
  108. else
  109. {
  110. record.StandardInformation.FileAttributes &= ~FileAttributes.Hidden;
  111. }
  112. }
  113. if (isReadonly.HasValue)
  114. {
  115. if (isReadonly.Value)
  116. {
  117. record.StandardInformation.FileAttributes |= FileAttributes.Readonly;
  118. }
  119. else
  120. {
  121. record.StandardInformation.FileAttributes &= ~FileAttributes.Readonly;
  122. }
  123. }
  124. if (isArchived.HasValue)
  125. {
  126. if (isArchived.Value)
  127. {
  128. record.StandardInformation.FileAttributes |= FileAttributes.Archive;
  129. }
  130. else
  131. {
  132. record.StandardInformation.FileAttributes &= ~FileAttributes.Archive;
  133. }
  134. }
  135. m_volume.MasterFileTable.UpdateFileRecord(record);
  136. }
  137. public override void SetDates(string path, DateTime? creationDT, DateTime? lastWriteDT, DateTime? lastAccessDT)
  138. {
  139. throw new NotImplementedException("The method or operation is not implemented.");
  140. }
  141. public long GetMaximumSizeToExtend()
  142. {
  143. return m_volume.GetMaximumSizeToExtend();
  144. }
  145. public void Extend(long numberOfAdditionalSectors)
  146. {
  147. m_volume.Extend(numberOfAdditionalSectors);
  148. }
  149. public override string Name
  150. {
  151. get
  152. {
  153. return "NTFS";
  154. }
  155. }
  156. public override long Size
  157. {
  158. get
  159. {
  160. return m_volume.Size;
  161. }
  162. }
  163. public override long FreeSpace
  164. {
  165. get
  166. {
  167. return m_volume.FreeSpace;
  168. }
  169. }
  170. public bool IsValidAndSupported
  171. {
  172. get
  173. {
  174. return m_volume.IsValidAndSupported;
  175. }
  176. }
  177. public static FileSystemEntry ToFileSystemEntry(string path, FileRecord record)
  178. {
  179. ulong size = record.IsDirectory ? 0 : record.DataRecord.DataRealSize;
  180. FileAttributes attributes = record.StandardInformation.FileAttributes;
  181. bool isHidden = (attributes & FileAttributes.Hidden) > 0;
  182. bool isReadonly = (attributes & FileAttributes.Readonly) > 0;
  183. bool isArchived = (attributes & FileAttributes.Archive) > 0;
  184. return new FileSystemEntry(path, record.FileName, record.IsDirectory, size, record.FileNameRecord.CreationTime, record.FileNameRecord.ModificationTime, record.FileNameRecord.LastAccessTime, isHidden, isReadonly, isArchived);
  185. }
  186. }
  187. }