1 #include "CyteKit/UCPlatform.h"
9 #include <sys/sysctl.h>
10 #include <sys/types.h>
12 #include <Menes/ObjectHandle.h>
14 void Finish(const char *finish) {
18 const char *cydia(getenv("CYDIA"));
22 int fd([[[[NSString stringWithUTF8String:cydia] componentsSeparatedByString:@" "] objectAtIndex:0] intValue]);
24 FILE *fout(fdopen(fd, "w"));
25 fprintf(fout, "finish:%s\n", finish);
29 static bool setnsfpn(const char *path) {
30 return system([[NSString stringWithFormat:@"/usr/libexec/cydia/setnsfpn %s", path] UTF8String]) == 0;
39 static StashStatus MoveStash() {
42 if (lstat("/var/stash", &stat) == -1)
43 return errno == ENOENT ? StashGood : StashFail;
44 else if (S_ISLNK(stat.st_mode))
46 else if (!S_ISDIR(stat.st_mode))
49 if (lstat("/var/db/stash", &stat) == -1) {
52 else return StashFail;
53 } else if (S_ISLNK(stat.st_mode))
54 // XXX: this is fixable
56 else if (!S_ISDIR(stat.st_mode))
59 if (!setnsfpn("/var/db/stash"))
61 if (system("mv -t /var/stash /var/db/stash/*") != 0)
63 if (rmdir("/var/db/stash") == -1)
67 if (!setnsfpn("/var/stash"))
70 if (rename("/var/stash", "/var/db/stash") == -1)
72 if (symlink("/var/db/stash", "/var/stash") != -1)
74 if (rename("/var/db/stash", "/var/stash") != -1)
77 fprintf(stderr, "/var/stash misplaced -- DO NOT REBOOT\n");
81 static bool FixProtections() {
82 const char *path("/var/lib");
84 if (!setnsfpn(path)) {
85 fprintf(stderr, "failed to setnsfpn %s\n", path);
92 static void FixPermissions() {
93 DIR *stash(opendir("/var/stash"));
97 while (dirent *entry = readdir(stash)) {
98 const char *folder(entry->d_name);
99 if (strlen(folder) != 8)
101 if (strncmp(folder, "_.", 2) != 0)
105 sprintf(path, "/var/stash/%s", folder);
108 if (lstat(path, &stat) == -1)
110 if (!S_ISDIR(stat.st_mode))
119 #define APPLICATIONS "/Applications"
120 static bool FixApplications() {
122 ssize_t length(readlink(APPLICATIONS, target, sizeof(target)));
126 if (length >= sizeof(target)) // >= "just in case" (I'm nervous)
128 target[length] = '\0';
130 if (strlen(target) != 30)
132 if (memcmp(target, "/var/stash/Applications.", 24) != 0)
134 if (strchr(target + 24, '/') != NULL)
138 if (lstat(target, &stat) == -1)
140 if (!S_ISDIR(stat.st_mode))
143 char temp[] = "/var/stash/_.XXXXXX";
144 if (mkdtemp(temp) == NULL)
152 if (chmod(temp, 0755) == -1)
155 char destiny[strlen(temp) + 32];
156 sprintf(destiny, "%s%s", temp, APPLICATIONS);
158 if (unlink(APPLICATIONS) == -1)
161 if (rename(target, destiny) == -1) {
162 if (symlink(target, APPLICATIONS) == -1)
163 fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
167 if (symlink(destiny, APPLICATIONS) != -1)
170 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
174 // unneccessary, but feels better (I'm nervous)
175 symlink(destiny, target);
177 [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
182 int main(int argc, const char *argv[]) {
183 if (argc < 2 || strcmp(argv[1], "configure") != 0)
186 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
190 if (kCFCoreFoundationVersionNumber >= 1000) {
191 if (!FixProtections())
193 switch (MoveStash()) {
198 fprintf(stderr, "failed to move stash\n");
205 #define OldCache_ "/var/root/Library/Caches/com.saurik.Cydia"
206 if (access(OldCache_, F_OK) == 0)
207 system("rm -rf " OldCache_);
209 #define NewCache_ "/var/mobile/Library/Caches/com.saurik.Cydia"
210 system("cd /; su -c 'mkdir -p " NewCache_ "' mobile");
211 if (access(NewCache_ "/lists", F_OK) != 0 && errno == ENOENT)
212 system("cp -at " NewCache_ " /var/lib/apt/lists");
213 system("chown -R 501.501 " NewCache_);
215 #define OldLibrary_ "/var/lib/cydia"
217 #define NewLibrary_ "/var/mobile/Library/Cydia"
218 system("cd /; su -c 'mkdir -p " NewLibrary_ "' mobile");
220 #define Cytore_ "/metadata.cb0"
222 #define CYDIA_LIST "/etc/apt/sources.list.d/cydia.list"
224 [[NSString stringWithFormat:@
225 "deb http://apt.saurik.com/ ios/%.2f main\n"
226 "deb http://apt.thebigboss.org/repofiles/cydia/ stable main\n"
227 "deb http://cydia.zodttd.com/repo/cydia/ stable main\n"
228 "deb http://apt.modmyi.com/ stable main\n"
229 , kCFCoreFoundationVersionNumber] writeToFile:@ CYDIA_LIST atomically:YES];
231 if (access(NewLibrary_ Cytore_, F_OK) != 0 && errno == ENOENT) {
232 if (access(NewCache_ Cytore_, F_OK) == 0)
233 system("mv -f " NewCache_ Cytore_ " " NewLibrary_);
234 else if (access(OldLibrary_ Cytore_, F_OK) == 0)
235 system("mv -f " OldLibrary_ Cytore_ " " NewLibrary_);
236 chown(NewLibrary_ Cytore_, 501, 501);
241 restart |= FixApplications();