2009-11-28 10:13:21 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* Copyright (c) 2009-2014 Juan Romero Pardines.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-11-28 10:13:21 +05:30
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
/*
|
2012-09-30 20:37:51 +05:30
|
|
|
* Returns true if entry is a configuration file, false otherwise.
|
2010-11-19 18:10:13 +05:30
|
|
|
*/
|
2011-01-20 07:25:40 +05:30
|
|
|
int HIDDEN
|
2013-12-25 13:10:45 +05:30
|
|
|
xbps_entry_is_a_conf_file(xbps_dictionary_t filesd,
|
2011-01-20 07:25:40 +05:30
|
|
|
const char *entry_pname)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t array;
|
2013-12-25 13:10:45 +05:30
|
|
|
xbps_dictionary_t d;
|
2012-09-30 20:37:51 +05:30
|
|
|
const char *cffile;
|
2010-11-19 18:10:13 +05:30
|
|
|
|
2013-12-25 13:10:45 +05:30
|
|
|
array = xbps_dictionary_get(filesd, "conf_files");
|
2013-06-20 13:56:12 +05:30
|
|
|
if (xbps_array_count(array) == 0)
|
2012-09-30 20:37:51 +05:30
|
|
|
return false;
|
2011-01-20 07:25:40 +05:30
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
|
2013-12-25 13:10:45 +05:30
|
|
|
d = xbps_array_get(array, i);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(d, "file", &cffile);
|
2012-09-30 20:37:51 +05:30
|
|
|
if (strcmp(cffile, entry_pname) == 0)
|
|
|
|
return true;
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
2012-09-30 20:37:51 +05:30
|
|
|
return false;
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
|
|
|
|
2011-01-20 07:25:40 +05:30
|
|
|
/*
|
|
|
|
* Returns 1 if entry should be installed, 0 if don't or -1 on error.
|
|
|
|
*/
|
2010-01-21 07:40:19 +05:30
|
|
|
int HIDDEN
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_entry_install_conf_file(struct xbps_handle *xhp,
|
2014-09-11 03:42:12 +05:30
|
|
|
xbps_dictionary_t binpkg_filesd,
|
|
|
|
xbps_dictionary_t pkg_filesd,
|
2011-01-20 07:25:40 +05:30
|
|
|
struct archive_entry *entry,
|
|
|
|
const char *entry_pname,
|
2019-03-19 17:42:08 +05:30
|
|
|
const char *pkgver,
|
2019-04-21 17:01:10 +05:30
|
|
|
bool mysymlink)
|
2009-11-28 10:13:21 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj, obj2;
|
|
|
|
xbps_object_iterator_t iter, iter2;
|
2014-07-30 19:48:57 +05:30
|
|
|
const char *version = NULL, *cffile, *sha256_new = NULL;
|
2020-02-10 17:41:47 +05:30
|
|
|
char buf[PATH_MAX], sha256_cur[XBPS_SHA256_SIZE];
|
|
|
|
const char *sha256_orig = NULL;
|
2009-11-28 10:13:21 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2014-09-11 03:42:12 +05:30
|
|
|
assert(xbps_object_type(binpkg_filesd) == XBPS_TYPE_DICTIONARY);
|
2014-09-06 00:28:20 +05:30
|
|
|
assert(entry);
|
|
|
|
assert(entry_pname);
|
|
|
|
assert(pkgver);
|
2009-11-28 10:13:21 +05:30
|
|
|
|
2014-09-11 03:42:12 +05:30
|
|
|
iter = xbps_array_iter_from_dict(binpkg_filesd, "conf_files");
|
2009-11-28 10:13:21 +05:30
|
|
|
if (iter == NULL)
|
2011-01-20 07:25:40 +05:30
|
|
|
return -1;
|
2009-11-28 10:13:21 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Get original hash for the file from current
|
|
|
|
* installed package.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: processing conf_file %s\n",
|
|
|
|
pkgver, entry_pname);
|
2011-01-16 21:25:01 +05:30
|
|
|
|
2019-04-21 17:01:10 +05:30
|
|
|
if (pkg_filesd == NULL || mysymlink) {
|
2014-07-30 19:48:57 +05:30
|
|
|
/*
|
2019-03-19 17:42:08 +05:30
|
|
|
* 1. File exists on disk but it's not managed by the same package.
|
|
|
|
* 2. File exists on disk as symlink.
|
2014-07-30 19:48:57 +05:30
|
|
|
* Install it as file.new-<version>.
|
|
|
|
*/
|
|
|
|
version = xbps_pkg_version(pkgver);
|
|
|
|
assert(version);
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: conf_file %s not currently "
|
2016-02-06 13:43:38 +05:30
|
|
|
"installed, renaming to %s.new-%s\n", pkgver,
|
|
|
|
entry_pname, entry_pname, version);
|
2014-07-30 19:48:57 +05:30
|
|
|
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);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-09-11 03:42:12 +05:30
|
|
|
iter2 = xbps_array_iter_from_dict(pkg_filesd, "conf_files");
|
2009-11-28 10:13:21 +05:30
|
|
|
if (iter2 != NULL) {
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj2 = xbps_object_iterator_next(iter2))) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj2,
|
2010-11-06 11:14:00 +05:30
|
|
|
"file", &cffile);
|
2014-05-22 14:22:57 +05:30
|
|
|
snprintf(buf, sizeof(buf), ".%s", cffile);
|
2011-01-20 07:25:40 +05:30
|
|
|
if (strcmp(entry_pname, buf) == 0) {
|
2020-02-10 17:41:47 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj2, "sha256", &sha256_orig);
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter2);
|
2009-11-28 10:13:21 +05:30
|
|
|
}
|
|
|
|
/*
|
|
|
|
* First case: original hash not found, install new file.
|
|
|
|
*/
|
|
|
|
if (sha256_orig == NULL) {
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: conf_file %s not installed\n",
|
|
|
|
pkgver, entry_pname);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare original, installed and new hash for current file.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter))) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "file", &cffile);
|
2014-05-22 14:22:57 +05:30
|
|
|
snprintf(buf, sizeof(buf), ".%s", cffile);
|
2011-01-20 07:25:40 +05:30
|
|
|
if (strcmp(entry_pname, buf)) {
|
2009-11-28 10:13:21 +05:30
|
|
|
continue;
|
|
|
|
}
|
2020-02-10 06:24:52 +05:30
|
|
|
if (!xbps_file_sha256(sha256_cur, sizeof sha256_cur, buf)) {
|
2009-11-28 10:13:21 +05:30
|
|
|
if (errno == ENOENT) {
|
|
|
|
/*
|
|
|
|
* File not installed, install new one.
|
|
|
|
*/
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: conf_file %s not "
|
|
|
|
"installed\n", pkgver, entry_pname);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
} else {
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = -1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 06:24:52 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new);
|
2009-11-28 10:13:21 +05:30
|
|
|
/*
|
|
|
|
* Orig = X, Curr = X, New = X
|
|
|
|
*
|
2012-10-26 13:53:12 +05:30
|
|
|
* Keep file as is (no changes).
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
|
|
|
if ((strcmp(sha256_orig, sha256_cur) == 0) &&
|
|
|
|
(strcmp(sha256_orig, sha256_new) == 0) &&
|
|
|
|
(strcmp(sha256_cur, sha256_new) == 0)) {
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: conf_file %s orig = X, "
|
|
|
|
"cur = X, new = X\n", pkgver, entry_pname);
|
2012-10-26 13:53:12 +05:30
|
|
|
rv = 0;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = X, New = Y
|
|
|
|
*
|
2020-01-25 03:39:12 +05:30
|
|
|
* Install new file (installed file hasn't been modified) if
|
|
|
|
* configuration option keepconfig is NOT set.
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
|
|
|
} else if ((strcmp(sha256_orig, sha256_cur) == 0) &&
|
|
|
|
(strcmp(sha256_orig, sha256_new)) &&
|
2020-01-25 03:39:12 +05:30
|
|
|
(strcmp(sha256_cur, sha256_new)) &&
|
|
|
|
(!(xhp->flags & XBPS_FLAG_KEEP_CONFIG))) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
|
2013-03-05 08:38:42 +05:30
|
|
|
0, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"Updating configuration file `%s' provided "
|
2013-03-05 08:38:42 +05:30
|
|
|
"by `%s'.", cffile, pkgver);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = Y, New = X
|
2012-10-26 13:53:12 +05:30
|
|
|
*
|
|
|
|
* Keep installed file as is because it has been modified,
|
|
|
|
* but new package doesn't contain new changes compared
|
|
|
|
* to the original version.
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
|
|
|
} else if ((strcmp(sha256_orig, sha256_new) == 0) &&
|
|
|
|
(strcmp(sha256_cur, sha256_new)) &&
|
|
|
|
(strcmp(sha256_orig, sha256_cur))) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
|
2013-03-05 08:38:42 +05:30
|
|
|
0, pkgver,
|
2011-11-24 15:53:08 +05:30
|
|
|
"Keeping modified configuration file `%s'.",
|
|
|
|
cffile);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = 0;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = Y, New = Y
|
2012-10-26 13:53:12 +05:30
|
|
|
*
|
|
|
|
* Keep file as is because changes made are compatible
|
|
|
|
* with new version.
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
|
|
|
} else if ((strcmp(sha256_cur, sha256_new) == 0) &&
|
|
|
|
(strcmp(sha256_orig, sha256_new)) &&
|
|
|
|
(strcmp(sha256_orig, sha256_cur))) {
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: conf_file %s orig = X, "
|
|
|
|
"cur = Y, new = Y\n", pkgver, entry_pname);
|
2011-11-24 15:53:08 +05:30
|
|
|
rv = 0;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = Y, New = Z
|
2020-01-25 03:39:12 +05:30
|
|
|
* or
|
|
|
|
* Orig = X, Curr = X, New = Y if keepconf is set
|
2012-10-26 13:53:12 +05:30
|
|
|
*
|
2011-11-24 15:53:08 +05:30
|
|
|
* Install new file as <file>.new-<version>
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
2020-01-25 03:39:12 +05:30
|
|
|
} else if (((strcmp(sha256_orig, sha256_cur)) &&
|
2009-11-28 10:13:21 +05:30
|
|
|
(strcmp(sha256_cur, sha256_new)) &&
|
2020-01-25 03:39:12 +05:30
|
|
|
(strcmp(sha256_orig, sha256_new))) ||
|
|
|
|
(xhp->flags & XBPS_FLAG_KEEP_CONFIG)) {
|
2013-03-05 08:38:42 +05:30
|
|
|
version = xbps_pkg_version(pkgver);
|
|
|
|
assert(version);
|
2014-05-22 14:22:57 +05:30
|
|
|
snprintf(buf, sizeof(buf), ".%s.new-%s", cffile, version);
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_set_cb_state(xhp, XBPS_STATE_CONFIG_FILE,
|
2014-07-30 19:48:57 +05:30
|
|
|
0, pkgver, "File `%s' exists, installing configuration file to `%s'.", cffile, buf);
|
2014-03-04 14:14:58 +05:30
|
|
|
archive_entry_copy_pathname(entry, buf);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(iter);
|
2009-11-28 10:13:21 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
xbps_dbg_printf(xhp, "%s: conf_file %s returned %d\n",
|
|
|
|
pkgver, entry_pname, rv);
|
2011-01-16 21:25:01 +05:30
|
|
|
|
2009-11-28 10:13:21 +05:30
|
|
|
return rv;
|
|
|
|
}
|