update/update.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jun 08 20:13:34 2018 +0100 (2018-06-08)
changeset 78 3339f2d0571f
parent 59 296eac3183bc
child 99 0121592e2512
permissions -rw-r--r--
Added tag 0.5.4 for changeset bed0d5a4511f
ali@3
     1
/*
ali@59
     2
 * Copyright (C) 2009, 2011, 2016  J. Ali Harlow <ali@juiblex.co.uk>
ali@3
     3
 *
ali@3
     4
 * This program is free software; you can redistribute it and/or modify
ali@3
     5
 * it under the terms of the GNU General Public License as published by
ali@3
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@3
     7
 * (at your option) any later version.
ali@3
     8
 *
ali@3
     9
 * This program is distributed in the hope that it will be useful,
ali@3
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@3
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@3
    12
 * GNU General Public License for more details.
ali@3
    13
 *
ali@3
    14
 * You should have received a copy of the GNU General Public License along
ali@3
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@3
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@3
    17
 */
ali@3
    18
ali@3
    19
#include <stdlib.h>
ali@13
    20
#include <stdio.h>
ali@61
    21
#include <errno.h>
ali@3
    22
#include <lua.h>
ali@3
    23
#include "config.h"
ali@3
    24
#include "plover/plover.h"
ali@3
    25
#include "whelk/whelk.h"
ali@3
    26
ali@3
    27
LUALIB_API int luaopen_posix(lua_State *L);
ali@3
    28
ali@61
    29
void update(const char *argv0)
ali@3
    30
{
ali@61
    31
    char *yum_uri,*local_database,*active_database,*alternate_database;
ali@61
    32
    gchar *s,*prefix,*distribution,*vendor_prefix;
ali@13
    33
    int ch;
ali@3
    34
    struct comps *comps;
ali@24
    35
    GError *error=NULL;
ali@59
    36
    s=plover_get_program(argv0);
ali@61
    37
    yum_uri=razor_path_to_uri(s);
ali@59
    38
    g_free(s);
ali@61
    39
    s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
ali@61
    40
    comps=plover_comps_new_from_uri(s,&error);
ali@61
    41
    g_free(s);
ali@61
    42
    if (g_error_matches(error,PLOVER_RAZOR_ERROR,
ali@61
    43
      RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE))
ali@61
    44
    {
ali@61
    45
	g_clear_error(&error);
ali@61
    46
	free(yum_uri);
ali@61
    47
	s=plover_get_program_directory(argv0);
ali@61
    48
	yum_uri=razor_path_to_uri(s);
ali@61
    49
	g_free(s);
ali@61
    50
	s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
ali@61
    51
	comps=plover_comps_new_from_uri(s,&error);
ali@61
    52
    }
ali@3
    53
    if (!comps)
ali@3
    54
    {
ali@61
    55
	fprintf(stderr,"%s\n",error->message);
ali@61
    56
	g_error_free(error);
ali@3
    57
	exit(1);
ali@3
    58
    }
ali@61
    59
    prefix=plover_comps_get_default_prefix(comps);
ali@61
    60
    if (prefix)
ali@61
    61
    {
ali@61
    62
	s=g_strconcat(prefix,"/var/lib/razor",NULL);
ali@61
    63
	local_database=razor_path_to_uri(s);
ali@61
    64
	g_free(s);
ali@61
    65
    }
ali@61
    66
    else
ali@61
    67
	local_database=NULL;
ali@61
    68
    switch(comps->database)
ali@61
    69
    {
ali@61
    70
	case COMPS_DATABASE_DISTRIBUTION_LOCAL:
ali@61
    71
	    active_database=local_database;
ali@61
    72
	    alternate_database=NULL;
ali@61
    73
	    break;
ali@61
    74
	case COMPS_DATABASE_GLOBAL:
ali@61
    75
	    active_database=NULL;
ali@61
    76
	    alternate_database=local_database;
ali@61
    77
	    break;
ali@61
    78
    }
ali@61
    79
    if (prefix)
ali@61
    80
    {
ali@61
    81
	distribution=g_strdup(comps->distribution);
ali@61
    82
	plover_comps_set_distribution(comps,NULL);
ali@61
    83
	vendor_prefix=plover_comps_get_default_prefix(comps);
ali@61
    84
	plover_comps_set_distribution(comps,distribution);
ali@61
    85
	g_free(distribution);
ali@61
    86
	razor_set_database_uri(alternate_database);
ali@61
    87
	if (plover_installed_files_match_prefix(vendor_prefix)==1)
ali@61
    88
	{
ali@61
    89
	    printf("There is an existing installation under %s\n"
ali@61
    90
	      "which is not compatible with this distribution. In order\n"
ali@61
    91
	      "to continue, the existing installation must be uninstalled.\n"
ali@61
    92
	      "Do you want to remove all packages in the existing installion? ",
ali@61
    93
	      prefix);
ali@61
    94
	    ch=getchar();
ali@61
    95
	    if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
ali@61
    96
		exit(1);
ali@61
    97
	    while(ch!='\n' && ch!=EOF)
ali@61
    98
		ch=getchar();
ali@61
    99
	    if (plover_remove(NULL,&error))
ali@61
   100
	    {
ali@61
   101
		fprintf(stderr,"%s\n",error->message);
ali@61
   102
		g_error_free(error);
ali@61
   103
		exit(1);
ali@61
   104
	    }
ali@61
   105
	}
ali@61
   106
	g_free(vendor_prefix);
ali@61
   107
    }
ali@61
   108
    razor_set_database_uri(active_database);
ali@61
   109
    if (prefix && !plover_installed_files_match_prefix(prefix))
ali@13
   110
    {
ali@13
   111
	printf("The existing installation is not under %s\n"
ali@13
   112
	  "In order to continue, all the existing packages must be removed.\n"
ali@13
   113
	  "Do you want to remove all existing packages? ",prefix);
ali@13
   114
	ch=getchar();
ali@13
   115
	if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
ali@13
   116
	    exit(1);
ali@13
   117
	while(ch!='\n' && ch!=EOF)
ali@13
   118
	    ch=getchar();
ali@24
   119
	if (plover_remove(NULL,&error))
ali@24
   120
	{
ali@24
   121
	    fprintf(stderr,"%s\n",error->message);
ali@24
   122
	    g_error_free(error);
ali@24
   123
	    exit(1);
ali@24
   124
	}
ali@13
   125
    }
ali@61
   126
    free(local_database);
ali@3
   127
    plover_comps_free(comps);
ali@61
   128
    if (!plover_update_uri(yum_uri,prefix,NULL,&error))
ali@24
   129
    {
ali@24
   130
	fprintf(stderr,"%s\n",error->message);
ali@24
   131
	g_error_free(error);
ali@24
   132
	exit(1);
ali@24
   133
    }
ali@24
   134
    g_free(prefix);
ali@61
   135
    free(yum_uri);
ali@3
   136
}
ali@3
   137
ali@3
   138
int main(int argc,char **argv)
ali@3
   139
{
ali@31
   140
    plover_exception_handler_init();
ali@38
   141
    razor_set_lua_loader("posix",(void (*)())luaopen_posix);
ali@38
   142
    razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
ali@61
   143
    update(argv[0]);
ali@13
   144
    exit(0);
ali@3
   145
}