]> git.saurik.com Git - cydia.git/blob - postinst.mm
Do not restart SpringBoard if symlink is damaged.
[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 bool success;
75 if (symlink(destiny, APPLICATIONS) != -1)
76 success = true;
77 else {
78 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
79 success = false;
80 }
81
82 // unneccessary, but feels better (I'm nervous)
83 symlink(destiny, target);
84
85 [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
86 return success;
87 }
88 }
89
90 _H<NSMutableDictionary> Sources_;
91 bool Changed_;
92
93 _H<NSString> System_;
94
95 int main(int argc, const char *argv[]) {
96 if (argc < 2 || strcmp(argv[1], "configure") != 0)
97 return 0;
98
99 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
100
101 size_t size;
102 sysctlbyname("kern.osversion", NULL, &size, NULL, 0);
103 char *osversion = new char[size];
104 if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) != -1)
105 System_ = [NSString stringWithUTF8String:osversion];
106
107 NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]);
108 NSUInteger version(0);
109
110 if (metadata != nil) {
111 Sources_ = [metadata objectForKey:@"Sources"];
112
113 if (NSNumber *number = [metadata objectForKey:@"Version"])
114 version = [number unsignedIntValue];
115 }
116
117 if (Sources_ == nil)
118 Sources_ = [NSMutableDictionary dictionaryWithCapacity:8];
119
120 if (version == 0) {
121 CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
122 CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
123 CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
124 CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./");
125 }
126
127 CydiaWriteSources();
128
129 if (FixApplications())
130 Finish("restart");
131
132 [pool release];
133 return 0;
134 }