]> git.saurik.com Git - uikittools.git/blame - uicache.mm
Do not output (null) if capabilities unavailable.
[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
a7cec9cf
JF
89 bool respring(false);
90
7e0ec87a
JF
91 NSString *home(NSHomeDirectory());
92 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", home]);
7fb7ce13 93
9972f381
JF
94 system("killall -SIGSTOP SpringBoard");
95 sleep(1);
96
97 @try {
98
4ab9b1c7
JF
99 DeleteCSStores([home UTF8String]);
100
31effc97
JF
101 system("killall lsd");
102
020dfc54
JF
103 Class $LSApplicationWorkspace(objc_getClass("LSApplicationWorkspace"));
104 LSApplicationWorkspace *workspace($LSApplicationWorkspace == nil ? nil : [$LSApplicationWorkspace defaultWorkspace]);
1094bdcb 105
c9bb7c8e
JF
106 if ([workspace respondsToSelector:@selector(invalidateIconCache:)])
107 while (![workspace invalidateIconCache:nil])
108 sleep(1);
109
020dfc54 110 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
1094bdcb
JF
111 NSFileManager *manager = [NSFileManager defaultManager];
112 NSError *error = nil;
113
0183260a 114 NSMutableDictionary *bundles([NSMutableDictionary dictionaryWithCapacity:16]);
020dfc54 115
3ffcdfdb
JF
116 id after = [cache objectForKey:@"System"];
117 if (after == nil) { error:
22d64ef4
JF
118 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
119 goto cached;
120 }
1094bdcb 121
3ffcdfdb
JF
122 id before([[after copy] autorelease]);
123 [after removeAllObjects];
124
125 NSArray *cached([cache objectForKey:@"InfoPlistCachedKeys"]);
1094bdcb 126
1bd75e96
JF
127 NSMutableSet *removed([NSMutableSet set]);
128 for (NSDictionary *info in [before allInfoDictionaries])
129 if (NSString *path = [info objectForKey:@"Path"])
130 [removed addObject:path];
131
1094bdcb
JF
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"];
020dfc54
JF
137
138 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
0183260a
JF
139 if (NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]) {
140 [bundles setObject:path forKey:identifier];
1bd75e96 141 [removed removeObject:path];
3ffcdfdb
JF
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
2a72f832
JF
159 [info setObject:path forKey:@"Path"];
160 [info setObject:@"System" forKey:@"ApplicationType"];
3ffcdfdb 161 [after addInfoDictionary:info];
020dfc54
JF
162 } else
163 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
1094bdcb
JF
164 }
165 }
166 } else goto error;
167
7fb7ce13 168 [cache writeToFile:path atomically:YES];
1094bdcb 169
37e55bd3 170 if (workspace != nil) {
97b54c51
JF
171 if ([workspace respondsToSelector:@selector(invalidateIconCache:)]) {
172 for (NSString *identifier in bundles)
0183260a 173 [workspace invalidateIconCache:identifier];
97b54c51
JF
174 } else {
175 for (NSString *identifier in bundles) {
176 NSString *path([bundles objectForKey:identifier]);
177 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
178 }
179 }
37e55bd3
JF
180
181 for (NSString *identifier in bundles) {
182 NSString *path([bundles objectForKey:identifier]);
0183260a 183 [workspace registerApplication:[NSURL fileURLWithPath:path]];
5ac04f0f 184 }
1bd75e96
JF
185
186 for (NSString *path in removed)
187 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
37e55bd3 188 }
7fb7ce13 189 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
22d64ef4 190 cached:
1094bdcb 191
a7cec9cf
JF
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]);
adefb9f1 195
a7cec9cf
JF
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]);
7e0ec87a 198
a7cec9cf
JF
199 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache", home] UTF8String]);
200 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache-small", home] UTF8String]);
adefb9f1 201
a7cec9cf
JF
202 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/com.apple.IconsCache", home] UTF8String]);
203 }
7e0ec87a 204
a7cec9cf 205 system("killall installd");
7e0ec87a 206
9972f381
JF
207 } @finally {
208 system("killall -SIGCONT SpringBoard");
209 }
210
a7cec9cf
JF
211 if (respring)
212 system("launchctl stop com.apple.SpringBoard");
213 else
214 notify_post("com.apple.mobile.application_installed");
1094bdcb
JF
215
216 [pool release];
217
218 return 0;
219}