/// <summary> /// Reads backup completion info from file. /// </summary> /// <param name="filePath">The path of the file to read from.</param> /// <returns>The read backup completion info.</returns> /// <exception cref="BackupCompleteInfoFileIOException">If the file could not be read.</exception> /// <exception cref="BackupCompleteInfoFileParseException">If the file is not valid backup completion info. /// </exception> public static BackupCompleteInfo Read(string filePath) { byte[] bytes; try { bytes = FilesystemException.ConvertSystemException(() => File.ReadAllBytes(filePath), () => filePath); } catch (FilesystemException e) { throw new BackupCompleteInfoFileIOException(filePath, e); } BackupCompleteInfo?value; try { value = JsonSerializer.Deserialize <BackupCompleteInfo>(bytes); } catch (JsonException e) { throw new BackupCompleteInfoFileParseException(filePath, e); } if (value is null) { throw new BackupCompleteInfoFileParseException(filePath, "null not allowed"); } else { return(value); } }
/// <summary> /// Writes backup completion info to file. <br/> /// The file is created new or overwritten if it exists. /// </summary> /// <param name="filePath">The path of the file to write to.</param> /// <param name="value">The backup completion info to write.</param> /// <exception cref="BackupCompleteInfoFileIOException">If the file could not be written to.</exception> public static void Write(string filePath, BackupCompleteInfo value) { JsonSerializerOptions jsonOptions = new() { WriteIndented = true }; // Pretty print for human reading. var bytes = JsonSerializer.SerializeToUtf8Bytes(value, jsonOptions); try { FilesystemException.ConvertSystemException(() => File.WriteAllBytes(filePath, bytes), () => filePath); } catch (FilesystemException e) { throw new BackupCompleteInfoFileIOException(filePath, e); } } }
/// <summary> /// Reads a backup manifest from file. /// </summary> /// <param name="filePath">The path to the manifest file.</param> /// <returns>The read backup manifest.</returns> /// <exception cref="BackupManifestFileIOException">If a filesystem error occurs during reading.</exception> /// <exception cref="BackupManifestFileParseException">If the file is not a valid backup manifest.</exception> public static BackupManifest Read(string filePath) { using var stream = OpenFile(filePath); BackupManifest manifest = new(); List <BackupManifest.Directory> directoryStack = new() { manifest.Root }; long lineNum = 0; while (true) { string?line; try { line = FilesystemException.ConvertSystemException(() => stream.ReadLine(), () => filePath); } catch (FilesystemException e) { throw new BackupManifestFileIOException(filePath, e); } if (line is null) { break; } lineNum++; // Empty lines ok, just skip them. if (line.Length == 0) { continue; } if (line.Length < 3 || line[2] != BackupManifestFileConstants.SEPARATOR) { throw new BackupManifestFileParseException(filePath, lineNum); } var command = line[0..2];
public BackupCompleteInfoFileIOException(string filePath, FilesystemException innerException) : base(filePath, $"Failed to access backup completion info file \"{filePath}\": {innerException.Reason}", innerException) { }