]> git.saurik.com Git - uikittools.git/blame - uicache.mm
Port to Xcode 4.6.3: newer versions of Xcode fail.
[uikittools.git] / uicache.mm
CommitLineData
a4ccf03b
JF
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
7fb7ce13
JF
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
020dfc54
JF
47#include <objc/runtime.h>
48
4ab9b1c7
JF
49#include "csstore.hpp"
50
1094bdcb
JF
51@interface NSMutableArray (Cydia)
52- (void) addInfoDictionary:(NSDictionary *)info;
53@end
54
55@implementation NSMutableArray (Cydia)
56
57- (void) addInfoDictionary:(NSDictionary *)info {
58 [self addObject:info];
59}
60
1bd75e96
JF
61- (NSArray *) allInfoDictionaries {
62 return self;
63}
64
1094bdcb
JF
65@end
66
67@interface NSMutableDictionary (Cydia)
68- (void) addInfoDictionary:(NSDictionary *)info;
69@end
70
71@implementation NSMutableDictionary (Cydia)
72
73- (void) addInfoDictionary:(NSDictionary *)info {
74 NSString *bundle = [info objectForKey:@"CFBundleIdentifier"];
75 [self setObject:info forKey:bundle];
76}
77
1bd75e96
JF
78- (NSArray *) allInfoDictionaries {
79 return [self allValues];
80}
81
1094bdcb
JF
82@end
83
a830525c
JF
84@interface LSApplicationWorkspace : NSObject
85+ (id) defaultWorkspace;
86- (BOOL) registerApplication:(id)application;
87- (BOOL) unregisterApplication:(id)application;
88- (BOOL) invalidateIconCache:(id)bundle;
89- (BOOL) registerApplicationDictionary:(id)application;
90- (BOOL) installApplication:(id)application withOptions:(id)options;
91- (BOOL) _LSPrivateRebuildApplicationDatabasesForSystemApps:(BOOL)system internal:(BOOL)internal user:(BOOL)user;
92@end
93
a7cec9cf 94int main(int argc, const char *argv[]) {
1094bdcb
JF
95 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
96
73b6bd3c
JF
97 Class $LSApplicationWorkspace(objc_getClass("LSApplicationWorkspace"));
98 LSApplicationWorkspace *workspace($LSApplicationWorkspace == nil ? nil : [$LSApplicationWorkspace defaultWorkspace]);
99
ad089983 100 if (kCFCoreFoundationVersionNumber > 1000) // this API is on iOS 7 but invaliding the icon cache is harder there
73b6bd3c
JF
101 if ([workspace respondsToSelector:@selector(_LSPrivateRebuildApplicationDatabasesForSystemApps:internal:user:)]) {
102 if (![workspace _LSPrivateRebuildApplicationDatabasesForSystemApps:YES internal:YES user:NO])
103 fprintf(stderr, "failed to rebuild application databases");
104 return 0;
105 }
106
a7cec9cf
JF
107 bool respring(false);
108
7e0ec87a
JF
109 NSString *home(NSHomeDirectory());
110 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", home]);
7fb7ce13 111
9972f381
JF
112 system("killall -SIGSTOP SpringBoard");
113 sleep(1);
114
115 @try {
116
4ab9b1c7
JF
117 DeleteCSStores([home UTF8String]);
118
31effc97
JF
119 system("killall lsd");
120
c9bb7c8e
JF
121 if ([workspace respondsToSelector:@selector(invalidateIconCache:)])
122 while (![workspace invalidateIconCache:nil])
123 sleep(1);
124
020dfc54 125 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
1094bdcb
JF
126 NSFileManager *manager = [NSFileManager defaultManager];
127 NSError *error = nil;
128
0183260a 129 NSMutableDictionary *bundles([NSMutableDictionary dictionaryWithCapacity:16]);
020dfc54 130
3ffcdfdb
JF
131 id after = [cache objectForKey:@"System"];
132 if (after == nil) { error:
22d64ef4
JF
133 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
134 goto cached;
135 }
1094bdcb 136
3ffcdfdb
JF
137 id before([[after copy] autorelease]);
138 [after removeAllObjects];
139
140 NSArray *cached([cache objectForKey:@"InfoPlistCachedKeys"]);
1094bdcb 141
1bd75e96
JF
142 NSMutableSet *removed([NSMutableSet set]);
143 for (NSDictionary *info in [before allInfoDictionaries])
144 if (NSString *path = [info objectForKey:@"Path"])
145 [removed addObject:path];
146
1094bdcb
JF
147 if (NSArray *apps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]) {
148 for (NSString *app in apps)
149 if ([app hasSuffix:@".app"]) {
150 NSString *path = [@"/Applications" stringByAppendingPathComponent:app];
151 NSString *plist = [path stringByAppendingPathComponent:@"Info.plist"];
020dfc54
JF
152
153 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
0183260a
JF
154 if (NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]) {
155 [bundles setObject:path forKey:identifier];
1bd75e96 156 [removed removeObject:path];
3ffcdfdb
JF
157
158 if (cached != nil) {
159 NSMutableDictionary *merged([before objectForKey:identifier]);
160 if (merged == nil)
161 merged = [NSMutableDictionary dictionary];
162 else
163 merged = [[merged mutableCopy] autorelease];
164
165 for (NSString *key in cached)
166 if (NSObject *value = [info objectForKey:key])
167 [merged setObject:value forKey:key];
168 else
169 [merged removeObjectForKey:key];
170
171 info = merged;
172 }
173
2a72f832
JF
174 [info setObject:path forKey:@"Path"];
175 [info setObject:@"System" forKey:@"ApplicationType"];
3ffcdfdb 176 [after addInfoDictionary:info];
020dfc54
JF
177 } else
178 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
1094bdcb
JF
179 }
180 }
181 } else goto error;
182
7fb7ce13 183 [cache writeToFile:path atomically:YES];
1094bdcb 184
37e55bd3 185 if (workspace != nil) {
97b54c51
JF
186 if ([workspace respondsToSelector:@selector(invalidateIconCache:)]) {
187 for (NSString *identifier in bundles)
0183260a 188 [workspace invalidateIconCache:identifier];
97b54c51
JF
189 } else {
190 for (NSString *identifier in bundles) {
191 NSString *path([bundles objectForKey:identifier]);
192 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
193 }
194 }
37e55bd3
JF
195
196 for (NSString *identifier in bundles) {
197 NSString *path([bundles objectForKey:identifier]);
1525054b
JF
198 if (kCFCoreFoundationVersionNumber >= 800)
199 [workspace registerApplicationDictionary:[after objectForKey:identifier]];
200 else
201 [workspace registerApplication:[NSURL fileURLWithPath:path]];
5ac04f0f 202 }
1bd75e96
JF
203
204 for (NSString *path in removed)
205 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
37e55bd3 206 }
7fb7ce13 207 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
22d64ef4 208 cached:
1094bdcb 209
a7cec9cf
JF
210 if (respring || kCFCoreFoundationVersionNumber >= 550.32) {
211 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons", home] UTF8String]);
212 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons.plist", home] UTF8String]);
adefb9f1 213
a7cec9cf
JF
214 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons", home] UTF8String]);
215 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons.plist", home] UTF8String]);
7e0ec87a 216
a7cec9cf
JF
217 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache", home] UTF8String]);
218 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache-small", home] UTF8String]);
adefb9f1 219
a7cec9cf
JF
220 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/com.apple.IconsCache", home] UTF8String]);
221 }
7e0ec87a 222
a7cec9cf 223 system("killall installd");
7e0ec87a 224
9972f381
JF
225 } @finally {
226 system("killall -SIGCONT SpringBoard");
227 }
228
a7cec9cf
JF
229 if (respring)
230 system("launchctl stop com.apple.SpringBoard");
231 else
232 notify_post("com.apple.mobile.application_installed");
1094bdcb
JF
233
234 [pool release];
235
236 return 0;
237}