]> git.saurik.com Git - cydia.git/blob - postinst.mm
a29694394eb9164ca7484ca6c8b4fee0da8bef1f
[cydia.git] / postinst.mm
1 #include <dirent.h>
2 #include <strings.h>
3
4 #include <Sources.h>
5
6 #include <sys/stat.h>
7 #include <sys/sysctl.h>
8 #include <sys/types.h>
9
10 #include <Menes/ObjectHandle.h>
11
12 void Finish(const char *finish) {
13 if (finish == NULL)
14 return;
15
16 const char *cydia(getenv("CYDIA"));
17 if (cydia == NULL)
18 return;
19
20 int fd([[[[NSString stringWithUTF8String:cydia] componentsSeparatedByString:@" "] objectAtIndex:0] intValue]);
21
22 FILE *fout(fdopen(fd, "w"));
23 fprintf(fout, "finish:%s\n", finish);
24 fclose(fout);
25 }
26
27 #define APPLICATIONS "/Applications"
28 static bool FixApplications() {
29 char target[1024];
30 ssize_t length(readlink(APPLICATIONS, target, sizeof(target)));
31 if (length == -1)
32 return false;
33
34 if (length >= sizeof(target)) // >= "just in case" (I'm nervous)
35 return false;
36 target[length] = '\0';
37
38 if (strlen(target) != 30)
39 return false;
40 if (memcmp(target, "/var/stash/Applications.", 24) != 0)
41 return false;
42 if (strchr(target + 24, '/') != NULL)
43 return false;
44
45 struct stat stat;
46 if (lstat(target, &stat) == -1)
47 return false;
48 if (!S_ISDIR(stat.st_mode))
49 return false;
50
51 char temp[] = "/var/stash/_.XXXXXX";
52 if (mkdtemp(temp) == NULL)
53 return false;
54
55 if (false) undo: {
56 unlink(temp);
57 return false;
58 }
59
60 if (chmod(temp, 0755) == -1)
61 goto undo;
62
63 char destiny[strlen(temp) + 32];
64 sprintf(destiny, "%s%s", temp, APPLICATIONS);
65
66 if (unlink(APPLICATIONS) == -1)
67 goto undo;
68
69 if (rename(target, destiny) == -1) {
70 if (symlink(target, APPLICATIONS) == -1)
71 fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
72 goto undo;
73 } else {
74 if (symlink(destiny, APPLICATIONS) == -1)
75 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
76
77 // unneccessary, but feels better (I'm nervous)
78 symlink(destiny, target);
79
80 [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
81 return true;
82 }
83 }
84
85 _H<NSMutableDictionary> Sources_;
86 bool Changed_;
87
88 _H<NSString> System_;
89
90 int main(int argc, const char *argv[]) {
91 if (argc < 2 || strcmp(argv[1], "configure") != 0)
92 return 0;
93
94 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
95
96 size_t size;
97 sysctlbyname("kern.osversion", NULL, &size, NULL, 0);
98 char *osversion = new char[size];
99 if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) != -1)
100 System_ = [NSString stringWithUTF8String:osversion];
101
102 NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]);
103 NSUInteger version(0);
104
105 if (metadata != nil) {
106 Sources_ = [metadata objectForKey:@"Sources"];
107
108 if (NSNumber *number = [metadata objectForKey:@"Version"])
109 version = [number unsignedIntValue];
110 }
111
112 if (Sources_ == nil)
113 Sources_ = [NSMutableDictionary dictionaryWithCapacity:8];
114
115 if (version == 0) {
116 CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
117 CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
118 CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
119 CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./");
120 }
121
122 CydiaWriteSources();
123
124 if (FixApplications())
125 Finish("restart");
126
127 [pool release];
128 return 0;
129 }