]> git.saurik.com Git - uikittools.git/blame_incremental - uicache.mm
Do not attempt to fix the iOS 6 bug on iOS 7 or 8.
[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 <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
86int 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 (kCFCoreFoundationVersionNumber > 1000) // this API is on iOS 7 but invaliding the icon cache is harder there
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
99 bool respring(false);
100
101 NSString *home(NSHomeDirectory());
102 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", home]);
103
104 system("killall -SIGSTOP SpringBoard");
105 sleep(1);
106
107 @try {
108
109 DeleteCSStores([home UTF8String]);
110
111 system("killall lsd");
112
113 if ([workspace respondsToSelector:@selector(invalidateIconCache:)])
114 while (![workspace invalidateIconCache:nil])
115 sleep(1);
116
117 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
118 NSFileManager *manager = [NSFileManager defaultManager];
119 NSError *error = nil;
120
121 NSMutableDictionary *bundles([NSMutableDictionary dictionaryWithCapacity:16]);
122
123 id after = [cache objectForKey:@"System"];
124 if (after == nil) { error:
125 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
126 goto cached;
127 }
128
129 id before([[after copy] autorelease]);
130 [after removeAllObjects];
131
132 NSArray *cached([cache objectForKey:@"InfoPlistCachedKeys"]);
133
134 NSMutableSet *removed([NSMutableSet set]);
135 for (NSDictionary *info in [before allInfoDictionaries])
136 if (NSString *path = [info objectForKey:@"Path"])
137 [removed addObject:path];
138
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"];
144
145 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
146 if (NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]) {
147 [bundles setObject:path forKey:identifier];
148 [removed removeObject:path];
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
166 [info setObject:path forKey:@"Path"];
167 [info setObject:@"System" forKey:@"ApplicationType"];
168 [after addInfoDictionary:info];
169 } else
170 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
171 }
172 }
173 } else goto error;
174
175 [cache writeToFile:path atomically:YES];
176
177 if (workspace != nil) {
178 if ([workspace respondsToSelector:@selector(invalidateIconCache:)]) {
179 for (NSString *identifier in bundles)
180 [workspace invalidateIconCache:identifier];
181 } else {
182 for (NSString *identifier in bundles) {
183 NSString *path([bundles objectForKey:identifier]);
184 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
185 }
186 }
187
188 for (NSString *identifier in bundles) {
189 NSString *path([bundles objectForKey:identifier]);
190 if (kCFCoreFoundationVersionNumber >= 800)
191 [workspace registerApplicationDictionary:[after objectForKey:identifier]];
192 else
193 [workspace registerApplication:[NSURL fileURLWithPath:path]];
194 }
195
196 for (NSString *path in removed)
197 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
198 }
199 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
200 cached:
201
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]);
205
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]);
208
209 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache", home] UTF8String]);
210 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache-small", home] UTF8String]);
211
212 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/com.apple.IconsCache", home] UTF8String]);
213 }
214
215 system("killall installd");
216
217 } @finally {
218 system("killall -SIGCONT SpringBoard");
219 }
220
221 if (respring)
222 system("launchctl stop com.apple.SpringBoard");
223 else
224 notify_post("com.apple.mobile.application_installed");
225
226 [pool release];
227
228 return 0;
229}