update/update.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Apr 25 17:41:00 2023 +0100 (2023-04-25)
changeset 109 2947214c450e
parent 61 31fb35727621
permissions -rw-r--r--
Partial fix for #5537
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@99
    31
    char *yum_uri,*local_database;
ali@99
    32
    char *active_database=NULL,*alternate_database=NULL;
ali@61
    33
    gchar *s,*prefix,*distribution,*vendor_prefix;
ali@13
    34
    int ch;
ali@3
    35
    struct comps *comps;
ali@24
    36
    GError *error=NULL;
ali@59
    37
    s=plover_get_program(argv0);
ali@61
    38
    yum_uri=razor_path_to_uri(s);
ali@59
    39
    g_free(s);
ali@61
    40
    s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
ali@61
    41
    comps=plover_comps_new_from_uri(s,&error);
ali@61
    42
    g_free(s);
ali@61
    43
    if (g_error_matches(error,PLOVER_RAZOR_ERROR,
ali@61
    44
      RAZOR_GENERAL_ERROR_UNSUPPORTED_ARCHIVE))
ali@61
    45
    {
ali@61
    46
	g_clear_error(&error);
ali@61
    47
	free(yum_uri);
ali@61
    48
	s=plover_get_program_directory(argv0);
ali@61
    49
	yum_uri=razor_path_to_uri(s);
ali@61
    50
	g_free(s);
ali@61
    51
	s=g_strconcat(yum_uri,"/repodata/comps.xml",NULL);
ali@61
    52
	comps=plover_comps_new_from_uri(s,&error);
ali@61
    53
    }
ali@3
    54
    if (!comps)
ali@3
    55
    {
ali@61
    56
	fprintf(stderr,"%s\n",error->message);
ali@61
    57
	g_error_free(error);
ali@3
    58
	exit(1);
ali@3
    59
    }
ali@61
    60
    prefix=plover_comps_get_default_prefix(comps);
ali@61
    61
    if (prefix)
ali@61
    62
    {
ali@61
    63
	s=g_strconcat(prefix,"/var/lib/razor",NULL);
ali@61
    64
	local_database=razor_path_to_uri(s);
ali@61
    65
	g_free(s);
ali@61
    66
    }
ali@61
    67
    else
ali@61
    68
	local_database=NULL;
ali@61
    69
    switch(comps->database)
ali@61
    70
    {
ali@61
    71
	case COMPS_DATABASE_DISTRIBUTION_LOCAL:
ali@61
    72
	    active_database=local_database;
ali@61
    73
	    break;
ali@61
    74
	case COMPS_DATABASE_GLOBAL:
ali@61
    75
	    alternate_database=local_database;
ali@61
    76
	    break;
ali@61
    77
    }
ali@61
    78
    if (prefix)
ali@61
    79
    {
ali@61
    80
	distribution=g_strdup(comps->distribution);
ali@61
    81
	plover_comps_set_distribution(comps,NULL);
ali@61
    82
	vendor_prefix=plover_comps_get_default_prefix(comps);
ali@61
    83
	plover_comps_set_distribution(comps,distribution);
ali@61
    84
	g_free(distribution);
ali@61
    85
	razor_set_database_uri(alternate_database);
ali@61
    86
	if (plover_installed_files_match_prefix(vendor_prefix)==1)
ali@61
    87
	{
ali@61
    88
	    printf("There is an existing installation under %s\n"
ali@61
    89
	      "which is not compatible with this distribution. In order\n"
ali@61
    90
	      "to continue, the existing installation must be uninstalled.\n"
ali@61
    91
	      "Do you want to remove all packages in the existing installion? ",
ali@61
    92
	      prefix);
ali@61
    93
	    ch=getchar();
ali@61
    94
	    if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
ali@61
    95
		exit(1);
ali@61
    96
	    while(ch!='\n' && ch!=EOF)
ali@61
    97
		ch=getchar();
ali@61
    98
	    if (plover_remove(NULL,&error))
ali@61
    99
	    {
ali@61
   100
		fprintf(stderr,"%s\n",error->message);
ali@61
   101
		g_error_free(error);
ali@61
   102
		exit(1);
ali@61
   103
	    }
ali@61
   104
	}
ali@61
   105
	g_free(vendor_prefix);
ali@61
   106
    }
ali@61
   107
    razor_set_database_uri(active_database);
ali@61
   108
    if (prefix && !plover_installed_files_match_prefix(prefix))
ali@13
   109
    {
ali@13
   110
	printf("The existing installation is not under %s\n"
ali@13
   111
	  "In order to continue, all the existing packages must be removed.\n"
ali@13
   112
	  "Do you want to remove all existing packages? ",prefix);
ali@13
   113
	ch=getchar();
ali@13
   114
	if (ch!='y' && ch!='Y' && ch!=EOF && ch!='\n')
ali@13
   115
	    exit(1);
ali@13
   116
	while(ch!='\n' && ch!=EOF)
ali@13
   117
	    ch=getchar();
ali@24
   118
	if (plover_remove(NULL,&error))
ali@24
   119
	{
ali@24
   120
	    fprintf(stderr,"%s\n",error->message);
ali@24
   121
	    g_error_free(error);
ali@24
   122
	    exit(1);
ali@24
   123
	}
ali@13
   124
    }
ali@61
   125
    free(local_database);
ali@3
   126
    plover_comps_free(comps);
ali@61
   127
    if (!plover_update_uri(yum_uri,prefix,NULL,&error))
ali@24
   128
    {
ali@24
   129
	fprintf(stderr,"%s\n",error->message);
ali@24
   130
	g_error_free(error);
ali@24
   131
	exit(1);
ali@24
   132
    }
ali@24
   133
    g_free(prefix);
ali@61
   134
    free(yum_uri);
ali@3
   135
}
ali@3
   136
ali@3
   137
int main(int argc,char **argv)
ali@3
   138
{
ali@31
   139
    plover_exception_handler_init();
ali@38
   140
    razor_set_lua_loader("posix",(void (*)())luaopen_posix);
ali@38
   141
    razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
ali@61
   142
    update(argv[0]);
ali@13
   143
    exit(0);
ali@3
   144
}