2024-02-28 15:45:30 +01:00
|
|
|
// Copyright (C) 2024, The Duplicati Team
|
|
|
|
|
// https://duplicati.com, hello@duplicati.com
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
2013-02-12 21:43:14 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-08-31 13:24:21 -04:00
|
|
|
using Duplicati.Library.Common.IO;
|
|
|
|
|
|
2013-02-12 21:43:14 +00:00
|
|
|
namespace Duplicati.Library.Utility
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This class represents a temporary file that will be automatically deleted when disposed
|
|
|
|
|
/// </summary>
|
2019-08-31 13:24:21 -04:00
|
|
|
public class TempFile : IDisposable
|
2013-02-12 21:43:14 +00:00
|
|
|
{
|
2013-09-10 20:44:24 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// The prefix applied to all temporary files
|
|
|
|
|
/// </summary>
|
2018-12-11 21:07:30 -08:00
|
|
|
public static string APPLICATION_PREFIX = Utility.getEntryAssembly().FullName.Substring(0, 3).ToLower(System.Globalization.CultureInfo.InvariantCulture) + "-";
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2013-02-12 21:43:14 +00:00
|
|
|
private string m_path;
|
|
|
|
|
private bool m_protect;
|
2018-01-20 15:02:41 +01:00
|
|
|
|
2013-04-16 22:36:19 +02:00
|
|
|
#if DEBUG
|
2016-09-15 11:39:27 +02:00
|
|
|
//In debug mode, we track the creation of temporary files, and encode the generating method into the name
|
2018-03-17 19:59:23 -07:00
|
|
|
private static readonly object m_lock = new object();
|
2019-10-19 10:56:21 -07:00
|
|
|
private static readonly Dictionary<string, System.Diagnostics.StackTrace> m_fileTrace = new Dictionary<string, System.Diagnostics.StackTrace>();
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2016-09-15 11:39:27 +02:00
|
|
|
public static System.Diagnostics.StackTrace GetStackTraceForTempFile(string filename)
|
|
|
|
|
{
|
2019-08-31 13:24:21 -04:00
|
|
|
lock (m_lock)
|
2016-09-15 11:39:27 +02:00
|
|
|
if (m_fileTrace.ContainsKey(filename))
|
|
|
|
|
return m_fileTrace[filename];
|
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2013-04-16 22:36:19 +02:00
|
|
|
private static string GenerateUniqueName()
|
|
|
|
|
{
|
2016-09-15 11:39:27 +02:00
|
|
|
var st = new System.Diagnostics.StackTrace();
|
2019-08-31 13:24:21 -04:00
|
|
|
foreach (var f in st.GetFrames())
|
2016-09-15 11:39:27 +02:00
|
|
|
if (f.GetMethod().DeclaringType.Assembly != typeof(TempFile).Assembly)
|
|
|
|
|
{
|
2015-09-08 16:41:16 +02:00
|
|
|
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));
|
2016-09-15 11:39:27 +02:00
|
|
|
if (n.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) >= 0)
|
2015-09-08 16:41:16 +02:00
|
|
|
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));
|
2016-09-15 11:39:27 +02:00
|
|
|
if (n.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) < 0)
|
|
|
|
|
{
|
2019-08-31 13:24:21 -04:00
|
|
|
lock (m_lock)
|
2016-09-15 11:39:27 +02:00
|
|
|
m_fileTrace.Add(n, st);
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2016-09-15 11:39:27 +02:00
|
|
|
var s = Guid.NewGuid().ToString();
|
2019-08-31 13:24:21 -04:00
|
|
|
lock (m_lock)
|
2016-09-15 11:39:27 +02:00
|
|
|
m_fileTrace.Add(s, st);
|
2019-08-31 13:24:21 -04:00
|
|
|
return s;
|
2013-04-16 22:36:19 +02:00
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
private static string GenerateUniqueName()
|
|
|
|
|
{
|
2016-09-15 11:39:27 +02:00
|
|
|
return APPLICATION_PREFIX + Guid.NewGuid().ToString();
|
2013-04-16 22:36:19 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
2017-12-25 04:12:19 +07:00
|
|
|
/// <summary>
|
|
|
|
|
/// The name of the temp file
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Name => m_path;
|
2013-09-10 20:44:24 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all temporary files found in the current tempdir, that matches the application prefix
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The application temp files.</returns>
|
2017-08-13 11:58:39 +01:00
|
|
|
private static IEnumerable<string> GetApplicationTempFiles()
|
2013-09-10 20:44:24 +02:00
|
|
|
{
|
2019-08-31 13:24:21 -04:00
|
|
|
#if DEBUG
|
|
|
|
|
return SystemIO.IO_OS.GetFiles(TempFolder.SystemTempPath, "Duplicati*");
|
|
|
|
|
#else
|
2018-11-02 21:34:07 +01:00
|
|
|
return SystemIO.IO_OS.GetFiles(TempFolder.SystemTempPath, APPLICATION_PREFIX + "*");
|
2019-08-31 13:24:21 -04:00
|
|
|
#endif
|
2013-09-10 20:44:24 +02:00
|
|
|
}
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2014-08-06 10:30:46 +02:00
|
|
|
/// <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)
|
|
|
|
|
{
|
2019-08-31 13:24:21 -04:00
|
|
|
#if DEBUG
|
|
|
|
|
var expires = TimeSpan.FromHours(3);
|
|
|
|
|
#else
|
2014-08-17 22:54:14 +02:00
|
|
|
var expires = TimeSpan.FromDays(30);
|
2019-08-31 13:24:21 -04:00
|
|
|
#endif
|
|
|
|
|
foreach (string e in GetApplicationTempFiles())
|
2019-08-31 12:39:09 -04:00
|
|
|
{
|
2014-08-06 10:30:46 +02:00
|
|
|
try
|
|
|
|
|
{
|
2019-08-31 12:38:16 -04:00
|
|
|
if (DateTime.UtcNow > (System.IO.File.GetLastWriteTimeUtc(e) + expires))
|
2019-08-31 12:39:09 -04:00
|
|
|
{
|
2014-08-06 10:30:46 +02:00
|
|
|
System.IO.File.Delete(e);
|
2019-08-31 12:39:09 -04:00
|
|
|
}
|
2014-08-06 10:30:46 +02:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-08-31 12:39:09 -04:00
|
|
|
errorcallback?.Invoke(e, ex);
|
2014-08-06 10:30:46 +02:00
|
|
|
}
|
2019-08-31 12:39:09 -04:00
|
|
|
}
|
2014-08-06 10:30:46 +02:00
|
|
|
}
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2013-02-12 21:43:14 +00:00
|
|
|
public TempFile()
|
2013-04-16 22:36:19 +02:00
|
|
|
: this(System.IO.Path.Combine(TempFolder.SystemTempPath, GenerateUniqueName()))
|
2013-02-12 21:43:14 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-16 22:36:19 +02:00
|
|
|
private TempFile(string path)
|
2013-02-12 21:43:14 +00:00
|
|
|
{
|
|
|
|
|
m_path = path;
|
|
|
|
|
m_protect = false;
|
|
|
|
|
if (!System.IO.File.Exists(m_path))
|
|
|
|
|
using (System.IO.File.Create(m_path))
|
2019-08-31 13:24:21 -04:00
|
|
|
{ /*Dispose it immediately*/ }
|
2013-02-12 21:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
}
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2013-04-16 22:36:19 +02:00
|
|
|
public static TempFile WrapExistingFile(string path)
|
|
|
|
|
{
|
2016-09-15 11:39:27 +02:00
|
|
|
return new TempFile(path);
|
2013-04-16 22:36:19 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-26 09:22:21 +01:00
|
|
|
public static TempFile CreateInFolder(string path, bool autocreatefolder)
|
2013-04-16 22:36:19 +02:00
|
|
|
{
|
2018-01-26 09:22:21 +01:00
|
|
|
if (autocreatefolder && !System.IO.Directory.Exists(path))
|
|
|
|
|
System.IO.Directory.CreateDirectory(path);
|
|
|
|
|
|
2016-09-15 11:39:27 +02:00
|
|
|
return new TempFile(System.IO.Path.Combine(path, GenerateUniqueName()));
|
2013-04-16 22:36:19 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-28 10:36:50 +02:00
|
|
|
public static TempFile CreateWithPrefix(string prefix)
|
2013-04-16 22:36:19 +02:00
|
|
|
{
|
2016-09-15 11:39:27 +02:00
|
|
|
return new TempFile(System.IO.Path.Combine(TempFolder.SystemTempPath, prefix + GenerateUniqueName()));
|
2013-04-16 22:36:19 +02:00
|
|
|
}
|
2013-02-12 21:43:14 +00:00
|
|
|
|
2013-07-10 14:51:25 +02:00
|
|
|
protected void Dispose(bool disposing)
|
2013-02-12 21:43:14 +00:00
|
|
|
{
|
2013-07-10 14:51:25 +02:00
|
|
|
if (disposing)
|
|
|
|
|
GC.SuppressFinalize(this);
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2013-02-12 21:43:14 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!m_protect && m_path != null && System.IO.File.Exists(m_path))
|
|
|
|
|
System.IO.File.Delete(m_path);
|
|
|
|
|
m_path = null;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2013-07-10 14:51:25 +02:00
|
|
|
#region IDisposable Members
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
2013-02-12 21:43:14 +00:00
|
|
|
|
|
|
|
|
#endregion
|
2019-08-31 13:24:21 -04:00
|
|
|
|
2013-07-10 14:51:25 +02:00
|
|
|
~TempFile()
|
|
|
|
|
{
|
|
|
|
|
Dispose(false);
|
|
|
|
|
}
|
2013-02-12 21:43:14 +00:00
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|