5 #import "keychain/ckks/CKKSPBFileStorage.h"
7 @interface CKKSPBFileStorage ()
8 @property NSURL *storageFile;
9 @property Class<CKKSPBCodable> storageClass;
10 @property id<CKKSPBCodable> protobufStorage;
13 @implementation CKKSPBFileStorage
15 - (CKKSPBFileStorage *)initWithStoragePath:(NSURL *)storageFile
16 storageClass:(Class<CKKSPBCodable>) storageClass
18 if ((self = [super init]) == nil) {
21 self.storageFile = storageFile;
22 self.storageClass = storageClass;
24 NSData *data = [NSData dataWithContentsOfURL:storageFile];
26 self.protobufStorage = [[self.storageClass alloc] initWithData:data];
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];
36 - (id _Nullable)storage
39 @synchronized (self) {
40 storage = self.protobufStorage;
45 - (void)setStorage:(id _Nonnull)storage
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];