/
CloudBackupProtoArchiver.swift
93 lines (79 loc) · 2.97 KB
/
CloudBackupProtoArchiver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
extension CloudBackup {
public enum ArchiveFrameError: Error {
case protoSerializationError(Error)
case fileIOError(Error)
/// An error generating the master key for a group, causing the group to be skipped.
case groupMasterKeyError(Error)
}
/// Note the plural; covers the archiving of multiple frames, typically
/// batched by type.
public enum ArchiveFramesResult<AppIdType> {
public struct Error {
public let objectId: AppIdType
public let error: ArchiveFrameError
}
case success
/// We managed to write some frames, but failed for others.
/// Note that some errors _may_ be terminal; the caller should check.
case partialSuccess([Error])
/// Catastrophic failure, e.g. we failed to read from the database at all
/// for an entire category of frame.
case completeFailure(Swift.Error)
}
public enum RestoringFrameError: Error {
case identifierNotFound
/// The proto contained invalid or self-contradictory data, e.g an invalid ACI.
case invalidProtoData
case databaseInsertionFailed(Error)
/// The contents of the frame are not recognized by any archiver and were ignored.
case unknownFrameType
}
public enum RestoreFrameResult<ProtoIdType> {
case success
case failure(ProtoIdType, RestoringFrameError)
}
}
public protocol CloudBackupProtoArchiver {
}
extension CloudBackupProtoArchiver {
/**
* Helper function to build a frame and write the proto to the backup file in one action
* with standard error handling.
*
* WARNING: any errors thrown in the ``frameBuilder`` function will become
* ``CloudBackup.ArchiveFrameError.protoSerializationError``s. The closure
* should only be used to build the frame proto and any sub protos, and not to capture errors encountered
* reading the information required to build the proto.
*/
internal static func writeFrameToStream(
_ stream: CloudBackupProtoOutputStream,
frameBuilder: (BackupProtoFrameBuilder) throws -> BackupProtoFrame
) -> CloudBackup.ArchiveFrameError? {
let frame: BackupProtoFrame
do {
frame = try frameBuilder(BackupProtoFrame.builder())
} catch {
return .protoSerializationError(error)
}
switch stream.writeFrame(frame) {
case .success:
return nil
case .fileIOError(let error):
return .fileIOError(error)
case .protoSerializationError(let error):
return .protoSerializationError(error)
}
}
}
extension CloudBackup.ArchiveFrameError {
func asArchiveFramesError<AppIdType>(
objectId: AppIdType
) -> CloudBackup.ArchiveFramesResult<AppIdType>.Error {
return .init(objectId: objectId, error: self)
}
}