< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 72
Coverable lines: 72
Total lines: 261
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 36
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\projects\jeremyansel-io-locator\JeremyAnsel.IO.Locator\JeremyAnsel.IO.Locator\SystemWritableFileLocator.cs

#LineLine coverage
 1// <copyright file="SystemWritableFileLocator.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.Locator
 9{
 10    using System;
 11    using System.IO;
 12
 13    /// <summary>
 14    /// A writable file system file locator.
 15    /// </summary>
 16    internal sealed class SystemWritableFileLocator : IWritableFileLocator
 17    {
 18        /// <summary>
 19        /// The root path.
 20        /// </summary>
 21        private string rootPath;
 22
 23        /// <summary>
 24        /// Initializes a new instance of the <see cref="SystemWritableFileLocator"/> class.
 25        /// </summary>
 26        /// <param name="root">The root path.</param>
 027        public SystemWritableFileLocator(string root)
 28        {
 029            if (!Directory.Exists(root))
 30            {
 031                throw new DirectoryNotFoundException();
 32            }
 33
 034            this.rootPath = root;
 035        }
 36
 37        /// <summary>
 38        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveWritableFileLocator"/> object.
 39        /// </summary>
 40        public void Dispose()
 41        {
 042            GC.SuppressFinalize(this);
 043        }
 44
 45        /// <summary>
 46        /// Create a file.
 47        /// </summary>
 48        /// <param name="path">A path.</param>
 49        public void Create(string path)
 50        {
 051            if (string.IsNullOrEmpty(path))
 52            {
 053                throw new ArgumentNullException(nameof(path));
 54            }
 55
 056            string fullPath = Utilities.PathNormalize(Path.Combine(this.rootPath, path));
 57
 058            if (Directory.Exists(fullPath) || File.Exists(fullPath))
 59            {
 060                throw new ArgumentOutOfRangeException(nameof(path));
 61            }
 62
 063            string dirPath = Path.GetDirectoryName(fullPath);
 64
 065            if (!Directory.Exists(dirPath))
 66            {
 067                Directory.CreateDirectory(dirPath);
 68            }
 69
 070            using (File.Create(fullPath))
 71            {
 072            }
 073        }
 74
 75        /// <summary>
 76        /// Write a file.
 77        /// </summary>
 78        /// <param name="path">A path.</param>
 79        /// <param name="data">The data.</param>
 80        public void Write(string path, Stream data)
 81        {
 082            if (string.IsNullOrEmpty(path))
 83            {
 084                throw new ArgumentNullException(nameof(path));
 85            }
 86
 087            if (data == null)
 88            {
 089                throw new ArgumentNullException(nameof(data));
 90            }
 91
 092            this.Create(path);
 93
 094            using (var file = File.OpenWrite(path))
 95            {
 096                data.CopyTo(file);
 097            }
 098        }
 99
 100        /// <summary>
 101        /// Write the files from a file locator.
 102        /// </summary>
 103        /// <param name="locator">A file locator.</param>
 104        public void WriteAll(IFileLocator locator)
 105        {
 0106            this.WriteAll(locator, string.Empty);
 0107        }
 108
 109        /// <summary>
 110        /// Write the files from a file locator.
 111        /// </summary>
 112        /// <param name="locator">A file locator.</param>
 113        /// <param name="root">The root path.</param>
 114        public void WriteAll(IFileLocator locator, string root)
 115        {
 0116            if (locator == null)
 117            {
 0118                throw new ArgumentNullException(nameof(locator));
 119            }
 120
 0121            foreach (var file in locator.EnumerateFiles(root))
 122            {
 0123                using (var stream = locator.Open(file))
 124                {
 0125                    this.Write(file, stream);
 0126                }
 127            }
 0128        }
 129    }
 130}

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

#LineLine coverage
 1// <copyright file="SystemWritableFileLocator.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.Locator
 9{
 10    using System;
 11    using System.IO;
 12
 13    /// <summary>
 14    /// A writable file system file locator.
 15    /// </summary>
 16    internal sealed class SystemWritableFileLocator : IWritableFileLocator
 17    {
 18        /// <summary>
 19        /// The root path.
 20        /// </summary>
 21        private string rootPath;
 22
 23        /// <summary>
 24        /// Initializes a new instance of the <see cref="SystemWritableFileLocator"/> class.
 25        /// </summary>
 26        /// <param name="root">The root path.</param>
 027        public SystemWritableFileLocator(string root)
 28        {
 029            if (!Directory.Exists(root))
 30            {
 031                throw new DirectoryNotFoundException();
 32            }
 33
 034            this.rootPath = root;
 035        }
 36
 37        /// <summary>
 38        /// Immediately releases the unmanaged resources used by the <see cref="ArchiveWritableFileLocator"/> object.
 39        /// </summary>
 40        public void Dispose()
 41        {
 042            GC.SuppressFinalize(this);
 043        }
 44
 45        /// <summary>
 46        /// Create a file.
 47        /// </summary>
 48        /// <param name="path">A path.</param>
 49        public void Create(string path)
 50        {
 051            if (string.IsNullOrEmpty(path))
 52            {
 053                throw new ArgumentNullException(nameof(path));
 54            }
 55
 056            string fullPath = Utilities.PathNormalize(Path.Combine(this.rootPath, path));
 57
 058            if (Directory.Exists(fullPath) || File.Exists(fullPath))
 59            {
 060                throw new ArgumentOutOfRangeException(nameof(path));
 61            }
 62
 063            string dirPath = Path.GetDirectoryName(fullPath);
 64
 065            if (!Directory.Exists(dirPath))
 66            {
 067                Directory.CreateDirectory(dirPath);
 68            }
 69
 070            using (File.Create(fullPath))
 71            {
 072            }
 073        }
 74
 75        /// <summary>
 76        /// Write a file.
 77        /// </summary>
 78        /// <param name="path">A path.</param>
 79        /// <param name="data">The data.</param>
 80        public void Write(string path, Stream data)
 81        {
 082            if (string.IsNullOrEmpty(path))
 83            {
 084                throw new ArgumentNullException(nameof(path));
 85            }
 86
 087            if (data == null)
 88            {
 089                throw new ArgumentNullException(nameof(data));
 90            }
 91
 092            this.Create(path);
 93
 094            using (var file = File.OpenWrite(path))
 95            {
 096                data.CopyTo(file);
 097            }
 098        }
 99
 100        /// <summary>
 101        /// Write the files from a file locator.
 102        /// </summary>
 103        /// <param name="locator">A file locator.</param>
 104        public void WriteAll(IFileLocator locator)
 105        {
 0106            this.WriteAll(locator, string.Empty);
 0107        }
 108
 109        /// <summary>
 110        /// Write the files from a file locator.
 111        /// </summary>
 112        /// <param name="locator">A file locator.</param>
 113        /// <param name="root">The root path.</param>
 114        public void WriteAll(IFileLocator locator, string root)
 115        {
 0116            if (locator == null)
 117            {
 0118                throw new ArgumentNullException(nameof(locator));
 119            }
 120
 0121            foreach (var file in locator.EnumerateFiles(root))
 122            {
 0123                using (var stream = locator.Open(file))
 124                {
 0125                    this.Write(file, stream);
 0126                }
 127            }
 0128        }
 129    }
 130}
 131