Pass installation-count to scripts
authorJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 7 Jul 2009 21:50:22 +0000 (22:50 +0100)
committerJ. Ali Harlow <ali@juiblex.co.uk>
Tue, 7 Jul 2009 21:50:22 +0000 (22:50 +0100)
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

index dd33048..f32e1f5 100644 (file)
@@ -368,7 +368,7 @@ static void razor_lua_preload(lua_State *L)
 }
 
 int run_lua_script(const char *root, const char *name, const char *body,
-                  ssize_t len)
+                  ssize_t len, int arg1)
 {
        int i, n;
        lua_State *L;
@@ -428,6 +428,20 @@ int run_lua_script(const char *root, const char *name, const char *body,
                return -1;
        }
 
+       lua_newtable(L);
+       lua_pushvalue(L, LUA_GLOBALSINDEX);
+       lua_pushliteral(L, "arg");
+       lua_pushvalue(L, -3);
+       lua_rawset(L, -3);
+       lua_pop(L, 1);
+       lua_pushliteral(L, "<lua>");
+       lua_rawseti(L, -2, 1);
+       if (arg1 >= 0) {
+               lua_pushinteger(L, arg1);
+               lua_rawseti(L, -2, 2);
+       }
+       lua_pop(L, 1);
+
        if (lua_pcall(L, 0, 0, 0)) {
                fprintf(stderr, "lua script failed: %s\n", lua_tostring(L, -1));
                lua_pop(L, 1);
index cfc4328..b7273b3 100644 (file)
@@ -189,10 +189,10 @@ void
 razor_merger_destroy(struct razor_merger *merger);
 
 int run_lua_script(const char *root, const char *name, const char *body,
-                  ssize_t len);
+                  ssize_t len, int arg1);
 
 int razor_run_script(const char *root, enum razor_property_flags script,
-                    const char *program, const char *body);
+                    const char *program, const char *body, int arg1);
 
 /* Utility functions */
 
index f2fd53d..44c3526 100644 (file)
@@ -472,12 +472,13 @@ razor_package_get_details(struct razor_set *set, struct razor_package *package,
  * @set: a %razor_set
  * @package: a %razor_package
  * @root: the root into which the package is currently installed
+ * @install_count: the value to pass to uninstall scripts
  *
  * Removes an installed package.
  **/
 RAZOR_EXPORT int
 razor_package_remove(struct razor_set *set, struct razor_package *package,
-                    const char *root)
+                    const char *root, int install_count)
 {
        struct razor_file_iterator *fi;
        struct razor_package_iterator *pi;
@@ -504,7 +505,8 @@ razor_package_remove(struct razor_set *set, struct razor_package *package,
                                  RAZOR_DETAIL_LAST);
 
        environment_set(&env);
-       retval = razor_run_script(root, RAZOR_PROPERTY_PREUN, program, script);
+       retval = razor_run_script(root, RAZOR_PROPERTY_PREUN, program, script,
+                                 install_count);
        environment_unset(&env);
 
        if (retval) {
@@ -539,7 +541,8 @@ razor_package_remove(struct razor_set *set, struct razor_package *package,
                                  RAZOR_DETAIL_LAST);
 
        environment_set(&env);
-       retval = razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script);
+       retval = razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script,
+                                 install_count);
        environment_unset(&env);
 
        environment_release(&env);
