forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCBlockFileReaderWriter.m
More file actions
48 lines (37 loc) · 1.63 KB
/
SCBlockFileReaderWriter.m
File metadata and controls
48 lines (37 loc) · 1.63 KB
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
//
// SCBlockFileReaderWriter.m
// SelfControl
//
// Created by Charlie Stigler on 1/19/21.
//
#import "SCBlockFileReaderWriter.h"
@implementation SCBlockFileReaderWriter
+ (BOOL)writeBlocklistToFileURL:(NSURL*)targetFileURL blockInfo:(NSDictionary*)blockInfo error:(NSError**)errRef {
NSDictionary* saveDict = @{@"HostBlacklist": [blockInfo objectForKey: @"Blocklist"],
@"BlockAsWhitelist": [blockInfo objectForKey: @"BlockAsWhitelist"]};
NSData* saveData = [NSPropertyListSerialization dataWithPropertyList: saveDict format: NSPropertyListBinaryFormat_v1_0 options: 0 error: errRef];
if (*errRef != nil) {
return NO;
}
if (![saveData writeToURL: targetFileURL atomically: YES]) {
NSLog(@"ERROR: Failed to write blocklist to URL %@", targetFileURL);
*errRef = [SCErr errorWithCode: 106];
return NO;
}
// for prettiness sake, attempt to hide the file extension
NSDictionary* attribs = @{NSFileExtensionHidden: @YES};
[[NSFileManager defaultManager] setAttributes: attribs ofItemAtPath: [targetFileURL path] error: errRef];
return YES;
}
+ (NSDictionary*)readBlocklistFromFile:(NSURL*)fileURL {
NSDictionary* openedDict = [NSDictionary dictionaryWithContentsOfURL: fileURL];
if (openedDict == nil || openedDict[@"HostBlacklist"] == nil || openedDict[@"BlockAsWhitelist"] == nil) {
NSLog(@"ERROR: Could not read a valid block from file %@", fileURL);
return nil;
}
return @{
@"Blocklist": openedDict[@"HostBlacklist"],
@"BlockAsWhitelist": openedDict[@"BlockAsWhitelist"]
};
}
@end