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