1 #include "CyteKit/UCPlatform.h"
7 #include <sys/sysctl.h>
10 #include <Menes/ObjectHandle.h>
12 void Finish(const char *finish) {
16 const char *cydia(getenv("CYDIA"));
20 int fd([[[[NSString stringWithUTF8String:cydia] componentsSeparatedByString:@" "] objectAtIndex:0] intValue]);
22 FILE *fout(fdopen(fd, "w"));
23 fprintf(fout, "finish:%s\n", finish);
27 static bool setnsfpn(const char *path) {
28 return system([[NSString stringWithFormat:@"/usr/libexec/cydia/setnsfpn %s", path] UTF8String]) == 0;
37 static StashStatus MoveStash() {
40 if (lstat("/var/stash", &stat) == -1)
41 return errno == ENOENT ? StashGood : StashFail;
42 else if (S_ISLNK(stat.st_mode))
44 else if (!S_ISDIR(stat.st_mode))
47 if (lstat("/var/db/stash", &stat) == -1) {
50 else return StashFail;
51 } else if (S_ISLNK(stat.st_mode))
52 // XXX: this is fixable
54 else if (!S_ISDIR(stat.st_mode))
57 if (!setnsfpn("/var/db/stash"))
59 if (system("mv -t /var/stash /var/db/stash/*") != 0)
61 if (rmdir("/var/db/stash") == -1)
65 if (!setnsfpn("/var/stash"))
68 if (rename("/var/stash", "/var/db/stash") == -1)
70 if (symlink("/var/db/stash", "/var/stash") != -1)
72 if (rename("/var/db/stash", "/var/stash") != -1)
75 fprintf(stderr, "/var/stash misplaced -- DO NOT REBOOT\n");
79 static bool FixProtections() {
80 const char *path("/var/lib");
82 if (!setnsfpn(path)) {
83 fprintf(stderr, "failed to setnsfpn %s\n", path);
90 static void FixPermissions() {
91 DIR *stash(opendir("/var/stash"));
95 while (dirent *entry = readdir(stash)) {
96 const char *folder(entry->d_name);
97 if (strlen(folder) != 8)
99 if (strncmp(folder, "_.", 2) != 0)
103 sprintf(path, "/var/stash/%s", folder);
106 if (lstat(path, &stat) == -1)
108 if (!S_ISDIR(stat.st_mode))
117 #define APPLICATIONS "/Applications"
118 static bool FixApplications() {
120 ssize_t length(readlink(APPLICATIONS, target, sizeof(target)));
124 if (length >= sizeof(target)) // >= "just in case" (I'm nervous)
126 target[length] = '\0';
128 if (strlen(target) != 30)
130 if (memcmp(target, "/var/stash/Applications.", 24) != 0)
132 if (strchr(target + 24, '/') != NULL)
136 if (lstat(target, &stat) == -1)
138 if (!S_ISDIR(stat.st_mode))
141 char temp[] = "/var/stash/_.XXXXXX";
142 if (mkdtemp(temp) == NULL)
150 if (chmod(temp, 0755) == -1)
153 char destiny[strlen(temp) + 32];
154 sprintf(destiny, "%s%s", temp, APPLICATIONS);
156 if (unlink(APPLICATIONS) == -1)
159 if (rename(target, destiny) == -1) {
160 if (symlink(target, APPLICATIONS) == -1)
161 fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
165 if (symlink(destiny, APPLICATIONS) != -1)
168 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
172 // unneccessary, but feels better (I'm nervous)
173 symlink(destiny, target);
175 [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
180 int main(int argc, const char *argv[]) {
181 if (argc < 2 || strcmp(argv[1], "configure") != 0)
184 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
188 if (kCFCoreFoundationVersionNumber >= 1000) {
189 if (!FixProtections())
191 switch (MoveStash()) {
196 fprintf(stderr, "failed to move stash\n");
203 #define OldCache_ "/var/root/Library/Caches/com.saurik.Cydia"
204 if (access(OldCache_, F_OK) == 0)
205 system("rm -rf " OldCache_);
207 #define NewCache_ "/var/mobile/Library/Caches/com.saurik.Cydia"
208 system("cd /; su -c 'mkdir -p " NewCache_ "' mobile");
209 if (access(NewCache_ "/lists", F_OK) != 0 && errno == ENOENT)
210 system("cp -at " NewCache_ " /var/lib/apt/lists");
211 system("chown -R 501.501 " NewCache_);
213 #define OldLibrary_ "/var/lib/cydia"
215 #define NewLibrary_ "/var/mobile/Library/Cydia"
216 system("cd /; su -c 'mkdir -p " NewLibrary_ "' mobile");
218 #define Cytore_ "/metadata.cb0"
220 #define CYDIA_LIST "/etc/apt/sources.list.d/cydia.list"
222 [[NSString stringWithFormat:@
223 "deb http://apt.saurik.com/ ios/%.2f main\n"
224 "deb http://apt.thebigboss.org/repofiles/cydia/ stable main\n"
225 "deb http://cydia.zodttd.com/repo/cydia/ stable main\n"
226 "deb http://apt.modmyi.com/ stable main\n"
227 , kCFCoreFoundationVersionNumber] writeToFile:@ CYDIA_LIST atomically:YES];
229 if (access(NewLibrary_ Cytore_, F_OK) != 0 && errno == ENOENT) {
230 if (access(NewCache_ Cytore_, F_OK) == 0)
231 system("mv -f " NewCache_ Cytore_ " " NewLibrary_);
232 else if (access(OldLibrary_ Cytore_, F_OK) == 0)
233 system("mv -f " OldLibrary_ Cytore_ " " NewLibrary_);
234 chown(NewLibrary_ Cytore_, 501, 501);
239 restart |= FixApplications();