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