-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetTimeMachine.m
More file actions
259 lines (248 loc) · 11.7 KB
/
setTimeMachine.m
File metadata and controls
259 lines (248 loc) · 11.7 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#import <Foundation/Foundation.h>
#import "utils.h"
void usage() {
printf("Usage:\tsetTimeMachine [OPTIONS...]\n");
printf("\t-h\tPrint this help.\n");
printf("\t-f <vol> [--enable | --disable] [--n <number>]\n");
printf("\t\tSet a way to backup snapshots for specify filesystem.\n");
printf("\t-s\tShow current settings.\n");
printf("\t-t [--h <hour>] [--m <minute>]\n");
printf("\t\tSet a time to backup snapshots(24-hour system).\n");
}
void showSettings() {
NSDictionary *launchdSettings = [NSDictionary dictionaryWithContentsOfFile:@"/Library/LaunchDaemons/com.michael.TimeMachine.plist"];
printf("Will backup snapshots at %d:%d.\n", [launchdSettings[@"StartCalendarInterval"][@"Hour"] intValue], [launchdSettings[@"StartCalendarInterval"][@"Minute"] intValue]);
CFDictionaryRef settings = loadPrefs();
if (settings == NULL) {
settings = CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL);
}
int max_rootfs_snapshot = 3, max_datafs_snapshot = 3;
if (!is_sealed("/") && (!CFDictionaryContainsKey(settings, CFSTR("rootfs_enabled")) || CFBooleanGetValue(CFDictionaryGetValue(settings, CFSTR("rootfs_enabled"))))) {
printf("TimeMachine for rootfs is enabled.\n");
} else {
printf("TimeMachine for rootfs is disabled.\n");
}
if (CFDictionaryContainsKey(settings, CFSTR("max_rootfs_snapshot"))) {
CFTypeRef num = CFDictionaryGetValue(settings, CFSTR("max_rootfs_snapshot"));
if (CFGetTypeID(num) == CFNumberGetTypeID()) {
CFNumberGetValue(num, kCFNumberIntType, &max_rootfs_snapshot);
if (max_rootfs_snapshot != 0) {
printf("Will save up to %d snapshots for rootfs.\n", max_rootfs_snapshot);
} else {
printf("Won't save snapshot for rootfs.\n");
}
} else {
CFPreferencesSetValue(CFSTR("max_rootfs_snapshot"), NULL, bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
printf("The max number of snapshots has not been set for rootfs (up to 3 snapshots will be saved by default).\n");
}
CFRelease(num);
} else {
printf("The max number of snapshots has not been set for rootfs (up to 3 snapshots will be saved by default).\n");
}
if (!CFDictionaryContainsKey(settings, CFSTR("datafs_enabled")) || CFBooleanGetValue(CFDictionaryGetValue(settings, CFSTR("datafs_enabled")))) {
printf("TimeMachine for datafs is enabled.\n");
} else {
printf("TimeMachine for datafs is disabled.\n");
}
if (CFDictionaryContainsKey(settings, CFSTR("max_datafs_snapshot"))) {
CFTypeRef num = CFDictionaryGetValue(settings, CFSTR("max_datafs_snapshot"));
if (CFGetTypeID(num) == CFNumberGetTypeID()) {
CFNumberGetValue(num, kCFNumberIntType, &max_datafs_snapshot);
if (max_datafs_snapshot != 0) {
printf("Will save up to %d snapshots for datafs.\n", max_datafs_snapshot);
} else {
printf("Won't save snapshot for datafs.\n");
}
} else {
CFPreferencesSetValue(CFSTR("max_datafs_snapshot"), NULL, bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
printf("The max number of snapshots has not been set for datafs (up to 3 snapshots will be saved by default).\n");
}
CFRelease(num);
} else {
printf("The max number of snapshots has not been set for datafs (up to 3 snapshots will be saved by default).\n");
}
CFRelease(settings);
}
int f(NSArray *args) {
NSString *filePath = nil, *number = nil;
if ([args count] > ([args indexOfObject:@"-f"] + 1)) {
filePath = args[([args indexOfObject:@"-f"] + 1)];
} else {
usage();
return 1;
}
if ([args containsObject:@"--n"]) {
if ([args count] > ([args indexOfObject:@"--n"] + 1) && is_number([args[([args indexOfObject:@"--n"] + 1)] UTF8String])) {
number = args[([args indexOfObject:@"--n"] + 1)];
} else {
usage();
return 1;
}
}
if (number == nil && ![args containsObject:@"--enable"] && ![args containsObject:@"--disable"]) {
usage();
return 1;
}
while ([filePath characterAtIndex:([[NSNumber numberWithUnsignedInteger:[filePath length]] intValue] - 1)] == '/' && [[NSNumber numberWithUnsignedInteger:[filePath length]] intValue] != 1) {
filePath = [filePath substringToIndex:([[NSNumber numberWithUnsignedInteger:[filePath length]] intValue] - 1)];
}
NSError *error = nil;
NSMutableDictionary *fileInfo = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error]];
if (error) {
usage();
return 1;
}
if ([fileInfo[@"NSFileType"] isEqualToString:@"NSFileTypeSymbolicLink"]) {
char *realPath = (char *)calloc(MAXPATHLEN, sizeof(char));
realpath([filePath UTF8String], realPath);
if (strlen(realPath) == 0) {
usage();
return 2;
}
realPath = (char *)realloc(realPath, (strlen(realPath) + 1) * sizeof(char));
filePath = [NSString stringWithFormat:@"%s", realPath];
free(realPath);
fileInfo = [NSMutableDictionary dictionaryWithDictionary:[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error]];
if (error) {
usage();
return 2;
}
}
if (![fileInfo[@"NSFileType"] isEqualToString:@"NSFileTypeDirectory"]) {
usage();
return 3;
}
if ([filePath isEqualToString:@"/"]) {
if ([args containsObject:@"--enable"]) {
printf("Will enable TimeMachine for rootfs.\n");
CFPreferencesSetValue(CFSTR("rootfs_enabled"), kCFBooleanTrue, bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
} else if ([args containsObject:@"--disable"]) {
printf("Will disable TimeMachine for rootfs.\n");
CFPreferencesSetValue(CFSTR("rootfs_enabled"), kCFBooleanFalse, bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
}
if (number != nil) {
CFPreferencesSetValue(CFSTR("max_rootfs_snapshot"), newInt([number intValue]), bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
printf("Successfully set TimeMachine to backup up to most %s snapshots for rootfs.\n", [number UTF8String]);
CFDictionaryRef settings = loadPrefs();
if (settings == NULL) {
settings = CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL);
}
if (!is_sealed("/") && (!CFDictionaryContainsKey(settings, CFSTR("rootfs_enabled")) || CFBooleanGetValue(CFDictionaryGetValue(settings, CFSTR("rootfs_enabled"))))) {
printf("Now delete the extra snapshot.\n");
if (do_timemachine("/", false, [number intValue]) == 0) {
printf("Successfully delete the extra snapshot.\n");
} else {
printf("There is nothing to do.\n");
}
} else {
printf("TimeMachine for rootfs is disabled.\n");
}
CFRelease(settings);
}
} else if ([filePath isEqualToString:@"/private/var"]) {
if ([args containsObject:@"--enable"]) {
printf("Will enable TimeMachine for datafs.\n");
CFPreferencesSetValue(CFSTR("datafs_enabled"), kCFBooleanTrue, bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
} else if ([args containsObject:@"--disable"]) {
printf("Will disable TimeMachine for datafs.\n");
CFPreferencesSetValue(CFSTR("datafs_enabled"), kCFBooleanFalse, bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
}
if (number != nil) {
CFPreferencesSetValue(CFSTR("max_datafs_snapshot"), newInt([number intValue]), bundleID, CFSTR("mobile"), kCFPreferencesAnyHost);
printf("Successfully set TimeMachine to backup up to most %s snapshots for datafs.\n", [number UTF8String]);
CFDictionaryRef settings = loadPrefs();
if (settings == NULL) {
settings = CFDictionaryCreate(NULL, NULL, NULL, 0, NULL, NULL);
}
if (!CFDictionaryContainsKey(settings, CFSTR("datafs_enabled")) || CFBooleanGetValue(CFDictionaryGetValue(settings, CFSTR("datafs_enabled")))) {
printf("Now delete the extra snapshot.\n");
if (do_timemachine("/private/var", false, [number intValue]) == 0) {
printf("Successfully delete the extra snapshot.\n");
} else {
printf("There is nothing to do.\n");
}
} else {
printf("TimeMachine for datafs is disabled.\n");
}
CFRelease(settings);
}
} else {
usage();
return 1;
}
return 0;
}
int t(NSArray *args) {
NSString *hour = nil, *minute = nil;
if ([args containsObject:@"--hour"] || [args containsObject:@"--minute"]) {
if ([args count] > ([args indexOfObject:@"--hour"] + 1) && is_number([args[([args indexOfObject:@"--hour"] + 1)] UTF8String]) && [args[([args indexOfObject:@"--hour"] + 1)] intValue] < 24) {
hour = args[([args indexOfObject:@"--hour"] + 1)];
}
if ([args count] > ([args indexOfObject:@"--minute"] + 1) && is_number([args[([args indexOfObject:@"--minute"] + 1)] UTF8String]) && [args[([args indexOfObject:@"--minute"] + 1)] intValue] < 60) {
minute = args[([args indexOfObject:@"--minute"] + 1)];
}
} else {
usage();
return 1;
}
if (hour == nil && minute == nil) {
usage();
return 1;
}
NSDictionary *const settings = [NSDictionary dictionaryWithContentsOfFile:@"/Library/LaunchDaemons/com.michael.TimeMachine.plist"];
if (hour == nil) {
hour = [NSString stringWithFormat:@"%@", settings[@"StartCalendarInterval"][@"Hour"]];
} else if (minute == nil) {
minute = [NSString stringWithFormat:@"%@", settings[@"StartCalendarInterval"][@"Minute"]];
}
printf("Will modify time to %s:%s.\n", [hour UTF8String], [minute UTF8String]);
run_system("launchctl unload /Library/LaunchDaemons/com.michael.TimeMachine.plist");
NSString *plistFile = @"/Library/LaunchDaemons/com.michael.TimeMachine.plist";
NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile];
plist[@"StartCalendarInterval"][@"Hour"] = @([hour integerValue]);
plist[@"StartCalendarInterval"][@"Minute"] = @([minute integerValue]);
[[NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil] writeToFile:plistFile atomically:YES];
run_system("launchctl load /Library/LaunchDaemons/com.michael.TimeMachine.plist");
return 0;
}
int main(const int argc, const char *argv[]) {
if (getuid() != 0) {
setuid(0);
}
if (getuid() != 0) {
printf("Can't set uid as 0.\n");
return 1;
}
NSMutableArray *args = [[NSMutableArray alloc] init];
for (int i = 1; i < argc; i++) {
[args addObject:[[NSString alloc] initWithUTF8String:argv[i]]];
}
if ([args containsObject:@"-h"]) {
usage();
return 0;
}
if ([args containsObject:@"-s"]) {
showSettings();
return 0;
}
if ([args containsObject:@"-f"] || [args containsObject:@"-t"]) {
int status;
if ([args containsObject:@"-f"]) {
status = f(args);
if (status != 0) {
return status;
}
}
if ([args containsObject:@"-t"]) {
status = t(args);
if (status != 0) {
return status;
}
}
} else {
usage();
return 1;
}
printf("Now exit.\n");
return 0;
}