Пример #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>
        ///     Load value from the filesystem.
        /// </summary>
        public static Guid Load(IAbsFileSystem fileSystem, AbsolutePath filePath)
        {
            Guid guid;

            try
            {
                // First try reading the GUID file
                guid = Read(fileSystem, filePath);
            }
            catch (Exception e) when(e is CacheException || e is IOException)
            {
                // If that fails, we likely need to create a Guid
                guid = CacheDeterminism.NewCacheGuid();
                try
                {
                    fileSystem.CreateDirectory(filePath.Parent);

                    // Write the Guid file
                    fileSystem.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(guid.ToString(SerializationFormat)));
                }
                catch (Exception ex) when(ex is IOException)
                {
                    // If we failed to write the Guid file we may have just missed getting the guid in the first place,
                    // so let us try to read it again.  This failure we let go all the way out.
                    guid = Read(fileSystem, filePath);
                }
            }

            return(guid);
        }
Пример #3
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));
        }
Пример #4
0
        public async Task PlaceFileExistingReplaces()
        {
            using (var placeDirectory = new DisposableDirectory(FileSystem))
            {
                var path = placeDirectory.Path / "file.dat";
                FileSystem.WriteAllBytes(path, new byte[0]);

                await RunTestAsync(ImplicitPin.None, null, async (context, session) =>
                {
                    var putResult = await session.PutRandomAsync(
                        context, ContentHashType, false, ContentByteCount, Token).ShouldBeSuccess();
                    var result = await session.PlaceFileAsync(
                        context,
                        putResult.ContentHash,
                        path,
                        FileAccessMode.ReadOnly,
                        FileReplacementMode.ReplaceExisting,
                        FileRealizationMode.Any,
                        Token).ShouldBeSuccess();
                    Assert.True(result.IsPlaced());
                });
            }
        }
Пример #5
0
        /// <summary>
        ///     Writes the given value number to the marker file.
        /// </summary>
        /// <param name="newValue">Value to write into the marker file.</param>
        public void WriteValueFile(int newValue)
        {
            Contract.Requires(newValue >= 0); // Allow writes of value 0 for tests

            _fileSystem.WriteAllBytes(_valueFilePath, BitConverter.GetBytes(newValue));
        }