]> git.saurik.com Git - cydia.git/blame - postinst.mm
As the comment admitted: do not compare localized.
[cydia.git] / postinst.mm
CommitLineData
86631cc4 1#include <dirent.h>
eaa9f122 2#include <strings.h>
86631cc4 3
eaa9f122
JF
4#include <Sources.h>
5
86631cc4 6#include <sys/stat.h>
407564b5 7#include <sys/sysctl.h>
86631cc4 8#include <sys/types.h>
407564b5 9
608321d0 10#include <Menes/ObjectHandle.h>
eaa9f122 11
86631cc4
JF
12void 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"
28static 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 {
2e007a72
JF
74 bool success;
75 if (symlink(destiny, APPLICATIONS) != -1)
76 success = true;
77 else {
86631cc4 78 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
2e007a72
JF
79 success = false;
80 }
86631cc4
JF
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];
2e007a72 86 return success;
86631cc4
JF
87 }
88}
89
eaa9f122 90_H<NSMutableDictionary> Sources_;
eaa9f122
JF
91bool Changed_;
92
407564b5 93_H<NSString> System_;
eaa9f122
JF
94
95int 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
407564b5
JF
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];
eaa9f122
JF
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"];
eaa9f122
JF
112
113 if (NSNumber *number = [metadata objectForKey:@"Version"])
114 version = [number unsignedIntValue];
115 }
116
eaa9f122
JF
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
86631cc4
JF
129 if (FixApplications())
130 Finish("restart");
131
eaa9f122
JF
132 [pool release];
133 return 0;
134}