index 19e732c..c45ea15 100644 (file)
@@ -110,7 +110,7 @@ razor_package_get_details(struct razor_set *set,
                          struct razor_package *package, ...);
 int
 razor_package_remove(struct razor_set *set, struct razor_package *package,
-                    const char *root);
+                    const char *root, int install_count);
 
 
 /**
@@ -291,7 +291,8 @@ struct razor_rpm *razor_rpm_open(const char *filename);
 void razor_rpm_get_details(struct razor_rpm *rpm, ...);
 void razor_rpm_set_relocations(struct razor_rpm *rpm,
                               struct razor_relocations *relocations);
-int razor_rpm_install(struct razor_rpm *rpm, const char *root);
+int razor_rpm_install(struct razor_rpm *rpm, const char *root,
+                     int install_count);
 int razor_rpm_close(struct razor_rpm *rpm);
 
 /**
index f3c2724..cfcb3e8 100644 (file)
@@ -874,7 +874,8 @@ static void chroot_pop(int fd)
 }
 
 static int
-run_script_lua(const char *root, unsigned int script_tag, const char *script)
+run_script_lua(const char *root, unsigned int script_tag, const char *script,
+              int arg1)
 {
        int root_fd, retval;
 #if HAVE_LUA
@@ -898,7 +899,8 @@ run_script_lua(const char *root, unsigned int script_tag, const char *script)
                        break;
        }
        root_fd = chroot_push(root);
-       retval = run_lua_script(root_fd < 0 ? root : NULL, name, script, -1);
+       retval = run_lua_script(root_fd < 0 ? root : NULL, name, script, -1,
+                               arg1);
        chroot_pop(root_fd);
 #else  /* HAVE_LUA */
        fprintf(stderr, "lua not available to run script\n");
@@ -909,10 +911,12 @@ run_script_lua(const char *root, unsigned int script_tag, const char *script)
 }
 
 static int
-run_script_external(const char *root, const char *program, const char *script)
+run_script_external(const char *root, const char *program, const char *script,
+                   int arg1)
 {
        int root_fd, retval;
        FILE *fp;
+       char buf[32], *command;
 
        if (program == NULL) {
 #if MSWIN_API
@@ -930,7 +934,14 @@ run_script_external(const char *root, const char *program, const char *script)
        }
 
        root_fd = chroot_push(root);
-       fp = popen(program, "w");
+       if (arg1 >= 0) {
+               sprintf(buf, "%d", arg1);
+               command = malloc(strlen(program) + strlen(buf) + 2);
+               sprintf(command, "%s %s", program, buf);
+       } else
+               command = strdup(program);
+       fp = popen(command, "w");
+       free(command);
 
        if (!fp) {
                perror(program);
@@ -950,12 +961,12 @@ run_script_external(const char *root, const char *program, const char *script)
 
 static int
 run_script(struct installer *installer,
-          unsigned int program_tag, unsigned int script_tag)
+          unsigned int program_tag, unsigned int script_tag, int arg1)
 {
        int i, retval;
        struct razor_rpm *rpm = installer->rpm;
        const char *script = NULL, *program = NULL, *prefix;
-       char buf[32], *p;
+       char buf[32];
        struct environment env;
 
        program = razor_rpm_get_indirect(rpm, program_tag, NULL);
@@ -975,9 +986,11 @@ run_script(struct installer *installer,
        }
 
        if (program && strcmp(program, "<lua>") == 0)
-               retval = run_script_lua(installer->root, script_tag, script);
+               retval = run_script_lua(installer->root, script_tag, script,
+                                       arg1);
        else
-               retval = run_script_external(installer->root, program, script);
+               retval = run_script_external(installer->root, program, script,
+                                            arg1);
 
        if (rpm->relocations) {
                environment_unset(&env);
@@ -989,7 +1002,7 @@ run_script(struct installer *installer,
 
 int
 razor_run_script(const char *root, enum razor_property_flags script,
-                const char *program, const char *body)
+                const char *program, const char *body, int arg1)
 {
        int retval;
        unsigned int script_tag;
@@ -1020,10 +1033,10 @@ razor_run_script(const char *root, enum razor_property_flags script,
                        script_tag = 0;
                        break;
                }
-               retval = run_script_lua(root, script_tag, body);
+               retval = run_script_lua(root, script_tag, body, arg1);
        }
        else
-               retval = run_script_external(root, program, body);
+               retval = run_script_external(root, program, body, arg1);
 
        return retval;
 }
@@ -1101,7 +1114,7 @@ fixed_hex_to_ulong(const char *hex, int length)
 }
 
 RAZOR_EXPORT int
-razor_rpm_install(struct razor_rpm *rpm, const char *root)
+razor_rpm_install(struct razor_rpm *rpm, const char *root, int install_count)
 {
        struct installer installer;
        struct cpio_file_header *header;
@@ -1130,7 +1143,7 @@ razor_rpm_install(struct razor_rpm *rpm, const char *root)
        if (installer_init(&installer))
                return -1;
 
-       run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN);
+       run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN, install_count);
 
        while (installer.stream.avail_in > 0) {
                installer.rest = sizeof *header;
@@ -1167,7 +1180,7 @@ razor_rpm_install(struct razor_rpm *rpm, const char *root)
        if (installer_finish(&installer))
                return -1;
 
-       run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN);
+       run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN, install_count);
 
        return 0;
 }
