plover/razor.c
changeset 19 5cafc65a6183
parent 13 b0a35bae4961
child 22 5cbb66c24fc6
     1.1 --- a/plover/razor.c	Thu Nov 10 11:15:09 2011 +0000
     1.2 +++ b/plover/razor.c	Tue Feb 21 22:55:40 2012 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4  /*
     1.5   * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     1.6   * Copyright (C) 2008  Red Hat, Inc
     1.7 - * Copyright (C) 2009, 2011  J. Ali Harlow <ali@juiblex.co.uk>
     1.8 + * Copyright (C) 2009, 2011, 2012  J. Ali Harlow <ali@juiblex.co.uk>
     1.9   *
    1.10   * This program is free software; you can redistribute it and/or modify
    1.11   * it under the terms of the GNU General Public License as published by
    1.12 @@ -39,8 +39,8 @@
    1.13  }
    1.14  
    1.15  struct razor_set *plover_relocate_packages(struct razor_set *set,
    1.16 -  struct razor_atomic *atomic,const char *base,
    1.17 -  struct razor_relocations *relocations)
    1.18 +  const char *base,struct razor_relocations *relocations,
    1.19 +  struct razor_error **error)
    1.20  {
    1.21      struct razor_importer *importer;
    1.22      struct razor_property_iterator *prop_iter;
    1.23 @@ -63,7 +63,7 @@
    1.24  	s=rpm_filename(name,version,arch);
    1.25  	file=plover_strconcat(base,"/rpms/",s,NULL);
    1.26  	free(s);
    1.27 -	rpm=razor_rpm_open(file,atomic);
    1.28 +	rpm=razor_rpm_open(file,error);
    1.29  	free(file);
    1.30  	if (!rpm)
    1.31  	{
    1.32 @@ -96,6 +96,10 @@
    1.33      return new;
    1.34  }
    1.35  
    1.36 +/*
    1.37 + * Returns 0 on success, -1 on failure and 1 if a RAZOR_INSTALL_ACTION_COMMIT
    1.38 + * is met (in which case the action is consumed).
    1.39 + */
    1.40  int plover_run_transaction(struct razor_transaction *trans,
    1.41    struct razor_install_iterator *ii,const char *base,const char *install_root,
    1.42    struct razor_set *system,struct razor_set *next,struct razor_atomic *atomic,
    1.43 @@ -104,10 +108,10 @@
    1.44      struct razor_package *package;
    1.45      enum razor_install_action action;
    1.46      struct razor_rpm *rpm;
    1.47 +    struct razor_error *error=NULL;
    1.48      const char *name,*version,*arch;
    1.49      char *s,*file;
    1.50 -    int count;
    1.51 -    razor_install_iterator_rewind(ii);
    1.52 +    int r,count;
    1.53      switch(stage)
    1.54      {
    1.55  	case RAZOR_STAGE_SCRIPTS_PRE:
    1.56 @@ -131,12 +135,12 @@
    1.57  	      RAZOR_DETAIL_LAST);
    1.58  	    if (stage==RAZOR_STAGE_FILES)
    1.59  		printf("  Removing : %s ",name);
    1.60 -	    razor_package_remove(system,next,atomic,package,install_root,
    1.61 +	    r=razor_package_remove(system,next,atomic,package,install_root,
    1.62  	      count,stage);
    1.63  	    if (stage==RAZOR_STAGE_FILES)
    1.64  		printf("\n");
    1.65  	}
    1.66 -	else
    1.67 +	else if (action==RAZOR_INSTALL_ACTION_ADD)
    1.68  	{
    1.69  	    razor_package_get_details(next,package,RAZOR_DETAIL_NAME,&name,
    1.70  	      RAZOR_DETAIL_VERSION,&version,RAZOR_DETAIL_ARCH,&arch,
    1.71 @@ -144,56 +148,102 @@
    1.72  	    s=rpm_filename(name,version,arch);
    1.73  	    file=plover_strconcat(base,"/rpms/",s,NULL);
    1.74  	    free(s);
    1.75 -	    rpm=razor_rpm_open(file,atomic);
    1.76 +	    rpm=razor_rpm_open(file,&error);
    1.77  	    free(file);
    1.78  	    if (!rpm)
    1.79 +	    {
    1.80 +		razor_atomic_abort(atomic,razor_error_get_msg(error));
    1.81 +		razor_error_free(error);
    1.82  		return -1;
    1.83 +	    }
    1.84  	    if (stage==RAZOR_STAGE_FILES)
    1.85  		printf("  Installing : %s ",name);
    1.86  	    if (relocations)
    1.87  		razor_rpm_set_relocations(rpm,relocations);
    1.88  	    razor_transaction_fixup_package(trans,package,rpm);
    1.89 -	    razor_rpm_install(rpm,atomic,install_root,1,stage);
    1.90 +	    r=razor_rpm_install(rpm,atomic,install_root,1,stage);
    1.91  	    razor_rpm_close(rpm);
    1.92  	    if (stage==RAZOR_STAGE_FILES)
    1.93  		printf("\n");
    1.94  	}
    1.95 +	else if (action==RAZOR_INSTALL_ACTION_COMMIT)
    1.96 +	    return 1;
    1.97 +	else
    1.98 +	    r=0;
    1.99  	if (razor_atomic_in_error_state(atomic))
   1.100  	    return -1;
   1.101 +	else if (r)
   1.102 +	{
   1.103 +	    razor_package_get_details(next,package,RAZOR_DETAIL_NAME,&name,
   1.104 +	      RAZOR_DETAIL_VERSION,&version,RAZOR_DETAIL_ARCH,&arch,
   1.105 +	      RAZOR_DETAIL_LAST);
   1.106 +	    /*
   1.107 +	     * If a pre or preun script fails, then we should
   1.108 +	     * treat that as a fatal error. post and postun
   1.109 +	     * script failures are treated as warnings. Be
   1.110 +	     * nice and tell the user _which_ script failed.
   1.111 +	     */
   1.112 +	    fprintf(stderr,
   1.113 +	      "%s: %s(%s-%s.%s) scriptlet failed, exit status %d\n",
   1.114 +	      stage==RAZOR_STAGE_SCRIPTS_PRE?"error":"warning",
   1.115 +	      action==RAZOR_INSTALL_ACTION_ADD?"%pre":"%preun",
   1.116 +	      name,version,arch,r);
   1.117 +	    if (stage==RAZOR_STAGE_SCRIPTS_PRE)
   1.118 +		return -1;
   1.119 +	}
   1.120      }
   1.121      return 0;
   1.122  }
   1.123  
   1.124 -/*
   1.125 - * Note: plover_commit_transaction() takes ownership of root which should
   1.126 - * not be used after it returns.
   1.127 - */
   1.128  int plover_commit_transaction(struct razor_transaction *trans,const char *base,
   1.129 -  const char *install_root,struct razor_root *root,struct razor_atomic *atomic,
   1.130 +  const char *install_root,struct razor_root *root,
   1.131    struct razor_relocations *relocations)
   1.132  {
   1.133 -    int retval;
   1.134 -    struct razor_set *next,*system;
   1.135 +    int r,retval;
   1.136 +    size_t pos;
   1.137 +    struct razor_set *system,*next,*set;
   1.138      struct razor_install_iterator *ii;
   1.139 +    struct razor_atomic *atomic;
   1.140      razor_transaction_resolve(trans);
   1.141      if (razor_transaction_describe(trans)>0)
   1.142 +	return -1;
   1.143 +    next=razor_transaction_commit(trans);
   1.144 +    system=razor_set_ref(razor_root_get_system_set(root));
   1.145 +    ii=razor_set_create_install_iterator(system,next);
   1.146 +    do
   1.147      {
   1.148 -	razor_root_close(root);
   1.149 -	return -1;
   1.150 -    }
   1.151 -    next=razor_transaction_commit(trans);
   1.152 -    system=razor_root_get_system_set(root);
   1.153 -    ii=razor_set_create_install_iterator(system,next);
   1.154 -    plover_run_transaction(trans,ii,base,install_root,system,next,atomic,
   1.155 -      relocations,RAZOR_STAGE_SCRIPTS_PRE);
   1.156 -    plover_run_transaction(trans,ii,base,install_root,system,next,atomic,
   1.157 -      relocations,RAZOR_STAGE_FILES);
   1.158 -    razor_root_update(root,next);
   1.159 -    razor_root_commit(root);
   1.160 -    retval=razor_atomic_commit(atomic);
   1.161 -    if (!retval)
   1.162 -	plover_run_transaction(trans,ii,base,install_root,system,next,atomic,
   1.163 -	  relocations,RAZOR_STAGE_SCRIPTS_POST);
   1.164 +	pos=razor_install_iterator_tell(ii);
   1.165 +	atomic=razor_atomic_open("package transaction");
   1.166 +	r=plover_run_transaction(trans,ii,base,install_root,system,next,atomic,
   1.167 +	  relocations,RAZOR_STAGE_SCRIPTS_PRE);
   1.168 +	if (r<0)
   1.169 +	    fprintf(stderr,"Transaction aborted\n");
   1.170 +	else
   1.171 +	{
   1.172 +	    razor_install_iterator_seek(ii,pos);
   1.173 +	    r=plover_run_transaction(trans,ii,base,install_root,system,next,
   1.174 +	      atomic,relocations,RAZOR_STAGE_FILES);
   1.175 +	    if (r==1)
   1.176 +	    {
   1.177 +		set=razor_install_iterator_commit_set(ii);
   1.178 +		razor_root_update(root,set,atomic);
   1.179 +		razor_set_unref(set);
   1.180 +	    }
   1.181 +	    else if (!r)
   1.182 +		razor_root_update(root,next,atomic);
   1.183 +	    retval=razor_atomic_commit(atomic);
   1.184 +	    if (retval)
   1.185 +		fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.186 +	    else
   1.187 +	    {
   1.188 +		razor_install_iterator_seek(ii,pos);
   1.189 +		plover_run_transaction(trans,ii,base,install_root,system,next,
   1.190 +		  atomic,relocations,RAZOR_STAGE_SCRIPTS_POST);
   1.191 +	    }
   1.192 +	}
   1.193 +	razor_atomic_destroy(atomic);
   1.194 +    } while(!retval && r==1);
   1.195 +    razor_set_unref(system);
   1.196      razor_set_unref(next);
   1.197      razor_install_iterator_destroy(ii);
   1.198      return retval;
   1.199 @@ -226,11 +276,11 @@
   1.200      int i,retval;
   1.201      char *s;
   1.202      char *install_root;
   1.203 -    struct razor_root *root;
   1.204      struct razor_set *system,*set,*upstream;
   1.205      struct razor_transaction *trans;
   1.206      struct razor_relocations *relocations;
   1.207 -    struct razor_atomic *atomic;
   1.208 +    struct razor_root *root;
   1.209 +    struct razor_error *error=NULL;
   1.210      install_root=getenv("RAZOR_ROOT");
   1.211      if (!install_root)
   1.212  	install_root="";
   1.213 @@ -241,37 +291,23 @@
   1.214      }
   1.215      else
   1.216  	relocations=NULL;
   1.217 -    atomic=razor_atomic_open("Install packages");
   1.218 -    /*
   1.219 -     * Calling razor_root_open() on a system that hasn't yet had
   1.220 -     * razor_root_create() run generates a confusing error message
   1.221 -     * on stderr. Avoid this by trying to open it R/O first which
   1.222 -     * fails without generating any error.
   1.223 -     */
   1.224 -    set=razor_root_open_read_only(install_root,atomic);
   1.225 -    if (set)
   1.226 -	razor_set_unref(set);
   1.227 -    else
   1.228 -	razor_root_create(install_root);
   1.229 -    root=razor_root_open(install_root,atomic);
   1.230 +    root=razor_root_open(install_root,NULL);
   1.231      if (!root)
   1.232      {
   1.233 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.234 -	razor_atomic_destroy(atomic);
   1.235 -	if (relocations)
   1.236 -	    razor_relocations_destroy(relocations);
   1.237 -	return -1;
   1.238 +	if (razor_root_create(install_root,&error))
   1.239 +	    root=NULL;
   1.240 +	else
   1.241 +	    root=razor_root_open(install_root,&error);
   1.242 +	if (!root)
   1.243 +	{
   1.244 +	    fprintf(stderr,"%s\n",razor_error_get_msg(error));
   1.245 +	    razor_error_free(error);
   1.246 +	    if (relocations)
   1.247 +		razor_relocations_destroy(relocations);
   1.248 +	    return -1;
   1.249 +	}
   1.250      }
   1.251      system=razor_root_get_system_set(root);
   1.252 -    if (!system)
   1.253 -    {
   1.254 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.255 -	razor_root_close(root);
   1.256 -	razor_atomic_destroy(atomic);
   1.257 -	if (relocations)
   1.258 -	    razor_relocations_destroy(relocations);
   1.259 -	return -1;
   1.260 -    }
   1.261      s=plover_strconcat(base,"/repodata",NULL);
   1.262      if (s)
   1.263      {
   1.264 @@ -280,13 +316,14 @@
   1.265  	    perror(s);
   1.266      }
   1.267      else
   1.268 +    {
   1.269 +	fprintf(stderr,"Not enough memory\n");
   1.270  	retval=-1;
   1.271 +    }
   1.272      free(s);
   1.273      if (retval<0)
   1.274      {
   1.275 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.276  	razor_root_close(root);
   1.277 -	razor_atomic_destroy(atomic);
   1.278  	if (relocations)
   1.279  	    razor_relocations_destroy(relocations);
   1.280  	return -1;
   1.281 @@ -294,16 +331,19 @@
   1.282      set=plover_razor_set_create_from_yum(base);
   1.283      if (set)
   1.284      {
   1.285 -	upstream=plover_relocate_packages(set,atomic,base,relocations);
   1.286 +	upstream=plover_relocate_packages(set,base,relocations,&error);
   1.287 +	if (!upstream)
   1.288 +	{
   1.289 +	    fprintf(stderr,"%s\n",razor_error_get_msg(error));
   1.290 +	    razor_error_free(error);
   1.291 +	}
   1.292  	razor_set_unref(set);
   1.293      }
   1.294      else
   1.295  	upstream=NULL;
   1.296      if (!upstream)
   1.297      {
   1.298 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.299  	razor_root_close(root);
   1.300 -	razor_atomic_destroy(atomic);
   1.301  	if (relocations)
   1.302  	    razor_relocations_destroy(relocations);
   1.303  	return -1;
   1.304 @@ -319,16 +359,10 @@
   1.305  	    break;
   1.306  	}
   1.307      if (!retval)
   1.308 -    {
   1.309 -	retval=plover_commit_transaction(trans,base,install_root,root,atomic,
   1.310 +	retval=plover_commit_transaction(trans,base,install_root,root,
   1.311  	  relocations);
   1.312 -	if (retval)
   1.313 -	    fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.314 -    }
   1.315 -    else
   1.316 -	razor_root_close(root);
   1.317      razor_transaction_destroy(trans);
   1.318 -    razor_atomic_destroy(atomic);
   1.319 +    razor_root_close(root);
   1.320      if (relocations)
   1.321  	razor_relocations_destroy(relocations);
   1.322      return retval;
   1.323 @@ -338,11 +372,11 @@
   1.324  {
   1.325      int i,retval;
   1.326      char *install_root,*s;
   1.327 -    struct razor_root *root;
   1.328      struct razor_set *system,*set,*upstream;
   1.329      struct razor_transaction *trans;
   1.330      struct razor_relocations *relocations;
   1.331 -    struct razor_atomic *atomic;
   1.332 +    struct razor_root *root;
   1.333 +    struct razor_error *error=NULL;
   1.334      install_root=getenv("RAZOR_ROOT");
   1.335      if (!install_root)
   1.336  	install_root="";
   1.337 @@ -353,36 +387,16 @@
   1.338      }
   1.339      else
   1.340  	relocations=NULL;
   1.341 -    atomic=razor_atomic_open("Update packages");
   1.342 -    set=razor_root_open_read_only(install_root,atomic);
   1.343 -    if (!set)
   1.344 +    root=razor_root_open(install_root,&error);
   1.345 +    if (!root)
   1.346      {
   1.347 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.348 -	razor_atomic_destroy(atomic);
   1.349 +	fprintf(stderr,"%s\n",razor_error_get_msg(error));
   1.350 +	razor_error_free(error);
   1.351  	if (relocations)
   1.352  	    razor_relocations_destroy(relocations);
   1.353  	return 0;
   1.354      }
   1.355 -    razor_set_unref(set);
   1.356 -    root=razor_root_open(install_root,atomic);
   1.357 -    if (!root)
   1.358 -    {
   1.359 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.360 -	razor_atomic_destroy(atomic);
   1.361 -	if (relocations)
   1.362 -	    razor_relocations_destroy(relocations);
   1.363 -	return -1;
   1.364 -    }
   1.365      system=razor_root_get_system_set(root);
   1.366 -    if (!system)
   1.367 -    {
   1.368 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.369 -	razor_root_close(root);
   1.370 -	razor_atomic_destroy(atomic);
   1.371 -	if (relocations)
   1.372 -	    razor_relocations_destroy(relocations);
   1.373 -	return -1;
   1.374 -    }
   1.375      s=plover_strconcat(base,"/repodata",NULL);
   1.376      if (s)
   1.377      {
   1.378 @@ -391,13 +405,14 @@
   1.379  	    perror(s);
   1.380      }
   1.381      else
   1.382 +    {
   1.383 +	fprintf(stderr,"Not enough memory");
   1.384  	retval=-1;
   1.385 +    }
   1.386      free(s);
   1.387      if (retval)
   1.388      {
   1.389 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.390  	razor_root_close(root);
   1.391 -	razor_atomic_destroy(atomic);
   1.392  	if (relocations)
   1.393  	    razor_relocations_destroy(relocations);
   1.394  	return -1;
   1.395 @@ -405,16 +420,19 @@
   1.396      set=plover_razor_set_create_from_yum(base);
   1.397      if (set)
   1.398      {
   1.399 -	upstream=plover_relocate_packages(set,atomic,base,relocations);
   1.400 +	upstream=plover_relocate_packages(set,base,relocations,&error);
   1.401 +	if (!upstream)
   1.402 +	{
   1.403 +	    fprintf(stderr,"%s\n",razor_error_get_msg(error));
   1.404 +	    razor_error_free(error);
   1.405 +	}
   1.406  	razor_set_unref(set);
   1.407      }
   1.408      else
   1.409  	upstream=NULL;
   1.410      if (!upstream)
   1.411      {
   1.412 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.413  	razor_root_close(root);
   1.414 -	razor_atomic_destroy(atomic);
   1.415  	if (relocations)
   1.416  	    razor_relocations_destroy(relocations);
   1.417  	return -1;
   1.418 @@ -433,18 +451,13 @@
   1.419  	}
   1.420      else
   1.421  	razor_transaction_update_all(trans);
   1.422 -    if (!retval) {
   1.423 -	retval=plover_commit_transaction(trans,base,install_root,root,atomic,
   1.424 +    if (!retval)
   1.425 +	retval=plover_commit_transaction(trans,base,install_root,root,
   1.426  	  relocations);
   1.427 -	if (retval)
   1.428 -	    fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.429 -    }
   1.430 -    else
   1.431 -	razor_root_close(root);
   1.432      razor_transaction_destroy(trans);
   1.433 +    razor_root_close(root);
   1.434      if (relocations)
   1.435  	razor_relocations_destroy(relocations);
   1.436 -    razor_atomic_destroy(atomic);
   1.437      return retval;
   1.438  }
   1.439  
   1.440 @@ -473,37 +486,21 @@
   1.441  {
   1.442      int i,retval=0;
   1.443      char *install_root;
   1.444 -    struct razor_root *root;
   1.445      struct razor_set *system,*set,*upstream;
   1.446      struct razor_transaction *trans;
   1.447 -    struct razor_atomic *atomic;
   1.448 +    struct razor_root *root;
   1.449 +    struct razor_error *error=NULL;
   1.450      install_root=getenv("RAZOR_ROOT");
   1.451      if (!install_root)
   1.452  	install_root="";
   1.453 -    atomic=razor_atomic_open("Remove packages");
   1.454 -    set=razor_root_open_read_only(install_root,atomic);
   1.455 -    if (!set)
   1.456 +    root=razor_root_open(install_root,&error);
   1.457 +    if (!root)
   1.458      {
   1.459 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.460 -	razor_atomic_destroy(atomic);
   1.461 +	fprintf(stderr,"%s\n",razor_error_get_msg(error));
   1.462 +	razor_error_free(error);
   1.463  	return 0;
   1.464      }
   1.465 -    razor_set_unref(set);
   1.466 -    root=razor_root_open(install_root,atomic);
   1.467 -    if (!root)
   1.468 -    {
   1.469 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.470 -	razor_atomic_destroy(atomic);
   1.471 -	return -1;
   1.472 -    }
   1.473      system=razor_root_get_system_set(root);
   1.474 -    if (!system)
   1.475 -    {
   1.476 -	fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.477 -	razor_root_close(root);
   1.478 -	razor_atomic_destroy(atomic);
   1.479 -	return -1;
   1.480 -    }
   1.481      upstream=razor_set_create_without_root();
   1.482      trans=razor_transaction_create(system,upstream);
   1.483      razor_set_unref(upstream);
   1.484 @@ -520,16 +517,9 @@
   1.485      else
   1.486  	plover_mark_packages_for_removal(trans,system,NULL);
   1.487      if (!retval)
   1.488 -    {
   1.489 -	retval=
   1.490 -	  plover_commit_transaction(trans,NULL,install_root,root,atomic,NULL);
   1.491 -	if (retval)
   1.492 -	    fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic));
   1.493 -    }
   1.494 -    else
   1.495 -	razor_root_close(root);
   1.496 +	retval=plover_commit_transaction(trans,NULL,install_root,root,NULL);
   1.497      razor_transaction_destroy(trans);
   1.498 -    razor_atomic_destroy(atomic);
   1.499 +    razor_root_close(root);
   1.500      return retval;
   1.501  }
   1.502  
   1.503 @@ -543,7 +533,6 @@
   1.504      const char *name;
   1.505      char *install_root;
   1.506      struct razor_set *set;
   1.507 -    struct razor_atomic *atomic;
   1.508      struct razor_package *package;
   1.509      struct razor_package_iterator *pi;
   1.510      struct razor_file_iterator *fi;
   1.511 @@ -553,8 +542,7 @@
   1.512      install_root=getenv("RAZOR_ROOT");
   1.513      if (!install_root)
   1.514  	install_root="";
   1.515 -    atomic=razor_atomic_open("Query packages");
   1.516 -    set=razor_root_open_read_only(install_root,atomic);
   1.517 +    set=razor_root_open_read_only(install_root,NULL);
   1.518      if (set)
   1.519      {
   1.520  	pi=razor_package_iterator_create(set);
   1.521 @@ -573,6 +561,5 @@
   1.522  	razor_package_iterator_destroy(pi);
   1.523  	razor_set_unref(set);
   1.524      }
   1.525 -    razor_atomic_destroy(atomic);
   1.526      return matches;
   1.527  }