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 NSMutableString *outString = [@"com.apple.os.update-" mutableCopy];
34 CFDataRef manifestHash, rootSnapshotName;
36 io_registry_entry_t chosen = IORegistryEntryFromPath(0, "IODeviceTree:/chosen");
38 rootSnapshotName = IORegistryEntryCreateCFProperty(chosen, CFSTR("root-snapshot-name"), kCFAllocatorDefault, 0);
40 if (rootSnapshotName != NULL && CFGetTypeID(rootSnapshotName) == CFDataGetTypeID()) {
41 CFStringRef snapshotString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, rootSnapshotName, kCFStringEncodingUTF8);
42 CFRelease(rootSnapshotName);
44 const char *ptr = CFStringGetCStringPtr(snapshotString, kCFStringEncodingUTF8);
46 if (CFStringGetCString(snapshotString, buffer, 100, kCFStringEncodingUTF8))
49 return [NSString stringWithUTF8String:ptr];
51 manifestHash = (CFDataRef)IORegistryEntryCreateCFProperty(chosen, CFSTR("boot-manifest-hash"), kCFAllocatorDefault, 0);
52 IOObjectRelease(chosen);
54 if (manifestHash == NULL || CFGetTypeID(manifestHash) != CFDataGetTypeID()) {
55 fprintf(stderr, "Unable to read boot-manifest-hash or root-snapshot-name\n");
59 length = CFDataGetLength(manifestHash);
60 bytes = CFDataGetBytePtr(manifestHash);
61 CFRelease(manifestHash);
63 for (int i = 0; i < length; i++)
64 [outString appendFormat:@"%02X", bytes[i]];
70 int restore(const char *vol, const char *snap) {
71 int fd = open(vol, O_RDONLY, 0);
73 int ret = fs_snapshot_revert(fd, snap, 0);
77 int mount(const char *vol, const char *snap, const char *mnt) {
78 int fd = open(vol, O_RDONLY, 0);
81 NSFileManager *fileManager = [NSFileManager defaultManager];
82 if(![fileManager fileExistsAtPath:[NSString stringWithUTF8String:mnt] isDirectory:&isDir])
83 if(![fileManager createDirectoryAtPath:[NSString stringWithUTF8String:mnt] withIntermediateDirectories:YES attributes:nil error:NULL])
84 NSLog(@"Error: Create folder failed %s", mnt);
86 int ret = fs_snapshot_mount(fd, mnt, snap, 0);
91 NSMutableSet *findApps(const char *root, const char *mnt) {
92 NSMutableString *rootApplications = [NSMutableString stringWithUTF8String:root];
93 rootApplications = [[rootApplications stringByAppendingString:@"/Applications"] mutableCopy];
95 NSMutableString *mntApplications = [NSMutableString stringWithUTF8String:mnt];
96 mntApplications = [[mntApplications stringByAppendingString:@"/Applications"] mutableCopy];
98 NSArray *rootApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootApplications error:nil];
99 NSArray *mntApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mntApplications error:nil];
101 NSMutableSet *ret = [[NSMutableSet alloc] init];
102 for (NSString *app in rootApps) {
103 if (![mntApps containsObject:app]) {
104 [ret addObject:[@"/Applications/" stringByAppendingString:app]];
111 int unregisterPath(NSString *path) {
112 path = [path stringByResolvingSymlinksInPath];
113 NSURL *url = [NSURL fileURLWithPath:path];
114 LSApplicationWorkspace *workspace = [LSApplicationWorkspace defaultWorkspace];
115 return [workspace unregisterApplication:url];
118 int rename(const char *vol, const char *snap) {
119 int fd = open(vol, O_RDONLY, 0);
121 int ret = fs_snapshot_rename(fd, snap, [bootsnapshot() UTF8String], 0);
126 NSArray *extrafiles = @[@"/var/lib", @"/var/cache"];
127 NSError *error = nil;
128 for (NSString *path in extrafiles) {
129 [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
134 int main(int argc, char *argv[]) {
141 char *snap = argv[2];
142 char *mnt = "/tmp/rootfsmnt";
144 printf("Restoring snapshot %s...\n", snap);
146 printf("Restored snapshot...\n");
147 printf("Mounting rootfs...\n");
148 mount(vol, snap, mnt);
149 printf("Mounted %s at %s\n", snap, mnt);
150 NSMutableSet *appSet = findApps(vol, mnt);
151 if ([appSet count]) {
152 for (NSString *app in appSet) {
153 printf("unregistering %s\n", [app UTF8String]);
157 printf("Cleaning up /var\n");
159 printf("Renaming snapshot...\n");
161 printf("Restoring %s on %s has succeeded\n", snap, vol);