/// <summary>
        /// Open the named file asynchronously for reading.
        /// </summary>
        /// <exception cref="FileNotFoundException">Throws if the file is not found.</exception>
        /// <remarks>
        /// Unlike <see cref="IReadableFileSystem{T}.OpenReadOnlyAsync(T, FileShare)"/> this function throws an exception if the file is missing.
        /// </remarks>
        public static async Task <StreamWithLength> OpenReadOnlySafeAsync(this IAbsFileSystem fileSystem, AbsolutePath path, FileShare share)
        {
            var stream = await fileSystem.OpenReadOnlyAsync(path, share);

            if (stream == null)
            {
                throw new FileNotFoundException($"The file '{path}' does not exist.");
            }

            return(stream.Value);
        }
Esempio n. 2
0
        /// <inheritdoc />
        public async Task <OpenStreamResult> OpenStreamAsync(Context context, ContentHash contentHash)
        {
            ObjectResult <SessionData> result = await _sessionState.GetDataAsync();

            if (!result.Succeeded)
            {
                return(new OpenStreamResult(result));
            }

            int          sessionId = result.Data.SessionId;
            AbsolutePath tempPath  = result.Data.TemporaryDirectory.CreateRandomFileName();

            var placeFileResult = await PlaceFileAsync(
                context,
                contentHash,
                tempPath,
                FileAccessMode.ReadOnly,
                FileReplacementMode.None,
                FileRealizationMode.HardLink,
                sessionId);

            if (placeFileResult.Succeeded)
            {
                try
                {
                    Stream stream = await _fileSystem.OpenReadOnlyAsync(tempPath, FileShare.Delete | FileShare.Read);

                    if (stream == null)
                    {
                        throw new ClientCanRetryException(context, $"Failed to open temp file {tempPath}. The service may have restarted");
                    }

                    return(new OpenStreamResult(stream));
                }
                catch (Exception ex) when(ex is DirectoryNotFoundException || ex is UnauthorizedAccessException)
                {
                    throw new ClientCanRetryException(context, $"Failed to open temp file {tempPath}. The service may be restarting", ex);
                }
                catch (Exception ex) when(!(ex is ClientCanRetryException))
                {
                    // The caller's retry policy needs to see ClientCanRetryExceptions in order to properly retry
                    return(new OpenStreamResult(ex));
                }
            }
            else if (placeFileResult.Code == PlaceFileResult.ResultCode.NotPlacedContentNotFound)
            {
                return(new OpenStreamResult(OpenStreamResult.ResultCode.ContentNotFound, placeFileResult.ErrorMessage));
            }
            else
            {
                return(new OpenStreamResult(placeFileResult));
            }
        }