1 #import <Foundation/Foundation.h>
2 #import <Foundation/NSFileManager.h>
3 #import <IOKit/IOKitLib.h>
4 #import <sys/snapshot.h>
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;
19 void usage(char *name) {
21 "Usage: %s [volume] [snapshot]\n", name);
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);
29 NSMutableString *manifestHash = [NSMutableString stringWithString:@""];
30 NSUInteger len = [data length];
31 Byte *buf = (Byte*)malloc(len);
32 memcpy(buf, [data bytes], len);
34 for (buf2 = 0; buf2 <= 19; buf2++) {
35 [manifestHash appendFormat:@"%02X", buf[buf2]];
37 // add com.apple.os.update-
38 return [NSString stringWithFormat:@"%@%@", @"com.apple.os.update-", manifestHash];
41 int restore(const char *vol, const char *snap) {
42 int fd = open(vol, O_RDONLY, 0);
44 int ret = fs_snapshot_revert(fd, snap, 0);
48 int mount(const char *vol, const char *snap, const char *mnt) {
49 int fd = open(vol, O_RDONLY, 0);
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);
57 int ret = fs_snapshot_mount(fd, mnt, snap, 0);
62 NSMutableSet *findApps(const char *root, const char *mnt) {
63 NSMutableString *rootApplications = [NSMutableString stringWithUTF8String:root];
64 rootApplications = [[rootApplications stringByAppendingString:@"/Applications"] mutableCopy];
66 NSMutableString *mntApplications = [NSMutableString stringWithUTF8String:mnt];
67 mntApplications = [[mntApplications stringByAppendingString:@"/Applications"] mutableCopy];
69 NSArray *rootApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootApplications error:nil];
70 NSArray *mntApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mntApplications error:nil];
72 NSMutableSet *ret = [[NSMutableSet alloc] init];
73 for (NSString *app in rootApps) {
74 if (![mntApps containsObject:app]) {
75 [ret addObject:[@"/Applications/" stringByAppendingString:app]];
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];
89 int rename(const char *vol, const char *snap) {
90 int fd = open(vol, O_RDONLY, 0);
92 int ret = fs_snapshot_rename(fd, snap, [bootsnapshot() UTF8String], 0);
96 int main(int argc, char *argv[]) {
103 char *snap = argv[2];
104 char *mnt = "/tmp/rootfsmnt";
106 printf("Restoring snapshot %s...\n", 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]);
119 printf("Renaming snapshot...\n");
121 printf("Restoring %s on %s has succeeded\n", snap, vol);