2009-11-28 10:13:21 +05:30
|
|
|
/*-
|
2011-01-20 07:25:40 +05:30
|
|
|
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
2009-11-28 10:13:21 +05:30
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
/*
|
|
|
|
* Returns 1 if entry is a configuration file, 0 if don't or -1 on error.
|
|
|
|
*/
|
2011-01-20 07:25:40 +05:30
|
|
|
int HIDDEN
|
|
|
|
xbps_entry_is_a_conf_file(prop_dictionary_t propsd,
|
|
|
|
const char *entry_pname)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
2011-01-16 21:25:01 +05:30
|
|
|
char *cffile;
|
2010-11-19 18:10:13 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2011-10-19 20:23:38 +05:30
|
|
|
assert(prop_object_type(propsd) == PROP_TYPE_DICTIONARY);
|
2011-01-20 07:25:40 +05:30
|
|
|
assert(entry_pname != NULL);
|
|
|
|
|
|
|
|
if (!prop_dictionary_get(propsd, "conf_files"))
|
|
|
|
return 0;
|
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
iter = xbps_array_iter_from_dict(propsd, "conf_files");
|
2010-11-19 18:10:13 +05:30
|
|
|
if (iter == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
|
|
|
cffile = xbps_xasprintf(".%s",
|
|
|
|
prop_string_cstring_nocopy(obj));
|
|
|
|
if (cffile == NULL) {
|
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-01-20 07:25:40 +05:30
|
|
|
if (strcmp(cffile, entry_pname) == 0) {
|
2010-11-19 18:10:13 +05:30
|
|
|
rv = 1;
|
|
|
|
free(cffile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(cffile);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
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
|
2011-01-20 07:25:40 +05:30
|
|
|
xbps_entry_install_conf_file(prop_dictionary_t filesd,
|
|
|
|
struct archive_entry *entry,
|
|
|
|
const char *entry_pname,
|
|
|
|
const char *pkgname,
|
|
|
|
const char *version)
|
2009-11-28 10:13:21 +05:30
|
|
|
{
|
|
|
|
prop_dictionary_t forigd;
|
|
|
|
prop_object_t obj, obj2;
|
|
|
|
prop_object_iterator_t iter, iter2;
|
2011-01-20 07:25:40 +05:30
|
|
|
const char *cffile, *sha256_new = NULL;
|
2009-11-28 10:13:21 +05:30
|
|
|
char *buf, *sha256_cur = NULL, *sha256_orig = NULL;
|
|
|
|
int rv = 0;
|
|
|
|
|
2011-10-19 20:23:38 +05:30
|
|
|
assert(prop_object_type(filesd) == PROP_TYPE_DICTIONARY);
|
2011-01-20 07:25:40 +05:30
|
|
|
assert(entry != NULL);
|
2011-01-25 08:44:33 +05:30
|
|
|
assert(entry_pname != NULL);
|
2011-01-20 07:25:40 +05:30
|
|
|
assert(pkgname != NULL);
|
2011-01-25 08:44:33 +05:30
|
|
|
assert(version != NULL);
|
2009-11-28 10:13:21 +05:30
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
iter = xbps_array_iter_from_dict(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.
|
|
|
|
*/
|
2011-01-16 21:25:01 +05:30
|
|
|
xbps_dbg_printf("%s: processing conf_file %s\n",
|
2011-01-20 07:25:40 +05:30
|
|
|
pkgname, entry_pname);
|
2011-01-16 21:25:01 +05:30
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
forigd = xbps_dictionary_from_metadata_plist(pkgname, XBPS_PKGFILES);
|
2009-11-28 10:13:21 +05:30
|
|
|
if (forigd == NULL) {
|
2011-01-16 21:25:01 +05:30
|
|
|
xbps_dbg_printf("%s: conf_file %s not currently installed\n",
|
2011-01-20 07:25:40 +05:30
|
|
|
pkgname, entry_pname);
|
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-06-01 13:07:32 +05:30
|
|
|
iter2 = xbps_array_iter_from_dict(forigd, "conf_files");
|
2009-11-28 10:13:21 +05:30
|
|
|
if (iter2 != NULL) {
|
|
|
|
while ((obj2 = prop_object_iterator_next(iter2))) {
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj2,
|
|
|
|
"file", &cffile);
|
2009-11-28 10:13:21 +05:30
|
|
|
buf = xbps_xasprintf(".%s", cffile);
|
|
|
|
if (buf == NULL) {
|
|
|
|
prop_object_iterator_release(iter2);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = -1;
|
2009-11-28 10:13:21 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2011-01-20 07:25:40 +05:30
|
|
|
if (strcmp(entry_pname, buf) == 0) {
|
2009-11-28 10:13:21 +05:30
|
|
|
prop_dictionary_get_cstring(obj2, "sha256",
|
|
|
|
&sha256_orig);
|
|
|
|
free(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
buf = NULL;
|
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter2);
|
|
|
|
}
|
|
|
|
prop_object_release(forigd);
|
|
|
|
/*
|
|
|
|
* First case: original hash not found, install new file.
|
|
|
|
*/
|
|
|
|
if (sha256_orig == NULL) {
|
2011-01-16 21:25:01 +05:30
|
|
|
xbps_dbg_printf("%s: conf_file %s unknown orig sha256\n",
|
2011-01-20 07:25:40 +05:30
|
|
|
pkgname, entry_pname);
|
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare original, installed and new hash for current file.
|
|
|
|
*/
|
|
|
|
while ((obj = prop_object_iterator_next(iter))) {
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "file", &cffile);
|
2009-11-28 10:13:21 +05:30
|
|
|
buf = xbps_xasprintf(".%s", cffile);
|
|
|
|
if (buf == NULL) {
|
|
|
|
prop_object_iterator_release(iter);
|
2011-01-20 07:25:40 +05:30
|
|
|
return -1;
|
2009-11-28 10:13:21 +05:30
|
|
|
}
|
2011-01-20 07:25:40 +05:30
|
|
|
if (strcmp(entry_pname, buf)) {
|
2009-11-28 10:13:21 +05:30
|
|
|
free(buf);
|
|
|
|
buf = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-01 13:07:32 +05:30
|
|
|
sha256_cur = xbps_file_hash(buf);
|
2009-11-28 10:13:21 +05:30
|
|
|
free(buf);
|
2010-11-06 11:14:00 +05:30
|
|
|
prop_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new);
|
2009-11-28 10:13:21 +05:30
|
|
|
if (sha256_cur == NULL) {
|
|
|
|
if (errno == ENOENT) {
|
|
|
|
/*
|
|
|
|
* File not installed, install new one.
|
|
|
|
*/
|
2011-01-16 21:25:01 +05:30
|
|
|
xbps_dbg_printf("%s: conf_file %s not "
|
2011-01-20 07:25:40 +05:30
|
|
|
"installed\n", pkgname, entry_pname);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = X, New = X
|
|
|
|
*
|
|
|
|
* Install new file.
|
|
|
|
*/
|
|
|
|
if ((strcmp(sha256_orig, sha256_cur) == 0) &&
|
|
|
|
(strcmp(sha256_orig, sha256_new) == 0) &&
|
|
|
|
(strcmp(sha256_cur, sha256_new) == 0)) {
|
2011-01-16 21:25:01 +05:30
|
|
|
xbps_dbg_printf("%s: conf_file %s orig = X,"
|
2011-01-20 07:25:40 +05:30
|
|
|
"cur = X, new = X\n", pkgname, entry_pname);
|
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = X, New = Y
|
|
|
|
*
|
|
|
|
* Install new file.
|
|
|
|
*/
|
|
|
|
} else if ((strcmp(sha256_orig, sha256_cur) == 0) &&
|
|
|
|
(strcmp(sha256_orig, sha256_new)) &&
|
|
|
|
(strcmp(sha256_cur, sha256_new))) {
|
2011-01-22 17:10:19 +05:30
|
|
|
xbps_printf("Updating configuration file `%s' "
|
2011-01-20 07:25:40 +05:30
|
|
|
"with new version.\n", cffile);
|
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = Y, New = X
|
|
|
|
*
|
|
|
|
* Keep current file as is.
|
|
|
|
*/
|
|
|
|
} else if ((strcmp(sha256_orig, sha256_new) == 0) &&
|
|
|
|
(strcmp(sha256_cur, sha256_new)) &&
|
|
|
|
(strcmp(sha256_orig, sha256_cur))) {
|
2011-01-22 17:10:19 +05:30
|
|
|
xbps_printf("Keeping modified configuration file "
|
2011-01-20 07:25:40 +05:30
|
|
|
"`%s'.\n", cffile);
|
|
|
|
rv = 0;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = Y, New = Y
|
|
|
|
*
|
|
|
|
* Install new file.
|
|
|
|
*/
|
|
|
|
} else if ((strcmp(sha256_cur, sha256_new) == 0) &&
|
|
|
|
(strcmp(sha256_orig, sha256_new)) &&
|
|
|
|
(strcmp(sha256_orig, sha256_cur))) {
|
2011-01-16 21:25:01 +05:30
|
|
|
xbps_dbg_printf("%s: conf_file %s orig = X,"
|
2011-01-20 07:25:40 +05:30
|
|
|
"cur = Y, new = Y\n", pkgname, entry_pname);
|
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Orig = X, Curr = Y, New = Z
|
|
|
|
*
|
2011-01-20 07:25:40 +05:30
|
|
|
* Install new file as file.new-<pkg_version>
|
2009-11-28 10:13:21 +05:30
|
|
|
*/
|
|
|
|
} else if ((strcmp(sha256_orig, sha256_cur)) &&
|
|
|
|
(strcmp(sha256_cur, sha256_new)) &&
|
|
|
|
(strcmp(sha256_orig, sha256_new))) {
|
2011-01-20 07:25:40 +05:30
|
|
|
buf = xbps_xasprintf(".%s.new-%s",
|
|
|
|
cffile, version);
|
2009-11-28 10:13:21 +05:30
|
|
|
if (buf == NULL) {
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = -1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
}
|
2011-01-22 17:10:19 +05:30
|
|
|
xbps_printf("Keeping modified configuration file "
|
2011-01-20 07:25:40 +05:30
|
|
|
"`%s'.\n", cffile);
|
2011-01-22 17:10:19 +05:30
|
|
|
xbps_printf("Installing new configuration file as "
|
2011-01-20 07:25:40 +05:30
|
|
|
"`%s.new-%s'\n", cffile, version);
|
2009-11-28 10:13:21 +05:30
|
|
|
archive_entry_set_pathname(entry, buf);
|
|
|
|
free(buf);
|
2011-01-20 07:25:40 +05:30
|
|
|
rv = 1;
|
2009-11-28 10:13:21 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (sha256_orig)
|
|
|
|
free(sha256_orig);
|
|
|
|
if (sha256_cur)
|
|
|
|
free(sha256_cur);
|
|
|
|
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
2011-01-16 21:25:01 +05:30
|
|
|
xbps_dbg_printf("%s: conf_file %s returned %d\n",
|
2011-01-20 07:25:40 +05:30
|
|
|
pkgname, entry_pname, rv);
|
2011-01-16 21:25:01 +05:30
|
|
|
|
2009-11-28 10:13:21 +05:30
|
|
|
return rv;
|
|
|
|
}
|