< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 33
Coverable lines: 33
Total lines: 153
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%10%
.ctor(...)0%20%
.ctor(...)100%10%
Dispose()100%10%
Exists(...)100%10%
Open(...)100%10%
EnumerateFiles()100%10%
EnumerateFiles()0%40%
Dispose(...)0%60%

File(s)

https://raw.githubusercontent.com/JeremyAnsel/JeremyAnsel.IO.Locator/16c8f4344390c6376cd2208cc1ae63a39862df37/JeremyAnsel.IO.Locator/JeremyAnsel.IO.DiscLocator/DiscFsFileLocator.cs

#LineLine coverage
 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
 8namespace 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>
 036        protected DiscFsFileLocator(string root, Func<Stream, DiscFileSystem> create)
 37        {
 038            this.fileStream = File.OpenRead(root);
 039            this.disc = create(this.fileStream);
 040        }
 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>
 048        protected DiscFsFileLocator(IFileLocator locator, string root, Func<Stream, DiscFileSystem> create)
 49        {
 050            if (locator != null)
 51            {
 052                this.fileStream = locator.Open(root);
 53            }
 54            else
 55            {
 056                this.fileStream = File.OpenRead(root);
 57            }
 58
 059            this.disc = create(this.fileStream);
 060        }
 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>
 067        protected DiscFsFileLocator(Stream root, Func<Stream, DiscFileSystem> create)
 68        {
 069            this.disc = create(root);
 070        }
 71
 72        /// <summary>
 73        /// Immediately releases the unmanaged resources used by the <see cref="DiscFsFileLocator"/> object.
 74        /// </summary>
 75        public void Dispose()
 76        {
 077            this.Dispose(true);
 078            GC.SuppressFinalize(this);
 079        }
 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        {
 088            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        {
 098            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        {
 0107            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        {
 0117            root = Utilities.PathNormalize(root);
 118
 0119            if (!this.disc.DirectoryExists(root))
 120            {
 0121                yield break;
 122            }
 123
 0124            foreach (var file in this.disc.GetFiles(root, "*.*", SearchOption.AllDirectories))
 125            {
 0126                yield return Utilities.PathNormalize(file);
 127            }
 0128        }
 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        {
 0136            if (disposing)
 137            {
 0138                if (this.disc != null)
 139                {
 0140                    this.disc.Dispose();
 0141                    this.disc = null;
 142                }
 143
 0144                if (this.fileStream != null)
 145                {
 0146                    this.fileStream.Dispose();
 0147                    this.fileStream = null;
 148                }
 149            }
 0150        }
 151    }
 152}
 153