diff -r b0a35bae4961 -r 5cafc65a6183 plover/razor.c --- a/plover/razor.c Thu Nov 10 11:15:09 2011 +0000 +++ b/plover/razor.c Tue Feb 21 22:55:40 2012 +0000 @@ -1,7 +1,7 @@ /* * Copyright (C) 2008 Kristian Høgsberg * Copyright (C) 2008 Red Hat, Inc - * Copyright (C) 2009, 2011 J. Ali Harlow + * Copyright (C) 2009, 2011, 2012 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,8 +39,8 @@ } struct razor_set *plover_relocate_packages(struct razor_set *set, - struct razor_atomic *atomic,const char *base, - struct razor_relocations *relocations) + const char *base,struct razor_relocations *relocations, + struct razor_error **error) { struct razor_importer *importer; struct razor_property_iterator *prop_iter; @@ -63,7 +63,7 @@ s=rpm_filename(name,version,arch); file=plover_strconcat(base,"/rpms/",s,NULL); free(s); - rpm=razor_rpm_open(file,atomic); + rpm=razor_rpm_open(file,error); free(file); if (!rpm) { @@ -96,6 +96,10 @@ return new; } +/* + * Returns 0 on success, -1 on failure and 1 if a RAZOR_INSTALL_ACTION_COMMIT + * is met (in which case the action is consumed). + */ int plover_run_transaction(struct razor_transaction *trans, struct razor_install_iterator *ii,const char *base,const char *install_root, struct razor_set *system,struct razor_set *next,struct razor_atomic *atomic, @@ -104,10 +108,10 @@ struct razor_package *package; enum razor_install_action action; struct razor_rpm *rpm; + struct razor_error *error=NULL; const char *name,*version,*arch; char *s,*file; - int count; - razor_install_iterator_rewind(ii); + int r,count; switch(stage) { case RAZOR_STAGE_SCRIPTS_PRE: @@ -131,12 +135,12 @@ RAZOR_DETAIL_LAST); if (stage==RAZOR_STAGE_FILES) printf(" Removing : %s ",name); - razor_package_remove(system,next,atomic,package,install_root, + r=razor_package_remove(system,next,atomic,package,install_root, count,stage); if (stage==RAZOR_STAGE_FILES) printf("\n"); } - else + else if (action==RAZOR_INSTALL_ACTION_ADD) { razor_package_get_details(next,package,RAZOR_DETAIL_NAME,&name, RAZOR_DETAIL_VERSION,&version,RAZOR_DETAIL_ARCH,&arch, @@ -144,56 +148,102 @@ s=rpm_filename(name,version,arch); file=plover_strconcat(base,"/rpms/",s,NULL); free(s); - rpm=razor_rpm_open(file,atomic); + rpm=razor_rpm_open(file,&error); free(file); if (!rpm) + { + razor_atomic_abort(atomic,razor_error_get_msg(error)); + razor_error_free(error); return -1; + } if (stage==RAZOR_STAGE_FILES) printf(" Installing : %s ",name); if (relocations) razor_rpm_set_relocations(rpm,relocations); razor_transaction_fixup_package(trans,package,rpm); - razor_rpm_install(rpm,atomic,install_root,1,stage); + r=razor_rpm_install(rpm,atomic,install_root,1,stage); razor_rpm_close(rpm); if (stage==RAZOR_STAGE_FILES) printf("\n"); } + else if (action==RAZOR_INSTALL_ACTION_COMMIT) + return 1; + else + r=0; if (razor_atomic_in_error_state(atomic)) return -1; + else if (r) + { + razor_package_get_details(next,package,RAZOR_DETAIL_NAME,&name, + RAZOR_DETAIL_VERSION,&version,RAZOR_DETAIL_ARCH,&arch, + RAZOR_DETAIL_LAST); + /* + * If a pre or preun script fails, then we should + * treat that as a fatal error. post and postun + * script failures are treated as warnings. Be + * nice and tell the user _which_ script failed. + */ + fprintf(stderr, + "%s: %s(%s-%s.%s) scriptlet failed, exit status %d\n", + stage==RAZOR_STAGE_SCRIPTS_PRE?"error":"warning", + action==RAZOR_INSTALL_ACTION_ADD?"%pre":"%preun", + name,version,arch,r); + if (stage==RAZOR_STAGE_SCRIPTS_PRE) + return -1; + } } return 0; } -/* - * Note: plover_commit_transaction() takes ownership of root which should - * not be used after it returns. - */ int plover_commit_transaction(struct razor_transaction *trans,const char *base, - const char *install_root,struct razor_root *root,struct razor_atomic *atomic, + const char *install_root,struct razor_root *root, struct razor_relocations *relocations) { - int retval; - struct razor_set *next,*system; + int r,retval; + size_t pos; + struct razor_set *system,*next,*set; struct razor_install_iterator *ii; + struct razor_atomic *atomic; razor_transaction_resolve(trans); if (razor_transaction_describe(trans)>0) + return -1; + next=razor_transaction_commit(trans); + system=razor_set_ref(razor_root_get_system_set(root)); + ii=razor_set_create_install_iterator(system,next); + do { - razor_root_close(root); - return -1; - } - next=razor_transaction_commit(trans); - system=razor_root_get_system_set(root); - ii=razor_set_create_install_iterator(system,next); - plover_run_transaction(trans,ii,base,install_root,system,next,atomic, - relocations,RAZOR_STAGE_SCRIPTS_PRE); - plover_run_transaction(trans,ii,base,install_root,system,next,atomic, - relocations,RAZOR_STAGE_FILES); - razor_root_update(root,next); - razor_root_commit(root); - retval=razor_atomic_commit(atomic); - if (!retval) - plover_run_transaction(trans,ii,base,install_root,system,next,atomic, - relocations,RAZOR_STAGE_SCRIPTS_POST); + pos=razor_install_iterator_tell(ii); + atomic=razor_atomic_open("package transaction"); + r=plover_run_transaction(trans,ii,base,install_root,system,next,atomic, + relocations,RAZOR_STAGE_SCRIPTS_PRE); + if (r<0) + fprintf(stderr,"Transaction aborted\n"); + else + { + razor_install_iterator_seek(ii,pos); + r=plover_run_transaction(trans,ii,base,install_root,system,next, + atomic,relocations,RAZOR_STAGE_FILES); + if (r==1) + { + set=razor_install_iterator_commit_set(ii); + razor_root_update(root,set,atomic); + razor_set_unref(set); + } + else if (!r) + razor_root_update(root,next,atomic); + retval=razor_atomic_commit(atomic); + if (retval) + fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); + else + { + razor_install_iterator_seek(ii,pos); + plover_run_transaction(trans,ii,base,install_root,system,next, + atomic,relocations,RAZOR_STAGE_SCRIPTS_POST); + } + } + razor_atomic_destroy(atomic); + } while(!retval && r==1); + razor_set_unref(system); razor_set_unref(next); razor_install_iterator_destroy(ii); return retval; @@ -226,11 +276,11 @@ int i,retval; char *s; char *install_root; - struct razor_root *root; struct razor_set *system,*set,*upstream; struct razor_transaction *trans; struct razor_relocations *relocations; - struct razor_atomic *atomic; + struct razor_root *root; + struct razor_error *error=NULL; install_root=getenv("RAZOR_ROOT"); if (!install_root) install_root=""; @@ -241,37 +291,23 @@ } else relocations=NULL; - atomic=razor_atomic_open("Install packages"); - /* - * Calling razor_root_open() on a system that hasn't yet had - * razor_root_create() run generates a confusing error message - * on stderr. Avoid this by trying to open it R/O first which - * fails without generating any error. - */ - set=razor_root_open_read_only(install_root,atomic); - if (set) - razor_set_unref(set); - else - razor_root_create(install_root); - root=razor_root_open(install_root,atomic); + root=razor_root_open(install_root,NULL); if (!root) { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_atomic_destroy(atomic); - if (relocations) - razor_relocations_destroy(relocations); - return -1; + if (razor_root_create(install_root,&error)) + root=NULL; + else + root=razor_root_open(install_root,&error); + if (!root) + { + fprintf(stderr,"%s\n",razor_error_get_msg(error)); + razor_error_free(error); + if (relocations) + razor_relocations_destroy(relocations); + return -1; + } } system=razor_root_get_system_set(root); - if (!system) - { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_root_close(root); - razor_atomic_destroy(atomic); - if (relocations) - razor_relocations_destroy(relocations); - return -1; - } s=plover_strconcat(base,"/repodata",NULL); if (s) { @@ -280,13 +316,14 @@ perror(s); } else + { + fprintf(stderr,"Not enough memory\n"); retval=-1; + } free(s); if (retval<0) { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); razor_root_close(root); - razor_atomic_destroy(atomic); if (relocations) razor_relocations_destroy(relocations); return -1; @@ -294,16 +331,19 @@ set=plover_razor_set_create_from_yum(base); if (set) { - upstream=plover_relocate_packages(set,atomic,base,relocations); + upstream=plover_relocate_packages(set,base,relocations,&error); + if (!upstream) + { + fprintf(stderr,"%s\n",razor_error_get_msg(error)); + razor_error_free(error); + } razor_set_unref(set); } else upstream=NULL; if (!upstream) { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); razor_root_close(root); - razor_atomic_destroy(atomic); if (relocations) razor_relocations_destroy(relocations); return -1; @@ -319,16 +359,10 @@ break; } if (!retval) - { - retval=plover_commit_transaction(trans,base,install_root,root,atomic, + retval=plover_commit_transaction(trans,base,install_root,root, relocations); - if (retval) - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - } - else - razor_root_close(root); razor_transaction_destroy(trans); - razor_atomic_destroy(atomic); + razor_root_close(root); if (relocations) razor_relocations_destroy(relocations); return retval; @@ -338,11 +372,11 @@ { int i,retval; char *install_root,*s; - struct razor_root *root; struct razor_set *system,*set,*upstream; struct razor_transaction *trans; struct razor_relocations *relocations; - struct razor_atomic *atomic; + struct razor_root *root; + struct razor_error *error=NULL; install_root=getenv("RAZOR_ROOT"); if (!install_root) install_root=""; @@ -353,36 +387,16 @@ } else relocations=NULL; - atomic=razor_atomic_open("Update packages"); - set=razor_root_open_read_only(install_root,atomic); - if (!set) + root=razor_root_open(install_root,&error); + if (!root) { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_atomic_destroy(atomic); + fprintf(stderr,"%s\n",razor_error_get_msg(error)); + razor_error_free(error); if (relocations) razor_relocations_destroy(relocations); return 0; } - razor_set_unref(set); - root=razor_root_open(install_root,atomic); - if (!root) - { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_atomic_destroy(atomic); - if (relocations) - razor_relocations_destroy(relocations); - return -1; - } system=razor_root_get_system_set(root); - if (!system) - { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_root_close(root); - razor_atomic_destroy(atomic); - if (relocations) - razor_relocations_destroy(relocations); - return -1; - } s=plover_strconcat(base,"/repodata",NULL); if (s) { @@ -391,13 +405,14 @@ perror(s); } else + { + fprintf(stderr,"Not enough memory"); retval=-1; + } free(s); if (retval) { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); razor_root_close(root); - razor_atomic_destroy(atomic); if (relocations) razor_relocations_destroy(relocations); return -1; @@ -405,16 +420,19 @@ set=plover_razor_set_create_from_yum(base); if (set) { - upstream=plover_relocate_packages(set,atomic,base,relocations); + upstream=plover_relocate_packages(set,base,relocations,&error); + if (!upstream) + { + fprintf(stderr,"%s\n",razor_error_get_msg(error)); + razor_error_free(error); + } razor_set_unref(set); } else upstream=NULL; if (!upstream) { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); razor_root_close(root); - razor_atomic_destroy(atomic); if (relocations) razor_relocations_destroy(relocations); return -1; @@ -433,18 +451,13 @@ } else razor_transaction_update_all(trans); - if (!retval) { - retval=plover_commit_transaction(trans,base,install_root,root,atomic, + if (!retval) + retval=plover_commit_transaction(trans,base,install_root,root, relocations); - if (retval) - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - } - else - razor_root_close(root); razor_transaction_destroy(trans); + razor_root_close(root); if (relocations) razor_relocations_destroy(relocations); - razor_atomic_destroy(atomic); return retval; } @@ -473,37 +486,21 @@ { int i,retval=0; char *install_root; - struct razor_root *root; struct razor_set *system,*set,*upstream; struct razor_transaction *trans; - struct razor_atomic *atomic; + struct razor_root *root; + struct razor_error *error=NULL; install_root=getenv("RAZOR_ROOT"); if (!install_root) install_root=""; - atomic=razor_atomic_open("Remove packages"); - set=razor_root_open_read_only(install_root,atomic); - if (!set) + root=razor_root_open(install_root,&error); + if (!root) { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_atomic_destroy(atomic); + fprintf(stderr,"%s\n",razor_error_get_msg(error)); + razor_error_free(error); return 0; } - razor_set_unref(set); - root=razor_root_open(install_root,atomic); - if (!root) - { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_atomic_destroy(atomic); - return -1; - } system=razor_root_get_system_set(root); - if (!system) - { - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - razor_root_close(root); - razor_atomic_destroy(atomic); - return -1; - } upstream=razor_set_create_without_root(); trans=razor_transaction_create(system,upstream); razor_set_unref(upstream); @@ -520,16 +517,9 @@ else plover_mark_packages_for_removal(trans,system,NULL); if (!retval) - { - retval= - plover_commit_transaction(trans,NULL,install_root,root,atomic,NULL); - if (retval) - fprintf(stderr,"%s\n",razor_atomic_get_error_msg(atomic)); - } - else - razor_root_close(root); + retval=plover_commit_transaction(trans,NULL,install_root,root,NULL); razor_transaction_destroy(trans); - razor_atomic_destroy(atomic); + razor_root_close(root); return retval; } @@ -543,7 +533,6 @@ const char *name; char *install_root; struct razor_set *set; - struct razor_atomic *atomic; struct razor_package *package; struct razor_package_iterator *pi; struct razor_file_iterator *fi; @@ -553,8 +542,7 @@ install_root=getenv("RAZOR_ROOT"); if (!install_root) install_root=""; - atomic=razor_atomic_open("Query packages"); - set=razor_root_open_read_only(install_root,atomic); + set=razor_root_open_read_only(install_root,NULL); if (set) { pi=razor_package_iterator_create(set); @@ -573,6 +561,5 @@ razor_package_iterator_destroy(pi); razor_set_unref(set); } - razor_atomic_destroy(atomic); return matches; }