MemoryFile.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. using Microsoft.Win32.SafeHandles;
  2. using Quadarax.Foundation.Core.IO.FileSystem.Memory;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.IO.Abstractions;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace Quadarax.Foundation.Core.IO.FileSystem.Memory
  11. {
  12. public class MemoryFile : IFile
  13. {
  14. private readonly MemoryFileSystem _fileSystem;
  15. public IFileSystem FileSystem => throw new NotImplementedException();
  16. public MemoryFile(MemoryFileSystem fileSystem)
  17. {
  18. _fileSystem = fileSystem;
  19. }
  20. public void AppendAllText(string path, string? contents)
  21. {
  22. if (!_fileSystem.FileSystemDictionary.TryGetValue(path, out var entry) || !(entry is MemoryFileEntry fileEntry))
  23. {
  24. throw new FileNotFoundException();
  25. }
  26. fileEntry.Content = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(fileEntry.Content) + contents);
  27. fileEntry.LastWriteTime = DateTime.Now;
  28. }
  29. public void WriteAllText(string path, string? contents)
  30. {
  31. var fileEntry = new MemoryFileEntry
  32. {
  33. Name = Path.GetFileName(path),
  34. FullName = path,
  35. CreationTime = DateTime.Now,
  36. LastAccessTime = DateTime.Now,
  37. LastWriteTime = DateTime.Now,
  38. Content = Encoding.UTF8.GetBytes(contents ?? string.Empty)
  39. };
  40. _fileSystem.FileSystemDictionary[path] = fileEntry;
  41. }
  42. public string ReadAllText(string path)
  43. {
  44. if (!_fileSystem.FileSystemDictionary.TryGetValue(path, out var entry) || !(entry is MemoryFileEntry fileEntry))
  45. {
  46. throw new FileNotFoundException();
  47. }
  48. fileEntry.LastAccessTime = DateTime.Now;
  49. return Encoding.UTF8.GetString(fileEntry.Content);
  50. }
  51. public bool Exists(string? path)
  52. {
  53. if (path == null)
  54. return false;
  55. return _fileSystem.FileSystemDictionary.TryGetValue(path, out var entry) && entry is MemoryFileEntry;
  56. }
  57. public Task AppendAllLinesAsync(string path, IEnumerable<string> contents, CancellationToken cancellationToken = default)
  58. {
  59. throw new NotImplementedException();
  60. }
  61. public Task AppendAllLinesAsync(string path, IEnumerable<string> contents, Encoding encoding, CancellationToken cancellationToken = default)
  62. {
  63. throw new NotImplementedException();
  64. }
  65. public Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. public Task AppendAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. public Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. public Task<string[]> ReadAllLinesAsync(string path, CancellationToken cancellationToken = default)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. public Task<string[]> ReadAllLinesAsync(string path, Encoding encoding, CancellationToken cancellationToken = default)
  82. {
  83. throw new NotImplementedException();
  84. }
  85. public Task<string> ReadAllTextAsync(string path, CancellationToken cancellationToken = default)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. public Task<string> ReadAllTextAsync(string path, Encoding encoding, CancellationToken cancellationToken = default)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. public IAsyncEnumerable<string> ReadLinesAsync(string path, CancellationToken cancellationToken = default)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. public IAsyncEnumerable<string> ReadLinesAsync(string path, Encoding encoding, CancellationToken cancellationToken = default)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. public Task WriteAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken = default)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. public Task WriteAllLinesAsync(string path, IEnumerable<string> contents, CancellationToken cancellationToken = default)
  106. {
  107. throw new NotImplementedException();
  108. }
  109. public Task WriteAllLinesAsync(string path, IEnumerable<string> contents, Encoding encoding, CancellationToken cancellationToken = default)
  110. {
  111. throw new NotImplementedException();
  112. }
  113. public Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default)
  114. {
  115. throw new NotImplementedException();
  116. }
  117. public Task WriteAllTextAsync(string path, string? contents, Encoding encoding, CancellationToken cancellationToken = default)
  118. {
  119. throw new NotImplementedException();
  120. }
  121. public void AppendAllLines(string path, IEnumerable<string> contents)
  122. {
  123. throw new NotImplementedException();
  124. }
  125. public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. public void AppendAllText(string path, string? contents, Encoding encoding)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public StreamWriter AppendText(string path)
  134. {
  135. throw new NotImplementedException();
  136. }
  137. public void Copy(string sourceFileName, string destFileName)
  138. {
  139. throw new NotImplementedException();
  140. }
  141. public void Copy(string sourceFileName, string destFileName, bool overwrite)
  142. {
  143. throw new NotImplementedException();
  144. }
  145. public FileSystemStream Create(string path)
  146. {
  147. throw new NotImplementedException();
  148. }
  149. public FileSystemStream Create(string path, int bufferSize)
  150. {
  151. throw new NotImplementedException();
  152. }
  153. public FileSystemStream Create(string path, int bufferSize, FileOptions options)
  154. {
  155. throw new NotImplementedException();
  156. }
  157. public IFileSystemInfo CreateSymbolicLink(string path, string pathToTarget)
  158. {
  159. throw new NotImplementedException();
  160. }
  161. public StreamWriter CreateText(string path)
  162. {
  163. throw new NotImplementedException();
  164. }
  165. public void Decrypt(string path)
  166. {
  167. throw new NotImplementedException();
  168. }
  169. public void Delete(string path)
  170. {
  171. throw new NotImplementedException();
  172. }
  173. public void Encrypt(string path)
  174. {
  175. throw new NotImplementedException();
  176. }
  177. public FileAttributes GetAttributes(string path)
  178. {
  179. throw new NotImplementedException();
  180. }
  181. public FileAttributes GetAttributes(SafeFileHandle fileHandle)
  182. {
  183. throw new NotImplementedException();
  184. }
  185. public DateTime GetCreationTime(string path)
  186. {
  187. throw new NotImplementedException();
  188. }
  189. public DateTime GetCreationTime(SafeFileHandle fileHandle)
  190. {
  191. throw new NotImplementedException();
  192. }
  193. public DateTime GetCreationTimeUtc(string path)
  194. {
  195. throw new NotImplementedException();
  196. }
  197. public DateTime GetCreationTimeUtc(SafeFileHandle fileHandle)
  198. {
  199. throw new NotImplementedException();
  200. }
  201. public DateTime GetLastAccessTime(string path)
  202. {
  203. throw new NotImplementedException();
  204. }
  205. public DateTime GetLastAccessTime(SafeFileHandle fileHandle)
  206. {
  207. throw new NotImplementedException();
  208. }
  209. public DateTime GetLastAccessTimeUtc(string path)
  210. {
  211. throw new NotImplementedException();
  212. }
  213. public DateTime GetLastAccessTimeUtc(SafeFileHandle fileHandle)
  214. {
  215. throw new NotImplementedException();
  216. }
  217. public DateTime GetLastWriteTime(string path)
  218. {
  219. throw new NotImplementedException();
  220. }
  221. public DateTime GetLastWriteTime(SafeFileHandle fileHandle)
  222. {
  223. throw new NotImplementedException();
  224. }
  225. public DateTime GetLastWriteTimeUtc(string path)
  226. {
  227. throw new NotImplementedException();
  228. }
  229. public DateTime GetLastWriteTimeUtc(SafeFileHandle fileHandle)
  230. {
  231. throw new NotImplementedException();
  232. }
  233. public UnixFileMode GetUnixFileMode(string path)
  234. {
  235. throw new NotImplementedException();
  236. }
  237. public UnixFileMode GetUnixFileMode(SafeFileHandle fileHandle)
  238. {
  239. throw new NotImplementedException();
  240. }
  241. public void Move(string sourceFileName, string destFileName)
  242. {
  243. throw new NotImplementedException();
  244. }
  245. public void Move(string sourceFileName, string destFileName, bool overwrite)
  246. {
  247. throw new NotImplementedException();
  248. }
  249. public FileSystemStream Open(string path, FileMode mode)
  250. {
  251. throw new NotImplementedException();
  252. }
  253. public FileSystemStream Open(string path, FileMode mode, FileAccess access)
  254. {
  255. throw new NotImplementedException();
  256. }
  257. public FileSystemStream Open(string path, FileMode mode, FileAccess access, FileShare share)
  258. {
  259. throw new NotImplementedException();
  260. }
  261. public FileSystemStream Open(string path, FileStreamOptions options)
  262. {
  263. throw new NotImplementedException();
  264. }
  265. public FileSystemStream OpenRead(string path)
  266. {
  267. throw new NotImplementedException();
  268. }
  269. public StreamReader OpenText(string path)
  270. {
  271. throw new NotImplementedException();
  272. }
  273. public FileSystemStream OpenWrite(string path)
  274. {
  275. throw new NotImplementedException();
  276. }
  277. public byte[] ReadAllBytes(string path)
  278. {
  279. throw new NotImplementedException();
  280. }
  281. public string[] ReadAllLines(string path)
  282. {
  283. throw new NotImplementedException();
  284. }
  285. public string[] ReadAllLines(string path, Encoding encoding)
  286. {
  287. throw new NotImplementedException();
  288. }
  289. public string ReadAllText(string path, Encoding encoding)
  290. {
  291. throw new NotImplementedException();
  292. }
  293. public IEnumerable<string> ReadLines(string path)
  294. {
  295. throw new NotImplementedException();
  296. }
  297. public IEnumerable<string> ReadLines(string path, Encoding encoding)
  298. {
  299. throw new NotImplementedException();
  300. }
  301. public void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName)
  302. {
  303. throw new NotImplementedException();
  304. }
  305. public void Replace(string sourceFileName, string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors)
  306. {
  307. throw new NotImplementedException();
  308. }
  309. public IFileSystemInfo? ResolveLinkTarget(string linkPath, bool returnFinalTarget)
  310. {
  311. throw new NotImplementedException();
  312. }
  313. public void SetAttributes(string path, FileAttributes fileAttributes)
  314. {
  315. throw new NotImplementedException();
  316. }
  317. public void SetAttributes(SafeFileHandle fileHandle, FileAttributes fileAttributes)
  318. {
  319. throw new NotImplementedException();
  320. }
  321. public void SetCreationTime(string path, DateTime creationTime)
  322. {
  323. throw new NotImplementedException();
  324. }
  325. public void SetCreationTime(SafeFileHandle fileHandle, DateTime creationTime)
  326. {
  327. throw new NotImplementedException();
  328. }
  329. public void SetCreationTimeUtc(string path, DateTime creationTimeUtc)
  330. {
  331. throw new NotImplementedException();
  332. }
  333. public void SetCreationTimeUtc(SafeFileHandle fileHandle, DateTime creationTimeUtc)
  334. {
  335. throw new NotImplementedException();
  336. }
  337. public void SetLastAccessTime(string path, DateTime lastAccessTime)
  338. {
  339. throw new NotImplementedException();
  340. }
  341. public void SetLastAccessTime(SafeFileHandle fileHandle, DateTime lastAccessTime)
  342. {
  343. throw new NotImplementedException();
  344. }
  345. public void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)
  346. {
  347. throw new NotImplementedException();
  348. }
  349. public void SetLastAccessTimeUtc(SafeFileHandle fileHandle, DateTime lastAccessTimeUtc)
  350. {
  351. throw new NotImplementedException();
  352. }
  353. public void SetLastWriteTime(string path, DateTime lastWriteTime)
  354. {
  355. throw new NotImplementedException();
  356. }
  357. public void SetLastWriteTime(SafeFileHandle fileHandle, DateTime lastWriteTime)
  358. {
  359. throw new NotImplementedException();
  360. }
  361. public void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)
  362. {
  363. throw new NotImplementedException();
  364. }
  365. public void SetLastWriteTimeUtc(SafeFileHandle fileHandle, DateTime lastWriteTimeUtc)
  366. {
  367. throw new NotImplementedException();
  368. }
  369. public void SetUnixFileMode(string path, UnixFileMode mode)
  370. {
  371. throw new NotImplementedException();
  372. }
  373. public void SetUnixFileMode(SafeFileHandle fileHandle, UnixFileMode mode)
  374. {
  375. throw new NotImplementedException();
  376. }
  377. public void WriteAllBytes(string path, byte[] bytes)
  378. {
  379. throw new NotImplementedException();
  380. }
  381. public void WriteAllLines(string path, string[] contents)
  382. {
  383. throw new NotImplementedException();
  384. }
  385. public void WriteAllLines(string path, IEnumerable<string> contents)
  386. {
  387. throw new NotImplementedException();
  388. }
  389. public void WriteAllLines(string path, string[] contents, Encoding encoding)
  390. {
  391. throw new NotImplementedException();
  392. }
  393. public void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)
  394. {
  395. throw new NotImplementedException();
  396. }
  397. public void WriteAllText(string path, string? contents, Encoding encoding)
  398. {
  399. throw new NotImplementedException();
  400. }
  401. public void AppendAllBytes(string path, byte[] bytes)
  402. {
  403. throw new NotImplementedException();
  404. }
  405. public void AppendAllBytes(string path, ReadOnlySpan<byte> bytes)
  406. {
  407. throw new NotImplementedException();
  408. }
  409. public Task AppendAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken = default)
  410. {
  411. throw new NotImplementedException();
  412. }
  413. public Task AppendAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default)
  414. {
  415. throw new NotImplementedException();
  416. }
  417. public void AppendAllText(string path, ReadOnlySpan<char> contents)
  418. {
  419. throw new NotImplementedException();
  420. }
  421. public void AppendAllText(string path, ReadOnlySpan<char> contents, Encoding encoding)
  422. {
  423. throw new NotImplementedException();
  424. }
  425. public Task AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default)
  426. {
  427. throw new NotImplementedException();
  428. }
  429. public Task AppendAllTextAsync(string path, ReadOnlyMemory<char> contents, Encoding encoding, CancellationToken cancellationToken = default)
  430. {
  431. throw new NotImplementedException();
  432. }
  433. public void WriteAllBytes(string path, ReadOnlySpan<byte> bytes)
  434. {
  435. throw new NotImplementedException();
  436. }
  437. public Task WriteAllBytesAsync(string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default)
  438. {
  439. throw new NotImplementedException();
  440. }
  441. public void WriteAllText(string path, ReadOnlySpan<char> contents)
  442. {
  443. throw new NotImplementedException();
  444. }
  445. public void WriteAllText(string path, ReadOnlySpan<char> contents, Encoding encoding)
  446. {
  447. throw new NotImplementedException();
  448. }
  449. public Task WriteAllTextAsync(string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default)
  450. {
  451. throw new NotImplementedException();
  452. }
  453. public Task WriteAllTextAsync(string path, ReadOnlyMemory<char> contents, Encoding encoding, CancellationToken cancellationToken = default)
  454. {
  455. throw new NotImplementedException();
  456. }
  457. // Implement other methods as needed
  458. }
  459. }