1 #import <Foundation/Foundation.h>
2 #import <Foundation/NSFileManager.h>
3 #import <IOKit/IOKitLib.h>
4 #import <sys/snapshot.h>
8 void usage(char *name) {
10 "Usage: %s [volume] [snapshot]\n", name);
13 NSString *bootsnapshot() {
14 const io_registry_entry_t chosen = IORegistryEntryFromPath(0, "IODeviceTree:/chosen");
15 const NSData *data = (__bridge const NSData *)IORegistryEntryCreateCFProperty(chosen, (__bridge CFStringRef)@"boot-manifest-hash", kCFAllocatorDefault, 0);
16 IOObjectRelease(chosen);
18 NSMutableString *manifestHash = [NSMutableString stringWithString:@""];
19 NSUInteger len = [data length];
20 Byte *buf = (Byte*)malloc(len);
21 memcpy(buf, [data bytes], len);
23 for (buf2 = 0; buf2 <= 19; buf2++) {
24 [manifestHash appendFormat:@"%02X", buf[buf2]];
26 // add com.apple.os.update-
27 return [NSString stringWithFormat:@"%@%@", @"com.apple.os.update-", manifestHash];
30 int restore(const char *vol, const char *snap) {
31 int fd = open(vol, O_RDONLY, 0);
33 int ret = fs_snapshot_revert(fd, snap, 0);
37 int mount(const char *vol, const char *snap, const char *mnt) {
38 int fd = open(vol, O_RDONLY, 0);
41 NSFileManager *fileManager = [NSFileManager defaultManager];
42 if(![fileManager fileExistsAtPath:[NSString stringWithUTF8String:mnt] isDirectory:&isDir])
43 if(![fileManager createDirectoryAtPath:[NSString stringWithUTF8String:mnt] withIntermediateDirectories:YES attributes:nil error:NULL])
44 NSLog(@"Error: Create folder failed %s", mnt);
46 int ret = fs_snapshot_mount(fd, mnt, snap, 0);
51 NSMutableSet *findApps(const char *root, const char *mnt) {
52 NSMutableString *rootApplications = [NSMutableString stringWithUTF8String:root];
53 rootApplications = [[rootApplications stringByAppendingString:@"/Applications"] mutableCopy];
55 NSMutableString *mntApplications = [NSMutableString stringWithUTF8String:mnt];
56 mntApplications = [[mntApplications stringByAppendingString:@"/Applications"] mutableCopy];
58 NSArray *rootApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootApplications error:nil];
59 NSArray *mntApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mntApplications error:nil];
61 NSMutableSet *ret = [[NSMutableSet alloc] init];
62 for (NSString *app in rootApps) {
63 if (![mntApps containsObject:app]) {
64 [ret addObject:[@"/Applications/" stringByAppendingString:app]];
71 int rename(const char *vol, const char *snap) {
72 int fd = open(vol, O_RDONLY, 0);
74 int ret = fs_snapshot_rename(fd, snap, [bootsnapshot() UTF8String], 0);
78 int main(int argc, char *argv[]) {
86 char *mnt = "/tmp/rootfsmnt";
88 printf("Restoring snapshot %s...\n", snap);
90 printf("Restored snapshot...\n");
91 printf("Mounting rootfs...\n");
92 mount(vol, snap, mnt);
93 printf("Mounted %s at %s\n", snap, mnt);
94 NSMutableSet *appSet = findApps(vol, mnt);
96 printf("Refreshing icon cache...\n");
97 NSMutableArray *argArray = [[NSMutableArray alloc] init];
98 for (NSString *app in appSet) {
99 [argArray addObject:@"-u"];
100 [argArray addObject:app];
102 NSTask *task = [[NSTask alloc] init];
103 [task setLaunchPath:@"/usr/bin/uicache"];
104 [task setArguments:argArray];
106 [task waitUntilExit];
108 printf("Renaming snapshot...\n");
110 printf("Restoring %s on %s has succeeded\n", snap, vol);