| | 1 | | // <copyright file="DiscFsFileLocator.cs" company="Jérémy Ansel"> |
| | 2 | | // Copyright (c) 2015, 2019 Jérémy Ansel |
| | 3 | | // </copyright> |
| | 4 | | // <license> |
| | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | 6 | | // </license> |
| | 7 | |
|
| | 8 | | namespace JeremyAnsel.IO.DiscLocator |
| | 9 | | { |
| | 10 | | using System; |
| | 11 | | using System.Collections.Generic; |
| | 12 | | using System.IO; |
| | 13 | | using DiscUtils; |
| | 14 | | using IO.Locator; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Base class for the disc file locators. |
| | 18 | | /// </summary> |
| | 19 | | internal abstract class DiscFsFileLocator : IFileLocator |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// A file stream. |
| | 23 | | /// </summary> |
| | 24 | | private Stream? fileStream; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// A disc. |
| | 28 | | /// </summary> |
| | 29 | | private DiscFileSystem? disc; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Initializes a new instance of the <see cref="DiscFsFileLocator"/> class. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="root">The root path.</param> |
| | 35 | | /// <param name="create">The create function.</param> |
| 0 | 36 | | protected DiscFsFileLocator(string root, Func<Stream, DiscFileSystem> create) |
| | 37 | | { |
| 0 | 38 | | this.fileStream = File.OpenRead(root); |
| 0 | 39 | | this.disc = create(this.fileStream); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Initializes a new instance of the <see cref="DiscFsFileLocator"/> class. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="locator">A file locator.</param> |
| | 46 | | /// <param name="root">The root path.</param> |
| | 47 | | /// <param name="create">The create function.</param> |
| 0 | 48 | | protected DiscFsFileLocator(IFileLocator? locator, string root, Func<Stream, DiscFileSystem> create) |
| | 49 | | { |
| 0 | 50 | | if (locator != null) |
| | 51 | | { |
| 0 | 52 | | this.fileStream = locator.Open(root); |
| | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 0 | 56 | | this.fileStream = File.OpenRead(root); |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | this.disc = create(this.fileStream); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Initializes a new instance of the <see cref="DiscFsFileLocator"/> class. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="root">A stream.</param> |
| | 66 | | /// <param name="create">The create function.</param> |
| 0 | 67 | | protected DiscFsFileLocator(Stream root, Func<Stream, DiscFileSystem> create) |
| | 68 | | { |
| 0 | 69 | | this.disc = create(root); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Immediately releases the unmanaged resources used by the <see cref="DiscFsFileLocator"/> object. |
| | 74 | | /// </summary> |
| | 75 | | public void Dispose() |
| | 76 | | { |
| 0 | 77 | | this.Dispose(true); |
| 0 | 78 | | GC.SuppressFinalize(this); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Indicates whether the specified path exists. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="path">A path.</param> |
| | 85 | | /// <returns>A boolean.</returns> |
| | 86 | | public bool Exists(string path) |
| | 87 | | { |
| 0 | 88 | | return this.disc!.FileExists(Utilities.PathNormalize(path)); |
| | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Open the specified file. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="path">A path.</param> |
| | 95 | | /// <returns>A stream.</returns> |
| | 96 | | public Stream Open(string path) |
| | 97 | | { |
| 0 | 98 | | return this.disc!.OpenFile(Utilities.PathNormalize(path), FileMode.Open, FileAccess.Read); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Enumerates the files. |
| | 103 | | /// </summary> |
| | 104 | | /// <returns>The files.</returns> |
| | 105 | | public IEnumerable<string> EnumerateFiles() |
| | 106 | | { |
| 0 | 107 | | return this.EnumerateFiles(string.Empty); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Enumerates the files. |
| | 112 | | /// </summary> |
| | 113 | | /// <param name="root">A root path.</param> |
| | 114 | | /// <returns>The files.</returns> |
| | 115 | | public IEnumerable<string> EnumerateFiles(string root) |
| | 116 | | { |
| 0 | 117 | | root = Utilities.PathNormalize(root); |
| | 118 | |
|
| 0 | 119 | | if (!this.disc!.DirectoryExists(root)) |
| | 120 | | { |
| 0 | 121 | | yield break; |
| | 122 | | } |
| | 123 | |
|
| 0 | 124 | | foreach (var file in this.disc.GetFiles(root, "*.*", SearchOption.AllDirectories)) |
| | 125 | | { |
| 0 | 126 | | yield return Utilities.PathNormalize(file); |
| | 127 | | } |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Immediately releases the unmanaged resources used by the <see cref="DiscFsFileLocator"/> object. |
| | 132 | | /// </summary> |
| | 133 | | /// <param name="disposing">A boolean.</param> |
| | 134 | | protected virtual void Dispose(bool disposing) |
| | 135 | | { |
| 0 | 136 | | if (disposing) |
| | 137 | | { |
| 0 | 138 | | if (this.disc != null) |
| | 139 | | { |
| 0 | 140 | | this.disc.Dispose(); |
| 0 | 141 | | this.disc = null; |
| | 142 | | } |
| | 143 | |
|
| 0 | 144 | | if (this.fileStream != null) |
| | 145 | | { |
| 0 | 146 | | this.fileStream.Dispose(); |
| 0 | 147 | | this.fileStream = null; |
| | 148 | | } |
| | 149 | | } |
| 0 | 150 | | } |
| | 151 | | } |
| | 152 | | } |
| | 153 | |
|