]> git.saurik.com Git - uikittools.git/blob - uicache.mm
Delete all of the .csstore files.
[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 @end
64
65 @interface NSMutableDictionary (Cydia)
66 - (void) addInfoDictionary:(NSDictionary *)info;
67 @end
68
69 @implementation NSMutableDictionary (Cydia)
70
71 - (void) addInfoDictionary:(NSDictionary *)info {
72 NSString *bundle = [info objectForKey:@"CFBundleIdentifier"];
73 [self setObject:info forKey:bundle];
74 }
75
76 @end
77
78 int main(int argc, const char *argv[]) {
79 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
80
81 bool respring(false);
82
83 NSString *home(NSHomeDirectory());
84 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", home]);
85
86 system("killall -SIGSTOP SpringBoard");
87 sleep(1);
88
89 @try {
90
91 DeleteCSStores([home UTF8String]);
92
93 system("killall lsd");
94
95 Class $LSApplicationWorkspace(objc_getClass("LSApplicationWorkspace"));
96 LSApplicationWorkspace *workspace($LSApplicationWorkspace == nil ? nil : [$LSApplicationWorkspace defaultWorkspace]);
97
98 if ([workspace respondsToSelector:@selector(invalidateIconCache:)])
99 while (![workspace invalidateIconCache:nil])
100 sleep(1);
101
102 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
103 NSFileManager *manager = [NSFileManager defaultManager];
104 NSError *error = nil;
105
106 NSMutableDictionary *bundles([NSMutableDictionary dictionaryWithCapacity:16]);
107
108 id system = [cache objectForKey:@"System"];
109 if (system == nil)
110 goto error;
111
112 [system removeAllObjects];
113
114 if (NSArray *apps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]) {
115 for (NSString *app in apps)
116 if ([app hasSuffix:@".app"]) {
117 NSString *path = [@"/Applications" stringByAppendingPathComponent:app];
118 NSString *plist = [path stringByAppendingPathComponent:@"Info.plist"];
119
120 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
121 if (NSString *identifier = [info objectForKey:@"CFBundleIdentifier"]) {
122 [bundles setObject:path forKey:identifier];
123 [info setObject:path forKey:@"Path"];
124 [info setObject:@"System" forKey:@"ApplicationType"];
125 [system addInfoDictionary:info];
126 } else
127 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
128 }
129 }
130 } else goto error;
131
132 [cache writeToFile:path atomically:YES];
133
134 if (workspace != nil) {
135 if ([workspace respondsToSelector:@selector(invalidateIconCache:)]) {
136 for (NSString *identifier in bundles)
137 [workspace invalidateIconCache:identifier];
138 } else {
139 for (NSString *identifier in bundles) {
140 NSString *path([bundles objectForKey:identifier]);
141 [workspace unregisterApplication:[NSURL fileURLWithPath:path]];
142 }
143 }
144
145 for (NSString *identifier in bundles) {
146 NSString *path([bundles objectForKey:identifier]);
147 [workspace registerApplication:[NSURL fileURLWithPath:path]];
148 }
149 }
150
151 if (false) error:
152 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
153 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
154
155 if (respring || kCFCoreFoundationVersionNumber >= 550.32) {
156 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons", home] UTF8String]);
157 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-icons.plist", home] UTF8String]);
158
159 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons", home] UTF8String]);
160 unlink([[NSString stringWithFormat:@"%@/Library/Caches/com.apple.springboard-imagecache-smallicons.plist", home] UTF8String]);
161
162 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache", home] UTF8String]);
163 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/SpringBoardIconCache-small", home] UTF8String]);
164
165 system([[NSString stringWithFormat:@"rm -rf %@/Library/Caches/com.apple.IconsCache", home] UTF8String]);
166 }
167
168 system("killall installd");
169
170 } @finally {
171 system("killall -SIGCONT SpringBoard");
172 }
173
174 if (respring)
175 system("launchctl stop com.apple.SpringBoard");
176 else
177 notify_post("com.apple.mobile.application_installed");
178
179 [pool release];
180
181 return 0;
182 }