2016-09-22 08:33:11 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
2020-09-17 11:35:29 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
|
using Umbraco.Extensions;
|
2016-09-22 08:33:11 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Core.IO
|
2016-09-22 08:33:11 +02:00
|
|
|
|
{
|
2016-11-04 18:40:42 +01:00
|
|
|
|
internal class ShadowWrapper : IFileSystem
|
2016-09-22 08:33:11 +02:00
|
|
|
|
{
|
2019-11-13 11:26:03 +01:00
|
|
|
|
private static readonly string ShadowFsPath = Constants.SystemDirectories.TempData.EnsureEndsWith('/') + "ShadowFs";
|
2017-05-30 18:43:24 +02:00
|
|
|
|
|
|
|
|
|
|
private readonly Func<bool> _isScoped;
|
2016-09-22 08:33:11 +02:00
|
|
|
|
private readonly IFileSystem _innerFileSystem;
|
|
|
|
|
|
private readonly string _shadowPath;
|
|
|
|
|
|
private ShadowFileSystem _shadowFileSystem;
|
|
|
|
|
|
private string _shadowDir;
|
2019-11-19 07:52:40 +01:00
|
|
|
|
private readonly IIOHelper _ioHelper;
|
2020-06-05 11:36:59 +02:00
|
|
|
|
private readonly IHostingEnvironment _hostingEnvironment;
|
2020-09-17 11:35:29 +02:00
|
|
|
|
private readonly ILoggerFactory _loggerFactory;
|
2016-09-22 08:33:11 +02:00
|
|
|
|
|
2020-09-17 11:35:29 +02:00
|
|
|
|
public ShadowWrapper(IFileSystem innerFileSystem, IIOHelper ioHelper, IHostingEnvironment hostingEnvironment, ILoggerFactory loggerFactory, string shadowPath, Func<bool> isScoped = null)
|
2016-09-22 08:33:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
_innerFileSystem = innerFileSystem;
|
2019-11-19 07:52:40 +01:00
|
|
|
|
_ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper));
|
2020-06-05 11:36:59 +02:00
|
|
|
|
_hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
|
2020-09-17 11:35:29 +02:00
|
|
|
|
_loggerFactory = loggerFactory;
|
2016-09-22 08:33:11 +02:00
|
|
|
|
_shadowPath = shadowPath;
|
2017-05-31 15:25:24 +02:00
|
|
|
|
_isScoped = isScoped;
|
2016-09-22 08:33:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-18 07:25:11 -08:00
|
|
|
|
public static string CreateShadowId(IHostingEnvironment hostingEnvironment)
|
2019-02-20 14:10:15 +01:00
|
|
|
|
{
|
|
|
|
|
|
const int retries = 50; // avoid infinite loop
|
2019-02-21 11:28:51 +01:00
|
|
|
|
const int idLength = 8; // 6 chars
|
2019-02-20 14:10:15 +01:00
|
|
|
|
|
|
|
|
|
|
// shorten a Guid to idLength chars, and see whether it collides
|
|
|
|
|
|
// with an existing directory or not - if it does, try again, and
|
|
|
|
|
|
// we should end up with a unique identifier eventually - but just
|
|
|
|
|
|
// detect infinite loops (just in case)
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < retries; i++)
|
|
|
|
|
|
{
|
2019-02-21 11:28:51 +01:00
|
|
|
|
var id = GuidUtils.ToBase32String(Guid.NewGuid(), idLength);
|
2019-02-20 14:10:15 +01:00
|
|
|
|
|
|
|
|
|
|
var virt = ShadowFsPath + "/" + id;
|
2020-11-18 07:25:11 -08:00
|
|
|
|
var shadowDir = hostingEnvironment.MapPathContentRoot(virt);
|
2019-02-20 14:10:15 +01:00
|
|
|
|
if (Directory.Exists(shadowDir))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(shadowDir);
|
|
|
|
|
|
return id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new Exception($"Could not get a shadow identifier (tried {retries} times)");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void Shadow(string id)
|
2016-09-22 08:33:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
// note: no thread-safety here, because ShadowFs is thread-safe due to the check
|
|
|
|
|
|
// on ShadowFileSystemsScope.None - and if None is false then we should be running
|
|
|
|
|
|
// in a single thread anyways
|
|
|
|
|
|
|
2020-11-26 14:16:24 +01:00
|
|
|
|
var virt = Path.Combine(ShadowFsPath , id , _shadowPath);
|
2020-11-18 07:25:11 -08:00
|
|
|
|
_shadowDir = _hostingEnvironment.MapPathContentRoot(virt);
|
2016-09-22 08:33:11 +02:00
|
|
|
|
Directory.CreateDirectory(_shadowDir);
|
2020-11-26 14:16:24 +01:00
|
|
|
|
var tempfs = new PhysicalFileSystem(_ioHelper, _hostingEnvironment, _loggerFactory.CreateLogger<PhysicalFileSystem>(), _shadowDir, _hostingEnvironment.ToAbsolute(virt));
|
2016-09-22 08:33:11 +02:00
|
|
|
|
_shadowFileSystem = new ShadowFileSystem(_innerFileSystem, tempfs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void UnShadow(bool complete)
|
|
|
|
|
|
{
|
|
|
|
|
|
var shadowFileSystem = _shadowFileSystem;
|
|
|
|
|
|
var dir = _shadowDir;
|
|
|
|
|
|
_shadowFileSystem = null;
|
|
|
|
|
|
_shadowDir = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// this may throw an AggregateException if some of the changes could not be applied
|
|
|
|
|
|
if (complete) shadowFileSystem.Complete();
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
// in any case, cleanup
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.Delete(dir, true);
|
2017-05-30 10:50:09 +02:00
|
|
|
|
|
|
|
|
|
|
// shadowPath make be path/to/dir, remove each
|
2021-01-11 09:04:05 +01:00
|
|
|
|
dir = dir.Replace('/', Path.DirectorySeparatorChar);
|
2020-11-18 07:25:11 -08:00
|
|
|
|
var min = _hostingEnvironment.MapPathContentRoot(ShadowFsPath).Length;
|
2021-01-11 09:04:05 +01:00
|
|
|
|
var pos = dir.LastIndexOf(Path.DirectorySeparatorChar);
|
2017-05-30 10:50:09 +02:00
|
|
|
|
while (pos > min)
|
|
|
|
|
|
{
|
|
|
|
|
|
dir = dir.Substring(0, pos);
|
|
|
|
|
|
if (Directory.EnumerateFileSystemEntries(dir).Any() == false)
|
|
|
|
|
|
Directory.Delete(dir, true);
|
|
|
|
|
|
else
|
|
|
|
|
|
break;
|
2021-01-11 09:04:05 +01:00
|
|
|
|
pos = dir.LastIndexOf(Path.DirectorySeparatorChar);
|
2017-05-30 10:50:09 +02:00
|
|
|
|
}
|
2016-09-22 08:33:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// ugly, isn't it? but if we cannot cleanup, bah, just leave it there
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-08 12:52:16 +01:00
|
|
|
|
public IFileSystem InnerFileSystem => _innerFileSystem;
|
|
|
|
|
|
|
2016-09-22 08:33:11 +02:00
|
|
|
|
private IFileSystem FileSystem
|
|
|
|
|
|
{
|
2017-05-12 14:49:44 +02:00
|
|
|
|
get
|
|
|
|
|
|
{
|
2017-05-30 18:43:24 +02:00
|
|
|
|
var isScoped = _isScoped();
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
|
|
// if the filesystem is created *after* shadowing starts, it won't be shadowing
|
|
|
|
|
|
// better not ignore that situation and raised a meaningful (?) exception
|
|
|
|
|
|
if (isScoped && _shadowFileSystem == null)
|
|
|
|
|
|
throw new Exception("The filesystems are shadowing, but this filesystem is not.");
|
|
|
|
|
|
|
|
|
|
|
|
return isScoped
|
|
|
|
|
|
? _shadowFileSystem
|
|
|
|
|
|
: _innerFileSystem;
|
|
|
|
|
|
}
|
2016-09-22 08:33:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetDirectories(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetDirectories(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteDirectory(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileSystem.DeleteDirectory(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteDirectory(string path, bool recursive)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileSystem.DeleteDirectory(path, recursive);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool DirectoryExists(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.DirectoryExists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddFile(string path, Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileSystem.AddFile(path, stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddFile(string path, Stream stream, bool overrideExisting)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileSystem.AddFile(path, stream, overrideExisting);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetFiles(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetFiles(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetFiles(string path, string filter)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetFiles(path, filter);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Stream OpenFile(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.OpenFile(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DeleteFile(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileSystem.DeleteFile(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool FileExists(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.FileExists(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetRelativePath(string fullPathOrUrl)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetRelativePath(fullPathOrUrl);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetFullPath(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetFullPath(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetUrl(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetUrl(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset GetLastModified(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetLastModified(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset GetCreated(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
return FileSystem.GetCreated(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public long GetSize(string path)
|
|
|
|
|
|
{
|
2016-11-03 10:39:21 +01:00
|
|
|
|
return FileSystem.GetSize(path);
|
2016-09-22 08:33:11 +02:00
|
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
|
|
public bool CanAddPhysical => FileSystem.CanAddPhysical;
|
|
|
|
|
|
|
|
|
|
|
|
public void AddFile(string path, string physicalPath, bool overrideIfExists = true, bool copy = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
FileSystem.AddFile(path, physicalPath, overrideIfExists, copy);
|
|
|
|
|
|
}
|
2016-09-22 08:33:11 +02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|