1 #import <Foundation/Foundation.h>
2 #import <Foundation/NSFileManager.h>
3 #import <sys/snapshot.h>
6 typedef char io_string_t[512];
7 typedef mach_port_t io_object_t;
8 typedef io_object_t io_registry_entry_t;
9 io_registry_entry_t IORegistryEntryFromPath(mach_port_t master, const io_string_t path);
10 CFTypeRef IORegistryEntryCreateCFProperty(io_registry_entry_t entry, CFStringRef key, CFAllocatorRef allocator, uint32_t options);
11 kern_return_t IOObjectRelease(io_object_t object);
13 @interface LSApplicationWorkspace : NSObject
14 + (id)defaultWorkspace;
15 - (BOOL)_LSPrivateRebuildApplicationDatabasesForSystemApps:(BOOL)arg1 internal:(BOOL)arg2 user:(BOOL)arg3;
16 - (BOOL)registerApplicationDictionary:(NSDictionary *)applicationDictionary;
17 - (BOOL)registerBundleWithInfo:(NSDictionary *)bundleInfo options:(NSDictionary *)options type:(unsigned long long)arg3 progress:(id)arg4 ;
18 - (BOOL)registerApplication:(NSURL *)url;
19 - (BOOL)registerPlugin:(NSURL *)url;
20 - (BOOL)unregisterApplication:(NSURL *)url;
21 - (NSArray *)installedPlugins;
22 -(void)_LSPrivateSyncWithMobileInstallation;
25 void usage(char *name) {
27 "Usage: %s [volume] [snapshot]\n", name);
30 NSString *bootsnapshot() {
31 const io_registry_entry_t chosen = IORegistryEntryFromPath(0, "IODeviceTree:/chosen");
32 const NSData *data = (__bridge const NSData *)IORegistryEntryCreateCFProperty(chosen, (__bridge CFStringRef)@"boot-manifest-hash", kCFAllocatorDefault, 0);
33 IOObjectRelease(chosen);
35 NSMutableString *manifestHash = [NSMutableString stringWithString:@""];
36 NSUInteger len = [data length];
37 Byte *buf = (Byte*)malloc(len);
38 memcpy(buf, [data bytes], len);
40 for (buf2 = 0; buf2 <= 19; buf2++) {
41 [manifestHash appendFormat:@"%02X", buf[buf2]];
43 // add com.apple.os.update-
44 return [NSString stringWithFormat:@"%@%@", @"com.apple.os.update-", manifestHash];
47 int restore(const char *vol, const char *snap) {
48 int fd = open(vol, O_RDONLY, 0);
50 int ret = fs_snapshot_revert(fd, snap, 0);
54 int mount(const char *vol, const char *snap, const char *mnt) {
55 int fd = open(vol, O_RDONLY, 0);
58 NSFileManager *fileManager = [NSFileManager defaultManager];
59 if(![fileManager fileExistsAtPath:[NSString stringWithUTF8String:mnt] isDirectory:&isDir])
60 if(![fileManager createDirectoryAtPath:[NSString stringWithUTF8String:mnt] withIntermediateDirectories:YES attributes:nil error:NULL])
61 NSLog(@"Error: Create folder failed %s", mnt);
63 int ret = fs_snapshot_mount(fd, mnt, snap, 0);
68 NSMutableSet *findApps(const char *root, const char *mnt) {
69 NSMutableString *rootApplications = [NSMutableString stringWithUTF8String:root];
70 rootApplications = [[rootApplications stringByAppendingString:@"/Applications"] mutableCopy];
72 NSMutableString *mntApplications = [NSMutableString stringWithUTF8String:mnt];
73 mntApplications = [[mntApplications stringByAppendingString:@"/Applications"] mutableCopy];
75 NSArray *rootApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootApplications error:nil];
76 NSArray *mntApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mntApplications error:nil];
78 NSMutableSet *ret = [[NSMutableSet alloc] init];
79 for (NSString *app in rootApps) {
80 if (![mntApps containsObject:app]) {
81 [ret addObject:[@"/Applications/" stringByAppendingString:app]];
88 int unregisterPath(NSString *path) {
89 path = [path stringByResolvingSymlinksInPath];
90 NSURL *url = [NSURL fileURLWithPath:path];
91 LSApplicationWorkspace *workspace = [LSApplicationWorkspace defaultWorkspace];
92 return [workspace unregisterApplication:url];
95 int rename(const char *vol, const char *snap) {
96 int fd = open(vol, O_RDONLY, 0);
98 int ret = fs_snapshot_rename(fd, snap, [bootsnapshot() UTF8String], 0);
102 int main(int argc, char *argv[]) {
109 char *snap = argv[2];
110 char *mnt = "/tmp/rootfsmnt";
112 printf("Restoring snapshot %s...\n", snap);
114 printf("Restored snapshot...\n");
115 printf("Mounting rootfs...\n");
116 mount(vol, snap, mnt);
117 printf("Mounted %s at %s\n", snap, mnt);
118 NSMutableSet *appSet = findApps(vol, mnt);
119 if ([appSet count]) {
120 for (NSString *app in appSet) {
121 printf("unregistering %s\n", [app UTF8String]);
125 printf("Renaming snapshot...\n");
127 printf("Restoring %s on %s has succeeded\n", snap, vol);