コード例 #1
0
        public static async Task <PutResult> PutRandomFileAsync(
            this IContentSession session,
            Context context,
            IAbsFileSystem fileSystem,
            HashType hashType,
            bool provideHash,
            long size,
            CancellationToken ct)
        {
            Contract.RequiresNotNull(session);
            Contract.RequiresNotNull(context);
            Contract.RequiresNotNull(fileSystem);

            using (var directory = new DisposableDirectory(fileSystem))
            {
                var c = context.CreateNested();

                // TODO: Fix this to work with size > int.Max (bug 1365340)
                var data = ThreadSafeRandom.GetBytes((int)size);
                var path = directory.CreateRandomFileName();
                fileSystem.WriteAllBytes(path, data);

                if (!provideHash)
                {
                    return(await session.PutFileAsync(c, hashType, path, FileRealizationMode.Any, ct).ConfigureAwait(false));
                }

                var hash = HashInfoLookup.Find(hashType).CreateContentHasher().GetContentHash(data);
                return(await session.PutFileAsync(c, hash, path, FileRealizationMode.Any, ct).ConfigureAwait(false));
            }
        }
コード例 #2
0
        /// <summary>
        ///     Put a randomly-sized content from a file into the store.
        /// </summary>
        public static Task <PutResult> PutRandomFileAsync(
            this IContentSession session,
            Context context,
            IAbsFileSystem fileSystem,
            AbsolutePath path,
            HashType hashType,
            bool provideHash,
            long size,
            CancellationToken ct)
        {
            Contract.RequiresNotNull(session);
            Contract.RequiresNotNull(context);
            Contract.RequiresNotNull(fileSystem);

            var c = context.CreateNested(nameof(ContentSessionExtensions));

            // TODO: Fix this to work with size > int.Max (bug 1365340)
            var data = ThreadSafeRandom.GetBytes((int)size);

            fileSystem.WriteAllBytes(path, data);

            if (!provideHash)
            {
                return(session.PutFileAsync(c, hashType, path, FileRealizationMode.Any, ct));
            }

            var hash = HashInfoLookup.Find(hashType).CreateContentHasher().GetContentHash(data);

            return(session.PutFileAsync(c, hash, path, FileRealizationMode.Any, ct));
        }
コード例 #3
0
 /// <inheritdoc />
 public Task <PutResult> PutFileAsync(
     Context context,
     HashType hashType,
     AbsolutePath path,
     FileRealizationMode realizationMode,
     CancellationToken cts,
     UrgencyHint urgencyHint = UrgencyHint.Nominal)
 {
     return(_sessionForPath.PutFileAsync(context, hashType, path, realizationMode, cts, urgencyHint));
 }
コード例 #4
0
        public async Task <PlaceFileResult> CreateTempAndPutAsync(
            OperationContext context,
            ContentHash contentHash,
            IContentSession contentSession)
        {
            using (var disposableFile = new DisposableFile(context, _fileSystem, AbsolutePath.CreateRandomFileName(_rootPath / "temp")))
            {
                PlaceFileResult placeTempFileResult = await PlaceFileAsync(context, contentHash, disposableFile.Path, FileAccessMode.ReadOnly, FileReplacementMode.FailIfExists, FileRealizationMode.HardLink, context.Token);

                if (!placeTempFileResult.Succeeded)
                {
                    return(placeTempFileResult);
                }
                PutResult putFileResult = await contentSession.PutFileAsync(context, contentHash, disposableFile.Path, FileRealizationMode.Any, context.Token);

                if (!putFileResult)
                {
                    return(new PlaceFileResult(putFileResult));
                }
                else
                {
                    return(new PlaceFileResult(PlaceFileResult.ResultCode.PlacedWithCopy, putFileResult.ContentSize));
                }
            }
        }
コード例 #5
0
        private async Task PutFileAsync(
            IContentSession session, IReadOnlyList <AbsolutePath> paths, List <PutResult> results)
        {
            var tasks = paths.Select(p => Task.Run(async() => await session.PutFileAsync(
                                                       _context, ContentHashType, p, FileRealizationMode.HardLink, CancellationToken.None)));

            foreach (var task in tasks.ToList())
            {
                var result = await task;
                results.Add(result);
            }
        }
コード例 #6
0
        /// <summary>
        ///     Attempt to ensure that all content is in VSTS, uploading any misses from the WriteThroughContentSession.
        /// </summary>
        private async Task <PinResult> UploadAllContentAsync(
            Context context, ContentHash selectorHash, IEnumerable <ContentHash> hashes, CancellationToken cts, UrgencyHint urgencyHint)
        {
            List <ContentHash> contentToUpload = new List <ContentHash>();

            var pinResult = await BackingContentSession.PinAsync(context, selectorHash, cts, urgencyHint).ConfigureAwait(false);

            if (pinResult.Code == PinResult.ResultCode.ContentNotFound)
            {
                contentToUpload.Add(selectorHash);
            }
            else if (!pinResult.Succeeded)
            {
                return(pinResult);
            }

            foreach (var contentHash in hashes)
            {
                pinResult = await BackingContentSession.PinAsync(context, contentHash, cts, urgencyHint).ConfigureAwait(false);

                if (pinResult.Code == PinResult.ResultCode.ContentNotFound)
                {
                    contentToUpload.Add(contentHash);
                }
                else if (!pinResult.Succeeded)
                {
                    return(pinResult);
                }
            }

            foreach (var contentHash in contentToUpload)
            {
                // TODO: Upload the content efficiently (in parallel and with caching of success) (bug 1365340)
                AbsolutePath tempFile = null;
                try
                {
                    tempFile = _tempDirectory.CreateRandomFileName();
                    var placeResult = await WriteThroughContentSession.PlaceFileAsync(
                        context,
                        contentHash,
                        tempFile,
                        FileAccessMode.Write,
                        FileReplacementMode.FailIfExists,
                        FileRealizationMode.Any,
                        CancellationToken.None).ConfigureAwait(false);

                    if (placeResult.Code == PlaceFileResult.ResultCode.NotPlacedContentNotFound)
                    {
                        return(PinResult.ContentNotFound);
                    }
                    else if (!placeResult.Succeeded)
                    {
                        return(new PinResult(placeResult));
                    }

                    var putResult = await BackingContentSession.PutFileAsync(
                        context, contentHash, tempFile, FileRealizationMode.Any, CancellationToken.None).ConfigureAwait(false);

                    if (!putResult.Succeeded)
                    {
                        return(new PinResult(putResult));
                    }
                }
                finally
                {
                    if (tempFile != null)
                    {
                        try
                        {
                            File.Delete(tempFile.Path);
                        }
                        catch (Exception e)
                        {
                            Tracer.Warning(context, $"Error deleting temporary file at {tempFile.Path}: {e}");
                        }
                    }
                }
            }

            return(PinResult.Success);
        }
コード例 #7
0
 /// <inheritdoc />
 protected override Task <PutResult> PutFileCoreAsync(OperationContext operationContext, ContentHash contentHash, AbsolutePath path, FileRealizationMode realizationMode, UrgencyHint urgencyHint, Counter retryCounter)
 {
     return(_innerSession.PutFileAsync(operationContext, contentHash, path, realizationMode, operationContext.Token, urgencyHint));
 }