]> git.saurik.com Git - uikittools.git/blob - uicache.mm
2eafcddc9702062c8d0b4f77cd981ca66ac966f6
[uikittools.git] / uicache.mm
1 /* UIKit Tools - command-line utilities for UIKit
2 * Copyright (C) 2008-2012 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #import <Foundation/Foundation.h>
41
42 #include <notify.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46
47 #include <objc/runtime.h>
48
49 #include <MobileCoreServices/LSApplicationWorkspace.h>
50
51 #include "csstore.hpp"
52
53 @interface NSMutableArray (Cydia)
54 - (void) addInfoDictionary:(NSDictionary *)info;
55 @end
56
57 @implementation NSMutableArray (Cydia)
58
59 - (void) addInfoDictionary:(NSDictionary *)info {
60 [self addObject:info];
61 }
62
63 - (NSArray *) allInfoDictionaries {
64 return self;
65 }
66
67 @end
68
69 @interface NSMutableDictionary (Cydia)
70 - (void) addInfoDictionary:(NSDictionary *)info;
71 @end
72
73 @implementation NSMutableDictionary (Cydia)
74
75 - (void) addInfoDictionary:(NSDictionary *)info {
76 NSString *bundle = [info objectForKey:@"CFBundleIdentifier"];
77 [self setObject:info forKey:bundle];
78 }
79
80 - (NSArray *) allInfoDictionaries {
81 return [self allValues];
82 }
83
84 @end
85
86 int main(int argc, const char *argv[]) {
87 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
88
89 Class $LSApplicationWorkspace(objc_getClass("LSApplicationWorkspace"));
90 LSApplicationWorkspace *workspace($LSApplicationWorkspace == nil ? nil : [$LSApplicationWorkspace defaultWorkspace]);
91
92 if ([workspace respondsToSelector:@selector(_LSPrivateRebuildApplicationDatabasesForSystemApps:internal:user:)]) {
93 if (![workspace _LSPrivateRebuildApplicationDatabasesForSystemApps:YES internal:YES user:NO])
94 fprintf(stderr, "failed to rebuild application databases");
95 return 0;
96 }
97
98 bool respring(false);
99
100 NSString *home(NSHomeDirectory());
101 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", home]);
102
103 system("killall -SIGSTOP SpringBoard");
104 sleep(1);
105
106 @try {
107
108 DeleteCSStores([home UTF8String]);
109
110 system("killall lsd");
111
112 if ([workspace respondsToSelector:@selector(invalidateIconCache:)])
113 while (![workspace invalidateIconCache:nil])
114 sleep(1);
115
116 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
117 NSFileManager *manager = [NSFileManager defaultManager];
118 NSError *error = nil;
119
120 NSMutableDictionary *bundles([NSMutableDictionary dictionaryWithCapacity:16]);
121
122 id after = [cache objectForKey:@"System"];
123 if (after == nil) { error:
124 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
125 goto cached;
126 }
127
128 id before([[after copy] autorelease]);
129 [after removeAllObjects];
130
131 NSArray *cached([cache objectForKey:@"InfoPlistCachedKeys"]);
132
133 NSMutableSet *removed([NSMutableSet set]);
134 for (NSDictionary *info in [before allInfoDictionaries])
135 if (NSString *path = [info objectForKey:@"Path"])
136 [removed addObject:path];
137
138 if (NSArray *apps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]) {
139 for (NSString *app in apps)
140 if ([app hasSuffix:@".app"]) {
141 NSString *path = [@"/Applications" stringByAppendingPathComponent:app];
142 NSString *plist = [path stringByAppendingPathComponent:@"Info.plist"];
143
144 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
145 if (NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]) {
146 [bundles setObject:path forKey:identifier];
147 [removed removeObject:path];
148
149 if (cached != nil) {
150 NSMutableDictionary *merged([before objectForKey:identifier]);
151 if (merged == nil)
152 merged = [NSMutableDictionary dictionary];
153 else
154 merged = [[merged mutableCopy] autorelease];
155
156 for (NSString *key in cached)
157 if (NSObject *value = [info objectForKey:key])
158 [merged setObject:value forKey:key];
159 else
160 [merged removeObjectForKey:key];
161
162 info = merged;
163 }
164
165 [info setObject:path forKey:@"Path"];
166 [info setObject:@"System" forKey:@"ApplicationType"];
167 [after addInfoDictionary:info];
168 } else
169 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
170 }
171 }
172 } else goto error;
173
174 [cache writeToFile:path atomically:YES];
175
176 if (workspace != nil) {
177 if ([workspace respondsToSelector:@selector(invalidateIconCache:)]) {
178 for (NSString *identifier in bundles)
179 [workspace invalidateIconCache:identifier];
180 } else {
181 for (NSString *identifier in bundles) {
182 NSString *path([bundles objectForKey:identifier]);
183 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
184 }
185 }
186
187 for (NSString *identifier in bundles) {
188 NSString *path([bundles objectForKey:identifier]);
189 if (kCFCoreFoundationVersionNumber >= 800)
190 [workspace registerApplicationDictionary:[after objectForKey:identifier]];
191 else
192 [workspace registerApplication:[NSURL fileURLWithPath:path]];
193 }
194
195 for (NSString *path in removed)
196 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
197 }
198 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
199 cached:
200
201 if (respring || kCFCoreFoundationVersionNumber >= 550.32) {
202 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons", home] UTF8String]);
203 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons.plist", home] UTF8String]);
204
205 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons", home] UTF8String]);
206 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons.plist", home] UTF8String]);
207
208 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache", home] UTF8String]);
209 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache-small", home] UTF8String]);
210
211 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/com.apple.IconsCache", home] UTF8String]);
212 }
213
214 system("killall installd");
215
216 } @finally {
217 system("killall -SIGCONT SpringBoard");
218 }
219
220 if (respring)
221 system("launchctl stop com.apple.SpringBoard");
222 else
223 notify_post("com.apple.mobile.application_installed");
224
225 [pool release];
226
227 return 0;
228 }