]> git.saurik.com Git - cydia.git/blame - postinst.mm
We do not actually need to setnsfpn on /var/cache.
[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
e4d9a4f2
JF
27static bool setnsfpn(const char *path) {
28 return system([[NSString stringWithFormat:@"/usr/libexec/cydia/setnsfpn %s", path] UTF8String]) == 0;
29}
30
bcc58607
JF
31static bool MoveStash() {
32 struct stat stat;
33
34 if (lstat("/var/stash", &stat) == -1)
35 return errno == ENOENT;
36 else if (S_ISLNK(stat.st_mode))
37 return true;
38 else if (!S_ISDIR(stat.st_mode))
39 return false;
40
41 if (lstat("/var/db/stash", &stat) == -1) {
42 if (errno == ENOENT)
43 goto move;
44 else return false;
45 } else if (S_ISLNK(stat.st_mode))
46 // XXX: this is fixable
47 return false;
48 else if (!S_ISDIR(stat.st_mode))
49 return false;
50 else {
51 if (!setnsfpn("/var/db/stash"))
52 return false;
53 if (system("mv -t /var/stash /var/db/stash/*") != 0)
54 return false;
55 if (rmdir("/var/db/stash") == -1)
56 return false;
57 } move:
58
59 if (!setnsfpn("/var/stash"))
60 return false;
61
62 if (rename("/var/stash", "/var/db/stash") == -1)
63 return false;
64 if (symlink("/var/db/stash", "/var/stash") != -1)
65 return true;
66 if (rename("/var/db/stash", "/var/stash") != -1)
67 return false;
68
69 fprintf(stderr, "/var/stash misplaced -- DO NOT REBOOT\n");
70 return false;
71}
72
82e370b3 73static bool FixProtections() {
3a686b9f
JF
74 const char *path("/var/lib");
75 mkdir(path, 0755);
76 if (!setnsfpn(path)) {
77 fprintf(stderr, "failed to setnsfpn %s\n", path);
78 return false;
82e370b3 79 }
bcc58607 80
82e370b3
JF
81 return true;
82}
83
b132a972
JF
84static void FixPermissions() {
85 DIR *stash(opendir("/var/stash"));
86 if (stash == NULL)
87 return;
88
89 while (dirent *entry = readdir(stash)) {
90 const char *folder(entry->d_name);
91 if (strlen(folder) != 8)
92 continue;
93 if (strncmp(folder, "_.", 2) != 0)
94 continue;
95
96 char path[1024];
97 sprintf(path, "/var/stash/%s", folder);
98
99 struct stat stat;
100 if (lstat(path, &stat) == -1)
101 continue;
102 if (!S_ISDIR(stat.st_mode))
103 continue;
104
105 chmod(path, 0755);
106 }
107
108 closedir(stash);
109}
110
86631cc4
JF
111#define APPLICATIONS "/Applications"
112static bool FixApplications() {
113 char target[1024];
114 ssize_t length(readlink(APPLICATIONS, target, sizeof(target)));
115 if (length == -1)
116 return false;
117
118 if (length >= sizeof(target)) // >= "just in case" (I'm nervous)
119 return false;
120 target[length] = '\0';
121
122 if (strlen(target) != 30)
123 return false;
124 if (memcmp(target, "/var/stash/Applications.", 24) != 0)
125 return false;
126 if (strchr(target + 24, '/') != NULL)
127 return false;
128
129 struct stat stat;
130 if (lstat(target, &stat) == -1)
131 return false;
132 if (!S_ISDIR(stat.st_mode))
133 return false;
134
135 char temp[] = "/var/stash/_.XXXXXX";
136 if (mkdtemp(temp) == NULL)
137 return false;
138
139 if (false) undo: {
140 unlink(temp);
141 return false;
142 }
143
144 if (chmod(temp, 0755) == -1)
145 goto undo;
146
147 char destiny[strlen(temp) + 32];
148 sprintf(destiny, "%s%s", temp, APPLICATIONS);
149
150 if (unlink(APPLICATIONS) == -1)
151 goto undo;
152
153 if (rename(target, destiny) == -1) {
154 if (symlink(target, APPLICATIONS) == -1)
155 fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
156 goto undo;
157 } else {
2e007a72
JF
158 bool success;
159 if (symlink(destiny, APPLICATIONS) != -1)
160 success = true;
161 else {
86631cc4 162 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
2e007a72
JF
163 success = false;
164 }
86631cc4
JF
165
166 // unneccessary, but feels better (I'm nervous)
167 symlink(destiny, target);
168
169 [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
2e007a72 170 return success;
86631cc4
JF
171 }
172}
173
eaa9f122 174_H<NSMutableDictionary> Sources_;
eaa9f122
JF
175bool Changed_;
176
407564b5 177_H<NSString> System_;
eaa9f122
JF
178
179int main(int argc, const char *argv[]) {
180 if (argc < 2 || strcmp(argv[1], "configure") != 0)
181 return 0;
182
183 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
184
5530e234
JF
185 bool restart(false);
186
187 if (kCFCoreFoundationVersionNumber >= 1000) {
82e370b3
JF
188 if (!FixProtections())
189 return 1;
5530e234
JF
190 if (MoveStash())
191 restart = true;
192 else {
193 fprintf(stderr, "failed to move stash\n");
194 return 1;
195 }
196 }
82e370b3 197
407564b5
JF
198 size_t size;
199 sysctlbyname("kern.osversion", NULL, &size, NULL, 0);
200 char *osversion = new char[size];
201 if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) != -1)
202 System_ = [NSString stringWithUTF8String:osversion];
eaa9f122
JF
203
204 NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]);
205 NSUInteger version(0);
206
207 if (metadata != nil) {
208 Sources_ = [metadata objectForKey:@"Sources"];
eaa9f122
JF
209
210 if (NSNumber *number = [metadata objectForKey:@"Version"])
211 version = [number unsignedIntValue];
212 }
213
eaa9f122
JF
214 if (Sources_ == nil)
215 Sources_ = [NSMutableDictionary dictionaryWithCapacity:8];
216
217 if (version == 0) {
218 CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
219 CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
220 CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
221 CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./");
222 }
223
224 CydiaWriteSources();
225
00407aa9
JF
226 #define OldCache_ "/var/root/Library/Caches/com.saurik.Cydia"
227 if (access(OldCache_, F_OK) == 0)
228 system("rm -rf " OldCache_);
229
e3799a2e 230 #define NewCache_ "/var/mobile/Library/Caches/com.saurik.Cydia"
4cfa3e5b
JF
231 system("cd /; su -c 'mkdir -p " NewCache_ "' mobile");
232
e3799a2e 233 if (access(NewCache_ "/lists", F_OK) != 0 && errno == ENOENT) {
e3799a2e
JF
234 system("cp -at " NewCache_ " /var/lib/apt/lists");
235 system("chown -R 501.501 " NewCache_ "/lists");
236 }
237
a4a93d59
JF
238 #define OldLibrary_ "/var/lib/cydia"
239
240 #define NewLibrary_ "/var/mobile/Library/Cydia"
241 system("cd /; su -c 'mkdir -p " NewLibrary_ "' mobile");
242
243 #define Cytore_ "/metadata.cb0"
244
245 if (access(NewLibrary_ Cytore_, F_OK) != 0 && errno == ENOENT) {
246 if (access(NewCache_ Cytore_, F_OK) == 0)
247 system("mv -f " NewCache_ Cytore_ " " NewLibrary_);
248 else if (access(OldLibrary_ Cytore_, F_OK) == 0)
249 system("mv -f " OldLibrary_ Cytore_ " " NewLibrary_);
250 chown(NewLibrary_ Cytore_, 501, 501);
251 }
1a2f455b 252
b132a972
JF
253 FixPermissions();
254
5530e234
JF
255 restart |= FixApplications();
256
257 if (restart)
86631cc4
JF
258 Finish("restart");
259
eaa9f122
JF
260 [pool release];
261 return 0;
262}