forked from sparkle-project/Sparkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinish_installation.m
214 lines (170 loc) · 6.78 KB
/
finish_installation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#import <AppKit/AppKit.h>
#import "SUInstaller.h"
#import "SUHost.h"
#import "SUStandardVersionComparator.h"
#import "SUStatusController.h"
#import "SUPlainInstallerInternals.h"
#import "SULog.h"
#include <unistd.h>
#define LONG_INSTALLATION_TIME 5 // If the Installation takes longer than this time the Application Icon is shown in the Dock so that the user has some feedback.
#define CHECK_FOR_PARENT_TO_QUIT_TIME .5 // Time this app uses to recheck if the parent has already died.
@interface TerminationListener : NSObject
{
const char *hostpath;
const char *executablepath;
pid_t parentprocessid;
const char *folderpath;
NSString *selfPath;
NSString *installationPath;
NSTimer *watchdogTimer;
NSTimer *longInstallationTimer;
SUHost *host;
BOOL shouldRelaunch;
}
- (void) parentHasQuit;
- (void) relaunch;
- (void) install;
- (void) showAppIconInDock:(NSTimer *)aTimer;
- (void) watchdog:(NSTimer *)aTimer;
@end
@implementation TerminationListener
- (id) initWithHostPath:(const char *)inhostpath executablePath:(const char *)execpath parentProcessId:(pid_t)ppid folderPath: (const char*)infolderpath shouldRelaunch:(BOOL)relaunch
selfPath: (NSString*)inSelfPath
{
if( !(self = [super init]) )
return nil;
hostpath = inhostpath;
executablepath = execpath;
parentprocessid = ppid;
folderpath = infolderpath;
selfPath = [inSelfPath retain];
shouldRelaunch = relaunch;
BOOL alreadyTerminated = (getppid() == 1); // ppid is launchd (1) => parent terminated already
if( alreadyTerminated )
[self parentHasQuit];
else
watchdogTimer = [[NSTimer scheduledTimerWithTimeInterval:CHECK_FOR_PARENT_TO_QUIT_TIME target:self selector:@selector(watchdog:) userInfo:nil repeats:YES] retain];
return self;
}
-(void) dealloc
{
[longInstallationTimer invalidate];
[longInstallationTimer release];
longInstallationTimer = nil;
[selfPath release];
selfPath = nil;
[installationPath release];
[watchdogTimer release];
watchdogTimer = nil;
[host release];
host = nil;
[super dealloc];
}
-(void) parentHasQuit
{
[watchdogTimer invalidate];
longInstallationTimer = [[NSTimer scheduledTimerWithTimeInterval: LONG_INSTALLATION_TIME
target: self selector: @selector(showAppIconInDock:)
userInfo:nil repeats:NO] retain];
if( folderpath )
[self install];
else
[self relaunch];
}
- (void) watchdog:(NSTimer *)aTimer
{
ProcessSerialNumber psn;
if (GetProcessForPID(parentprocessid, &psn) == procNotFound)
[self parentHasQuit];
}
- (void)showAppIconInDock:(NSTimer *)aTimer;
{
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType( &psn, kProcessTransformToForegroundApplication );
}
- (void) relaunch
{
if (shouldRelaunch)
{
NSString *appPath = nil;
if( !folderpath || strcmp(executablepath, hostpath) != 0 )
appPath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:executablepath length:strlen(executablepath)];
else
appPath = installationPath;
[[NSWorkspace sharedWorkspace] openFile: appPath];
}
if (folderpath)
{
NSError *theError = nil;
if( ![SUPlainInstaller _removeFileAtPath: [SUInstaller updateFolder] error: &theError] )
SULog( @"Couldn't remove update folder: %@.", theError );
}
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
[[NSFileManager defaultManager] removeFileAtPath: selfPath handler: nil];
#else
[[NSFileManager defaultManager] removeItemAtPath: selfPath error: NULL];
#endif
exit(EXIT_SUCCESS);
}
- (void) install
{
NSBundle *theBundle = [NSBundle bundleWithPath: [[NSFileManager defaultManager] stringWithFileSystemRepresentation: hostpath length:strlen(hostpath)]];
host = [[SUHost alloc] initWithBundle: theBundle];
installationPath = [[host installationPath] copy];
// Perhaps a poor assumption but: if we're not relaunching, we assume we shouldn't be showing any UI either. Because non-relaunching installations are kicked off without any user interaction, we shouldn't be interrupting them.
if (shouldRelaunch) {
SUStatusController* statusCtl = [[SUStatusController alloc] initWithHost: host]; // We quit anyway after we've installed, so leak this for now.
[statusCtl setButtonTitle: SULocalizedString(@"Cancel Update",@"") target: nil action: Nil isDefault: NO];
[statusCtl beginActionWithTitle: SULocalizedString(@"Installing update...",@"")
maxProgressValue: 0 statusText: @""];
[statusCtl showWindow: self];
}
[SUInstaller installFromUpdateFolder: [[NSFileManager defaultManager] stringWithFileSystemRepresentation: folderpath length: strlen(folderpath)]
overHost: host
installationPath: installationPath
delegate: self synchronously: NO
versionComparator: [SUStandardVersionComparator defaultComparator]];
}
- (void) installerFinishedForHost:(SUHost *)aHost
{
[self relaunch];
}
- (void) installerForHost:(SUHost *)host failedWithError:(NSError *)error
{
// Perhaps a poor assumption but: if we're not relaunching, we assume we shouldn't be showing any UI either. Because non-relaunching installations are kicked off without any user interaction, we shouldn't be interrupting them.
if (shouldRelaunch)
NSRunAlertPanel( @"", @"%@", @"OK", @"", @"", [error localizedDescription] );
exit(EXIT_FAILURE);
}
@end
int main (int argc, const char * argv[])
{
if( argc < 5 || argc > 6 )
return EXIT_FAILURE;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//ProcessSerialNumber psn = { 0, kCurrentProcess };
//TransformProcessType( &psn, kProcessTransformToForegroundApplication );
[[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
#if 0 // Cmdline tool
NSString* selfPath = nil;
if( argv[0][0] == '/' )
selfPath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation: argv[0] length: strlen(argv[0])];
else
{
selfPath = [[NSFileManager defaultManager] currentDirectoryPath];
selfPath = [selfPath stringByAppendingPathComponent: [[NSFileManager defaultManager] stringWithFileSystemRepresentation: argv[0] length: strlen(argv[0])]];
}
#else
NSString* selfPath = [[NSBundle mainBundle] bundlePath];
#endif
[NSApplication sharedApplication];
[[[TerminationListener alloc] initWithHostPath: (argc > 1) ? argv[1] : NULL
executablePath: (argc > 2) ? argv[2] : NULL
parentProcessId: (argc > 3) ? atoi(argv[3]) : 0
folderPath: (argc > 4) ? argv[4] : NULL
shouldRelaunch: (argc > 5) ? atoi(argv[5]) : 1
selfPath: selfPath] autorelease];
[[NSApplication sharedApplication] run];
[pool drain];
return EXIT_SUCCESS;
}