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