/// <inheritdoc /> public async Task <IReadOnlyList <ConfigurationFile> > ListDirectory(string configurationRelativePath, ISystemIdentity systemIdentity, CancellationToken cancellationToken) { await EnsureDirectories(cancellationToken).ConfigureAwait(false); var path = ValidateConfigRelativePath(configurationRelativePath); if (configurationRelativePath == null) { configurationRelativePath = "/"; } List <ConfigurationFile> result = new List <ConfigurationFile>(); void ListImpl() { var enumerator = synchronousIOManager.GetDirectories(path, cancellationToken); try { result.AddRange(enumerator.Select(x => new ConfigurationFile { IsDirectory = true, Path = ioManager.ConcatPath(configurationRelativePath, x), }).OrderBy(file => file.Path)); } catch (IOException e) { logger.LogDebug("IOException while writing {0}: {1}", path, e); result = null; return; } enumerator = synchronousIOManager.GetFiles(path, cancellationToken); result.AddRange(enumerator.Select(x => new ConfigurationFile { IsDirectory = false, Path = ioManager.ConcatPath(configurationRelativePath, x), }).OrderBy(file => file.Path)); } using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false)) if (systemIdentity == null) { ListImpl(); } else { await systemIdentity.RunImpersonated(ListImpl, cancellationToken).ConfigureAwait(false); } return(result); }
/// <inheritdoc /> public async Task <bool> CreateDirectory(string configurationRelativePath, ISystemIdentity systemIdentity, CancellationToken cancellationToken) { await EnsureDirectories(cancellationToken).ConfigureAwait(false); var path = ValidateConfigRelativePath(configurationRelativePath); bool?result = null; void DoCreate() => result = synchronousIOManager.CreateDirectory(path, cancellationToken); if (systemIdentity == null) { await Task.Factory.StartNew(DoCreate, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false); } else { await systemIdentity.RunImpersonated(DoCreate, cancellationToken).ConfigureAwait(false); } return(result.Value); }
/// <inheritdoc /> public async Task <bool> DeleteDirectory(string configurationRelativePath, ISystemIdentity systemIdentity, CancellationToken cancellationToken) { await EnsureDirectories(cancellationToken).ConfigureAwait(false); var path = ValidateConfigRelativePath(configurationRelativePath); var result = false; using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false)) { void CheckDeleteImpl() => result = synchronousIOManager.DeleteDirectory(path); if (systemIdentity != null) { await systemIdentity.RunImpersonated(CheckDeleteImpl, cancellationToken).ConfigureAwait(false); } else { CheckDeleteImpl(); } } return(result); }
/// <inheritdoc /> public async Task <ConfigurationFile> Write(string configurationRelativePath, ISystemIdentity systemIdentity, byte[] data, string previousHash, CancellationToken cancellationToken) { await EnsureDirectories(cancellationToken).ConfigureAwait(false); var path = ValidateConfigRelativePath(configurationRelativePath); ConfigurationFile result = null; void WriteImpl() { lock (this) try { var fileHash = previousHash; var success = synchronousIOManager.WriteFileChecked(path, data, ref fileHash, cancellationToken); if (!success) { return; } result = new ConfigurationFile { Content = data, IsDirectory = false, LastReadHash = fileHash, AccessDenied = false, Path = configurationRelativePath }; } catch (UnauthorizedAccessException) { //this happens on windows, dunno about linux bool isDirectory; try { isDirectory = synchronousIOManager.IsDirectory(path); } catch { isDirectory = false; } result = new ConfigurationFile { Path = configurationRelativePath }; if (!isDirectory) { result.AccessDenied = true; } else { result.IsDirectory = true; } } } using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false)) if (systemIdentity == null) { await Task.Factory.StartNew(WriteImpl, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false); } else { await systemIdentity.RunImpersonated(WriteImpl, cancellationToken).ConfigureAwait(false); } return(result); }
/// <inheritdoc /> public async Task <ConfigurationFile> Read(string configurationRelativePath, ISystemIdentity systemIdentity, CancellationToken cancellationToken) { await EnsureDirectories(cancellationToken).ConfigureAwait(false); var path = ValidateConfigRelativePath(configurationRelativePath); ConfigurationFile result = null; void ReadImpl() { lock (this) try { var content = synchronousIOManager.ReadFile(path); string sha1String; #pragma warning disable CA5350 // Do not use insecure cryptographic algorithm SHA1. using (var sha1 = new SHA1Managed()) #pragma warning restore CA5350 // Do not use insecure cryptographic algorithm SHA1. sha1String = String.Join("", sha1.ComputeHash(content).Select(b => b.ToString("x2", CultureInfo.InvariantCulture))); result = new ConfigurationFile { Content = content, IsDirectory = false, LastReadHash = sha1String, AccessDenied = false, Path = configurationRelativePath }; } catch (UnauthorizedAccessException) { //this happens on windows, dunno about linux bool isDirectory; try { isDirectory = synchronousIOManager.IsDirectory(path); } catch { isDirectory = false; } result = new ConfigurationFile { Path = configurationRelativePath }; if (!isDirectory) { result.AccessDenied = true; } else { result.IsDirectory = true; } } } using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false)) if (systemIdentity == null) { await Task.Factory.StartNew(ReadImpl, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false); } else { await systemIdentity.RunImpersonated(ReadImpl, cancellationToken).ConfigureAwait(false); } return(result); }