C# utility class to retry failed file access attempts (including file move), with a specified function.

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.DotNet.Cli.Utils
{
    /// <summary>
    /// File Access Retrier Util.
    /// </summary>
    /// <remarks>
    /// https://github.com/dotnet/cli/blob/master/src/Microsoft.DotNet.Cli.Utils/FileAccessRetryer.cs
    /// </remarks>
    public static class FileAccessRetrier
    {
        public static async Task<T> RetryOnFileAccessFailure<T>(Func<T> func, int maxRetries = 3000, TimeSpan sleepDuration = default(TimeSpan))
        {
            var attemptsLeft = maxRetries;

            if (sleepDuration == default(TimeSpan))
            {
                sleepDuration = TimeSpan.FromMilliseconds(10);
            }

            while (true)
            {
                if (attemptsLeft < 1)
                {
                    throw new InvalidOperationException(LocalizableStrings.CouldNotAccessAssetsFile);
                }

                attemptsLeft--;

                try
                {
                    return func();
                }
                catch (UnauthorizedAccessException)
                {
                    // This can occur when the file is being deleted
                    // Or when an admin user has locked the file
                    await Task.Delay(sleepDuration);

                    continue;
                }
                catch (IOException)
                {
                    await Task.Delay(sleepDuration);

                    continue;
                }
            }
        }

        /// <summary>
        /// Run Directory.Move and File.Move in Windows has a chance to get IOException with
        /// HResult 0x80070005 due to Indexer. But this error is transient.
        /// </summary>
        internal static void RetryOnMoveAccessFailure(Action action)
        {
            const int ERROR_HRESULT_ACCESS_DENIED = unchecked((int)0x80070005);
            int nextWaitTime = 10;
            int remainRetry = 10;

            while (true)
            {
                try
                {
                    action();
                    break;
                }
                catch (IOException e) when (e.HResult == ERROR_HRESULT_ACCESS_DENIED)
                {
                    Thread.Sleep(nextWaitTime);
                    nextWaitTime *= 2;
                    remainRetry--;
                    if (remainRetry == 0)
                    {
                        throw;
                    }
                }
            }
        }
    }
}

//
// Usage:
//
// https://github.com/dotnet/cli/blob/39bcd0282e95f345a20c505661b86df6257c99cb/src/dotnet/ToolPackage/ToolPackageUninstaller.cs#L31

if (Directory.Exists(packageDirectory.Value))
{
    // Use the staging directory for uninstall
    // This prevents cross-device moves when temp is mounted to a different device
    var tempPath = _toolPackageStoreQuery.GetRandomStagingDirectory().Value;
    FileAccessRetrier.RetryOnMoveAccessFailure(() => Directory.Move(packageDirectory.Value, tempPath));
    tempPackageDirectory = tempPath;
}