]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/src/NSURL+SOSPlistStore.m
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / utilities / src / NSURL+SOSPlistStore.m
1 //
2 // NSURL+SOSPlistWriting.h
3 //
4
5 #import <Security/Security.h>
6 #import <Foundation/NSPropertyList.h>
7 #import <Foundation/NSArray.h>
8 #import <Foundation/NSPropertyList.h>
9 #import <Foundation/NSData.h>
10 #import <Foundation/NSURL.h>
11 #import <Foundation/NSDictionary.h>
12 #import <utilities/debugging.h>
13 #import <utilities/SecFileLocations.h>
14
15 #if ! __has_feature(objc_arc)
16 #error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag
17 #endif
18
19 // may want to have this hold incoming events in file as well
20
21 @implementation NSURL (SOSPlistWriting)
22
23 - (BOOL)writePlist: (id) plist
24 {
25 NSError *error = nil;
26 if (![NSPropertyListSerialization propertyList: plist isValidForFormat: NSPropertyListXMLFormat_v1_0])
27 {
28 secerror("can't save PersistentState as XML");
29 return false;
30 }
31
32 NSData *data = [NSPropertyListSerialization dataWithPropertyList: plist
33 format: NSPropertyListXMLFormat_v1_0 options: 0 error: &error];
34 if (data == nil)
35 {
36 secerror("error serializing PersistentState to xml: %@", error);
37 return false;
38 }
39
40 BOOL writeStatus = [data writeToURL:self options: NSDataWritingAtomic error: &error];
41 if (!writeStatus)
42 secerror("error writing PersistentState to file: %@", error);
43
44 return writeStatus;
45 }
46
47 - (id) readPlist
48 {
49 NSError *error = nil;
50 NSData *data = [NSData dataWithContentsOfURL: self options: 0 error: &error];
51 if (data == nil)
52 {
53 secdebug("keyregister", "error reading PersistentState from %@: %@", self, error);
54 return nil;
55 }
56
57 // Now the deserializing:
58
59 NSPropertyListFormat format;
60 id plist = [NSPropertyListSerialization propertyListWithData: data
61 options: NSPropertyListMutableContainersAndLeaves format: &format error: &error];
62
63 if (plist == nil)
64 secerror("could not deserialize PersistentState from %@: %@", self, error);
65
66 return plist;
67 }
68
69 @end
70