Пример #1
0
        public async Task <CopyFileResult> CopyFileAsync(AbsolutePath path, AbsolutePath destinationPath, long contentSize, bool overwrite, CancellationToken cancellationToken)
        {
            FilesCopied.AddOrUpdate(destinationPath, p => path, (dest, prevPath) => overwrite ? path : prevPath);

            if (!File.Exists(path.Path))
            {
                return(new CopyFileResult(CopyFileResult.ResultCode.SourcePathError, $"Source file {path} doesn't exist."));
            }

            if (File.Exists(destinationPath.Path))
            {
                if (!overwrite)
                {
                    return(new CopyFileResult(
                               CopyFileResult.ResultCode.DestinationPathError,
                               $"Destination file {destinationPath} exists but overwrite not specified."));
                }
            }

            if (FilesToCorrupt.ContainsKey(path))
            {
                TestGlobal.Logger.Debug($"Corrupting file {path}");
#pragma warning disable AsyncFixer02 // WriteAllBytesAsync should be used instead of File.WriteAllBytes
                await Task.Run(
                    () => File.WriteAllBytes(destinationPath.Path, ThreadSafeRandom.GetBytes(150)));

#pragma warning restore AsyncFixer02 // WriteAllBytesAsync should be used instead of File.WriteAllBytes
            }
            else
            {
                await Task.Run(() => File.Copy(path.Path, destinationPath.Path), cancellationToken);
            }

            return(CopyFileResult.SuccessWithSize(new System.IO.FileInfo(destinationPath.Path).Length));
        }
Пример #2
0
        public async Task <CopyFileResult> CopyToAsync(AbsolutePath sourcePath, Stream destinationStream, long expectedContentSize, CancellationToken cancellationToken)
        {
            long startPosition = destinationStream.Position;

            FilesCopied.AddOrUpdate(sourcePath, p => sourcePath, (dest, prevPath) => prevPath);

            if (!File.Exists(sourcePath.Path))
            {
                return(new CopyFileResult(CopyFileResult.ResultCode.SourcePathError, $"Source file {sourcePath} doesn't exist."));
            }

            Stream s;

            if (FilesToCorrupt.ContainsKey(sourcePath))
            {
                TestGlobal.Logger.Debug($"Corrupting file {sourcePath}");
                s = new MemoryStream(ThreadSafeRandom.GetBytes((int)expectedContentSize));
            }
            else
            {
                s = File.OpenRead(sourcePath.Path);
            }

            return(await s.CopyToAsync(destinationStream).ContinueWith((_) => CopyFileResult.SuccessWithSize(destinationStream.Position - startPosition)));
        }
Пример #3
0
        private Stream GetStream(AbsolutePath sourcePath, long expectedContentSize)
        {
            Stream s;

            if (FilesToCorrupt.ContainsKey(sourcePath))
            {
                TestGlobal.Logger.Debug($"Corrupting file {sourcePath}");
                s = new MemoryStream(ThreadSafeRandom.GetBytes((int)expectedContentSize));
            }
            else
            {
                s = File.OpenRead(sourcePath.Path);
            }

            return(s);
        }
Пример #4
0
        private async Task <Stream> GetStreamAsync(AbsolutePath sourcePath)
        {
            Stream s;

            if (FilesToCorrupt.ContainsKey(sourcePath))
            {
                TestGlobal.Logger.Debug($"Corrupting file {sourcePath}");
                s = new MemoryStream(ThreadSafeRandom.GetBytes(100));
            }
            else
            {
                s = await _fileSystem.OpenReadOnlySafeAsync(sourcePath, FileShare.Read);
            }

            return(s);
        }