lib: add binary package internalization step

- internalize scripts so we can use them before unpacking packages.
- moves some required metadata checks out of the package unpack stage
  so errors do not leave the system in a half unpacked state.

xbps_transaction_files will be changed later to use the loaded
files.plist instead of opening each binary package on its own again.
This commit is contained in:
Duncan Overbruck
2021-06-24 17:08:53 +02:00
parent c00fe9f3e1
commit ab3fb82a7f
5 changed files with 252 additions and 79 deletions

View File

@@ -73,25 +73,22 @@ unpack_archive(struct xbps_handle *xhp,
const char *fname,
struct archive *ar)
{
xbps_dictionary_t binpkg_propsd, binpkg_filesd, pkg_filesd, obsd;
xbps_dictionary_t binpkg_filesd, pkg_filesd, obsd;
xbps_array_t array, obsoletes;
xbps_data_t data;
xbps_trans_type_t ttype;
const struct stat *entry_statp;
void *instbuf = NULL, *rembuf = NULL;
struct stat st;
struct xbps_unpack_cb_data xucd;
struct archive_entry *entry;
size_t instbufsiz = 0, rembufsiz = 0;
ssize_t entry_size;
const char *entry_pname, *binpkg_pkgver, *pkgname;
const char *entry_pname, *pkgname;
char *buf = NULL;
int ar_rv, rv, error, entry_type, flags;
bool preserve, update, file_exists, keep_conf_file;
bool skip_extract, force, xucd_stats;
uid_t euid;
binpkg_propsd = binpkg_filesd = pkg_filesd = NULL;
binpkg_filesd = pkg_filesd = NULL;
force = preserve = update = file_exists = false;
xucd_stats = false;
ar_rv = rv = error = entry_type = flags = 0;
@@ -161,45 +158,20 @@ unpack_archive(struct xbps_handle *xhp,
entry_pname = archive_entry_pathname(entry);
entry_size = archive_entry_size(entry);
if (strcmp("./INSTALL", entry_pname) == 0) {
/*
* Store file in a buffer to execute it later.
*/
instbufsiz = entry_size;
instbuf = malloc(entry_size);
assert(instbuf);
if (archive_read_data(ar, instbuf, entry_size) != entry_size) {
rv = EINVAL;
goto out;
}
} else if (strcmp("./REMOVE", entry_pname) == 0) {
/*
* Store file in a buffer to execute it later.
*/
rembufsiz = entry_size;
rembuf = malloc(entry_size);
assert(rembuf);
if (archive_read_data(ar, rembuf, entry_size) != entry_size) {
rv = EINVAL;
goto out;
}
} else if (strcmp("./props.plist", entry_pname) == 0) {
binpkg_propsd = xbps_archive_get_dictionary(ar, entry);
if (binpkg_propsd == NULL) {
rv = EINVAL;
goto out;
}
if (strcmp("./INSTALL", entry_pname) == 0 ||
strcmp("./REMOVE", entry_pname) == 0 ||
strcmp("./props.plist", entry_pname) == 0) {
archive_read_data_skip(ar);
} else if (strcmp("./files.plist", entry_pname) == 0) {
binpkg_filesd = xbps_archive_get_dictionary(ar, entry);
if (binpkg_filesd == NULL) {
rv = EINVAL;
goto out;
}
} else {
archive_read_data_skip(ar);
}
if (binpkg_filesd)
break;
} else {
break;
}
}
/*
* If there was any error extracting files from archive, error out.
@@ -214,53 +186,27 @@ unpack_archive(struct xbps_handle *xhp,
/*
* Bail out if required metadata files are not in archive.
*/
if (binpkg_propsd == NULL || binpkg_filesd == NULL) {
if (binpkg_filesd == NULL) {
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, ENODEV, pkgver,
"%s: [unpack] invalid binary package `%s'.", pkgver, fname);
rv = ENODEV;
goto out;
}
/*
* Check that the pkgver in the binpkg matches the one we're looking for.
*/
xbps_dictionary_get_cstring_nocopy(binpkg_propsd, "pkgver", &binpkg_pkgver);
if (strcmp(pkgver, binpkg_pkgver) != 0) {
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, EINVAL, pkgver,
"%s: [unpack] pkgver mismatch repodata: `%s' binpkg: `%s'.", fname, pkgver, binpkg_pkgver);
rv = EINVAL;
goto out;
}
/*
* Internalize current pkg metadata files plist.
*/
pkg_filesd = xbps_pkgdb_get_pkg_files(xhp, pkgname);
/* Add pkg install/remove scripts data objects into our dictionary */
if (instbuf != NULL) {
data = xbps_data_create_data(instbuf, instbufsiz);
assert(data);
xbps_dictionary_set(pkg_repod, "install-script", data);
xbps_object_release(data);
}
if (rembuf != NULL) {
data = xbps_data_create_data(rembuf, rembufsiz);
assert(data);
xbps_dictionary_set(pkg_repod, "remove-script", data);
xbps_object_release(data);
}
/*
* Execute INSTALL "pre" ACTION before unpacking files.
*/
if (instbuf != NULL) {
rv = xbps_pkg_exec_buffer(xhp, instbuf, instbufsiz, pkgver, "pre", update);
if (rv != 0) {
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver,
"%s: [unpack] INSTALL script failed to execute pre ACTION: %s",
pkgver, strerror(rv));
goto out;
}
rv = xbps_pkg_exec_script(xhp, pkg_repod, "install-script", "pre", update);
if (rv != 0) {
xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver,
"%s: [unpack] INSTALL script failed to execute pre ACTION: %s",
pkgver, strerror(rv));
goto out;
}
/*
* Unpack all files on archive now.
@@ -523,14 +469,7 @@ out:
unlink(buf);
free(buf);
}
if (xbps_object_type(binpkg_propsd) == XBPS_TYPE_DICTIONARY)
xbps_object_release(binpkg_propsd);
if (xbps_object_type(binpkg_filesd) == XBPS_TYPE_DICTIONARY)
xbps_object_release(binpkg_filesd);
if (instbuf != NULL)
free(instbuf);
if (rembuf != NULL)
free(rembuf);
xbps_object_release(binpkg_filesd);
return rv;
}