]> git.cameronkatri.com Git - snaprestore.git/blob - src/snaprestore.m
Fix build
[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 NSMutableString *outString = [@"com.apple.os.update-" mutableCopy];
32 const UInt8 *bytes;
33 CFIndex length;
34 CFDataRef manifestHash, rootSnapshotName;
35
36 io_registry_entry_t chosen = IORegistryEntryFromPath(0, "IODeviceTree:/chosen");
37
38 rootSnapshotName = IORegistryEntryCreateCFProperty(chosen, CFSTR("root-snapshot-name"), kCFAllocatorDefault, 0);
39
40 if (rootSnapshotName != NULL && CFGetTypeID(rootSnapshotName) == CFDataGetTypeID()) {
41 CFStringRef snapshotString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, rootSnapshotName, kCFStringEncodingUTF8);
42 CFRelease(rootSnapshotName);
43 char buffer[100];
44 const char *ptr = CFStringGetCStringPtr(snapshotString, kCFStringEncodingUTF8);
45 if (ptr == NULL) {
46 if (CFStringGetCString(snapshotString, buffer, 100, kCFStringEncodingUTF8))
47 ptr = buffer;
48 }
49 return [NSString stringWithUTF8String:ptr];
50 } else {
51 manifestHash = (CFDataRef)IORegistryEntryCreateCFProperty(chosen, CFSTR("boot-manifest-hash"), kCFAllocatorDefault, 0);
52 IOObjectRelease(chosen);
53
54 if (manifestHash == NULL || CFGetTypeID(manifestHash) != CFDataGetTypeID()) {
55 fprintf(stderr, "Unable to read boot-manifest-hash or root-snapshot-name\n");
56 exit(1);
57 }
58
59 length = CFDataGetLength(manifestHash);
60 bytes = CFDataGetBytePtr(manifestHash);
61 CFRelease(manifestHash);
62
63 for (int i = 0; i < length; i++)
64 [outString appendFormat:@"%02X", bytes[i]];
65 }
66
67 return outString;
68 }
69
70 int restore(const char *vol, const char *snap) {
71 int fd = open(vol, O_RDONLY, 0);
72
73 int ret = fs_snapshot_revert(fd, snap, 0);
74 return ret;
75 }
76
77 int mount(const char *vol, const char *snap, const char *mnt) {
78 int fd = open(vol, O_RDONLY, 0);
79
80 BOOL isDir;
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);
85
86 int ret = fs_snapshot_mount(fd, mnt, snap, 0);
87
88 return ret;
89 }
90
91 NSMutableSet *findApps(const char *root, const char *mnt) {
92 NSMutableString *rootApplications = [NSMutableString stringWithUTF8String:root];
93 rootApplications = [[rootApplications stringByAppendingString:@"/Applications"] mutableCopy];
94
95 NSMutableString *mntApplications = [NSMutableString stringWithUTF8String:mnt];
96 mntApplications = [[mntApplications stringByAppendingString:@"/Applications"] mutableCopy];
97
98 NSArray *rootApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:rootApplications error:nil];
99 NSArray *mntApps = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:mntApplications error:nil];
100
101 NSMutableSet *ret = [[NSMutableSet alloc] init];
102 for (NSString *app in rootApps) {
103 if (![mntApps containsObject:app]) {
104 [ret addObject:[@"/Applications/" stringByAppendingString:app]];
105 }
106 }
107
108 return ret;
109 }
110
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];
116 }
117
118 int rename(const char *vol, const char *snap) {
119 int fd = open(vol, O_RDONLY, 0);
120
121 int ret = fs_snapshot_rename(fd, snap, [bootsnapshot() UTF8String], 0);
122 return ret;
123 }
124
125 int clean() {
126 NSArray *extrafiles = @[@"/var/lib", @"/var/cache"];
127 NSError *error = nil;
128 for (NSString *path in extrafiles) {
129 [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
130 }
131 return 0;
132 }
133
134 int main(int argc, char *argv[]) {
135 if (argc != 3) {
136 usage(argv[0]);
137 return 0;
138 }
139
140 char *vol = argv[1];
141 char *snap = argv[2];
142 char *mnt = "/tmp/rootfsmnt";
143
144 printf("Restoring snapshot %s...\n", snap);
145 restore(vol, 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]);
154 unregisterPath(app);
155 }
156 }
157 printf("Cleaning up /var\n");
158 clean();
159 printf("Renaming snapshot...\n");
160 rename(vol, snap);
161 printf("Restoring %s on %s has succeeded\n", snap, vol);
162 return 0;
163 }