]> git.cameronkatri.com Git - snaprestore.git/blob - src/snaprestore.m
Remove uicache dependency
[snaprestore.git] / src / snaprestore.m
1 #import <Foundation/Foundation.h>
2 #import <Foundation/NSFileManager.h>
3 #import <IOKit/IOKitLib.h>
4 #import <sys/snapshot.h>
5 #import <getopt.h>
6
7 @interface LSApplicationWorkspace : NSObject
8 + (id)defaultWorkspace;
9 - (BOOL)_LSPrivateRebuildApplicationDatabasesForSystemApps:(BOOL)arg1 internal:(BOOL)arg2 user:(BOOL)arg3;
10 - (BOOL)registerApplicationDictionary:(NSDictionary *)applicationDictionary;
11 - (BOOL)registerBundleWithInfo:(NSDictionary *)bundleInfo options:(NSDictionary *)options type:(unsigned long long)arg3 progress:(id)arg4 ;
12 - (BOOL)registerApplication:(NSURL *)url;
13 - (BOOL)registerPlugin:(NSURL *)url;
14 - (BOOL)unregisterApplication:(NSURL *)url;
15 - (NSArray *)installedPlugins;
16 -(void)_LSPrivateSyncWithMobileInstallation;
17 @end
18
19 void usage(char *name) {
20 printf(
21 "Usage: %s [volume] [snapshot]\n", name);
22 }
23
24 NSString *bootsnapshot() {
25 const io_registry_entry_t chosen = IORegistryEntryFromPath(0, "IODeviceTree:/chosen");
26 const NSData *data = (__bridge const NSData *)IORegistryEntryCreateCFProperty(chosen, (__bridge CFStringRef)@"boot-manifest-hash", kCFAllocatorDefault, 0);
27 IOObjectRelease(chosen);
28
29 NSMutableString *manifestHash = [NSMutableString stringWithString:@""];
30 NSUInteger len = [data length];
31 Byte *buf = (Byte*)malloc(len);
32 memcpy(buf, [data bytes], len);
33 int buf2;
34 for (buf2 = 0; buf2 <= 19; buf2++) {
35 [manifestHash appendFormat:@"%02X", buf[buf2]];
36 }
37 // add com.apple.os.update-
38 return [NSString stringWithFormat:@"%@%@", @"com.apple.os.update-", manifestHash];
39 }
40
41 int restore(const char *vol, const char *snap) {
42 int fd = open(vol, O_RDONLY, 0);
43
44 int ret = fs_snapshot_revert(fd, snap, 0);
45 return ret;
46 }
47
48 int mount(const char *vol, const char *snap, const char *mnt) {
49 int fd = open(vol, O_RDONLY, 0);
50
51 BOOL isDir;
52 NSFileManager *fileManager = [NSFileManager defaultManager];
53 if(![fileManager fileExistsAtPath:[NSString stringWithUTF8String:mnt] isDirectory:&isDir])
54 if(![fileManager createDirectoryAtPath:[NSString stringWithUTF8String:mnt] withIntermediateDirectories:YES attributes:nil error:NULL])
55 NSLog(@"Error: Create folder failed %s", mnt);
56
57 int ret = fs_snapshot_mount(fd, mnt, snap, 0);
58
59 return ret;
60 }
61
62 NSMutableSet *findApps(const char *root, const char *mnt) {
63 NSMutableString *rootApplications = [NSMutableString stringWithUTF8String:root];
64 rootApplications = [[rootApplications stringByAppendingString:@"/Applications"] mutableCopy];
65
66 NSMutableString *mntApplications = [NSMutableString stringWithUTF8String:mnt];
67 mntApplications = [[mntApplications stringByAppendingString:@"/Applications"] mutableCopy];
68
69 NSArray *rootApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootApplications error:nil];
70 NSArray *mntApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mntApplications error:nil];
71
72 NSMutableSet *ret = [[NSMutableSet alloc] init];
73 for (NSString *app in rootApps) {
74 if (![mntApps containsObject:app]) {
75 [ret addObject:[@"/Applications/" stringByAppendingString:app]];
76 }
77 }
78
79 return ret;
80 }
81
82 int unregisterPath(NSString *path) {
83 path = [path stringByResolvingSymlinksInPath];
84 NSURL *url = [NSURL fileURLWithPath:path];
85 LSApplicationWorkspace *workspace = [LSApplicationWorkspace defaultWorkspace];
86 return [workspace unregisterApplication:url];
87 }
88
89 int rename(const char *vol, const char *snap) {
90 int fd = open(vol, O_RDONLY, 0);
91
92 int ret = fs_snapshot_rename(fd, snap, [bootsnapshot() UTF8String], 0);
93 return ret;
94 }
95
96 int main(int argc, char *argv[]) {
97 if (argc != 3) {
98 usage(argv[0]);
99 return 0;
100 }
101
102 char *vol = argv[1];
103 char *snap = argv[2];
104 char *mnt = "/tmp/rootfsmnt";
105
106 printf("Restoring snapshot %s...\n", snap);
107 restore(vol, snap);
108 printf("Restored snapshot...\n");
109 printf("Mounting rootfs...\n");
110 mount(vol, snap, mnt);
111 printf("Mounted %s at %s\n", snap, mnt);
112 NSMutableSet *appSet = findApps(vol, mnt);
113 if ([appSet count]) {
114 for (NSString *app in appSet) {
115 printf("unregistering %s\n", [app UTF8String]);
116 unregisterPath(app);
117 }
118 }
119 printf("Renaming snapshot...\n");
120 rename(vol, snap);
121 printf("Restoring %s on %s has succeeded\n", snap, vol);
122 return 0;
123 }