/// <summary> /// Serialize hibernated session information to the standard filename in the given directory. /// </summary> public static async Task WriteProtectedAsync <TInfo>(this HibernatedSessions <TInfo> sessions, IAbsFileSystem fileSystem, AbsolutePath rootPath, string fileName) { Contract.Requires(fileSystem != null); Contract.Requires(rootPath != null); // Due to abnormal process termination, the file that we'll be writing can be corrupted. // To prevent this issue we first write the file into a temporary location and then we "move it" into a final location. using (var tempFolder = new DisposableDirectory(fileSystem, rootPath / "Temp")) { var jsonTempPath = tempFolder.CreateRandomFileName(); var jsonPath = rootPath / fileName; using (var memoryStream = new MemoryStream()) { sessions.SerializeToJSON(memoryStream); var bytes = memoryStream.ToArray(); var protectedBytes = ProtectedData.Protect(bytes, optionalEntropy: null, DataProtectionScope.CurrentUser); using var fileStream = await fileSystem.OpenSafeAsync(jsonTempPath, FileAccess.Write, FileMode.Create, FileShare.None); await fileStream.Stream.WriteAsync(protectedBytes, 0, protectedBytes.Length); } fileSystem.MoveFile(jsonTempPath, jsonPath, replaceExisting: true); } }
private Task WriteBatchAsync(string[] logEventInfos) { return(_context.PerformOperationAsync(Tracer, async() => { // TODO: retry policy for writing to file? var blobName = GenerateBlobName(); var stagingLogFilePath = _configuration.StagingFolderPath / blobName; var logFile = (await WriteLogsToFileAsync(_context, stagingLogFilePath, logEventInfos).ThrowIfFailure()).Value; var uploadLogFilePath = _configuration.UploadFolderPath / blobName; _fileSystem.MoveFile(stagingLogFilePath, uploadLogFilePath, replaceExisting: true); logFile.Path = uploadLogFilePath; _uploadQueue.Enqueue(logFile); return BoolResult.Success; }, counter: Counters[AzureBlobStorageLogCounters.ProcessBatchCalls])); }
/// <summary> /// Serialize hibernated session information to the standard filename in the given directory. /// </summary> public static void Write <TInfo>(this HibernatedSessions <TInfo> sessions, IAbsFileSystem fileSystem, AbsolutePath rootPath, string fileName) { Contract.Requires(rootPath != null); // Due to abnormal process termination, the file that we'll be writing can be corrupted. // To prevent this issue we first write the file into a temporary location and then we "move it" into a final location. using (var tempFolder = new DisposableDirectory(fileSystem, rootPath / "Temp")) { var jsonTempPath = tempFolder.CreateRandomFileName(); var jsonPath = rootPath / fileName; using (var stream = fileSystem.Open(jsonTempPath, FileAccess.Write, FileMode.Create, FileShare.None)) { sessions.SerializeToJSON(stream); } fileSystem.MoveFile(jsonTempPath, jsonPath, replaceExisting: true); } }