]> git.cameronkatri.com Git - snaprestore.git/blob - src/snaprestore.m
Clean /var
[snaprestore.git] / src / snaprestore.m
1 #import <Foundation/Foundation.h>
2 #import <Foundation/NSFileManager.h>
3 #import <sys/snapshot.h>
4 #import <getopt.h>
5
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);
12
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;
23 @end
24
25 void usage(char *name) {
26 printf(
27 "Usage: %s [volume] [snapshot]\n", name);
28 }
29
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);
34
35 NSMutableString *manifestHash = [NSMutableString stringWithString:@""];
36 NSUInteger len = [data length];
37 Byte *buf = (Byte*)malloc(len);
38 memcpy(buf, [data bytes], len);
39 int buf2;
40 for (buf2 = 0; buf2 <= 19; buf2++) {
41 [manifestHash appendFormat:@"%02X", buf[buf2]];
42 }
43 // add com.apple.os.update-
44 return [NSString stringWithFormat:@"%@%@", @"com.apple.os.update-", manifestHash];
45 }
46
47 int restore(const char *vol, const char *snap) {
48 int fd = open(vol, O_RDONLY, 0);
49
50 int ret = fs_snapshot_revert(fd, snap, 0);
51 return ret;
52 }
53
54 int mount(const char *vol, const char *snap, const char *mnt) {
55 int fd = open(vol, O_RDONLY, 0);
56
57 BOOL isDir;
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);
62
63 int ret = fs_snapshot_mount(fd, mnt, snap, 0);
64
65 return ret;
66 }
67
68 NSMutableSet *findApps(const char *root, const char *mnt) {
69 NSMutableString *rootApplications = [NSMutableString stringWithUTF8String:root];
70 rootApplications = [[rootApplications stringByAppendingString:@"/Applications"] mutableCopy];
71
72 NSMutableString *mntApplications = [NSMutableString stringWithUTF8String:mnt];
73 mntApplications = [[mntApplications stringByAppendingString:@"/Applications"] mutableCopy];
74
75 NSArray *rootApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootApplications error:nil];
76 NSArray *mntApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mntApplications error:nil];
77
78 NSMutableSet *ret = [[NSMutableSet alloc] init];
79 for (NSString *app in rootApps) {
80 if (![mntApps containsObject:app]) {
81 [ret addObject:[@"/Applications/" stringByAppendingString:app]];
82 }
83 }
84
85 return ret;
86 }
87
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];
93 }
94
95 int rename(const char *vol, const char *snap) {
96 int fd = open(vol, O_RDONLY, 0);
97
98 int ret = fs_snapshot_rename(fd, snap, [bootsnapshot() UTF8String], 0);
99 return ret;
100 }
101
102 int clean() {
103 NSArray *extrafiles = @[@"/var/lib", @"/var/cache"];
104 NSError *error = nil;
105 for (NSString *path in extrafiles) {
106 [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
107 }
108 return 0;
109 }
110
111 int main(int argc, char *argv[]) {
112 if (argc != 3) {
113 usage(argv[0]);
114 return 0;
115 }
116
117 char *vol = argv[1];
118 char *snap = argv[2];
119 char *mnt = "/tmp/rootfsmnt";
120
121 printf("Restoring snapshot %s...\n", snap);
122 restore(vol, snap);
123 printf("Restored snapshot...\n");
124 printf("Mounting rootfs...\n");
125 mount(vol, snap, mnt);
126 printf("Mounted %s at %s\n", snap, mnt);
127 NSMutableSet *appSet = findApps(vol, mnt);
128 if ([appSet count]) {
129 for (NSString *app in appSet) {
130 printf("unregistering %s\n", [app UTF8String]);
131 unregisterPath(app);
132 }
133 }
134 printf("Cleaning up /var\n");
135 clean();
136 printf("Renaming snapshot...\n");
137 rename(vol, snap);
138 printf("Restoring %s on %s has succeeded\n", snap, vol);
139 return 0;
140 }