libxbps: simplify how configuration files are handled, use cached values if possible.
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2009-2010 Juan Romero Pardines.
|
||||
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -24,11 +24,9 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <xbps_api.h>
|
||||
#include "xbps_api_impl.h"
|
||||
@ -36,14 +34,21 @@
|
||||
/*
|
||||
* Returns 1 if entry is a configuration file, 0 if don't or -1 on error.
|
||||
*/
|
||||
static int
|
||||
entry_is_conf_file(prop_dictionary_t propsd, struct archive_entry *entry)
|
||||
int HIDDEN
|
||||
xbps_entry_is_a_conf_file(prop_dictionary_t propsd,
|
||||
const char *entry_pname)
|
||||
{
|
||||
prop_object_t obj;
|
||||
prop_object_iterator_t iter;
|
||||
char *cffile;
|
||||
int rv = 0;
|
||||
|
||||
assert(propsd != NULL);
|
||||
assert(entry_pname != NULL);
|
||||
|
||||
if (!prop_dictionary_get(propsd, "conf_files"))
|
||||
return 0;
|
||||
|
||||
iter = xbps_get_array_iter_from_dict(propsd, "conf_files");
|
||||
if (iter == NULL)
|
||||
return -1;
|
||||
@ -55,7 +60,7 @@ entry_is_conf_file(prop_dictionary_t propsd, struct archive_entry *entry)
|
||||
rv = -1;
|
||||
goto out;
|
||||
}
|
||||
if (strcmp(cffile, archive_entry_pathname(entry)) == 0) {
|
||||
if (strcmp(cffile, entry_pname) == 0) {
|
||||
rv = 1;
|
||||
free(cffile);
|
||||
break;
|
||||
@ -68,47 +73,43 @@ out:
|
||||
return rv;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns 1 if entry should be installed, 0 if don't or -1 on error.
|
||||
*/
|
||||
int HIDDEN
|
||||
xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
prop_dictionary_t propsd,
|
||||
struct archive_entry *entry,
|
||||
int *flags,
|
||||
bool *skip)
|
||||
xbps_entry_install_conf_file(prop_dictionary_t filesd,
|
||||
struct archive_entry *entry,
|
||||
const char *entry_pname,
|
||||
const char *pkgname,
|
||||
const char *version)
|
||||
{
|
||||
prop_dictionary_t forigd;
|
||||
prop_object_t obj, obj2;
|
||||
prop_object_iterator_t iter, iter2;
|
||||
const char *pkgname, *cffile, *sha256_new = NULL;
|
||||
const char *cffile, *sha256_new = NULL;
|
||||
char *buf, *sha256_cur = NULL, *sha256_orig = NULL;
|
||||
int rv = 0;
|
||||
bool install_new = false;
|
||||
|
||||
/*
|
||||
* Check that current entry is really a configuration file.
|
||||
*/
|
||||
rv = entry_is_conf_file(propsd, entry);
|
||||
if (rv == -1 || rv == 0)
|
||||
return rv;
|
||||
assert(filesd != NULL);
|
||||
assert(entry != NULL);
|
||||
assert(pkgname != NULL);
|
||||
|
||||
rv = 0;
|
||||
iter = xbps_get_array_iter_from_dict(filesd, "conf_files");
|
||||
if (iter == NULL)
|
||||
return EINVAL;
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* Get original hash for the file from current
|
||||
* installed package.
|
||||
*/
|
||||
prop_dictionary_get_cstring_nocopy(propsd, "pkgname", &pkgname);
|
||||
|
||||
xbps_dbg_printf("%s: processing conf_file %s\n",
|
||||
pkgname, archive_entry_pathname(entry));
|
||||
pkgname, entry_pname);
|
||||
|
||||
forigd = xbps_get_pkg_dict_from_metadata_plist(pkgname, XBPS_PKGFILES);
|
||||
if (forigd == NULL) {
|
||||
xbps_dbg_printf("%s: conf_file %s not currently installed\n",
|
||||
pkgname, archive_entry_pathname(entry));
|
||||
install_new = true;
|
||||
pkgname, entry_pname);
|
||||
rv = 1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -120,10 +121,10 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
buf = xbps_xasprintf(".%s", cffile);
|
||||
if (buf == NULL) {
|
||||
prop_object_iterator_release(iter2);
|
||||
rv = ENOMEM;
|
||||
rv = -1;
|
||||
goto out;
|
||||
}
|
||||
if (strcmp(archive_entry_pathname(entry), buf) == 0) {
|
||||
if (strcmp(entry_pname, buf) == 0) {
|
||||
prop_dictionary_get_cstring(obj2, "sha256",
|
||||
&sha256_orig);
|
||||
free(buf);
|
||||
@ -139,9 +140,9 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
* First case: original hash not found, install new file.
|
||||
*/
|
||||
if (sha256_orig == NULL) {
|
||||
install_new = true;
|
||||
xbps_dbg_printf("%s: conf_file %s unknown orig sha256\n",
|
||||
pkgname, archive_entry_pathname(entry));
|
||||
pkgname, entry_pname);
|
||||
rv = 1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
@ -153,9 +154,9 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
buf = xbps_xasprintf(".%s", cffile);
|
||||
if (buf == NULL) {
|
||||
prop_object_iterator_release(iter);
|
||||
return ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
if (strcmp(archive_entry_pathname(entry), buf)) {
|
||||
if (strcmp(entry_pname, buf)) {
|
||||
free(buf);
|
||||
buf = NULL;
|
||||
continue;
|
||||
@ -168,13 +169,12 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
/*
|
||||
* File not installed, install new one.
|
||||
*/
|
||||
install_new = true;
|
||||
xbps_dbg_printf("%s: conf_file %s not "
|
||||
"installed\n", pkgname,
|
||||
archive_entry_pathname(entry));
|
||||
"installed\n", pkgname, entry_pname);
|
||||
rv = 1;
|
||||
break;
|
||||
} else {
|
||||
rv = errno;
|
||||
rv = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -187,10 +187,9 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
if ((strcmp(sha256_orig, sha256_cur) == 0) &&
|
||||
(strcmp(sha256_orig, sha256_new) == 0) &&
|
||||
(strcmp(sha256_cur, sha256_new) == 0)) {
|
||||
install_new = true;
|
||||
xbps_dbg_printf("%s: conf_file %s orig = X,"
|
||||
"cur = X, new = X\n", pkgname,
|
||||
archive_entry_pathname(entry));
|
||||
"cur = X, new = X\n", pkgname, entry_pname);
|
||||
rv = 1;
|
||||
break;
|
||||
/*
|
||||
* Orig = X, Curr = X, New = Y
|
||||
@ -200,9 +199,9 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
} else if ((strcmp(sha256_orig, sha256_cur) == 0) &&
|
||||
(strcmp(sha256_orig, sha256_new)) &&
|
||||
(strcmp(sha256_cur, sha256_new))) {
|
||||
printf("Updating %s file with new version.\n",
|
||||
cffile);
|
||||
install_new = true;
|
||||
printf("Updating configuration file `%s' "
|
||||
"with new version.\n", cffile);
|
||||
rv = 1;
|
||||
break;
|
||||
/*
|
||||
* Orig = X, Curr = Y, New = X
|
||||
@ -212,8 +211,9 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
} else if ((strcmp(sha256_orig, sha256_new) == 0) &&
|
||||
(strcmp(sha256_cur, sha256_new)) &&
|
||||
(strcmp(sha256_orig, sha256_cur))) {
|
||||
printf("Keeping modified file %s.\n", cffile);
|
||||
*skip = true;
|
||||
printf("Keeping modified configuration file "
|
||||
"`%s'.\n", cffile);
|
||||
rv = 0;
|
||||
break;
|
||||
/*
|
||||
* Orig = X, Curr = Y, New = Y
|
||||
@ -223,38 +223,36 @@ xbps_config_file_from_archive_entry(prop_dictionary_t filesd,
|
||||
} else if ((strcmp(sha256_cur, sha256_new) == 0) &&
|
||||
(strcmp(sha256_orig, sha256_new)) &&
|
||||
(strcmp(sha256_orig, sha256_cur))) {
|
||||
install_new = true;
|
||||
xbps_dbg_printf("%s: conf_file %s orig = X,"
|
||||
"cur = Y, new = Y\n", pkgname,
|
||||
archive_entry_pathname(entry));
|
||||
"cur = Y, new = Y\n", pkgname, entry_pname);
|
||||
rv = 1;
|
||||
break;
|
||||
/*
|
||||
* Orig = X, Curr = Y, New = Z
|
||||
*
|
||||
* Install new file as file.new.
|
||||
* Install new file as file.new-<pkg_version>
|
||||
*/
|
||||
} else if ((strcmp(sha256_orig, sha256_cur)) &&
|
||||
(strcmp(sha256_cur, sha256_new)) &&
|
||||
(strcmp(sha256_orig, sha256_new))) {
|
||||
buf = xbps_xasprintf(".%s.new", cffile);
|
||||
buf = xbps_xasprintf(".%s.new-%s",
|
||||
cffile, version);
|
||||
if (buf == NULL) {
|
||||
rv = ENOMEM;
|
||||
rv = -1;
|
||||
break;
|
||||
}
|
||||
printf("Keeping modified file %s.\n", cffile);
|
||||
printf("Installing new version as %s.new.\n", cffile);
|
||||
install_new = true;
|
||||
printf("Keeping modified configuration file "
|
||||
"`%s'.\n", cffile);
|
||||
printf("Installing new configuration file as "
|
||||
"`%s.new-%s'\n", cffile, version);
|
||||
archive_entry_set_pathname(entry, buf);
|
||||
free(buf);
|
||||
rv = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
if (install_new) {
|
||||
*flags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE;
|
||||
*flags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER;
|
||||
}
|
||||
if (sha256_orig)
|
||||
free(sha256_orig);
|
||||
if (sha256_cur)
|
||||
@ -263,7 +261,7 @@ out:
|
||||
prop_object_iterator_release(iter);
|
||||
|
||||
xbps_dbg_printf("%s: conf_file %s returned %d\n",
|
||||
pkgname, archive_entry_pathname(entry));
|
||||
pkgname, entry_pname, rv);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
Reference in New Issue
Block a user