aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/IO/FileSystem.cs
blob: f31fc53de52f9e5ed894e5940c6b44486e336bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using MediaBrowser.Model.Logging;
using System;
using System.IO;

namespace MediaBrowser.Controller.IO
{
    /// <summary>
    /// Class FileSystem
    /// </summary>
    public static class FileSystem
    {
        /// <summary>
        /// Gets the creation time UTC.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="logger">The logger.</param>
        /// <returns>DateTime.</returns>
        public static DateTime GetLastWriteTimeUtc(FileSystemInfo info, ILogger logger)
        {
            // This could throw an error on some file systems that have dates out of range

            try
            {
                return info.LastWriteTimeUtc;
            }
            catch (Exception ex)
            {
                logger.ErrorException("Error determining LastAccessTimeUtc for {0}", ex, info.FullName);
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// Gets the creation time UTC.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="logger">The logger.</param>
        /// <returns>DateTime.</returns>
        public static DateTime GetCreationTimeUtc(FileSystemInfo info, ILogger logger)
        {
            // This could throw an error on some file systems that have dates out of range

            try
            {
                return info.CreationTimeUtc;
            }
            catch (Exception ex)
            {
                logger.ErrorException("Error determining CreationTimeUtc for {0}", ex, info.FullName);
                return DateTime.MinValue;
            }
        }

        /// <summary>
        /// Copies all.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <exception cref="System.ArgumentNullException">source</exception>
        /// <exception cref="System.ArgumentException">The source and target directories are the same</exception>
        public static void CopyAll(string source, string target)
        {
            if (string.IsNullOrEmpty(source))
            {
                throw new ArgumentNullException("source");
            }
            if (string.IsNullOrEmpty(target))
            {
                throw new ArgumentNullException("target");
            }

            if (source.Equals(target, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException("The source and target directories are the same");
            }

            // Check if the target directory exists, if not, create it. 
            Directory.CreateDirectory(target);

            foreach (var file in Directory.EnumerateFiles(source))
            {
                File.Copy(file, Path.Combine(target, Path.GetFileName(file)), true);
            }

            // Copy each subdirectory using recursion. 
            foreach (var dir in Directory.EnumerateDirectories(source))
            {
                CopyAll(dir, Path.Combine(target, Path.GetFileName(dir)));
            }
        }
    }
}