|
ali@24
|
1 |
/*
|
|
ali@96
|
2 |
* Copyright (C) 2014, 2020 J. Ali Harlow <ali@juiblex.co.uk>
|
|
ali@24
|
3 |
*
|
|
ali@24
|
4 |
* This program is free software; you can redistribute it and/or modify
|
|
ali@24
|
5 |
* it under the terms of the GNU General Public License as published by
|
|
ali@24
|
6 |
* the Free Software Foundation; either version 2 of the License, or
|
|
ali@24
|
7 |
* (at your option) any later version.
|
|
ali@24
|
8 |
*
|
|
ali@24
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
ali@24
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
ali@24
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
ali@24
|
12 |
* GNU General Public License for more details.
|
|
ali@24
|
13 |
*
|
|
ali@24
|
14 |
* You should have received a copy of the GNU General Public License along
|
|
ali@24
|
15 |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
ali@24
|
16 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
ali@24
|
17 |
*
|
|
ali@24
|
18 |
* References:
|
|
ali@24
|
19 |
* http://www.transmissionzero.co.uk/computing/win32-apps-with-mingw/
|
|
ali@24
|
20 |
*/
|
|
ali@24
|
21 |
|
|
ali@24
|
22 |
#include "config.h"
|
|
ali@24
|
23 |
#ifndef WIN32
|
|
ali@24
|
24 |
#define _XOPEN_SOURCE 500
|
|
ali@24
|
25 |
#endif
|
|
ali@24
|
26 |
#include <stdlib.h>
|
|
ali@24
|
27 |
#include <stdio.h>
|
|
ali@24
|
28 |
#include <string.h>
|
|
ali@97
|
29 |
#include <errno.h>
|
|
ali@24
|
30 |
#include <lua.h>
|
|
ali@24
|
31 |
#include <razor.h>
|
|
ali@24
|
32 |
#include <plover/plover.h>
|
|
ali@24
|
33 |
#include <whelk/whelk.h>
|
|
ali@24
|
34 |
#ifdef WIN32
|
|
ali@24
|
35 |
#include <windows.h>
|
|
ali@24
|
36 |
#include <process.h>
|
|
ali@24
|
37 |
#include <commctrl.h>
|
|
ali@24
|
38 |
#include "resource.h"
|
|
ali@24
|
39 |
|
|
ali@24
|
40 |
#ifndef FOF_NO_UI
|
|
ali@24
|
41 |
#define FOF_NO_UI (FOF_SILENT|FOF_NOCONFIRMATION|FOF_NOERRORUI|\
|
|
ali@24
|
42 |
FOF_NOCONFIRMMKDIR)
|
|
ali@24
|
43 |
#endif
|
|
ali@24
|
44 |
|
|
ali@24
|
45 |
#else /* WIN32 */
|
|
ali@24
|
46 |
#include <ftw.h>
|
|
ali@24
|
47 |
#endif /* WIN32 */
|
|
ali@96
|
48 |
#include "post.h"
|
|
ali@24
|
49 |
|
|
ali@24
|
50 |
#ifdef WIN32
|
|
ali@24
|
51 |
/* Under WIN32, g_spawn requires a helper program which we'd rather avoid */
|
|
ali@24
|
52 |
#undef USE_G_SPAWN
|
|
ali@24
|
53 |
#else
|
|
ali@24
|
54 |
#define USE_G_SPAWN
|
|
ali@24
|
55 |
#endif
|
|
ali@24
|
56 |
|
|
ali@24
|
57 |
LUALIB_API int luaopen_posix(lua_State *L);
|
|
ali@24
|
58 |
|
|
ali@24
|
59 |
#ifdef WIN32
|
|
ali@24
|
60 |
DWORD main_thread_id;
|
|
ali@24
|
61 |
#endif
|
|
ali@24
|
62 |
|
|
ali@24
|
63 |
gchar *prefix;
|
|
ali@24
|
64 |
|
|
ali@24
|
65 |
int verify_and_fix(const char *root)
|
|
ali@24
|
66 |
{
|
|
ali@24
|
67 |
return 0;
|
|
ali@24
|
68 |
}
|
|
ali@24
|
69 |
|
|
ali@24
|
70 |
#ifdef WIN32
|
|
ali@24
|
71 |
INT_PTR CALLBACK ProgressDialogProc(HWND dialog,UINT msg,WPARAM w_param,
|
|
ali@24
|
72 |
LPARAM l_param)
|
|
ali@24
|
73 |
{
|
|
ali@24
|
74 |
HWND progress;
|
|
ali@24
|
75 |
DWORD style;
|
|
ali@24
|
76 |
switch (msg)
|
|
ali@24
|
77 |
{
|
|
ali@24
|
78 |
case WM_INITDIALOG:
|
|
ali@24
|
79 |
progress=GetDlgItem(dialog,IDC_PROGRESS);
|
|
ali@24
|
80 |
style=GetWindowLong(progress,GWL_STYLE);
|
|
ali@24
|
81 |
SetWindowLong(progress,GWL_STYLE,style|PBS_MARQUEE);
|
|
ali@24
|
82 |
SendMessage(progress,PBM_SETMARQUEE,(WPARAM)TRUE,(LPARAM)30);
|
|
ali@24
|
83 |
return (INT_PTR)TRUE;
|
|
ali@24
|
84 |
}
|
|
ali@24
|
85 |
return (INT_PTR)FALSE;
|
|
ali@24
|
86 |
}
|
|
ali@24
|
87 |
#endif
|
|
ali@24
|
88 |
|
|
ali@24
|
89 |
#ifdef WIN32
|
|
ali@24
|
90 |
__stdcall
|
|
ali@24
|
91 |
#endif
|
|
ali@24
|
92 |
unsigned pre_install_thread(void *data)
|
|
ali@24
|
93 |
{
|
|
ali@97
|
94 |
gboolean changed;
|
|
ali@24
|
95 |
int retval;
|
|
ali@95
|
96 |
const char *repository=data;
|
|
ali@46
|
97 |
gchar *s,*uri;
|
|
ali@24
|
98 |
char *install[]={"plover-gtkui",NULL};
|
|
ali@97
|
99 |
char **pkgs=install;
|
|
ali@97
|
100 |
struct comps *comps;
|
|
ali@97
|
101 |
struct comps_group *group;
|
|
ali@97
|
102 |
struct comps_requirement *pkg;
|
|
ali@97
|
103 |
struct plover_vector *packages=NULL;
|
|
ali@24
|
104 |
GError *error=NULL;
|
|
ali@94
|
105 |
plover__uri_handler_init();
|
|
ali@24
|
106 |
prefix=plover_pre_install_prefix();
|
|
ali@24
|
107 |
s=g_strconcat(prefix,"/var/log/pre-install",NULL);
|
|
ali@24
|
108 |
plover_log_open(s);
|
|
ali@24
|
109 |
g_free(s);
|
|
ali@24
|
110 |
s=g_strconcat(prefix,"/var/lib/razor",NULL);
|
|
ali@46
|
111 |
uri=razor_path_to_uri(s);
|
|
ali@24
|
112 |
g_free(s);
|
|
ali@46
|
113 |
razor_set_database_uri(uri);
|
|
ali@46
|
114 |
free(uri);
|
|
ali@24
|
115 |
if (verify_and_fix(prefix))
|
|
ali@24
|
116 |
{
|
|
ali@24
|
117 |
g_free(prefix);
|
|
ali@24
|
118 |
return -1;
|
|
ali@24
|
119 |
}
|
|
ali@97
|
120 |
s=g_strconcat(repository,"/repodata/comps.xml",NULL);
|
|
ali@97
|
121 |
comps=plover_comps_new_from_uri(s,&error);
|
|
ali@97
|
122 |
retval=!comps;
|
|
ali@97
|
123 |
g_free(s);
|
|
ali@97
|
124 |
if (!retval)
|
|
ali@97
|
125 |
{
|
|
ali@97
|
126 |
group=plover_comps_lookup_group(comps,"installer");
|
|
ali@97
|
127 |
if (group)
|
|
ali@97
|
128 |
{
|
|
ali@97
|
129 |
packages=plover_vector_new();
|
|
ali@97
|
130 |
do
|
|
ali@97
|
131 |
{
|
|
ali@97
|
132 |
changed=FALSE;
|
|
ali@97
|
133 |
for(pkg=group->packages;pkg;pkg=pkg->next)
|
|
ali@97
|
134 |
{
|
|
ali@97
|
135 |
if (plover_vector_contains(packages,pkg->name))
|
|
ali@97
|
136 |
continue;
|
|
ali@97
|
137 |
if (pkg->type==COMPS_REQUIREMENT_DEFAULT ||
|
|
ali@97
|
138 |
pkg->type==COMPS_REQUIREMENT_MANDATORY ||
|
|
ali@97
|
139 |
pkg->type==COMPS_REQUIREMENT_CONDITIONAL &&
|
|
ali@97
|
140 |
plover_vector_contains(packages,pkg->requires))
|
|
ali@97
|
141 |
{
|
|
ali@97
|
142 |
changed=TRUE;
|
|
ali@97
|
143 |
plover_vector_append(packages,pkg->name);
|
|
ali@97
|
144 |
}
|
|
ali@97
|
145 |
}
|
|
ali@97
|
146 |
} while(changed);
|
|
ali@97
|
147 |
pkgs=packages->strings;
|
|
ali@97
|
148 |
}
|
|
ali@97
|
149 |
}
|
|
ali@97
|
150 |
if (!retval)
|
|
ali@97
|
151 |
retval=!plover_install_uri(repository,prefix,pkgs,&error);
|
|
ali@24
|
152 |
if (!retval)
|
|
ali@95
|
153 |
retval=!plover_update_uri(repository,prefix,NULL,&error);
|
|
ali@97
|
154 |
if (packages)
|
|
ali@97
|
155 |
plover_vector_free(packages);
|
|
ali@97
|
156 |
if (comps)
|
|
ali@97
|
157 |
plover_comps_free(comps);
|
|
ali@24
|
158 |
if (error)
|
|
ali@24
|
159 |
{
|
|
ali@24
|
160 |
fprintf(stderr,"%s\n",error->message);
|
|
ali@24
|
161 |
g_error_free(error);
|
|
ali@24
|
162 |
}
|
|
ali@24
|
163 |
#ifdef WIN32
|
|
ali@24
|
164 |
PostQuitMessage(retval);
|
|
ali@24
|
165 |
PostThreadMessage(main_thread_id,WM_QUIT,retval,0);
|
|
ali@24
|
166 |
_endthreadex(retval);
|
|
ali@24
|
167 |
#endif
|
|
ali@24
|
168 |
return retval;
|
|
ali@24
|
169 |
}
|
|
ali@24
|
170 |
|
|
ali@24
|
171 |
/*
|
|
ali@24
|
172 |
* The idea is that if pre_install() fails, update/setup should fall back
|
|
ali@24
|
173 |
* to console interfaces.
|
|
ali@24
|
174 |
*/
|
|
ali@24
|
175 |
#ifdef WIN32
|
|
ali@24
|
176 |
HANDLE
|
|
ali@24
|
177 |
#else
|
|
ali@24
|
178 |
void *
|
|
ali@24
|
179 |
#endif
|
|
ali@95
|
180 |
pre_install(const char *repository)
|
|
ali@24
|
181 |
{
|
|
ali@24
|
182 |
#ifdef WIN32
|
|
ali@24
|
183 |
HANDLE retval;
|
|
ali@24
|
184 |
#else
|
|
ali@24
|
185 |
void *retval;
|
|
ali@24
|
186 |
#endif
|
|
ali@38
|
187 |
razor_set_lua_loader("posix",(void (*)())luaopen_posix);
|
|
ali@38
|
188 |
razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
|
|
ali@24
|
189 |
#ifdef WIN32
|
|
ali@95
|
190 |
retval=(HANDLE)_beginthreadex(NULL,0,pre_install_thread,(void *)repository,
|
|
ali@95
|
191 |
0,NULL);
|
|
ali@24
|
192 |
#else
|
|
ali@95
|
193 |
if (pre_install_thread((void *)repository))
|
|
ali@24
|
194 |
retval=NULL;
|
|
ali@24
|
195 |
else
|
|
ali@24
|
196 |
retval=(void *)1; /* Non-NULL to indicate success */
|
|
ali@24
|
197 |
#endif
|
|
ali@24
|
198 |
return retval;
|
|
ali@24
|
199 |
}
|
|
ali@24
|
200 |
|
|
ali@24
|
201 |
#ifndef WIN32
|
|
ali@24
|
202 |
int remove_ignore(const char *fpath,const struct stat *sb,int typeflag,
|
|
ali@24
|
203 |
struct FTW *ftwbuf)
|
|
ali@24
|
204 |
{
|
|
ali@24
|
205 |
(void)remove(fpath);
|
|
ali@24
|
206 |
return 0;
|
|
ali@24
|
207 |
}
|
|
ali@24
|
208 |
#endif
|
|
ali@24
|
209 |
|
|
ali@24
|
210 |
gboolean deltree(const char *path)
|
|
ali@24
|
211 |
{
|
|
ali@24
|
212 |
#ifdef WIN32
|
|
ali@24
|
213 |
/* Based on g_local_file_trash() */
|
|
ali@24
|
214 |
SHFILEOPSTRUCTW op={0};
|
|
ali@24
|
215 |
gboolean success;
|
|
ali@24
|
216 |
wchar_t *wfilename;
|
|
ali@24
|
217 |
long len;
|
|
ali@24
|
218 |
wfilename=g_utf8_to_utf16(path,-1,NULL,&len,NULL);
|
|
ali@24
|
219 |
/* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
|
|
ali@24
|
220 |
wfilename=g_renew(wchar_t,wfilename,len+2);
|
|
ali@24
|
221 |
wfilename[len+1]=0;
|
|
ali@24
|
222 |
op.wFunc=FO_DELETE;
|
|
ali@24
|
223 |
op.pFrom=wfilename;
|
|
ali@24
|
224 |
op.fFlags=FOF_NO_UI;
|
|
ali@24
|
225 |
success=!SHFileOperationW(&op);
|
|
ali@24
|
226 |
if (success && op.fAnyOperationsAborted)
|
|
ali@24
|
227 |
success=FALSE;
|
|
ali@24
|
228 |
g_free(wfilename);
|
|
ali@24
|
229 |
return success;
|
|
ali@24
|
230 |
#else
|
|
ali@24
|
231 |
return nftw(path,remove_ignore,64,FTW_DEPTH|FTW_PHYS);
|
|
ali@24
|
232 |
#endif
|
|
ali@24
|
233 |
}
|
|
ali@24
|
234 |
|
|
ali@24
|
235 |
gboolean pre_uninstall(void)
|
|
ali@24
|
236 |
{
|
|
ali@24
|
237 |
gboolean success;
|
|
ali@46
|
238 |
gchar *s,*uri;
|
|
ali@24
|
239 |
GError *error=NULL;
|
|
ali@38
|
240 |
razor_set_lua_loader("posix",(void (*)())luaopen_posix);
|
|
ali@38
|
241 |
razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
|
|
ali@24
|
242 |
prefix=plover_pre_install_prefix();
|
|
ali@24
|
243 |
s=g_strconcat(prefix,"/var/lib/razor",NULL);
|
|
ali@46
|
244 |
uri=razor_path_to_uri(s);
|
|
ali@24
|
245 |
g_free(s);
|
|
ali@46
|
246 |
razor_set_database_uri(uri);
|
|
ali@46
|
247 |
free(uri);
|
|
ali@24
|
248 |
success=plover_remove(NULL,&error);
|
|
ali@24
|
249 |
if (error)
|
|
ali@24
|
250 |
{
|
|
ali@24
|
251 |
fprintf(stderr,"%s\n",error->message);
|
|
ali@24
|
252 |
g_error_free(error);
|
|
ali@24
|
253 |
}
|
|
ali@24
|
254 |
deltree(prefix);
|
|
ali@24
|
255 |
return success;
|
|
ali@24
|
256 |
}
|
|
ali@24
|
257 |
|
|
ali@24
|
258 |
#if defined(WIN32) && !defined(USE_G_SPAWN)
|
|
ali@24
|
259 |
/* Based on glib's g_spawn_win32.c */
|
|
ali@24
|
260 |
|
|
ali@24
|
261 |
static gchar *
|
|
ali@24
|
262 |
win32_cmdline_quote(const char *string)
|
|
ali@24
|
263 |
{
|
|
ali@24
|
264 |
const gchar *p=string;
|
|
ali@24
|
265 |
gchar *retval,*q;
|
|
ali@24
|
266 |
gint len=0;
|
|
ali@24
|
267 |
gboolean need_dblquotes=FALSE;
|
|
ali@24
|
268 |
while (*p)
|
|
ali@24
|
269 |
{
|
|
ali@24
|
270 |
if (*p==' ' || *p=='\t')
|
|
ali@24
|
271 |
need_dblquotes=TRUE;
|
|
ali@24
|
272 |
else if (*p=='"')
|
|
ali@24
|
273 |
len++;
|
|
ali@24
|
274 |
else if (*p=='\\')
|
|
ali@24
|
275 |
{
|
|
ali@24
|
276 |
const gchar *pp=p;
|
|
ali@24
|
277 |
while (*pp && *pp=='\\')
|
|
ali@24
|
278 |
pp++;
|
|
ali@24
|
279 |
if (*pp=='"')
|
|
ali@24
|
280 |
len++;
|
|
ali@24
|
281 |
}
|
|
ali@24
|
282 |
len++;
|
|
ali@24
|
283 |
p++;
|
|
ali@24
|
284 |
}
|
|
ali@24
|
285 |
q=retval=g_malloc(len+need_dblquotes*2+1);
|
|
ali@24
|
286 |
p=string;
|
|
ali@24
|
287 |
if (need_dblquotes)
|
|
ali@24
|
288 |
*q++='"';
|
|
ali@24
|
289 |
while (*p)
|
|
ali@24
|
290 |
{
|
|
ali@24
|
291 |
if (*p=='"')
|
|
ali@24
|
292 |
*q++='\\';
|
|
ali@24
|
293 |
else if (*p=='\\')
|
|
ali@24
|
294 |
{
|
|
ali@24
|
295 |
const gchar *pp=p;
|
|
ali@24
|
296 |
while (*pp && *pp=='\\')
|
|
ali@24
|
297 |
pp++;
|
|
ali@24
|
298 |
if (*pp=='"')
|
|
ali@24
|
299 |
*q++='\\';
|
|
ali@24
|
300 |
}
|
|
ali@24
|
301 |
*q++=*p;
|
|
ali@24
|
302 |
p++;
|
|
ali@24
|
303 |
}
|
|
ali@24
|
304 |
if (need_dblquotes)
|
|
ali@24
|
305 |
*q++='"';
|
|
ali@24
|
306 |
*q++='\0';
|
|
ali@24
|
307 |
return retval;
|
|
ali@24
|
308 |
}
|
|
ali@24
|
309 |
|
|
ali@24
|
310 |
/* Create a win32-style wide-character argv with suitable quoting */
|
|
ali@24
|
311 |
wchar_t **win32_argv_import(char **argv,GError **error)
|
|
ali@24
|
312 |
{
|
|
ali@24
|
313 |
int i,n;
|
|
ali@24
|
314 |
gchar *s;
|
|
ali@24
|
315 |
wchar_t **wargv;
|
|
ali@24
|
316 |
GError *tmp_error=NULL;
|
|
ali@24
|
317 |
n=g_strv_length(argv);
|
|
ali@24
|
318 |
wargv=g_new(wchar_t *,n+1);
|
|
ali@24
|
319 |
for(i=0;i<n;i++)
|
|
ali@24
|
320 |
{
|
|
ali@24
|
321 |
s=win32_cmdline_quote(argv[i]);
|
|
ali@24
|
322 |
wargv[i]=g_utf8_to_utf16(s,-1,NULL,NULL,&tmp_error);
|
|
ali@24
|
323 |
g_free(s);
|
|
ali@24
|
324 |
if (!wargv[i])
|
|
ali@24
|
325 |
{
|
|
ali@24
|
326 |
g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
|
|
ali@24
|
327 |
"Invalid argument #%d: %s",i,tmp_error->message);
|
|
ali@24
|
328 |
g_error_free(tmp_error);
|
|
ali@24
|
329 |
for(i--;i>=0;i--)
|
|
ali@24
|
330 |
g_free(wargv[i]);
|
|
ali@24
|
331 |
g_free(wargv);
|
|
ali@24
|
332 |
return FALSE;
|
|
ali@24
|
333 |
}
|
|
ali@24
|
334 |
}
|
|
ali@24
|
335 |
wargv[i]=NULL;
|
|
ali@24
|
336 |
return wargv;
|
|
ali@24
|
337 |
}
|
|
ali@24
|
338 |
|
|
ali@24
|
339 |
gboolean spawn_sync(char **argv,GError **error)
|
|
ali@24
|
340 |
{
|
|
ali@24
|
341 |
wchar_t *wargv0,**wargv;
|
|
ali@24
|
342 |
gintptr rc;
|
|
ali@24
|
343 |
GError *tmp_error=NULL;
|
|
ali@24
|
344 |
wargv0=g_utf8_to_utf16(argv[0],-1,NULL,NULL,&tmp_error);
|
|
ali@24
|
345 |
if (!wargv0)
|
|
ali@24
|
346 |
{
|
|
ali@24
|
347 |
fprintf(stderr,"Conversion error in post\n");
|
|
ali@24
|
348 |
g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
|
|
ali@24
|
349 |
"Invalid program name: %s",tmp_error->message);
|
|
ali@24
|
350 |
g_error_free(tmp_error);
|
|
ali@24
|
351 |
return FALSE;
|
|
ali@24
|
352 |
}
|
|
ali@24
|
353 |
wargv=win32_argv_import(argv,error);
|
|
ali@24
|
354 |
if (!wargv)
|
|
ali@24
|
355 |
{
|
|
ali@24
|
356 |
fprintf(stderr,"Conversion error in post\n");
|
|
ali@24
|
357 |
g_free(wargv0);
|
|
ali@24
|
358 |
return FALSE;
|
|
ali@24
|
359 |
}
|
|
ali@24
|
360 |
errno=0;
|
|
ali@24
|
361 |
rc=_wspawnvp(P_WAIT,wargv0,(const wchar_t **)wargv);
|
|
ali@24
|
362 |
g_free(wargv0);
|
|
ali@24
|
363 |
g_strfreev((gchar **)wargv);
|
|
ali@24
|
364 |
if (rc==-1 && errno!=0)
|
|
ali@24
|
365 |
{
|
|
ali@24
|
366 |
fprintf(stderr,"Failed to start post command (%s)\n",g_strerror(errno));
|
|
ali@24
|
367 |
g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
|
|
ali@24
|
368 |
"Failed to execute post command: %s",g_strerror(errno));
|
|
ali@24
|
369 |
return FALSE;
|
|
ali@24
|
370 |
}
|
|
ali@24
|
371 |
if (rc!=EXIT_SUCCESS)
|
|
ali@24
|
372 |
{
|
|
ali@24
|
373 |
fprintf(stderr,"post command failed (%ld)\n",(long)rc);
|
|
ali@24
|
374 |
g_set_error(error,G_SPAWN_ERROR,G_SPAWN_ERROR_FAILED,
|
|
ali@24
|
375 |
"Post command exited with code %ld",(long)rc);
|
|
ali@24
|
376 |
return FALSE;
|
|
ali@24
|
377 |
}
|
|
ali@24
|
378 |
return TRUE;
|
|
ali@24
|
379 |
}
|
|
ali@24
|
380 |
|
|
ali@24
|
381 |
#endif /* defined(WIN32) && !defined(USE_G_SPAWN) */
|
|
ali@24
|
382 |
|
|
ali@96
|
383 |
gboolean pre_install_spawn_sync(char **argv,GError **error)
|
|
ali@96
|
384 |
{
|
|
ali@96
|
385 |
GError *tmp_error=NULL;
|
|
ali@96
|
386 |
#ifdef USE_G_SPAWN
|
|
ali@96
|
387 |
gchar *standard_output,*standard_error;
|
|
ali@96
|
388 |
int exit_status;
|
|
ali@96
|
389 |
if (!g_spawn_sync(NULL,argv,NULL,G_SPAWN_SEARCH_PATH,NULL,NULL,
|
|
ali@96
|
390 |
&standard_output,&standard_error,&exit_status,&tmp_error))
|
|
ali@96
|
391 |
{
|
|
ali@96
|
392 |
fprintf(stderr,"Failed to start post command\n");
|
|
ali@96
|
393 |
g_propagate_prefixed_error(error,tmp_error,"%s: ",argv[0]);
|
|
ali@96
|
394 |
return FALSE;
|
|
ali@96
|
395 |
}
|
|
ali@96
|
396 |
if (standard_output && *standard_output)
|
|
ali@96
|
397 |
{
|
|
ali@96
|
398 |
printf("Output from post command %s:\n",argv[0]);
|
|
ali@96
|
399 |
fputs(standard_output,stdout);
|
|
ali@96
|
400 |
}
|
|
ali@96
|
401 |
g_free(standard_output);
|
|
ali@96
|
402 |
if (standard_error && *standard_error)
|
|
ali@96
|
403 |
{
|
|
ali@96
|
404 |
printf("Error output from post command %s:\n",argv[0]);
|
|
ali@96
|
405 |
fputs(standard_error,stdout);
|
|
ali@96
|
406 |
}
|
|
ali@96
|
407 |
g_free(standard_error);
|
|
ali@96
|
408 |
if (!g_spawn_check_exit_status(exit_status,&tmp_error))
|
|
ali@96
|
409 |
{
|
|
ali@96
|
410 |
fprintf(stderr,"post command failed\n");
|
|
ali@96
|
411 |
g_propagate_prefixed_error(error,tmp_error,"%s: ",argv[0]);
|
|
ali@96
|
412 |
return FALSE;
|
|
ali@96
|
413 |
}
|
|
ali@96
|
414 |
#else
|
|
ali@96
|
415 |
if (!spawn_sync(argv,&tmp_error))
|
|
ali@96
|
416 |
{
|
|
ali@96
|
417 |
g_propagate_prefixed_error(error,tmp_error,"%s: ",argv[0]);
|
|
ali@96
|
418 |
return FALSE;
|
|
ali@96
|
419 |
}
|
|
ali@96
|
420 |
#endif
|
|
ali@96
|
421 |
return TRUE;
|
|
ali@96
|
422 |
}
|
|
ali@96
|
423 |
|
|
ali@24
|
424 |
/*
|
|
ali@24
|
425 |
* Run a command after completing request.
|
|
ali@24
|
426 |
*
|
|
ali@24
|
427 |
* Command may refer to %INSTALL_PREFIX% which will be replaced by the
|
|
ali@95
|
428 |
* (first) install prefix used, by %TEST_RESULT% which will be replaced
|
|
ali@80
|
429 |
* by either "pass" or "fail" depending as to whether the request succeeded
|
|
ali@95
|
430 |
* or not and/or by %REPOSITORY% which will be replaced by the URI of the
|
|
ali@95
|
431 |
* repository used. Command may also include double quotes which will be used
|
|
ali@95
|
432 |
* to affect how the command is split into arguments much like a shell does.
|
|
ali@24
|
433 |
*/
|
|
ali@95
|
434 |
gboolean run_post(int argc,char **argv,gboolean test_result,
|
|
ali@95
|
435 |
const char *repository,GError **error)
|
|
ali@24
|
436 |
{
|
|
ali@24
|
437 |
int i,post_argc;
|
|
ali@24
|
438 |
char *s;
|
|
ali@24
|
439 |
gchar *expanded;
|
|
ali@24
|
440 |
gchar **post_argv;
|
|
ali@24
|
441 |
GError *tmp_error=NULL;
|
|
ali@87
|
442 |
if (argc<2)
|
|
ali@24
|
443 |
{
|
|
ali@24
|
444 |
g_set_error_literal(error,G_FILE_ERROR,G_FILE_ERROR_NOENT,
|
|
ali@24
|
445 |
"--post: No command given");
|
|
ali@24
|
446 |
return FALSE;
|
|
ali@24
|
447 |
}
|
|
ali@24
|
448 |
printf("Running post command: %s\n",argv[1]);
|
|
ali@24
|
449 |
if (!g_shell_parse_argv(argv[1],&post_argc,&post_argv,&tmp_error))
|
|
ali@24
|
450 |
{
|
|
ali@24
|
451 |
g_propagate_prefixed_error(error,tmp_error,"%s: ",argv[1]);
|
|
ali@24
|
452 |
return FALSE;
|
|
ali@24
|
453 |
}
|
|
ali@24
|
454 |
for(i=0;i<post_argc;i++)
|
|
ali@24
|
455 |
{
|
|
ali@24
|
456 |
s=strstr(post_argv[i],"%INSTALL_PREFIX%");
|
|
ali@24
|
457 |
if (s)
|
|
ali@24
|
458 |
{
|
|
ali@24
|
459 |
*s='\0';
|
|
ali@24
|
460 |
s+=strlen("%INSTALL_PREFIX%");
|
|
ali@24
|
461 |
expanded=g_strconcat(post_argv[i],prefix,s,NULL);
|
|
ali@24
|
462 |
g_free(post_argv[i]);
|
|
ali@24
|
463 |
post_argv[i]=expanded;
|
|
ali@24
|
464 |
}
|
|
ali@24
|
465 |
s=strstr(post_argv[i],"%TEST_RESULT%");
|
|
ali@24
|
466 |
if (s)
|
|
ali@24
|
467 |
{
|
|
ali@24
|
468 |
*s='\0';
|
|
ali@24
|
469 |
s+=strlen("%TEST_RESULT%");
|
|
ali@24
|
470 |
expanded=g_strconcat(post_argv[i],test_result?"pass":"fail",s,NULL);
|
|
ali@24
|
471 |
g_free(post_argv[i]);
|
|
ali@24
|
472 |
post_argv[i]=expanded;
|
|
ali@24
|
473 |
}
|
|
ali@95
|
474 |
s=strstr(post_argv[i],"%REPOSITORY%");
|
|
ali@95
|
475 |
if (s)
|
|
ali@95
|
476 |
{
|
|
ali@95
|
477 |
*s='\0';
|
|
ali@95
|
478 |
s+=strlen("%REPOSITORY%");
|
|
ali@95
|
479 |
expanded=g_strconcat(post_argv[i],repository,s,NULL);
|
|
ali@95
|
480 |
g_free(post_argv[i]);
|
|
ali@95
|
481 |
post_argv[i]=expanded;
|
|
ali@95
|
482 |
}
|
|
ali@24
|
483 |
}
|
|
ali@96
|
484 |
return pre_install_spawn_sync(post_argv,error);
|
|
ali@24
|
485 |
}
|
|
ali@24
|
486 |
|
|
ali@24
|
487 |
#ifdef WIN32
|
|
ali@95
|
488 |
DWORD win32_pre_install_gui(char *repository)
|
|
ali@24
|
489 |
{
|
|
ali@24
|
490 |
HANDLE thread;
|
|
ali@24
|
491 |
INITCOMMONCONTROLSEX icc={0,};
|
|
ali@24
|
492 |
MSG msg;
|
|
ali@24
|
493 |
DWORD retval;
|
|
ali@24
|
494 |
main_thread_id=GetCurrentThreadId();
|
|
ali@95
|
495 |
thread=(HANDLE)pre_install(repository);
|
|
ali@24
|
496 |
if (!thread)
|
|
ali@24
|
497 |
return EXIT_FAILURE;
|
|
ali@24
|
498 |
icc.dwSize=sizeof(icc);
|
|
ali@24
|
499 |
icc.dwICC=ICC_WIN95_CLASSES;
|
|
ali@24
|
500 |
InitCommonControlsEx(&icc);
|
|
ali@24
|
501 |
DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_PROGRESSDIALOG),NULL,
|
|
ali@24
|
502 |
&ProgressDialogProc);
|
|
ali@24
|
503 |
while(GetMessage(&msg,NULL,0,0)>0)
|
|
ali@24
|
504 |
{
|
|
ali@24
|
505 |
TranslateMessage(&msg);
|
|
ali@24
|
506 |
DispatchMessage(&msg);
|
|
ali@24
|
507 |
}
|
|
ali@24
|
508 |
WaitForSingleObject(thread,INFINITE);
|
|
ali@24
|
509 |
GetExitCodeThread(thread,&retval);
|
|
ali@24
|
510 |
CloseHandle(thread);
|
|
ali@24
|
511 |
return retval;
|
|
ali@24
|
512 |
}
|
|
ali@24
|
513 |
#endif /* WIN32 */
|
|
ali@24
|
514 |
|
|
ali@95
|
515 |
gchar *pre_install_default_repository(const char *argv0)
|
|
ali@94
|
516 |
{
|
|
ali@94
|
517 |
size_t length;
|
|
ali@94
|
518 |
void *contents;
|
|
ali@94
|
519 |
gchar *path;
|
|
ali@94
|
520 |
gchar *s,*uri;
|
|
ali@94
|
521 |
struct razor_error *tmp_error=NULL;
|
|
ali@94
|
522 |
/*
|
|
ali@95
|
523 |
* The default repository is the executable itself if it's an archive
|
|
ali@94
|
524 |
* or otherwise the directory in which the executable is stored.
|
|
ali@94
|
525 |
*/
|
|
ali@94
|
526 |
path=plover_get_program(argv0);
|
|
ali@94
|
527 |
uri=razor_path_to_uri(path);
|
|
ali@95
|
528 |
free(path);
|
|
ali@94
|
529 |
s=g_strconcat(uri,"/repodata/comps.xml",NULL);
|
|
ali@94
|
530 |
contents=razor_uri_get_contents(s,&length,FALSE,&tmp_error);
|
|
ali@94
|
531 |
g_free(s);
|
|
ali@94
|
532 |
if (contents)
|
|
ali@94
|
533 |
razor_uri_free_contents(contents,length);
|
|
ali@94
|
534 |
else
|
|
ali@94
|
535 |
{
|
|
ali@94
|
536 |
if (razor_error_matches(tmp_error,RAZOR_GENERAL_ERROR,
|
|
ali@94
|
537 |
RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE))
|
|
ali@94
|
538 |
{
|
|
ali@94
|
539 |
path=plover_get_program_directory(argv0);
|
|
ali@95
|
540 |
uri=razor_path_to_uri(path);
|
|
ali@95
|
541 |
free(path);
|
|
ali@94
|
542 |
}
|
|
ali@94
|
543 |
razor_error_free(tmp_error);
|
|
ali@94
|
544 |
}
|
|
ali@95
|
545 |
return uri;
|
|
ali@94
|
546 |
}
|
|
ali@94
|
547 |
|
|
ali@96
|
548 |
gboolean pre_install_post(int argc,char **argv,gboolean success,
|
|
ali@96
|
549 |
const char *repository)
|
|
ali@96
|
550 |
{
|
|
ali@96
|
551 |
gchar *s,*uri;
|
|
ali@96
|
552 |
struct post *post=NULL;
|
|
ali@96
|
553 |
GError *error=NULL;
|
|
ali@96
|
554 |
if (!argv && !success)
|
|
ali@96
|
555 |
{
|
|
ali@96
|
556 |
/*
|
|
ali@96
|
557 |
* If no --post command has been given, then we want to run the
|
|
ali@96
|
558 |
* program specified in %INSTALL_PREFIX%/etc/pre-inst.post
|
|
ali@96
|
559 |
* However, if the pre-install failed, then that file isn't likely
|
|
ali@96
|
560 |
* to exist (and it's requirements even less so) so we issue an error
|
|
ali@96
|
561 |
* instead. Unfortunately, it's hard to include the error message
|
|
ali@96
|
562 |
* here. It will have been logged in the plover log, but that's not
|
|
ali@96
|
563 |
* much help to users.
|
|
ali@96
|
564 |
*/
|
|
ali@96
|
565 |
#ifndef WIN32
|
|
ali@96
|
566 |
fprintf(stderr,
|
|
ali@96
|
567 |
"Installation failed: Failed to unpack the main installer\n");
|
|
ali@96
|
568 |
#else
|
|
ali@96
|
569 |
MessageBox(NULL,"Failed to unpack the main installer",
|
|
ali@96
|
570 |
"Installation failed",MB_ICONERROR|MB_OK);
|
|
ali@96
|
571 |
#endif
|
|
ali@96
|
572 |
return FALSE;
|
|
ali@96
|
573 |
}
|
|
ali@96
|
574 |
if (!argv)
|
|
ali@96
|
575 |
{
|
|
ali@96
|
576 |
post=pre_install_post_new(repository,prefix);
|
|
ali@96
|
577 |
uri=razor_path_to_uri(prefix);
|
|
ali@96
|
578 |
s=g_strconcat(uri,"/etc/pre-inst.post",NULL);
|
|
ali@96
|
579 |
g_free(uri);
|
|
ali@96
|
580 |
pre_install_post_load_uri(post,s,&error);
|
|
ali@96
|
581 |
if (error)
|
|
ali@96
|
582 |
g_debug("Failed to load post configuration from pre-inst.post: %s",
|
|
ali@96
|
583 |
error->message);
|
|
ali@96
|
584 |
g_free(s);
|
|
ali@96
|
585 |
if (g_error_matches(error,PLOVER_POSIX_ERROR,ENOENT))
|
|
ali@96
|
586 |
{
|
|
ali@96
|
587 |
/*
|
|
ali@96
|
588 |
* If no post configuration file exists, that's not an error;
|
|
ali@96
|
589 |
* there simply is no post action configured.
|
|
ali@96
|
590 |
*/
|
|
ali@96
|
591 |
g_clear_error(&error);
|
|
ali@96
|
592 |
}
|
|
ali@96
|
593 |
}
|
|
ali@96
|
594 |
if (post && post->argc>0)
|
|
ali@96
|
595 |
pre_install_spawn_sync(post->argv,&error);
|
|
ali@96
|
596 |
else if (argv)
|
|
ali@96
|
597 |
run_post(argc,argv,success,repository,&error);
|
|
ali@96
|
598 |
if (post)
|
|
ali@96
|
599 |
pre_install_post_free(post);
|
|
ali@96
|
600 |
if (error)
|
|
ali@96
|
601 |
{
|
|
ali@96
|
602 |
#ifndef WIN32
|
|
ali@96
|
603 |
fprintf(stderr,"Error in post: %s\n",error->message);
|
|
ali@96
|
604 |
#else
|
|
ali@96
|
605 |
MessageBox(NULL,error->message,"Error in post",MB_ICONERROR|MB_OK);
|
|
ali@96
|
606 |
#endif
|
|
ali@96
|
607 |
g_error_free(error);
|
|
ali@96
|
608 |
success=FALSE;
|
|
ali@96
|
609 |
}
|
|
ali@96
|
610 |
return success;
|
|
ali@96
|
611 |
}
|
|
ali@96
|
612 |
|
|
ali@24
|
613 |
int main(int argc,char **argv)
|
|
ali@24
|
614 |
{
|
|
ali@80
|
615 |
gboolean success,uninstall=FALSE,enable_post=FALSE;
|
|
ali@24
|
616 |
GError *error=NULL;
|
|
ali@95
|
617 |
gchar *path=NULL,*repository=NULL;
|
|
ali@80
|
618 |
GOptionContext *context;
|
|
ali@80
|
619 |
GOptionEntry options[]={
|
|
ali@95
|
620 |
{"repository",0,0,G_OPTION_ARG_STRING,&repository,
|
|
ali@95
|
621 |
"Repository location","uri"},
|
|
ali@80
|
622 |
{"path",0,0,G_OPTION_ARG_FILENAME,&path,
|
|
ali@80
|
623 |
"Repository path","path"},
|
|
ali@80
|
624 |
{"uninstall",'u',0,G_OPTION_ARG_NONE,&uninstall,
|
|
ali@80
|
625 |
"Uninstall all packages",NULL},
|
|
ali@80
|
626 |
{"post",0,0,G_OPTION_ARG_NONE,&enable_post,
|
|
ali@80
|
627 |
"Run command after request is processed",NULL},
|
|
ali@80
|
628 |
{NULL}
|
|
ali@80
|
629 |
};
|
|
ali@24
|
630 |
#ifdef WIN32
|
|
ali@24
|
631 |
/*
|
|
ali@24
|
632 |
* pre-inst is normally a GUI application, but rpm scripts may well
|
|
ali@24
|
633 |
* call console applications and it looks ugly if console windows keep
|
|
ali@24
|
634 |
* popping up. Avoid this by allocating our own console and hiding it.
|
|
ali@24
|
635 |
* Note:
|
|
ali@24
|
636 |
* - If pre-inst is a console application (typically for debugging),
|
|
ali@24
|
637 |
* then skip this step.
|
|
ali@24
|
638 |
* - Call ShowWindow twice to negate special handling on first call.
|
|
ali@24
|
639 |
*/
|
|
ali@24
|
640 |
if (!GetConsoleWindow())
|
|
ali@24
|
641 |
{
|
|
ali@24
|
642 |
AllocConsole();
|
|
ali@24
|
643 |
ShowWindow(GetConsoleWindow(),SW_HIDE);
|
|
ali@24
|
644 |
ShowWindow(GetConsoleWindow(),SW_HIDE);
|
|
ali@24
|
645 |
}
|
|
ali@24
|
646 |
#endif
|
|
ali@31
|
647 |
plover_exception_handler_init();
|
|
ali@80
|
648 |
context=g_option_context_new("[command] - install the main installer");
|
|
ali@80
|
649 |
g_option_context_add_main_entries(context,options,NULL);
|
|
ali@80
|
650 |
g_option_context_set_description(context,
|
|
ali@80
|
651 |
"If --post is specified, then the command to run and its arguments\n"
|
|
ali@80
|
652 |
"should be listed at the end of the command line.\n"
|
|
ali@80
|
653 |
"Command may refer to %INSTALL_PREFIX% which will be replaced by the\n"
|
|
ali@95
|
654 |
"(first) install prefix used, by %TEST_RESULT% which will be\n"
|
|
ali@80
|
655 |
"replaced by either \"pass\" or \"fail\" depending as to whether the\n"
|
|
ali@95
|
656 |
"request succeeded or not and/or by %REPOSITORY% which will be\n"
|
|
ali@95
|
657 |
"replaced by the URI of the repository used. Command may also include\n"
|
|
ali@95
|
658 |
"double quotes which will be used to affect how the command is split\n"
|
|
ali@95
|
659 |
"into arguments much like a shell does.");
|
|
ali@80
|
660 |
g_option_context_set_strict_posix(context,TRUE);
|
|
ali@80
|
661 |
g_option_context_set_ignore_unknown_options(context,TRUE);
|
|
ali@80
|
662 |
if (!g_option_context_parse(context,&argc,&argv,&error))
|
|
ali@24
|
663 |
{
|
|
ali@80
|
664 |
g_printerr("pre-install: %s\n",error->message);
|
|
ali@80
|
665 |
g_printerr("Use \"%s --help\" for help\n",(*argv)[0]);
|
|
ali@80
|
666 |
exit(1);
|
|
ali@80
|
667 |
}
|
|
ali@95
|
668 |
if (repository && path)
|
|
ali@95
|
669 |
{
|
|
ali@95
|
670 |
g_printerr("pre-install: "
|
|
ali@95
|
671 |
"Only one of --repository and --path can be specified\n");
|
|
ali@95
|
672 |
exit(1);
|
|
ali@95
|
673 |
}
|
|
ali@95
|
674 |
if (path)
|
|
ali@95
|
675 |
repository=razor_path_to_uri(path);
|
|
ali@95
|
676 |
else if (!repository)
|
|
ali@95
|
677 |
repository=pre_install_default_repository(argv[0]);
|
|
ali@80
|
678 |
if (uninstall)
|
|
ali@24
|
679 |
success=pre_uninstall();
|
|
ali@80
|
680 |
else
|
|
ali@80
|
681 |
{
|
|
ali@80
|
682 |
#ifdef WIN32
|
|
ali@95
|
683 |
success=win32_pre_install_gui(repository)==EXIT_SUCCESS;
|
|
ali@80
|
684 |
#else
|
|
ali@95
|
685 |
success=!!pre_install(repository);
|
|
ali@80
|
686 |
#endif
|
|
ali@24
|
687 |
}
|
|
ali@96
|
688 |
if (enable_post)
|
|
ali@96
|
689 |
success=pre_install_post(argc,argv,success,repository);
|
|
ali@96
|
690 |
else if (!uninstall)
|
|
ali@96
|
691 |
success=pre_install_post(0,NULL,success,repository);
|
|
ali@24
|
692 |
#ifdef WIN32
|
|
ali@24
|
693 |
return success?EXIT_SUCCESS:EXIT_FAILURE;
|
|
ali@24
|
694 |
#else
|
|
ali@24
|
695 |
return success?0:1;
|
|
ali@24
|
696 |
#endif
|
|
ali@24
|
697 |
}
|