]> git.saurik.com Git - uikittools.git/blame_incremental - uicache.mm
Port to Xcode 4.6.3: newer versions of Xcode fail.
[uikittools.git] / uicache.mm
... / ...
CommitLineData
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 "csstore.hpp"
50
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
61- (NSArray *) allInfoDictionaries {
62 return self;
63}
64
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
78- (NSArray *) allInfoDictionaries {
79 return [self allValues];
80}
81
82@end
83
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
94int main(int argc, const char *argv[]) {
95 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
96
97 Class $LSApplicationWorkspace(objc_getClass("LSApplicationWorkspace"));
98 LSApplicationWorkspace *workspace($LSApplicationWorkspace == nil ? nil : [$LSApplicationWorkspace defaultWorkspace]);
99
100 if (kCFCoreFoundationVersionNumber > 1000) // this API is on iOS 7 but invaliding the icon cache is harder there
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
107 bool respring(false);
108
109 NSString *home(NSHomeDirectory());
110 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", home]);
111
112 system("killall -SIGSTOP SpringBoard");
113 sleep(1);
114
115 @try {
116
117 DeleteCSStores([home UTF8String]);
118
119 system("killall lsd");
120
121 if ([workspace respondsToSelector:@selector(invalidateIconCache:)])
122 while (![workspace invalidateIconCache:nil])
123 sleep(1);
124
125 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
126 NSFileManager *manager = [NSFileManager defaultManager];
127 NSError *error = nil;
128
129 NSMutableDictionary *bundles([NSMutableDictionary dictionaryWithCapacity:16]);
130
131 id after = [cache objectForKey:@"System"];
132 if (after == nil) { error:
133 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
134 goto cached;
135 }
136
137 id before([[after copy] autorelease]);
138 [after removeAllObjects];
139
140 NSArray *cached([cache objectForKey:@"InfoPlistCachedKeys"]);
141
142 NSMutableSet *removed([NSMutableSet set]);
143 for (NSDictionary *info in [before allInfoDictionaries])
144 if (NSString *path = [info objectForKey:@"Path"])
145 [removed addObject:path];
146
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"];
152
153 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
154 if (NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]) {
155 [bundles setObject:path forKey:identifier];
156 [removed removeObject:path];
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
174 [info setObject:path forKey:@"Path"];
175 [info setObject:@"System" forKey:@"ApplicationType"];
176 [after addInfoDictionary:info];
177 } else
178 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
179 }
180 }
181 } else goto error;
182
183 [cache writeToFile:path atomically:YES];
184
185 if (workspace != nil) {
186 if ([workspace respondsToSelector:@selector(invalidateIconCache:)]) {
187 for (NSString *identifier in bundles)
188 [workspace invalidateIconCache:identifier];
189 } else {
190 for (NSString *identifier in bundles) {
191 NSString *path([bundles objectForKey:identifier]);
192 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
193 }
194 }
195
196 for (NSString *identifier in bundles) {
197 NSString *path([bundles objectForKey:identifier]);
198 if (kCFCoreFoundationVersionNumber >= 800)
199 [workspace registerApplicationDictionary:[after objectForKey:identifier]];
200 else
201 [workspace registerApplication:[NSURL fileURLWithPath:path]];
202 }
203
204 for (NSString *path in removed)
205 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
206 }
207 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
208 cached:
209
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]);
213
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]);
216
217 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache", home] UTF8String]);
218 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache-small", home] UTF8String]);
219
220 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/com.apple.IconsCache", home] UTF8String]);
221 }
222
223 system("killall installd");
224
225 } @finally {
226 system("killall -SIGCONT SpringBoard");
227 }
228
229 if (respring)
230 system("launchctl stop com.apple.SpringBoard");
231 else
232 notify_post("com.apple.mobile.application_installed");
233
234 [pool release];
235
236 return 0;
237}