index 6be0308..b069ad0 100644 (file)
@@ -94,7 +94,7 @@ int main(int argc, char *argv[])
        free(s);
 
        script = razor_file_get_contents(test_file, &len);
-       r = run_lua_script(root, test_file, script, len);
+       r = run_lua_script(root, test_file, script, len, -1);
        razor_file_free_contents(script, len);
 
        recursive_remove(root);
index 28eddc8..92f70c5 100644 (file)
@@ -907,7 +907,7 @@ install_package(struct razor_transaction *trans, struct razor_set *set,
        if (relocations)
                razor_rpm_set_relocations(rpm, relocations);
        razor_transaction_fixup_package(trans, package, rpm);
-       retval = razor_rpm_install(rpm, install_root);
+       retval = razor_rpm_install(rpm, install_root, 1);
        if (retval < 0)
                fprintf(stderr, "failed to install rpm %s\n", file);
        razor_rpm_close(rpm);
@@ -932,7 +932,7 @@ update_packages(struct razor_transaction *trans, struct razor_set *system,
                                                 relocations);
                else if (action == RAZOR_INSTALL_ACTION_REMOVE)
                        retval = razor_package_remove(set, package,
-                                                     install_root);
+                                                     install_root, 0);
        }
        razor_install_iterator_destroy(ii);
 
index ed7a619..25ca630 100644 (file)
@@ -33,7 +33,7 @@ prefix=posix.getenv("RPM_INSTALL_PREFIX0")
 if prefix==nil then
     prefix="/usr"
 end
-if posix.stat(prefix.."/bin/zap")~=nil then
+if arg[2]==1 and posix.stat(prefix.."/bin/zap")~=nil then
     mkdir_missing(prefix.."/var")
     mkdir_missing(prefix.."/var/lib")
     posix.mkdir(prefix.."/var/lib/zip")
@@ -47,7 +47,7 @@ prefix=posix.getenv("RPM_INSTALL_PREFIX0")
 if prefix==nil then
     prefix="/usr"
 end
-if posix.stat(prefix.."/bin/zap")~=nil then
+if arg[2]==0 and posix.stat(prefix.."/bin/zap")~=nil then
     os.remove(prefix.."/var/lib/zip/data.zap")
     os.remove(prefix.."/var/lib/zip")
 end
index 5523a0f..7b23ed0 100644 (file)
@@ -35,7 +35,7 @@ prefix=posix.getenv("RPM_INSTALL_PREFIX0")
 if prefix==nil then
     prefix="/usr"
 end
-if posix.stat(prefix.."/bin/zip")~=nil then
+if arg[2]==1 and posix.stat(prefix.."/bin/zip")~=nil then
     mkdir_missing(prefix.."/var")
     mkdir_missing(prefix.."/var/lib")
     posix.mkdir(prefix.."/var/lib/zsh")
@@ -49,7 +49,7 @@ prefix=posix.getenv("RPM_INSTALL_PREFIX0")
 if prefix==nil then
     prefix="/usr"
 end
-if posix.stat(prefix.."/bin/zip")~=nil then
+if arg[2]==0 and posix.stat(prefix.."/bin/zip")~=nil then
     os.remove(prefix.."/var/lib/zsh/data.zip")
     os.remove(prefix.."/var/lib/zsh")
 end