]> git.saurik.com Git - uikittools.git/blob - uicache.mm
Do not output (null) if capabilities unavailable.
[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 bool respring(false);
90
91 NSString *home(NSHomeDirectory());
92 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", home]);
93
94 system("killall -SIGSTOP SpringBoard");
95 sleep(1);
96
97 @try {
98
99 DeleteCSStores([home UTF8String]);
100
101 system("killall lsd");
102
103 Class $LSApplicationWorkspace(objc_getClass("LSApplicationWorkspace"));
104 LSApplicationWorkspace *workspace($LSApplicationWorkspace == nil ? nil : [$LSApplicationWorkspace defaultWorkspace]);
105
106 if ([workspace respondsToSelector:@selector(invalidateIconCache:)])
107 while (![workspace invalidateIconCache:nil])
108 sleep(1);
109
110 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
111 NSFileManager *manager = [NSFileManager defaultManager];
112 NSError *error = nil;
113
114 NSMutableDictionary *bundles([NSMutableDictionary dictionaryWithCapacity:16]);
115
116 id after = [cache objectForKey:@"System"];
117 if (after == nil) { error:
118 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
119 goto cached;
120 }
121
122 id before([[after copy] autorelease]);
123 [after removeAllObjects];
124
125 NSArray *cached([cache objectForKey:@"InfoPlistCachedKeys"]);
126
127 NSMutableSet *removed([NSMutableSet set]);
128 for (NSDictionary *info in [before allInfoDictionaries])
129 if (NSString *path = [info objectForKey:@"Path"])
130 [removed addObject:path];
131
132 if (NSArray *apps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]) {
133 for (NSString *app in apps)
134 if ([app hasSuffix:@".app"]) {
135 NSString *path = [@"/Applications" stringByAppendingPathComponent:app];
136 NSString *plist = [path stringByAppendingPathComponent:@"Info.plist"];
137
138 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
139 if (NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]) {
140 [bundles setObject:path forKey:identifier];
141 [removed removeObject:path];
142
143 if (cached != nil) {
144 NSMutableDictionary *merged([before objectForKey:identifier]);
145 if (merged == nil)
146 merged = [NSMutableDictionary dictionary];
147 else
148 merged = [[merged mutableCopy] autorelease];
149
150 for (NSString *key in cached)
151 if (NSObject *value = [info objectForKey:key])
152 [merged setObject:value forKey:key];
153 else
154 [merged removeObjectForKey:key];
155
156 info = merged;
157 }
158
159 [info setObject:path forKey:@"Path"];
160 [info setObject:@"System" forKey:@"ApplicationType"];
161 [after addInfoDictionary:info];
162 } else
163 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
164 }
165 }
166 } else goto error;
167
168 [cache writeToFile:path atomically:YES];
169
170 if (workspace != nil) {
171 if ([workspace respondsToSelector:@selector(invalidateIconCache:)]) {
172 for (NSString *identifier in bundles)
173 [workspace invalidateIconCache:identifier];
174 } else {
175 for (NSString *identifier in bundles) {
176 NSString *path([bundles objectForKey:identifier]);
177 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
178 }
179 }
180
181 for (NSString *identifier in bundles) {
182 NSString *path([bundles objectForKey:identifier]);
183 [workspace registerApplication:[NSURL fileURLWithPath:path]];
184 }
185
186 for (NSString *path in removed)
187 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
188 }
189 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
190 cached:
191
192 if (respring || kCFCoreFoundationVersionNumber >= 550.32) {
193 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons", home] UTF8String]);
194 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons.plist", home] UTF8String]);
195
196 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons", home] UTF8String]);
197 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons.plist", home] UTF8String]);
198
199 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache", home] UTF8String]);
200 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache-small", home] UTF8String]);
201
202 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/com.apple.IconsCache", home] UTF8String]);
203 }
204
205 system("killall installd");
206
207 } @finally {
208 system("killall -SIGCONT SpringBoard");
209 }
210
211 if (respring)
212 system("launchctl stop com.apple.SpringBoard");
213 else
214 notify_post("com.apple.mobile.application_installed");
215
216 [pool release];
217
218 return 0;
219 }