]> git.saurik.com Git - cydia.git/blob - postinst.mm
Add a way to opt out of filesystem reorganization.
[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 static bool setnsfpn(const char *path) {
28 return system([[NSString stringWithFormat:@"/usr/libexec/cydia/setnsfpn %s", path] UTF8String]) == 0;
29 }
30
31 static 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
73 static bool FixProtections() {
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;
79 }
80
81 return true;
82 }
83
84 static 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
111 #define APPLICATIONS "/Applications"
112 static 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 {
158 bool success;
159 if (symlink(destiny, APPLICATIONS) != -1)
160 success = true;
161 else {
162 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
163 success = false;
164 }
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];
170 return success;
171 }
172 }
173
174 int main(int argc, const char *argv[]) {
175 if (argc < 2 || strcmp(argv[1], "configure") != 0)
176 return 0;
177
178 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
179
180 bool restart(false);
181
182 if (kCFCoreFoundationVersionNumber >= 1000) {
183 if (!FixProtections())
184 return 1;
185 if (MoveStash())
186 restart = true;
187 else {
188 fprintf(stderr, "failed to move stash\n");
189 return 1;
190 }
191 }
192
193 #define OldCache_ "/var/root/Library/Caches/com.saurik.Cydia"
194 if (access(OldCache_, F_OK) == 0)
195 system("rm -rf " OldCache_);
196
197 #define NewCache_ "/var/mobile/Library/Caches/com.saurik.Cydia"
198 system("cd /; su -c 'mkdir -p " NewCache_ "' mobile");
199 if (access(NewCache_ "/lists", F_OK) != 0 && errno == ENOENT)
200 system("cp -at " NewCache_ " /var/lib/apt/lists");
201 system("chown -R 501.501 " NewCache_);
202
203 #define OldLibrary_ "/var/lib/cydia"
204
205 #define NewLibrary_ "/var/mobile/Library/Cydia"
206 system("cd /; su -c 'mkdir -p " NewLibrary_ "' mobile");
207
208 #define Cytore_ "/metadata.cb0"
209
210 #define CYDIA_LIST "/etc/apt/sources.list.d/cydia.list"
211 unlink(CYDIA_LIST);
212 [[NSString stringWithFormat:@
213 "deb http://apt.saurik.com/ ios/%.2f main\n"
214 "deb http://apt.thebigboss.org/repofiles/cydia/ stable main\n"
215 "deb http://cydia.zodttd.com/repo/cydia/ stable main\n"
216 "deb http://apt.modmyi.com/ stable main\n"
217 , kCFCoreFoundationVersionNumber] writeToFile:@ CYDIA_LIST atomically:YES];
218
219 if (access(NewLibrary_ Cytore_, F_OK) != 0 && errno == ENOENT) {
220 if (access(NewCache_ Cytore_, F_OK) == 0)
221 system("mv -f " NewCache_ Cytore_ " " NewLibrary_);
222 else if (access(OldLibrary_ Cytore_, F_OK) == 0)
223 system("mv -f " OldLibrary_ Cytore_ " " NewLibrary_);
224 chown(NewLibrary_ Cytore_, 501, 501);
225 }
226
227 FixPermissions();
228
229 restart |= FixApplications();
230
231 if (restart)
232 Finish("restart");
233
234 [pool release];
235 return 0;
236 }