Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TempFile.cs « Utility « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a1750833ae9647b702edca82909f72cc57400ec1 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#region Disclaimer / License
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;

namespace Duplicati.Library.Utility
{
    /// <summary>
    /// This class represents a temporary file that will be automatically deleted when disposed
    /// </summary>
    public class TempFile : IDisposable 
    {
        /// <summary>
        /// The prefix applied to all temporary files
        /// </summary>
        public static string APPLICATION_PREFIX = Utility.getEntryAssembly().FullName.Substring(0, 3).ToLower() + "-";
        
        private string m_path;
        private bool m_protect;
                
#if DEBUG
        //In debug mode, we track the creation of temporary files, and encode the generating method into the name
        private static object m_lock = new object();
        private static Dictionary<string, System.Diagnostics.StackTrace> m_fileTrace = new Dictionary<string, System.Diagnostics.StackTrace>();
        
        public static System.Diagnostics.StackTrace GetStackTraceForTempFile(string filename)
        {
            lock(m_lock)
                if (m_fileTrace.ContainsKey(filename))
                    return m_fileTrace[filename];
                else
                    return null;
        }
        
        private static string GenerateUniqueName()
        {
            var st = new System.Diagnostics.StackTrace();
            foreach(var f in st.GetFrames())
                if (f.GetMethod().DeclaringType.Assembly != typeof(TempFile).Assembly)
                {
                    var n = string.Format("{0}_{1}_{2}_{3}", f.GetMethod().DeclaringType.FullName, f.GetMethod().Name, Library.Utility.Utility.SerializeDateTime(DateTime.UtcNow), Guid.NewGuid().ToString().Substring(0, 8));
                    if (n.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) >= 0)
                        n = string.Format("{0}_{1}_{2}_{3}", f.GetMethod().DeclaringType.Name, f.GetMethod().Name, Library.Utility.Utility.SerializeDateTime(DateTime.UtcNow), Guid.NewGuid().ToString().Substring(0, 8));
                    if (n.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) < 0)
                    {
                        lock(m_lock)
                            m_fileTrace.Add(n, st);
                        return n;
                    }
                }
                
            var s = Guid.NewGuid().ToString();
            lock(m_lock)
                m_fileTrace.Add(s, st);
            return s;            
        }

#else
        private static string GenerateUniqueName()
        {
            return APPLICATION_PREFIX + Guid.NewGuid().ToString();
        }
#endif
        /// <summary>
        /// The name of the temp file
        /// </summary>
        public string Name => m_path;

        /// <summary>
        /// Gets all temporary files found in the current tempdir, that matches the application prefix
        /// </summary>
        /// <returns>The application temp files.</returns>
        public static IEnumerable<string> GetApplicationTempFiles()
        {
            return System.IO.Directory.GetFiles(TempFolder.SystemTempPath, APPLICATION_PREFIX + "*");
        }
        
        /// <summary>
        /// Attempts to delete all temporary files for this application
        /// </summary>
        public static void RemoveAllApplicationTempFiles()
        {
            foreach(var s in GetApplicationTempFiles())
                try { System.IO.File.Delete(s); }
                catch { }
        }

        /// <summary>
        /// Removes all old temporary files for this application.
        /// </summary>
        /// <param name="errorcallback">An optional callback method for logging errors</param>
        public static void RemoveOldApplicationTempFiles(Action<string, Exception> errorcallback = null)
        {
            var expires = TimeSpan.FromDays(30);
            foreach(var e in GetApplicationTempFiles())
                try
                {
                    if (DateTime.Now > (System.IO.File.GetLastWriteTimeUtc(e) + expires))
                        System.IO.File.Delete(e);
                }
                catch (Exception ex)
                {
                    if (errorcallback != null)
                        errorcallback(e, ex);
                }
        }
        
        public TempFile()
            : this(System.IO.Path.Combine(TempFolder.SystemTempPath, GenerateUniqueName()))
        {
        }

        private TempFile(string path)
        {
            m_path = path;
            m_protect = false;
            if (!System.IO.File.Exists(m_path))
                using (System.IO.File.Create(m_path))
                { /*Dispose it immediately*/ } 
        }

        /// <summary>
        /// A value indicating if the file is protected, meaning that it will not be deleted when the instance is disposed.
        /// Defaults to false, meaning that the file will be deleted when disposed.
        /// </summary>
        public bool Protected
        {
            get { return m_protect; }
            set { m_protect = value; }
        }

        public static implicit operator string(TempFile path)
        {
            return path == null ? null : path.m_path;
        }

        public static implicit operator TempFile(string path)
        {
            return new TempFile(path);
        }
        
        public static TempFile WrapExistingFile(string path)
        {
            return new TempFile(path);
        }

        public static TempFile CreateInFolder(string path, bool autocreatefolder)
        {
            if (autocreatefolder && !System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

            return new TempFile(System.IO.Path.Combine(path, GenerateUniqueName()));
        }

        public static TempFile CreateWithPrefix(string prefix)
        {
            return new TempFile(System.IO.Path.Combine(TempFolder.SystemTempPath, prefix + GenerateUniqueName()));
        }

        protected void Dispose(bool disposing)
        {
            if (disposing)
                GC.SuppressFinalize(this);
                
            try
            {
                if (!m_protect && m_path != null && System.IO.File.Exists(m_path))
                    System.IO.File.Delete(m_path);
                m_path = null;
            }
            catch
            {
            }
        }
        
        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
        }

        #endregion
        
        ~TempFile()
        {
            Dispose(false);
        }

        /// <summary>
        /// Swaps two instances of temporary files, equivalent to renaming the files but requires no IO
        /// </summary>
        /// <param name="tf">The temp file to swap with</param>
        public void Swap(TempFile tf)
        {
            string p = m_path;
            m_path = tf.m_path;
            tf.m_path = p;
        }
    }
}