Pass installation-count to scripts
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue Jul 07 22:50:22 2009 +0100 (2009-07-07)
changeset 376d15a16347c77
parent 375 c903635ae422
child 377 5549419824b4
Pass installation-count to scripts
librazor/lua.c
librazor/razor-internal.h
librazor/razor.c
librazor/razor.h
librazor/rpm.c
librazor/test-lua.c
src/main.c
test/zip.spec
test/zsh.spec
     1.1 --- a/librazor/lua.c	Mon Jul 06 18:19:13 2009 +0100
     1.2 +++ b/librazor/lua.c	Tue Jul 07 22:50:22 2009 +0100
     1.3 @@ -368,7 +368,7 @@
     1.4  }
     1.5  
     1.6  int run_lua_script(const char *root, const char *name, const char *body,
     1.7 -		   ssize_t len)
     1.8 +		   ssize_t len, int arg1)
     1.9  {
    1.10  	int i, n;
    1.11  	lua_State *L;
    1.12 @@ -428,6 +428,20 @@
    1.13  		return -1;
    1.14  	}
    1.15  
    1.16 +	lua_newtable(L);
    1.17 +	lua_pushvalue(L, LUA_GLOBALSINDEX);
    1.18 +	lua_pushliteral(L, "arg");
    1.19 +	lua_pushvalue(L, -3);
    1.20 +	lua_rawset(L, -3);
    1.21 +	lua_pop(L, 1);
    1.22 +	lua_pushliteral(L, "<lua>");
    1.23 +	lua_rawseti(L, -2, 1);
    1.24 +	if (arg1 >= 0) {
    1.25 +		lua_pushinteger(L, arg1);
    1.26 +		lua_rawseti(L, -2, 2);
    1.27 +	}
    1.28 +	lua_pop(L, 1);
    1.29 +
    1.30  	if (lua_pcall(L, 0, 0, 0)) {
    1.31  		fprintf(stderr, "lua script failed: %s\n", lua_tostring(L, -1));
    1.32  		lua_pop(L, 1);
     2.1 --- a/librazor/razor-internal.h	Mon Jul 06 18:19:13 2009 +0100
     2.2 +++ b/librazor/razor-internal.h	Tue Jul 07 22:50:22 2009 +0100
     2.3 @@ -189,10 +189,10 @@
     2.4  razor_merger_destroy(struct razor_merger *merger);
     2.5  
     2.6  int run_lua_script(const char *root, const char *name, const char *body,
     2.7 -		   ssize_t len);
     2.8 +		   ssize_t len, int arg1);
     2.9  
    2.10  int razor_run_script(const char *root, enum razor_property_flags script,
    2.11 -		     const char *program, const char *body);
    2.12 +		     const char *program, const char *body, int arg1);
    2.13  
    2.14  /* Utility functions */
    2.15  
     3.1 --- a/librazor/razor.c	Mon Jul 06 18:19:13 2009 +0100
     3.2 +++ b/librazor/razor.c	Tue Jul 07 22:50:22 2009 +0100
     3.3 @@ -472,12 +472,13 @@
     3.4   * @set: a %razor_set
     3.5   * @package: a %razor_package
     3.6   * @root: the root into which the package is currently installed
     3.7 + * @install_count: the value to pass to uninstall scripts
     3.8   *
     3.9   * Removes an installed package.
    3.10   **/
    3.11  RAZOR_EXPORT int
    3.12  razor_package_remove(struct razor_set *set, struct razor_package *package,
    3.13 -		     const char *root)
    3.14 +		     const char *root, int install_count)
    3.15  {
    3.16  	struct razor_file_iterator *fi;
    3.17  	struct razor_package_iterator *pi;
    3.18 @@ -504,7 +505,8 @@
    3.19  				  RAZOR_DETAIL_LAST);
    3.20  
    3.21  	environment_set(&env);
    3.22 -	retval = razor_run_script(root, RAZOR_PROPERTY_PREUN, program, script);
    3.23 +	retval = razor_run_script(root, RAZOR_PROPERTY_PREUN, program, script,
    3.24 +				  install_count);
    3.25  	environment_unset(&env);
    3.26  
    3.27  	if (retval) {
    3.28 @@ -539,7 +541,8 @@
    3.29  				  RAZOR_DETAIL_LAST);
    3.30  
    3.31  	environment_set(&env);
    3.32 -	retval = razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script);
    3.33 +	retval = razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script,
    3.34 +				  install_count);
    3.35  	environment_unset(&env);
    3.36  
    3.37  	environment_release(&env);
     4.1 --- a/librazor/razor.h	Mon Jul 06 18:19:13 2009 +0100
     4.2 +++ b/librazor/razor.h	Tue Jul 07 22:50:22 2009 +0100
     4.3 @@ -110,7 +110,7 @@
     4.4  			  struct razor_package *package, ...);
     4.5  int
     4.6  razor_package_remove(struct razor_set *set, struct razor_package *package,
     4.7 -		     const char *root);
     4.8 +		     const char *root, int install_count);
     4.9  
    4.10  
    4.11  /**
    4.12 @@ -291,7 +291,8 @@
    4.13  void razor_rpm_get_details(struct razor_rpm *rpm, ...);
    4.14  void razor_rpm_set_relocations(struct razor_rpm *rpm,
    4.15  			       struct razor_relocations *relocations);
    4.16 -int razor_rpm_install(struct razor_rpm *rpm, const char *root);
    4.17 +int razor_rpm_install(struct razor_rpm *rpm, const char *root,
    4.18 +		      int install_count);
    4.19  int razor_rpm_close(struct razor_rpm *rpm);
    4.20  
    4.21  /**
     5.1 --- a/librazor/rpm.c	Mon Jul 06 18:19:13 2009 +0100
     5.2 +++ b/librazor/rpm.c	Tue Jul 07 22:50:22 2009 +0100
     5.3 @@ -874,7 +874,8 @@
     5.4  }
     5.5  
     5.6  static int
     5.7 -run_script_lua(const char *root, unsigned int script_tag, const char *script)
     5.8 +run_script_lua(const char *root, unsigned int script_tag, const char *script,
     5.9 +	       int arg1)
    5.10  {
    5.11  	int root_fd, retval;
    5.12  #if HAVE_LUA
    5.13 @@ -898,7 +899,8 @@
    5.14  			break;
    5.15  	}
    5.16  	root_fd = chroot_push(root);
    5.17 -	retval = run_lua_script(root_fd < 0 ? root : NULL, name, script, -1);
    5.18 +	retval = run_lua_script(root_fd < 0 ? root : NULL, name, script, -1,
    5.19 +				arg1);
    5.20  	chroot_pop(root_fd);
    5.21  #else	/* HAVE_LUA */
    5.22  	fprintf(stderr, "lua not available to run script\n");
    5.23 @@ -909,10 +911,12 @@
    5.24  }
    5.25  
    5.26  static int
    5.27 -run_script_external(const char *root, const char *program, const char *script)
    5.28 +run_script_external(const char *root, const char *program, const char *script,
    5.29 +		    int arg1)
    5.30  {
    5.31  	int root_fd, retval;
    5.32  	FILE *fp;
    5.33 +	char buf[32], *command;
    5.34  
    5.35  	if (program == NULL) {
    5.36  #if MSWIN_API
    5.37 @@ -930,7 +934,14 @@
    5.38  	}
    5.39  
    5.40  	root_fd = chroot_push(root);
    5.41 -	fp = popen(program, "w");
    5.42 +	if (arg1 >= 0) {
    5.43 +		sprintf(buf, "%d", arg1);
    5.44 +		command = malloc(strlen(program) + strlen(buf) + 2);
    5.45 +		sprintf(command, "%s %s", program, buf);
    5.46 +	} else
    5.47 +		command = strdup(program);
    5.48 +	fp = popen(command, "w");
    5.49 +	free(command);
    5.50  
    5.51  	if (!fp) {
    5.52  		perror(program);
    5.53 @@ -950,12 +961,12 @@
    5.54  
    5.55  static int
    5.56  run_script(struct installer *installer,
    5.57 -	   unsigned int program_tag, unsigned int script_tag)
    5.58 +	   unsigned int program_tag, unsigned int script_tag, int arg1)
    5.59  {
    5.60  	int i, retval;
    5.61  	struct razor_rpm *rpm = installer->rpm;
    5.62  	const char *script = NULL, *program = NULL, *prefix;
    5.63 -	char buf[32], *p;
    5.64 +	char buf[32];
    5.65  	struct environment env;
    5.66  
    5.67  	program = razor_rpm_get_indirect(rpm, program_tag, NULL);
    5.68 @@ -975,9 +986,11 @@
    5.69  	}
    5.70  
    5.71  	if (program && strcmp(program, "<lua>") == 0)
    5.72 -		retval = run_script_lua(installer->root, script_tag, script);
    5.73 +		retval = run_script_lua(installer->root, script_tag, script,
    5.74 +					arg1);
    5.75  	else
    5.76 -		retval = run_script_external(installer->root, program, script);
    5.77 +		retval = run_script_external(installer->root, program, script,
    5.78 +					     arg1);
    5.79  
    5.80  	if (rpm->relocations) {
    5.81  		environment_unset(&env);
    5.82 @@ -989,7 +1002,7 @@
    5.83  
    5.84  int
    5.85  razor_run_script(const char *root, enum razor_property_flags script,
    5.86 -		 const char *program, const char *body)
    5.87 +		 const char *program, const char *body, int arg1)
    5.88  {
    5.89  	int retval;
    5.90  	unsigned int script_tag;
    5.91 @@ -1020,10 +1033,10 @@
    5.92  			script_tag = 0;
    5.93  			break;
    5.94  		}
    5.95 -		retval = run_script_lua(root, script_tag, body);
    5.96 +		retval = run_script_lua(root, script_tag, body, arg1);
    5.97  	}
    5.98  	else
    5.99 -		retval = run_script_external(root, program, body);
   5.100 +		retval = run_script_external(root, program, body, arg1);
   5.101  
   5.102  	return retval;
   5.103  }
   5.104 @@ -1101,7 +1114,7 @@
   5.105  }
   5.106  
   5.107  RAZOR_EXPORT int
   5.108 -razor_rpm_install(struct razor_rpm *rpm, const char *root)
   5.109 +razor_rpm_install(struct razor_rpm *rpm, const char *root, int install_count)
   5.110  {
   5.111  	struct installer installer;
   5.112  	struct cpio_file_header *header;
   5.113 @@ -1130,7 +1143,7 @@
   5.114  	if (installer_init(&installer))
   5.115  		return -1;
   5.116  
   5.117 -	run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN);
   5.118 +	run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN, install_count);
   5.119  
   5.120  	while (installer.stream.avail_in > 0) {
   5.121  		installer.rest = sizeof *header;
   5.122 @@ -1167,7 +1180,7 @@
   5.123  	if (installer_finish(&installer))
   5.124  		return -1;
   5.125  
   5.126 -	run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN);
   5.127 +	run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN, install_count);
   5.128  
   5.129  	return 0;
   5.130  }
     6.1 --- a/librazor/test-lua.c	Mon Jul 06 18:19:13 2009 +0100
     6.2 +++ b/librazor/test-lua.c	Tue Jul 07 22:50:22 2009 +0100
     6.3 @@ -94,7 +94,7 @@
     6.4  	free(s);
     6.5  
     6.6  	script = razor_file_get_contents(test_file, &len);
     6.7 -	r = run_lua_script(root, test_file, script, len);
     6.8 +	r = run_lua_script(root, test_file, script, len, -1);
     6.9  	razor_file_free_contents(script, len);
    6.10  
    6.11  	recursive_remove(root);
     7.1 --- a/src/main.c	Mon Jul 06 18:19:13 2009 +0100
     7.2 +++ b/src/main.c	Tue Jul 07 22:50:22 2009 +0100
     7.3 @@ -907,7 +907,7 @@
     7.4  	if (relocations)
     7.5  		razor_rpm_set_relocations(rpm, relocations);
     7.6  	razor_transaction_fixup_package(trans, package, rpm);
     7.7 -	retval = razor_rpm_install(rpm, install_root);
     7.8 +	retval = razor_rpm_install(rpm, install_root, 1);
     7.9  	if (retval < 0)
    7.10  		fprintf(stderr, "failed to install rpm %s\n", file);
    7.11  	razor_rpm_close(rpm);
    7.12 @@ -932,7 +932,7 @@
    7.13  						 relocations);
    7.14  		else if (action == RAZOR_INSTALL_ACTION_REMOVE)
    7.15  			retval = razor_package_remove(set, package,
    7.16 -						      install_root);
    7.17 +						      install_root, 0);
    7.18  	}
    7.19  	razor_install_iterator_destroy(ii);
    7.20  
     8.1 --- a/test/zip.spec	Mon Jul 06 18:19:13 2009 +0100
     8.2 +++ b/test/zip.spec	Tue Jul 07 22:50:22 2009 +0100
     8.3 @@ -33,7 +33,7 @@
     8.4  if prefix==nil then
     8.5      prefix="/usr"
     8.6  end
     8.7 -if posix.stat(prefix.."/bin/zap")~=nil then
     8.8 +if arg[2]==1 and posix.stat(prefix.."/bin/zap")~=nil then
     8.9      mkdir_missing(prefix.."/var")
    8.10      mkdir_missing(prefix.."/var/lib")
    8.11      posix.mkdir(prefix.."/var/lib/zip")
    8.12 @@ -47,7 +47,7 @@
    8.13  if prefix==nil then
    8.14      prefix="/usr"
    8.15  end
    8.16 -if posix.stat(prefix.."/bin/zap")~=nil then
    8.17 +if arg[2]==0 and posix.stat(prefix.."/bin/zap")~=nil then
    8.18      os.remove(prefix.."/var/lib/zip/data.zap")
    8.19      os.remove(prefix.."/var/lib/zip")
    8.20  end
     9.1 --- a/test/zsh.spec	Mon Jul 06 18:19:13 2009 +0100
     9.2 +++ b/test/zsh.spec	Tue Jul 07 22:50:22 2009 +0100
     9.3 @@ -35,7 +35,7 @@
     9.4  if prefix==nil then
     9.5      prefix="/usr"
     9.6  end
     9.7 -if posix.stat(prefix.."/bin/zip")~=nil then
     9.8 +if arg[2]==1 and posix.stat(prefix.."/bin/zip")~=nil then
     9.9      mkdir_missing(prefix.."/var")
    9.10      mkdir_missing(prefix.."/var/lib")
    9.11      posix.mkdir(prefix.."/var/lib/zsh")
    9.12 @@ -49,7 +49,7 @@
    9.13  if prefix==nil then
    9.14      prefix="/usr"
    9.15  end
    9.16 -if posix.stat(prefix.."/bin/zip")~=nil then
    9.17 +if arg[2]==0 and posix.stat(prefix.."/bin/zip")~=nil then
    9.18      os.remove(prefix.."/var/lib/zsh/data.zip")
    9.19      os.remove(prefix.."/var/lib/zsh")
    9.20  end