Fix #50 (Existent files on disk might be overwritten by packages with conf_files).

This commit is contained in:
Juan RP 2014-07-30 16:18:57 +02:00
parent e92e86c2db
commit cc6f9901f1
4 changed files with 54 additions and 20 deletions

4
NEWS
View File

@ -1,5 +1,9 @@
xbps-0.38 (???): xbps-0.38 (???):
* Always preserve files that exist on disk, but are managed by a package.
When such condition ocurrs install the pkg configuration file as `file.new-<version>`.
This fixes GH issue #50 https://github.com/voidlinux/xbps/issues/50.
* Implemented support for post-install and pre-remove messages, as explained * Implemented support for post-install and pre-remove messages, as explained
in issue #44 https://github.com/voidlinux/xbps/issues/44. in issue #44 https://github.com/voidlinux/xbps/issues/44.

View File

@ -68,7 +68,7 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
xbps_dictionary_t forigd; xbps_dictionary_t forigd;
xbps_object_t obj, obj2; xbps_object_t obj, obj2;
xbps_object_iterator_t iter, iter2; xbps_object_iterator_t iter, iter2;
const char *cffile, *sha256_new = NULL; const char *version = NULL, *cffile, *sha256_new = NULL;
char buf[PATH_MAX], *sha256_cur = NULL, *sha256_orig = NULL; char buf[PATH_MAX], *sha256_cur = NULL, *sha256_orig = NULL;
int rv = 0; int rv = 0;
@ -90,8 +90,18 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
forigd = xbps_pkgdb_get_pkg_metadata(xhp, pkgname); forigd = xbps_pkgdb_get_pkg_metadata(xhp, pkgname);
if (forigd == NULL) { if (forigd == NULL) {
/*
* File exists on disk but it's not managed by the same package.
* Install it as file.new-<version>.
*/
version = xbps_pkg_version(pkgver);
assert(version);
xbps_dbg_printf(xhp, "%s: conf_file %s not currently " xbps_dbg_printf(xhp, "%s: conf_file %s not currently "
"installed\n", pkgver, entry_pname); "installed, renaming to %s.new-%s\n", pkgver, entry_pname, version);
snprintf(buf, sizeof(buf), "%s.new-%s", entry_pname, version);
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
0, pkgver, "File `%s' exists, installing configuration file to `%s'.", entry_pname, buf);
archive_entry_copy_pathname(entry, buf);
rv = 1; rv = 1;
goto out; goto out;
} }
@ -207,13 +217,11 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
} else if ((strcmp(sha256_orig, sha256_cur)) && } else if ((strcmp(sha256_orig, sha256_cur)) &&
(strcmp(sha256_cur, sha256_new)) && (strcmp(sha256_cur, sha256_new)) &&
(strcmp(sha256_orig, sha256_new))) { (strcmp(sha256_orig, sha256_new))) {
const char *version;
version = xbps_pkg_version(pkgver); version = xbps_pkg_version(pkgver);
assert(version); assert(version);
snprintf(buf, sizeof(buf), ".%s.new-%s", cffile, version); snprintf(buf, sizeof(buf), ".%s.new-%s", cffile, version);
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE, xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
0, pkgver, "Installing new configuration file to `%s'.", buf); 0, pkgver, "File `%s' exists, installing configuration file to `%s'.", cffile, buf);
archive_entry_copy_pathname(entry, buf); archive_entry_copy_pathname(entry, buf);
rv = 1; rv = 1;
break; break;

View File

@ -410,8 +410,8 @@ unpack_archive(struct xbps_handle *xhp,
"matches existing SHA256, " "matches existing SHA256, "
"skipping...\n", "skipping...\n",
pkgver, entry_pname); pkgver, entry_pname);
skip_extract = true;
} }
skip_extract = true;
rv = 0; rv = 0;
} }
} }
@ -497,20 +497,6 @@ unpack_archive(struct xbps_handle *xhp,
"mode to %s.\n", pkgver, entry_pname, "mode to %s.\n", pkgver, entry_pname,
archive_entry_strmode(entry)); archive_entry_strmode(entry));
} }
if (!update && conf_file && file_exists && !skip_extract) {
/*
* If installing new package preserve old configuration
* file but renaming it to <file>.old.
*/
buf = xbps_xasprintf("%s.old", entry_pname);
(void)rename(entry_pname, buf);
free(buf);
buf = NULL;
xbps_set_cb_state(xhp,
XBPS_STATE_CONFIG_FILE, 0, pkgver,
"Renamed old configuration file "
"`%s' to `%s.old'.", entry_pname, entry_pname);
}
if (!force && skip_extract) { if (!force && skip_extract) {
archive_read_data_skip(ar); archive_read_data_skip(ar);
continue; continue;

View File

@ -128,8 +128,44 @@ tc3_body() {
atf_check_equal $rval 0 atf_check_equal $rval 0
} }
# 4th test: existent file on disk; new package defines configuration file.
# result: keep on-disk file as is, install new conf file as file.new-<version>.
atf_test_case tc4
tc4_head() {
atf_set "descr" "Tests for configuration file handling: existent on-disk, new install"
}
tc4_body() {
mkdir repo
cd repo
mkdir -p pkg_a/etc
echo "fooblah" > pkg_a/etc/cf1.conf
chmod 755 pkg_a/etc/cf1.conf
xbps-create -A noarch -n a-0.1_1 -s "pkg a" --config-files "/etc/cf1.conf" pkg_a
atf_check_equal $? 0
rm -rf pkg_a
xbps-rindex -a *.xbps
atf_check_equal $? 0
mkdir -p rootdir/etc
echo "blahblah" > rootdir/etc/cf1.conf
xbps-install -C null.conf -r rootdir --repository=$PWD -yvd a
atf_check_equal $? 0
result="$(cat rootdir/etc/cf1.conf)"
resultpkg="$(cat rootdir/etc/cf1.conf.new-0.1_1)"
rval=1
if [ "${result}" = "blahblah" -a "${resultpkg}" = "fooblah" ]; then
rval=0
fi
atf_check_equal $rval 0
}
atf_init_test_cases() { atf_init_test_cases() {
atf_add_test_case tc1 atf_add_test_case tc1
atf_add_test_case tc2 atf_add_test_case tc2
atf_add_test_case tc3 atf_add_test_case tc3
atf_add_test_case tc4
} }