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 FixProtections() {
28 for (const char *path : (const char *[]) {"/var/lib", "/var/cache", "/var/stash"}) {
30 if (system([[NSString stringWithFormat:@"/usr/libexec/cydia/setnsfpn %s", path] UTF8String]) != 0) {
31 fprintf(stderr, "failed to setnsfpn %s\n", path);
38 static void FixPermissions() {
39 DIR *stash(opendir("/var/stash"));
43 while (dirent *entry = readdir(stash)) {
44 const char *folder(entry->d_name);
45 if (strlen(folder) != 8)
47 if (strncmp(folder, "_.", 2) != 0)
51 sprintf(path, "/var/stash/%s", folder);
54 if (lstat(path, &stat) == -1)
56 if (!S_ISDIR(stat.st_mode))
65 #define APPLICATIONS "/Applications"
66 static bool FixApplications() {
68 ssize_t length(readlink(APPLICATIONS, target, sizeof(target)));
72 if (length >= sizeof(target)) // >= "just in case" (I'm nervous)
74 target[length] = '\0';
76 if (strlen(target) != 30)
78 if (memcmp(target, "/var/stash/Applications.", 24) != 0)
80 if (strchr(target + 24, '/') != NULL)
84 if (lstat(target, &stat) == -1)
86 if (!S_ISDIR(stat.st_mode))
89 char temp[] = "/var/stash/_.XXXXXX";
90 if (mkdtemp(temp) == NULL)
98 if (chmod(temp, 0755) == -1)
101 char destiny[strlen(temp) + 32];
102 sprintf(destiny, "%s%s", temp, APPLICATIONS);
104 if (unlink(APPLICATIONS) == -1)
107 if (rename(target, destiny) == -1) {
108 if (symlink(target, APPLICATIONS) == -1)
109 fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
113 if (symlink(destiny, APPLICATIONS) != -1)
116 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
120 // unneccessary, but feels better (I'm nervous)
121 symlink(destiny, target);
123 [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
128 _H<NSMutableDictionary> Sources_;
131 _H<NSString> System_;
133 int main(int argc, const char *argv[]) {
134 if (argc < 2 || strcmp(argv[1], "configure") != 0)
137 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
139 if (kCFCoreFoundationVersionNumber >= 1000)
140 if (!FixProtections())
144 sysctlbyname("kern.osversion", NULL, &size, NULL, 0);
145 char *osversion = new char[size];
146 if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) != -1)
147 System_ = [NSString stringWithUTF8String:osversion];
149 NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]);
150 NSUInteger version(0);
152 if (metadata != nil) {
153 Sources_ = [metadata objectForKey:@"Sources"];
155 if (NSNumber *number = [metadata objectForKey:@"Version"])
156 version = [number unsignedIntValue];
160 Sources_ = [NSMutableDictionary dictionaryWithCapacity:8];
163 CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
164 CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
165 CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
166 CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./");
171 #define OldCache_ "/var/root/Library/Caches/com.saurik.Cydia"
172 if (access(OldCache_, F_OK) == 0)
173 system("rm -rf " OldCache_);
175 #define NewCache_ "/var/mobile/Library/Caches/com.saurik.Cydia"
176 if (access(NewCache_ "/lists", F_OK) != 0 && errno == ENOENT) {
177 system("su -c 'mkdir -p " NewCache_ "' mobile");
178 system("cp -at " NewCache_ " /var/lib/apt/lists");
179 system("chown -R 501.501 " NewCache_ "/lists");
184 if (FixApplications())