Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
pkg: fix -N options after WAL activation
Baptiste Daroussin committed 1 month ago
commit bd31a2d430be4899fb40d2dc46ad324da5092ce0
parent c52e14b
2 files changed +51 -1
modified libpkg/pkg_status.c
@@ -83,7 +83,23 @@ pkg_status(int *count)

	dbsuccess = (sqlite3_initialize() == SQLITE_OK);
	if (dbsuccess) {
-
		dbsuccess = (sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK);
+
		/*
+
		 * When we don't have write access, open with immutable=1
+
		 * to handle databases in WAL journal mode.  WAL requires
+
		 * -shm/-wal sidecar files which cannot be created without
+
		 * write access, causing queries to fail.
+
		 */
+
		if (faccessat(AT_FDCWD, dbpath, W_OK, AT_EACCESS) != 0) {
+
			char uripath[MAXPATHLEN];
+
			snprintf(uripath, sizeof(uripath),
+
			    "file:%s?immutable=1", dbpath);
+
			dbsuccess = (sqlite3_open_v2(uripath, &db,
+
			    SQLITE_OPEN_READONLY | SQLITE_OPEN_URI,
+
			    NULL) == SQLITE_OK);
+
		} else {
+
			dbsuccess = (sqlite3_open_v2(dbpath, &db,
+
			    SQLITE_OPEN_READONLY, NULL) == SQLITE_OK);
+
		}
		if (dbsuccess) {
			dbsuccess = (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK);
			if (dbsuccess) {
modified tests/frontend/pkg.sh
@@ -4,6 +4,8 @@

tests_init \
	pkg_no_database \
+
	pkg_activate \
+
	pkg_activate_wal_readonly \
	pkg_config_defaults \
	pkg_create_manifest_bad_syntax \
	pkg_repo_load_order \
@@ -20,6 +22,38 @@ pkg_no_database_body() {
	    env -i UBSAN_OPTIONS="${UBSAN_OPTIONS}" LSAN_OPTIONS="${LSAN_OPTIONS}" ASAN_OPTIONS="${ASAN_OPTIONS}" PATH="${PATH}" DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}" LD_LIBRARY_PATH="${LD_LIBRARY_PATH}" pkg -o PKG_DBDIR=/dev/null -N
}

+
pkg_activate_body() {
+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1.0" "/usr/local"
+
	atf_check -o ignore pkg register -M test.ucl
+

+
	atf_check \
+
		-e match:"1 packages installed" \
+
		-s exit:0 \
+
		pkg -N
+
}
+

+
pkg_activate_wal_readonly_body() {
+
	# Reproduce issue #2620: pkg -N fails for regular users when
+
	# the database is in WAL journal mode (set by root's pkg upgrade)
+
	# because SQLite needs to create -shm/-wal sidecar files.
+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1.0" "/usr/local"
+
	atf_check -o ignore pkg register -M test.ucl
+

+
	# After pkg register the DB is in WAL mode.
+
	# Remove sidecar files and make DB read-only to simulate
+
	# a regular user without write access.
+
	rm -f local.sqlite-shm local.sqlite-wal
+
	chmod 444 local.sqlite
+

+
	atf_check \
+
		-e match:"1 packages installed" \
+
		-s exit:0 \
+
		pkg -N
+

+
	# Restore for cleanup
+
	chmod 644 local.sqlite
+
}
+

pkg_config_defaults_body()
{
	atf_check \