/// <summary>
        ///     Check if a valid content store configuration is present in a CAS root directory.
        /// </summary>
        /// <summary>
        ///     Deserialize a ContentStoreConfiguration from JSON in the standard filename in a CAS root directory.
        /// </summary>
        public static Result <ContentStoreConfiguration> ReadContentStoreConfiguration(this IAbsFileSystem fileSystem, AbsolutePath rootPath)
        {
            Contract.Requires(rootPath != null);

            AbsolutePath jsonPath = rootPath / FileName;
            ContentStoreConfiguration configuration;

            if (!fileSystem.DirectoryExists(rootPath))
            {
                return(new Result <ContentStoreConfiguration>($"Directory path=[{rootPath}] does not exist"));
            }

            if (!fileSystem.FileExists(jsonPath))
            {
                return(new Result <ContentStoreConfiguration>($"ContentStoreConfiguration not present at path=[{jsonPath}]"));
            }

            using (Stream stream = fileSystem.OpenReadOnly(jsonPath, FileShare.None))
            {
                configuration = stream.DeserializeFromJSON <ContentStoreConfiguration>();
            }

            if (!configuration.IsValid)
            {
                return(new Result <ContentStoreConfiguration>($"Invalid content store configuration at path=[{jsonPath}]"));
            }

            return(new Result <ContentStoreConfiguration>(configuration));
        }
Exemple #2
0
        /// <summary>
        ///     Deserialize hibernated sessions information from the standard filename in the given directory.
        /// </summary>
        public static Task <HibernatedSessions <TInfo> > ReadHibernatedSessionsAsync <TInfo>(this IAbsFileSystem fileSystem, AbsolutePath rootPath, string fileName)
        {
            Contract.Requires(rootPath != null);

            var jsonPath = rootPath / fileName;

            using (Stream stream = fileSystem.OpenReadOnly(jsonPath, FileShare.None))
            {
                return(Task.FromResult(stream.DeserializeFromJSON <HibernatedSessions <TInfo> >()));
            }
        }
Exemple #3
0
        /// <summary>
        ///     Deserialize hibernated sessions information from the standard filename in the given directory.
        /// </summary>
        public static async Task <HibernatedSessions <TInfo> > ReadProtectedHibernatedSessionsAsync <TInfo>(this IAbsFileSystem fileSystem, AbsolutePath rootPath, string fileName)
        {
            Contract.Requires(rootPath != null);

            var jsonPath = rootPath / fileName;

            using var fileStreamWithLength = fileSystem.OpenReadOnly(jsonPath, FileShare.None);
            var bytes = new byte[fileStreamWithLength.Length];
            await fileStreamWithLength.Stream.ReadAsync(bytes, 0, (int)fileStreamWithLength.Length);

            var protectedBytes = ProtectedData.Unprotect(bytes, optionalEntropy: null, DataProtectionScope.CurrentUser);

            using var stream = new MemoryStream(protectedBytes);
            return(stream.DeserializeFromJSON <HibernatedSessions <TInfo> >());
        }
        private Stream GetStream(AbsolutePath sourcePath)
        {
            Stream s;

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

            return(s);
        }
        /// <summary>
        ///     Loads pin history from disk if exists, otherwise create a new instance.
        /// </summary>
        public static PinSizeHistory LoadOrCreateNew(
            IAbsFileSystem fileSystem,
            IClock clock,
            AbsolutePath directoryPath,
            int?newCapacity = default(int?))
        {
            Contract.Requires(fileSystem != null);
            Contract.Requires(directoryPath != null);

            var filePath = directoryPath / BinaryFileName;

            try
            {
                if (!fileSystem.FileExists(filePath))
                {
                    return(new PinSizeHistory(
                               clock,
                               newCapacity.HasValue ? PinHistoryBuffer.Create(newCapacity.Value) : PinHistoryBuffer.Create(),
                               clock.UtcNow.Ticks,
                               directoryPath));
                }

                using (var stream = fileSystem.OpenReadOnly(filePath, FileShare.Delete))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        var pinHistoryBuffer = PinHistoryBuffer.Deserialize(reader, newCapacity);
                        var timeStampInTick  = reader.ReadInt64();
                        return(new PinSizeHistory(clock, pinHistoryBuffer, timeStampInTick, directoryPath));
                    }
                }
            }
            catch (IOException)
            {
                return(new PinSizeHistory(
                           clock,
                           newCapacity.HasValue ? PinHistoryBuffer.Create(newCapacity.Value) : PinHistoryBuffer.Create(),
                           clock.UtcNow.Ticks,
                           directoryPath));
            }
        }
        private LoadQuotaResult LoadOrCreateNew(IAbsFileSystem fileSystem, MaxSizeQuota initialElasticQuota, AbsolutePath rootPath)
        {
            var filePath = rootPath / BinaryFileName;

            try
            {
                if (!fileSystem.FileExists(filePath))
                {
                    return(CreateNew(fileSystem, initialElasticQuota, rootPath));
                }

                using (var stream = fileSystem.OpenReadOnly(filePath, FileShare.Delete))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        return(new LoadQuotaResult(new MaxSizeQuota(reader.ReadInt64(), reader.ReadInt64()), reader.ReadInt64()));
                    }
                }
            }
            catch (IOException)
            {
                return(CreateNew(fileSystem, initialElasticQuota, rootPath));
            }
        }