MemoryFileSystem.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Abstractions;
  5. namespace Quadarax.Foundation.Core.IO.FileSystem.Memory
  6. {
  7. public class MemoryFileSystem : IFileSystem
  8. {
  9. private readonly Dictionary<string, MemoryFileSystemEntry> _fileSystem;
  10. public MemoryFileSystem()
  11. {
  12. _fileSystem = new Dictionary<string, MemoryFileSystemEntry>(StringComparer.OrdinalIgnoreCase);
  13. File = new MemoryFile(this);
  14. Directory = new MemoryDirectory(this);
  15. Path = new MemoryPath(this);
  16. FileInfo = new MemoryFileInfoFactory(this);
  17. DirectoryInfo = new MemoryDirectoryInfoFactory(this);
  18. }
  19. public IFile File { get; }
  20. public IDirectory Directory { get; }
  21. public IPath Path { get; }
  22. public IFileInfoFactory FileInfo { get; }
  23. public IDirectoryInfoFactory DirectoryInfo { get; }
  24. public IDriveInfoFactory DriveInfo => throw new NotImplementedException();
  25. public IFileStreamFactory FileStream => throw new NotImplementedException();
  26. public IFileSystemWatcherFactory FileSystemWatcher => throw new NotImplementedException();
  27. // Implement other properties as needed
  28. internal Dictionary<string, MemoryFileSystemEntry> FileSystemDictionary => _fileSystem;
  29. IFileVersionInfoFactory IFileSystem.FileVersionInfo => throw new NotImplementedException();
  30. internal MemoryFileSystemEntry? ResolveSymbolicLink(MemoryFileSystemEntry entry)
  31. {
  32. int maxSymlinkDepth = 40; // Prevent infinite loops
  33. while (entry.IsSymbolicLink && maxSymlinkDepth > 0)
  34. {
  35. if (FileSystemDictionary.TryGetValue(entry.SymbolicLinkTarget, out var targetEntry))
  36. {
  37. entry = targetEntry;
  38. maxSymlinkDepth--;
  39. }
  40. else
  41. {
  42. // Broken symlink
  43. return null;
  44. }
  45. }
  46. if (maxSymlinkDepth == 0)
  47. {
  48. throw new IOException("Too many levels of symbolic links");
  49. }
  50. return entry;
  51. }
  52. }
  53. }