]> git.saurik.com Git - cydia.git/blob - postinst.mm
Fix permissions of _.* containers on new stashes.
[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 void FixPermissions() {
28 DIR *stash(opendir("/var/stash"));
29 if (stash == NULL)
30 return;
31
32 while (dirent *entry = readdir(stash)) {
33 const char *folder(entry->d_name);
34 if (strlen(folder) != 8)
35 continue;
36 if (strncmp(folder, "_.", 2) != 0)
37 continue;
38
39 char path[1024];
40 sprintf(path, "/var/stash/%s", folder);
41
42 struct stat stat;
43 if (lstat(path, &stat) == -1)
44 continue;
45 if (!S_ISDIR(stat.st_mode))
46 continue;
47
48 chmod(path, 0755);
49 }
50
51 closedir(stash);
52 }
53
54 #define APPLICATIONS "/Applications"
55 static bool FixApplications() {
56 char target[1024];
57 ssize_t length(readlink(APPLICATIONS, target, sizeof(target)));
58 if (length == -1)
59 return false;
60
61 if (length >= sizeof(target)) // >= "just in case" (I'm nervous)
62 return false;
63 target[length] = '\0';
64
65 if (strlen(target) != 30)
66 return false;
67 if (memcmp(target, "/var/stash/Applications.", 24) != 0)
68 return false;
69 if (strchr(target + 24, '/') != NULL)
70 return false;
71
72 struct stat stat;
73 if (lstat(target, &stat) == -1)
74 return false;
75 if (!S_ISDIR(stat.st_mode))
76 return false;
77
78 char temp[] = "/var/stash/_.XXXXXX";
79 if (mkdtemp(temp) == NULL)
80 return false;
81
82 if (false) undo: {
83 unlink(temp);
84 return false;
85 }
86
87 if (chmod(temp, 0755) == -1)
88 goto undo;
89
90 char destiny[strlen(temp) + 32];
91 sprintf(destiny, "%s%s", temp, APPLICATIONS);
92
93 if (unlink(APPLICATIONS) == -1)
94 goto undo;
95
96 if (rename(target, destiny) == -1) {
97 if (symlink(target, APPLICATIONS) == -1)
98 fprintf(stderr, "/Applications damaged -- DO NOT REBOOT\n");
99 goto undo;
100 } else {
101 bool success;
102 if (symlink(destiny, APPLICATIONS) != -1)
103 success = true;
104 else {
105 fprintf(stderr, "/var/stash/Applications damaged -- DO NOT REBOOT\n");
106 success = false;
107 }
108
109 // unneccessary, but feels better (I'm nervous)
110 symlink(destiny, target);
111
112 [@APPLICATIONS writeToFile:[NSString stringWithFormat:@"%s.lnk", temp] atomically:YES encoding:NSNonLossyASCIIStringEncoding error:NULL];
113 return success;
114 }
115 }
116
117 _H<NSMutableDictionary> Sources_;
118 bool Changed_;
119
120 _H<NSString> System_;
121
122 int main(int argc, const char *argv[]) {
123 if (argc < 2 || strcmp(argv[1], "configure") != 0)
124 return 0;
125
126 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
127
128 size_t size;
129 sysctlbyname("kern.osversion", NULL, &size, NULL, 0);
130 char *osversion = new char[size];
131 if (sysctlbyname("kern.osversion", osversion, &size, NULL, 0) != -1)
132 System_ = [NSString stringWithUTF8String:osversion];
133
134 NSDictionary *metadata([[[NSMutableDictionary alloc] initWithContentsOfFile:@"/var/lib/cydia/metadata.plist"] autorelease]);
135 NSUInteger version(0);
136
137 if (metadata != nil) {
138 Sources_ = [metadata objectForKey:@"Sources"];
139
140 if (NSNumber *number = [metadata objectForKey:@"Version"])
141 version = [number unsignedIntValue];
142 }
143
144 if (Sources_ == nil)
145 Sources_ = [NSMutableDictionary dictionaryWithCapacity:8];
146
147 if (version == 0) {
148 CydiaAddSource(@"http://apt.thebigboss.org/repofiles/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
149 CydiaAddSource(@"http://apt.modmyi.com/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
150 CydiaAddSource(@"http://cydia.zodttd.com/repo/cydia/", @"stable", [NSMutableArray arrayWithObject:@"main"]);
151 CydiaAddSource(@"http://repo666.ultrasn0w.com/", @"./");
152 }
153
154 CydiaWriteSources();
155
156 FixPermissions();
157
158 if (FixApplications())
159 Finish("restart");
160
161 [pool release];
162 return 0;
163 }