Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add a variable to notify scripts when they are running for an upgrade
Baptiste Daroussin committed 6 years ago
commit 5f91af05076f9b40cc9424ed02691348ee8b8487
parent 5278d0b
8 files changed +164 -21
modified libpkg/lua_scripts.c
@@ -213,7 +213,7 @@ lua_override_ios(lua_State *L)
}

int
-
pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type)
+
pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type, bool upgrade)
{
	int ret = EPKG_OK;
	struct pkg_lua_script *lscript;
@@ -269,6 +269,8 @@ pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type)
				ctx.pkg_rootdir = "/";
			lua_pushstring(L, ctx.pkg_rootdir);
			lua_setglobal(L, "pkg_rootdir");
+
			lua_pushboolean(L, (upgrade));
+
			lua_setglobal(L, "pkg_upgrade");
			lua_pushcfunction(L, lua_print_msg);
			luaL_newlib(L, pkg_lib);
			lua_setglobal(L, "pkg");
@@ -298,6 +300,7 @@ pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type)
		memset(&pfd, 0, sizeof(pfd));
		pfd.fd = cur_pipe[0];
		pfd.events = POLLIN | POLLERR | POLLHUP;
+

		f = fdopen(pfd.fd, "r");
		for (;;) {
			if (poll(&pfd, 1, -1) == -1) {
modified libpkg/pkg_add.c
@@ -964,11 +964,11 @@ pkg_add_cleanup_old(struct pkgdb *db, struct pkg *old, struct pkg *new, int flag
	 * Execute pre deinstall scripts
	 */
	if ((flags & PKG_ADD_NOSCRIPT) == 0) {
-
		ret = pkg_script_run(old, PKG_SCRIPT_PRE_DEINSTALL);
+
		ret = pkg_script_run(old, PKG_SCRIPT_PRE_DEINSTALL, (old != NULL));
		if (ret != EPKG_OK && ctx.developer_mode) {
			return (ret);
		} else {
-
			ret = pkg_lua_script_run(old, PKG_LUA_PRE_DEINSTALL);
+
			ret = pkg_lua_script_run(old, PKG_LUA_PRE_DEINSTALL, (old != NULL));
			if (ret != EPKG_OK && ctx.developer_mode) {
				return (ret);
			} else {
@@ -1117,9 +1117,9 @@ pkg_add_common(struct pkgdb *db, const char *path, unsigned flags,
	 * Execute pre-install scripts
	 */
	if ((flags & PKG_ADD_NOSCRIPT) == 0) {
-
		if ((retcode = pkg_script_run(pkg, PKG_SCRIPT_PRE_INSTALL)) != EPKG_OK)
+
		if ((retcode = pkg_script_run(pkg, PKG_SCRIPT_PRE_INSTALL, (local != NULL))) != EPKG_OK)
			goto cleanup;
-
		if ((retcode = pkg_lua_script_run(pkg, PKG_LUA_PRE_INSTALL)) != EPKG_OK)
+
		if ((retcode = pkg_lua_script_run(pkg, PKG_LUA_PRE_INSTALL, (local != NULL))) != EPKG_OK)
			goto cleanup;
	}

@@ -1165,8 +1165,8 @@ cleanup_reg:
	if (retcode != EPKG_OK)
		goto cleanup;
	if ((flags & PKG_ADD_NOSCRIPT) == 0) {
-
		pkg_script_run(pkg, PKG_SCRIPT_POST_INSTALL);
-
		pkg_lua_script_run(pkg, PKG_LUA_POST_INSTALL);
+
		pkg_script_run(pkg, PKG_SCRIPT_POST_INSTALL, (local != NULL));
+
		pkg_lua_script_run(pkg, PKG_LUA_POST_INSTALL, (local != NULL));
	}

	/*
modified libpkg/pkg_delete.c
@@ -91,11 +91,11 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)

	if ((flags & PKG_DELETE_NOSCRIPT) == 0) {
		if (!(flags & PKG_DELETE_UPGRADE)) {
-
			ret = pkg_script_run(pkg, PKG_SCRIPT_PRE_DEINSTALL);
+
			ret = pkg_script_run(pkg, PKG_SCRIPT_PRE_DEINSTALL, false);
			if (ret != EPKG_OK && ctx.developer_mode)
				return (ret);
		}
-
		ret = pkg_lua_script_run(pkg, PKG_LUA_PRE_DEINSTALL);
+
		ret = pkg_lua_script_run(pkg, PKG_LUA_PRE_DEINSTALL, false);
		if (ret != EPKG_OK && ctx.developer_mode)
			return (ret);
	}
@@ -105,8 +105,8 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags)
		return (ret);

	if ((flags & (PKG_DELETE_NOSCRIPT | PKG_DELETE_UPGRADE)) == 0) {
-
		pkg_script_run(pkg, PKG_SCRIPT_POST_DEINSTALL);
-
		pkg_lua_script_run(pkg, PKG_LUA_POST_DEINSTALL);
+
		pkg_script_run(pkg, PKG_SCRIPT_POST_DEINSTALL, false);
+
		pkg_lua_script_run(pkg, PKG_LUA_POST_DEINSTALL, false);
	}

	ret = pkg_delete_dirs(db, pkg, NULL);
modified libpkg/pkg_ports.c
@@ -1321,8 +1321,8 @@ pkg_add_port(struct pkgdb *db, struct pkg *pkg, const char *input_path,

	if (!testing) {
		/* Execute pre-install scripts */
-
		pkg_script_run(pkg, PKG_SCRIPT_PRE_INSTALL);
-
		pkg_lua_script_run(pkg, PKG_LUA_PRE_INSTALL);
+
		pkg_script_run(pkg, PKG_SCRIPT_PRE_INSTALL, false);
+
		pkg_lua_script_run(pkg, PKG_LUA_PRE_INSTALL, false);

		if (input_path != NULL) {
			pkg_register_cleanup_callback(pkg_rollback_cb, pkg);
@@ -1335,8 +1335,8 @@ pkg_add_port(struct pkgdb *db, struct pkg *pkg, const char *input_path,
		}

		/* Execute post-install scripts */
-
		pkg_script_run(pkg, PKG_SCRIPT_POST_INSTALL);
-
		pkg_lua_script_run(pkg, PKG_LUA_POST_INSTALL);
+
		pkg_script_run(pkg, PKG_SCRIPT_POST_INSTALL, false);
+
		pkg_lua_script_run(pkg, PKG_LUA_POST_INSTALL, false);
	}

	if (rc == EPKG_OK) {
modified libpkg/private/pkg.h
@@ -701,8 +701,8 @@ int pkg_repo_load_fingerprints(struct pkg_repo *repo);

int pkg_start_stop_rc_scripts(struct pkg *, pkg_rc_attr attr);

-
int pkg_script_run(struct pkg *, pkg_script type);
-
int pkg_lua_script_run(struct pkg *, pkg_lua_script type);
+
int pkg_script_run(struct pkg *, pkg_script type, bool upgrade);
+
int pkg_lua_script_run(struct pkg *, pkg_lua_script type, bool upgrade);
ucl_object_t *pkg_lua_script_to_ucl(struct pkg_lua_script *);

int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae,
modified libpkg/scripts.c
@@ -51,7 +51,7 @@
extern char **environ;

int
-
pkg_script_run(struct pkg * const pkg, pkg_script type)
+
pkg_script_run(struct pkg * const pkg, pkg_script type, bool upgrade)
{
	UT_string *script_cmd;
	size_t i, j;
@@ -116,6 +116,9 @@ pkg_script_run(struct pkg * const pkg, pkg_script type)
			continue;
		if (j == map[i].a || j == map[i].b) {
			utstring_clear(script_cmd);
+
			if (upgrade) {
+
				setenv("PKG_UPGRADE", "true", 1);
+
			}
			setenv("PKG_PREFIX", pkg->prefix, 1);
			if (ctx.pkg_rootdir == NULL)
				ctx.pkg_rootdir = "/";
modified tests/frontend/lua.sh
@@ -8,7 +8,8 @@ tests_init \
	script_rooteddir \
	script_remove \
	script_execute \
-
	script_rename
+
	script_rename \
+
	script_upgrade

script_basic_body() {
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
@@ -189,3 +190,69 @@ pkg: lua script failed\n"
		-s exit:0 \
		pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz
}
+

+
script_upgrade_body() {
+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
+
	cat << EOF >> test.ucl
+
lua_scripts: {
+
  post-install: [ <<EOS
+
if pkg_upgrade then
+
 pkg.print_msg("upgrade : ".. tostring(pkg_upgrade))
+
end
+
EOS
+
,
+
  ]
+
}
+
EOF
+

+

+
	atf_check \
+
		-o empty \
+
		-e empty \
+
		-s exit:0 \
+
		pkg create -M test.ucl
+
	mkdir -p ${TMPDIR}/target
+
	atf_check \
+
		-e empty \
+
		-o empty \
+
		-s exit:0 \
+
		pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz
+

+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "2"
+
	cat << EOF >> test.ucl
+
lua_scripts: {
+
  post-install: [ <<EOS
+
if pkg_upgrade then
+
 pkg.print_msg("upgrade:".. tostring(pkg_upgrade))
+
end
+
EOS
+
,
+
  ]
+
}
+
EOF
+

+
	rm ${TMPDIR}/test-1.txz
+
	atf_check \
+
		-o empty \
+
		-e empty \
+
		-s exit:0 \
+
		pkg create -M test.ucl
+
	mkdir -p ${TMPDIR}/target
+
	atf_check \
+
		-o ignore \
+
		-e empty \
+
		-s exit:0 \
+
		pkg repo .
+
	mkdir reposconf
+
	cat <<EOF >> reposconf/repo.conf
+
local: {
+
	url: file:///${TMPDIR},
+
	enabled: true
+
}
+
EOF
+
	atf_check \
+
		-e empty \
+
		-o match:"upgrade:true" \
+
		-s exit:0 \
+
		pkg -o REPOS_DIR="${TMPDIR}/reposconf" -r ${TMPDIR}/target upgrade -y
+
}
modified tests/frontend/shellscript.sh
@@ -4,7 +4,8 @@

tests_init \
	basic \
-
	message
+
	message \
+
	upgrade

basic_body() {
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
@@ -39,7 +40,7 @@ message_body() {
scripts: {
  post-install: <<EOS
	echo this is post install1
-
	echo this is a message >&\${PKG_MSGFD}
+
	echo this is a message >\&\${PKG_MSGFD}
	echo this is post install2
EOS
}
@@ -59,3 +60,72 @@ EOF
		pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz

}
+

+
upgrade_body() {
+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
+
	cat << EOF >> test.ucl
+
scripts: {
+
  post-install: <<EOS
+
if [ -n "\${PKG_UPGRADE+x}" ]; then
+
	echo "upgrade:\${PKG_UPGRADE}"
+
	echo "upgrade:\${PKG_UPGRADE}">&\${PKG_MSGFD}
+
fi
+
EOS
+
}
+
EOF
+

+

+
	atf_check \
+
		-o empty \
+
		-e empty \
+
		-s exit:0 \
+
		pkg create -M test.ucl
+
	mkdir -p ${TMPDIR}/target
+
	atf_check \
+
		-e empty \
+
		-o empty \
+
		-s exit:0 \
+
		pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz
+

+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "2"
+
	cat << EOF >> test.ucl
+
scripts: {
+
  post-install: <<EOS
+
if [ -n "\${PKG_UPGRADE+x}" ]; then
+
	echo "upgrade:\${PKG_UPGRADE}">\&\${PKG_MSGFD}
+
	echo this is a message >\&\${PKG_MSGFD}
+
fi
+
EOS
+
}
+
EOF
+

+
	rm ${TMPDIR}/test-1.txz
+
	atf_check \
+
		-o empty \
+
		-e empty \
+
		-s exit:0 \
+
		pkg create -M test.ucl
+
	mkdir -p ${TMPDIR}/target
+
	atf_check \
+
		-o ignore \
+
		-e empty \
+
		-s exit:0 \
+
		pkg info -R -F ./test-2.txz
+
	atf_check \
+
		-o ignore \
+
		-e empty \
+
		-s exit:0 \
+
		pkg repo .
+
	mkdir reposconf
+
	cat <<EOF >> reposconf/repo.conf
+
local: {
+
	url: file:///${TMPDIR},
+
	enabled: true
+
}
+
EOF
+
	atf_check \
+
		-e empty \
+
		-o match:"upgrade:1" \
+
		-s exit:0 \
+
		pkg -o REPOS_DIR="${TMPDIR}/reposconf" -r ${TMPDIR}/target upgrade -y
+
}