/// <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 async Task <ObjectResult <ContentStoreConfiguration> > ReadContentStoreConfigurationAsync( this IAbsFileSystem fileSystem, AbsolutePath rootPath) { Contract.Requires(fileSystem != null); Contract.Requires(rootPath != null); AbsolutePath jsonPath = rootPath / FileName; ContentStoreConfiguration configuration; if (!fileSystem.DirectoryExists(rootPath)) { return(new ObjectResult <ContentStoreConfiguration>($"Directory path=[{rootPath}] does not exist")); } if (!fileSystem.FileExists(jsonPath)) { return(new ObjectResult <ContentStoreConfiguration>($"ContentStoreConfiguration not present at path=[{jsonPath}]")); } using (var stream = await fileSystem.OpenReadOnlySafeAsync(jsonPath, FileShare.None)) { configuration = stream.DeserializeFromJSON <ContentStoreConfiguration>(); } if (!configuration.IsValid) { return(new ObjectResult <ContentStoreConfiguration>($"Invalid content store configuration at path=[{jsonPath}]")); } return(new ObjectResult <ContentStoreConfiguration>(configuration)); }
/// <summary> /// Deserialize hibernated sessions information from the standard filename in the given directory. /// </summary> public static async Task <HibernatedSessions> ReadHibernatedSessionsAsync(this IAbsFileSystem fileSystem, AbsolutePath rootPath) { Contract.Requires(fileSystem != null); Contract.Requires(rootPath != null); var jsonPath = rootPath / FileName; using (Stream stream = await fileSystem.OpenReadOnlySafeAsync(jsonPath, FileShare.None)) { return(stream.DeserializeFromJSON <HibernatedSessions>()); } }
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); }
/// <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(fileSystem != null); Contract.Requires(rootPath != null); var jsonPath = rootPath / fileName; using var fileStreamWithLength = await fileSystem.OpenReadOnlySafeAsync(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> >()); }
/// <summary> /// Loads pin history from disk if exists, otherwise create a new instance. /// </summary> public static async Task <PinSizeHistory> LoadOrCreateNewAsync( 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 = await fileSystem.OpenReadOnlySafeAsync(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 async Task <LoadQuotaResult> LoadOrCreateNewAsync(IAbsFileSystem fileSystem, MaxSizeQuota initialElasticQuota, AbsolutePath rootPath) { var filePath = rootPath / BinaryFileName; try { if (!fileSystem.FileExists(filePath)) { return(await CreateNewAsync(fileSystem, initialElasticQuota, rootPath)); } using (var stream = await fileSystem.OpenReadOnlySafeAsync(filePath, FileShare.Delete)) { using (var reader = new BinaryReader(stream)) { return(new LoadQuotaResult(new MaxSizeQuota(reader.ReadInt64(), reader.ReadInt64()), reader.ReadInt64())); } } } catch (IOException) { return(await CreateNewAsync(fileSystem, initialElasticQuota, rootPath)); } }