]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/CKKSPBFileStorage.m
Security-59306.140.5.tar.gz
[apple/security.git] / keychain / ckks / CKKSPBFileStorage.m
1 //
2 // CKKSPBFileStorage.m
3 //
4
5 #import "keychain/ckks/CKKSPBFileStorage.h"
6
7 @interface CKKSPBFileStorage ()
8 @property NSURL *storageFile;
9 @property Class<CKKSPBCodable> storageClass;
10 @property id<CKKSPBCodable> protobufStorage;
11 @end
12
13 @implementation CKKSPBFileStorage
14
15 - (CKKSPBFileStorage *)initWithStoragePath:(NSURL *)storageFile
16 storageClass:(Class<CKKSPBCodable>) storageClass
17 {
18 if ((self = [super init]) == nil) {
19 return nil;
20 }
21 self.storageFile = storageFile;
22 self.storageClass = storageClass;
23
24 NSData *data = [NSData dataWithContentsOfURL:storageFile];
25 if (data != nil) {
26 self.protobufStorage = [[self.storageClass alloc] initWithData:data];
27 }
28 /* if not storage, or storage is corrupted, this function will return a empty storage */
29 if (self.protobufStorage == nil) {
30 self.protobufStorage = [[self.storageClass alloc] init];
31 }
32
33 return self;
34 }
35
36 - (id _Nullable)storage
37 {
38 __block id storage;
39 @synchronized (self) {
40 storage = self.protobufStorage;
41 }
42 return storage;
43 }
44
45 - (void)setStorage:(id _Nonnull)storage
46 {
47 @synchronized (self) {
48 id<CKKSPBCodable> c = storage;
49 NSData *data = c.data;
50 [data writeToURL:self.storageFile atomically:YES];
51 self.protobufStorage = [[self.storageClass alloc] initWithData:data];
52 }
53 }
54
55
56 @end