ATF_TC_BODY(find_all_orphans_test, tc) { struct xbps_handle xh; xbps_array_t res; xbps_dictionary_t pkgd; xbps_string_t pstr; const char *pkgver, *tcsdir; unsigned int i; /* get test source dir */ tcsdir = atf_tc_get_config_var(tc, "srcdir"); memset(&xh, 0, sizeof(xh)); xbps_strlcpy(xh.rootdir, tcsdir, sizeof(xh.rootdir)); xbps_strlcpy(xh.metadir, tcsdir, sizeof(xh.metadir)); ATF_REQUIRE_EQ(xbps_init(&xh), 0); pstr = xbps_string_create(); res = xbps_find_pkg_orphans(&xh, NULL); for (i = 0; i < xbps_array_count(res); i++) { pkgd = xbps_array_get(res, i); xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver); xbps_string_append_cstring(pstr, pkgver); xbps_string_append_cstring(pstr, "\n"); } printf("%s", xbps_string_cstring_nocopy(pstr)); ATF_REQUIRE_STREQ(xbps_string_cstring_nocopy(pstr), expected_output_all); }
static int remove_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname) { unsigned int i, cnt; cnt = xbps_array_count(a); for (i = 0; i < cnt; i++) { xbps_string_t str; char *l, *lnk; str = xbps_array_get(a, i); l = left(xbps_string_cstring_nocopy(str)); assert(l); if (l[0] != '/') { const char *tgt; char *tgt_dup, *tgt_dir; tgt = right(xbps_string_cstring_nocopy(str)); tgt_dup = strdup(tgt); assert(tgt_dup); tgt_dir = dirname(tgt_dup); lnk = xbps_xasprintf("%s%s/%s", xhp->rootdir, tgt_dir, l); free(tgt_dup); } else { lnk = xbps_xasprintf("%s%s", xhp->rootdir, l); } xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_REMOVED, 0, NULL, "Removing '%s' alternatives group symlink: %s", grname, l); unlink(lnk); free(lnk); free(l); } return 0; }
ATF_TC_BODY(pkgdb_get_pkg_revdeps_test, tc) { struct xbps_handle xh; xbps_array_t res; xbps_string_t pstr; const char *tcsdir, *str; const char *eout = "four-0.1_1\ntwo-0.1_1\n"; unsigned int i; /* get test source dir */ tcsdir = atf_tc_get_config_var(tc, "srcdir"); memset(&xh, 0, sizeof(xh)); strncpy(xh.rootdir, tcsdir, sizeof(xh.rootdir)); strncpy(xh.metadir, tcsdir, sizeof(xh.metadir)); xh.flags = XBPS_FLAG_DEBUG; ATF_REQUIRE_EQ(xbps_init(&xh), 0); res = xbps_pkgdb_get_pkg_revdeps(&xh, "mixed"); ATF_REQUIRE_EQ(xbps_object_type(res), XBPS_TYPE_ARRAY); pstr = xbps_string_create(); for (i = 0; i < xbps_array_count(res); i++) { xbps_array_get_cstring_nocopy(res, i, &str); xbps_string_append_cstring(pstr, str); xbps_string_append_cstring(pstr, "\n"); } ATF_REQUIRE_STREQ(xbps_string_cstring_nocopy(pstr), eout); }
int HIDDEN xbps_transaction_package_replace(struct xbps_handle *xhp, xbps_array_t pkgs) { for (unsigned int i = 0; i < xbps_array_count(pkgs); i++) { xbps_array_t replaces; xbps_object_t obj, obj2; xbps_object_iterator_t iter; const char *pkgver; char *pkgname; obj = xbps_array_get(pkgs, i); replaces = xbps_dictionary_get(obj, "replaces"); if (replaces == NULL || xbps_array_count(replaces) == 0) continue; iter = xbps_array_iterator(replaces); assert(iter); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); pkgname = xbps_pkg_name(pkgver); assert(pkgname); while ((obj2 = xbps_object_iterator_next(iter)) != NULL) { xbps_dictionary_t instd, reppkgd; const char *tract, *pattern, *curpkgver; char *curpkgname; bool instd_auto = false; pattern = xbps_string_cstring_nocopy(obj2); /* * Find the installed package that matches the pattern * to be replaced. */ if (((instd = xbps_pkgdb_get_pkg(xhp, pattern)) == NULL) && ((instd = xbps_pkgdb_get_virtualpkg(xhp, pattern)) == NULL)) continue; xbps_dictionary_get_cstring_nocopy(instd, "pkgver", &curpkgver); curpkgname = xbps_pkg_name(curpkgver); assert(curpkgname); /* * Check that we are not replacing the same package, * due to virtual packages. */ if (strcmp(pkgname, curpkgname) == 0) { free(curpkgname); continue; } /* * Make sure to not add duplicates. */ xbps_dictionary_get_bool(instd, "automatic-install", &instd_auto); reppkgd = xbps_find_pkg_in_array(pkgs, curpkgname, NULL); if (reppkgd) { xbps_dictionary_get_cstring_nocopy(reppkgd, "transaction", &tract); if (strcmp(tract, "remove") == 0) continue; /* * Package contains replaces="pkgpattern", but the * package that should be replaced is also in the * transaction and it's going to be updated. */ xbps_dictionary_set_bool(reppkgd, "automatic-install", instd_auto); xbps_array_replace_dict_by_name(pkgs, reppkgd, curpkgname); continue; } /* * If new package is providing a virtual package to the * package that we want to replace we should respect * the automatic-install object. */ if (xbps_match_virtual_pkg_in_dict(obj, pattern)) { xbps_dictionary_set_bool(obj, "automatic-install", instd_auto); } xbps_dbg_printf(xhp, "Package `%s' will be replaced by `%s', " "matched with `%s'\n", curpkgver, pkgver, pattern); /* * Add package dictionary into the transaction and mark * it as to be "removed". */ xbps_dictionary_set_cstring_nocopy(instd, "transaction", "remove"); if (!xbps_array_add_first(pkgs, instd)) return EINVAL; free(curpkgname); } xbps_object_iterator_release(iter); free(pkgname); } return 0; }
static int create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname) { unsigned int i, cnt; cnt = xbps_array_count(a); for (i = 0; i < cnt; i++) { xbps_string_t str; char *tgt_dup, *tgt_dir, *lnk_dup, *lnk_dir; char *l, *lnk, *tgt = NULL; const char *tgt0; int rv; str = xbps_array_get(a, i); l = left(xbps_string_cstring_nocopy(str)); assert(l); tgt0 = right(xbps_string_cstring_nocopy(str)); assert(tgt0); /* always create target dir, for dangling symlinks */ tgt_dup = strdup(tgt0); assert(tgt_dup); tgt_dir = dirname(tgt_dup); if (strcmp(tgt_dir, ".")) { tgt = xbps_xasprintf("%s%s", xhp->rootdir, tgt_dir); if (xbps_mkpath(tgt, 0755) != 0) { if (errno != EEXIST) { rv = errno; xbps_dbg_printf(xhp, "failed to create " "target dir '%s' for group '%s': %s\n", tgt, grname, strerror(errno)); free(tgt_dup); free(tgt); free(l); return rv; } } free(tgt); } /* always create link dir, for dangling symlinks */ lnk_dup = strdup(l); assert(lnk_dup); lnk_dir = dirname(lnk_dup); if (strcmp(lnk_dir, ".")) { lnk = xbps_xasprintf("%s%s", xhp->rootdir, lnk_dir); if (xbps_mkpath(lnk, 0755) != 0) { if (errno != EEXIST) { rv = errno; xbps_dbg_printf(xhp, "failed to create symlink" "dir '%s' for group '%s': %s\n", lnk, grname, strerror(errno)); free(tgt_dup); free(lnk_dup); free(lnk); free(l); return rv; } } free(lnk); } free(lnk_dup); if (l[0] != '/') { lnk = xbps_xasprintf("%s%s/%s", xhp->rootdir, tgt_dir, l); free(tgt_dup); tgt_dup = strdup(tgt0); assert(tgt_dup); tgt = strdup(basename(tgt_dup)); free(tgt_dup); } else { free(tgt_dup); tgt = strdup(tgt0); lnk = xbps_xasprintf("%s%s", xhp->rootdir, l); } xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_LINK_ADDED, 0, NULL, "Creating '%s' alternatives group symlink: %s -> %s", grname, l, tgt); unlink(lnk); if (tgt[0] == '/') { tgt_dup = relpath(lnk + strlen(xhp->rootdir), tgt); free(tgt); tgt = tgt_dup; } if ((rv = symlink(tgt, lnk)) != 0) { xbps_dbg_printf(xhp, "failed to create alt symlink '%s'" "for group '%s': %s\n", lnk, grname, strerror(errno)); free(tgt); free(lnk); free(l); return rv; } free(tgt); free(lnk); free(l); } return 0; }
xbps_array_t xbps_find_pkg_obsoletes(struct xbps_handle *xhp, xbps_dictionary_t instd, xbps_dictionary_t newd) { xbps_array_t instfiles, newfiles, obsoletes; xbps_object_t obj, obj2; xbps_string_t oldstr, newstr; unsigned int i, x; const char *oldhash; char *file; int rv = 0; bool found; assert(xbps_object_type(instd) == XBPS_TYPE_DICTIONARY); assert(xbps_object_type(newd) == XBPS_TYPE_DICTIONARY); obsoletes = xbps_array_create(); assert(obsoletes); instfiles = merge_filelist(instd); if (xbps_array_count(instfiles) == 0) { /* nothing to check if current pkg does not own any file */ xbps_object_release(instfiles); return obsoletes; } newfiles = merge_filelist(newd); /* * Iterate over files list from installed package. */ for (i = 0; i < xbps_array_count(instfiles); i++) { found = false; obj = xbps_array_get(instfiles, i); if (xbps_object_type(obj) != XBPS_TYPE_DICTIONARY) { /* ignore unexistent files */ continue; } oldstr = xbps_dictionary_get(obj, "file"); if (oldstr == NULL) continue; file = xbps_xasprintf(".%s", xbps_string_cstring_nocopy(oldstr)); oldhash = NULL; xbps_dictionary_get_cstring_nocopy(obj, "sha256", &oldhash); if (oldhash) { rv = xbps_file_hash_check(file, oldhash); if (rv == ENOENT || rv == ERANGE) { /* * Skip unexistent and files that do not * match the hash. */ free(file); continue; } } /* * Check if current file is available in new pkg filelist. */ for (x = 0; x < xbps_array_count(newfiles); x++) { obj2 = xbps_array_get(newfiles, x); newstr = xbps_dictionary_get(obj2, "file"); assert(newstr); /* * Skip files with same path. */ if (xbps_string_equals(oldstr, newstr)) { found = true; break; } } if (found) { free(file); continue; } /* * Do not add required symlinks for the * system transition to /usr. */ if ((strcmp(file, "./bin") == 0) || (strcmp(file, "./bin/") == 0) || (strcmp(file, "./sbin") == 0) || (strcmp(file, "./sbin/") == 0) || (strcmp(file, "./lib") == 0) || (strcmp(file, "./lib/") == 0) || (strcmp(file, "./lib64/") == 0) || (strcmp(file, "./lib64") == 0)) { free(file); continue; } /* * Obsolete found, add onto the array. */ xbps_dbg_printf(xhp, "found obsolete: %s\n", file); xbps_array_add_cstring(obsoletes, file); free(file); } xbps_object_release(instfiles); xbps_object_release(newfiles); return obsoletes; }
int HIDDEN xbps_transaction_package_replace(struct xbps_handle *xhp) { xbps_array_t replaces, unsorted; xbps_dictionary_t instd, reppkgd, filesd; xbps_object_t obj, obj2; xbps_object_iterator_t iter; const char *pattern, *pkgver, *curpkgver; char *buf, *pkgname, *curpkgname; bool instd_auto, sr; unsigned int i; unsorted = xbps_dictionary_get(xhp->transd, "unsorted_deps"); for (i = 0; i < xbps_array_count(unsorted); i++) { obj = xbps_array_get(unsorted, i); replaces = xbps_dictionary_get(obj, "replaces"); if (replaces == NULL || xbps_array_count(replaces) == 0) continue; iter = xbps_array_iterator(replaces); assert(iter); while ((obj2 = xbps_object_iterator_next(iter)) != NULL) { pattern = xbps_string_cstring_nocopy(obj2); /* * Find the installed package that matches the pattern * to be replaced. */ if (((instd = xbps_pkgdb_get_pkg(xhp, pattern)) == NULL) && ((instd = xbps_pkgdb_get_virtualpkg(xhp, pattern)) == NULL)) continue; xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(instd, "pkgver", &curpkgver); pkgname = xbps_pkg_name(pkgver); assert(pkgname); curpkgname = xbps_pkg_name(curpkgver); assert(curpkgver); /* * Check that we are not replacing the same package, * due to virtual packages. */ if (strcmp(pkgname, curpkgname) == 0) { free(pkgname); free(curpkgname); continue; } xbps_dbg_printf(xhp, "Package `%s' will be replaced by `%s', " "matched with `%s'\n", curpkgver, pkgver, pattern); instd_auto = false; xbps_dictionary_get_bool(instd, "automatic-install", &instd_auto); /* * Package contains replaces="pkgpattern", but the * package that should be replaced is also in the * transaction and it's going to be updated. */ if ((reppkgd = xbps_find_pkg_in_array(unsorted, curpkgname))) { xbps_dbg_printf(xhp, "found replaced pkg " "in transaction\n"); xbps_dictionary_set_bool(instd, "remove-and-update", true); xbps_dictionary_set_bool(reppkgd, "automatic-install", instd_auto); xbps_dictionary_set_bool(reppkgd, "skip-obsoletes", true); xbps_array_replace_dict_by_name(unsorted, reppkgd, curpkgname); } /* * If new package is providing a virtual package to the * package that we want to replace we should respect * the automatic-install object. */ if (xbps_match_virtual_pkg_in_dict(obj, pattern, true) || xbps_match_virtual_pkg_in_dict(instd, pkgname, false)) { xbps_dictionary_set_bool(obj, "automatic-install", instd_auto); } sr = false; xbps_dictionary_get_bool(obj, "softreplace", &sr); if (sr) { xbps_dictionary_set_bool(obj, "automatic-install", instd_auto); xbps_dictionary_set_bool(instd, "softreplace", true); buf = xbps_xasprintf("%s/.%s.plist", xhp->metadir, curpkgname); filesd = xbps_dictionary_internalize_from_file(buf); free(buf); assert(filesd != NULL); buf = xbps_xasprintf("%s/.%s.plist", xhp->metadir, pkgname); if (!xbps_dictionary_externalize_to_file(filesd, buf)) { free(buf); xbps_object_release(filesd); xbps_object_iterator_release(iter); free(pkgname); free(curpkgname); return errno; } xbps_object_release(filesd); free(buf); } /* * Add package dictionary into the transaction and mark * it as to be "removed". */ xbps_dictionary_set_cstring_nocopy(instd, "transaction", "remove"); xbps_array_add(unsorted, instd); free(pkgname); free(curpkgname); } xbps_object_iterator_release(iter); } return 0; }
static int unpack_archive(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod, const char *pkgver, const char *fname, struct archive *ar) { xbps_dictionary_t propsd, filesd, old_filesd; xbps_array_t array, obsoletes; xbps_object_t obj; void *instbuf = NULL, *rembuf = NULL; struct stat st; struct xbps_unpack_cb_data xucd; struct archive_entry *entry; size_t i, entry_idx = 0, instbufsiz = 0, rembufsiz = 0; ssize_t entry_size; const char *file, *entry_pname, *transact, *tgtlnk; char *pkgname, *dname, *buf, *buf2, *p, *p2; int ar_rv, rv, entry_type, flags; bool preserve, update, conf_file, file_exists, skip_obsoletes; bool softreplace, skip_extract, force, metafile; uid_t euid; assert(xbps_object_type(pkg_repod) == XBPS_TYPE_DICTIONARY); assert(ar != NULL); propsd = filesd = old_filesd = NULL; force = preserve = update = conf_file = file_exists = false; skip_obsoletes = softreplace = metafile = false; xbps_dictionary_get_bool(pkg_repod, "preserve", &preserve); xbps_dictionary_get_bool(pkg_repod, "skip-obsoletes", &skip_obsoletes); xbps_dictionary_get_bool(pkg_repod, "softreplace", &softreplace); xbps_dictionary_get_cstring_nocopy(pkg_repod, "transaction", &transact); euid = geteuid(); pkgname = xbps_pkg_name(pkgver); assert(pkgname); if (xhp->flags & XBPS_FLAG_FORCE_UNPACK) force = true; if (xhp->unpack_cb != NULL) { /* initialize data for unpack cb */ memset(&xucd, 0, sizeof(xucd)); } if (access(xhp->rootdir, R_OK) == -1) { if (errno != ENOENT) { rv = errno; goto out; } if (xbps_mkpath(xhp->rootdir, 0750) == -1) { rv = errno; goto out; } } if (chdir(xhp->rootdir) == -1) { xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, errno, pkgver, "%s: [unpack] failed to chdir to rootdir `%s': %s", pkgver, xhp->rootdir, strerror(errno)); rv = errno; goto out; } if (strcmp(transact, "update") == 0) update = true; /* * Process the archive files. */ flags = set_extract_flags(euid); for (;;) { ar_rv = archive_read_next_header(ar, &entry); if (ar_rv == ARCHIVE_EOF || ar_rv == ARCHIVE_FATAL) break; else if (ar_rv == ARCHIVE_RETRY) continue; entry_pname = archive_entry_pathname(entry); entry_size = archive_entry_size(entry); entry_type = archive_entry_filetype(entry); /* * Ignore directories from archive. */ if (entry_type == AE_IFDIR) { archive_read_data_skip(ar); continue; } if (strcmp("./INSTALL", entry_pname) == 0) { /* * Store file in a buffer and execute * the "pre" action from it. */ instbufsiz = entry_size; instbuf = malloc(entry_size); assert(instbuf); if (archive_read_data(ar, instbuf, entry_size) != entry_size) { rv = EINVAL; goto out; } 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; } continue; } else if (strcmp("./REMOVE", entry_pname) == 0) { /* store file in a buffer */ rembufsiz = entry_size; rembuf = malloc(entry_size); assert(rembuf); if (archive_read_data(ar, rembuf, entry_size) != entry_size) { rv = EINVAL; goto out; } continue; } else if (strcmp("./files.plist", entry_pname) == 0) { filesd = xbps_archive_get_dictionary(ar, entry); if (filesd == NULL) { rv = errno; goto out; } continue; } else if (strcmp("./props.plist", entry_pname) == 0) { propsd = xbps_archive_get_dictionary(ar, entry); if (propsd == NULL) { rv = errno; goto out; } continue; } /* * XXX: duplicate code. * Create the metaplist file before unpacking any real file. */ if (propsd && filesd && !metafile) { rv = create_pkg_metaplist(xhp, pkgname, pkgver, propsd, filesd, instbuf, instbufsiz, rembuf, rembufsiz); if (rv != 0) { xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver, "%s: [unpack] failed to create metaplist file: %s", pkgver, strerror(rv)); goto out; } metafile = true; } /* * If XBPS_PKGFILES or XBPS_PKGPROPS weren't found * in the archive at this phase, skip all data. */ if (propsd == NULL || filesd == NULL) { archive_read_data_skip(ar); /* * If we have processed 4 entries and the two * required metadata files weren't found, bail out. * This is not an XBPS binary package. */ if (entry_idx >= 3) { xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, ENODEV, pkgver, "%s: [unpack] invalid binary package `%s'.", pkgver, fname); rv = ENODEV; goto out; } entry_idx++; continue; } /* * Prepare unpack callback ops. */ if (xhp->unpack_cb != NULL) { xucd.xhp = xhp; xucd.pkgver = pkgver; xucd.entry = entry_pname; xucd.entry_size = entry_size; xucd.entry_is_conf = false; } /* * Compute total entries in progress data, if set. * total_entries = files + conf_files + links. */ if (xhp->unpack_cb != NULL) { xucd.entry_total_count = 0; array = xbps_dictionary_get(filesd, "files"); xucd.entry_total_count += (ssize_t)xbps_array_count(array); array = xbps_dictionary_get(filesd, "conf_files"); xucd.entry_total_count += (ssize_t)xbps_array_count(array); array = xbps_dictionary_get(filesd, "links"); xucd.entry_total_count += (ssize_t)xbps_array_count(array); } /* * Always check that extracted file exists and hash * doesn't match, in that case overwrite the file. * Otherwise skip extracting it. */ conf_file = skip_extract = file_exists = false; if (lstat(entry_pname, &st) == 0) file_exists = true; if (!force && (entry_type == AE_IFREG)) { buf = strchr(entry_pname, '.') + 1; assert(buf != NULL); if (file_exists) { /* * Handle configuration files. Check if current * entry is a configuration file and take action * if required. Skip packages that don't have * "conf_files" array on its XBPS_PKGPROPS * dictionary. */ if (xbps_entry_is_a_conf_file(propsd, buf)) { conf_file = true; if (xhp->unpack_cb != NULL) xucd.entry_is_conf = true; rv = xbps_entry_install_conf_file(xhp, filesd, entry, entry_pname, pkgver, pkgname); if (rv == -1) { /* error */ goto out; } else if (rv == 0) { /* * Keep curfile as is. */ skip_extract = true; } } else { rv = xbps_file_hash_check_dictionary( xhp, filesd, "files", buf); if (rv == -1) { /* error */ xbps_dbg_printf(xhp, "%s: failed to check" " hash for `%s': %s\n", pkgver, entry_pname, strerror(errno)); goto out; } else if (rv == 0) { /* * hash match, skip extraction. */ xbps_dbg_printf(xhp, "%s: file %s " "matches existing SHA256, " "skipping...\n", pkgver, entry_pname); skip_extract = true; } } } } else if (!force && (entry_type == AE_IFLNK)) { /* * Check if current link from binpkg hasn't been * modified, otherwise extract new link. */ buf = realpath(entry_pname, NULL); if (buf) { if (strcmp(xhp->rootdir, "/")) { p = buf; p += strlen(xhp->rootdir); } else p = buf; tgtlnk = find_pkg_symlink_target(filesd, entry_pname); assert(tgtlnk); if (strncmp(tgtlnk, "./", 2) == 0) { buf2 = strdup(entry_pname); assert(buf2); dname = dirname(buf2); p2 = xbps_xasprintf("%s/%s", dname, tgtlnk); free(buf2); } else { p2 = strdup(tgtlnk); assert(p2); } xbps_dbg_printf(xhp, "%s: symlink %s cur: %s " "new: %s\n", pkgver, entry_pname, p, p2); if (strcmp(p, p2) == 0) { xbps_dbg_printf(xhp, "%s: symlink " "%s matched, skipping...\n", pkgver, entry_pname); skip_extract = true; } free(buf); free(p2); } } /* * Check if current file mode differs from file mode * in binpkg and apply perms if true. */ if (!force && file_exists && skip_extract && (archive_entry_mode(entry) != st.st_mode)) { if (chmod(entry_pname, archive_entry_mode(entry)) != 0) { xbps_dbg_printf(xhp, "%s: failed " "to set perms %s to %s: %s\n", pkgver, archive_entry_strmode(entry), entry_pname, strerror(errno)); rv = EINVAL; goto out; } xbps_dbg_printf(xhp, "%s: entry %s changed file " "mode to %s.\n", pkgver, entry_pname, archive_entry_strmode(entry)); } /* * Check if current uid/gid differs from file in binpkg, * and change permissions if true. */ if ((!force && file_exists && skip_extract && (euid == 0)) && (((archive_entry_uid(entry) != st.st_uid)) || ((archive_entry_gid(entry) != st.st_gid)))) { if (lchown(entry_pname, archive_entry_uid(entry), archive_entry_gid(entry)) != 0) { xbps_dbg_printf(xhp, "%s: failed " "to set uid/gid to %u:%u (%s)\n", pkgver, archive_entry_uid(entry), archive_entry_gid(entry), strerror(errno)); } else { xbps_dbg_printf(xhp, "%s: entry %s changed " "uid/gid to %u:%u.\n", pkgver, entry_pname, archive_entry_uid(entry), archive_entry_gid(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) { archive_read_data_skip(ar); continue; } /* * Reset entry_pname again because if entry's pathname * has been changed it will become a dangling pointer. */ entry_pname = archive_entry_pathname(entry); /* * Extract entry from archive. */ if (archive_read_extract(ar, entry, flags) != 0) { rv = archive_errno(ar); xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver, "%s: [unpack] failed to extract file `%s': %s", pkgver, entry_pname, strerror(rv)); } else { if (xhp->unpack_cb != NULL) { xucd.entry_extract_count++; (*xhp->unpack_cb)(&xucd, xhp->unpack_cb_data); } } } /* * XXX: duplicate code. * Create the metaplist file if it wasn't created before. */ if (propsd && filesd && !metafile) { rv = create_pkg_metaplist(xhp, pkgname, pkgver, propsd, filesd, instbuf, instbufsiz, rembuf, rembufsiz); if (rv != 0) { xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver, "%s: [unpack] failed to create metaplist file: %s", pkgver, strerror(rv)); goto out; } } /* * If there was any error extracting files from archive, error out. */ if ((rv = archive_errno(ar)) != 0) { xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver, NULL, "%s: [unpack] failed to extract files: %s", pkgver, fname, archive_error_string(ar)); goto out; } /* * Skip checking for obsolete files on: * - New package installation without "softreplace" keyword. * - Package with "preserve" keyword. * - Package with "skip-obsoletes" keyword. */ if (skip_obsoletes || preserve || (!softreplace && !update)) goto out; /* * Check and remove obsolete files on: * - Package upgrade. * - Package with "softreplace" keyword. */ old_filesd = xbps_pkgdb_get_pkg_metadata(xhp, pkgname); if (old_filesd == NULL) goto out; obsoletes = xbps_find_pkg_obsoletes(xhp, old_filesd, filesd); for (i = 0; i < xbps_array_count(obsoletes); i++) { obj = xbps_array_get(obsoletes, i); file = xbps_string_cstring_nocopy(obj); if (remove(file) == -1) { xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE_OBSOLETE_FAIL, errno, pkgver, "%s: failed to remove obsolete entry `%s': %s", pkgver, file, strerror(errno)); continue; } xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FILE_OBSOLETE, 0, pkgver, "%s: removed obsolete entry: %s", pkgver, file); xbps_object_release(obj); } xbps_object_release(old_filesd); out: if (xbps_object_type(filesd) == XBPS_TYPE_DICTIONARY) xbps_object_release(filesd); if (xbps_object_type(propsd) == XBPS_TYPE_DICTIONARY) xbps_object_release(propsd); if (pkgname != NULL) free(pkgname); if (instbuf != NULL) free(instbuf); if (rembuf != NULL) free(rembuf); return rv; }
static int add_missing_reqdep(struct xbps_handle *xhp, const char *reqpkg) { xbps_array_t mdeps; xbps_object_iterator_t iter = NULL; xbps_object_t obj; unsigned int idx = 0; bool add_pkgdep, pkgfound, update_pkgdep; int rv = 0; assert(reqpkg != NULL); add_pkgdep = update_pkgdep = pkgfound = false; mdeps = xbps_dictionary_get(xhp->transd, "missing_deps"); iter = xbps_array_iterator(mdeps); if (iter == NULL) goto out; while ((obj = xbps_object_iterator_next(iter)) != NULL) { const char *curdep, *curver, *pkgver; char *curpkgnamedep = NULL, *pkgnamedep = NULL; assert(xbps_object_type(obj) == XBPS_TYPE_STRING); curdep = xbps_string_cstring_nocopy(obj); curver = xbps_pkgpattern_version(curdep); pkgver = xbps_pkgpattern_version(reqpkg); if (curver == NULL || pkgver == NULL) goto out; curpkgnamedep = xbps_pkgpattern_name(curdep); if (curpkgnamedep == NULL) goto out; pkgnamedep = xbps_pkgpattern_name(reqpkg); if (pkgnamedep == NULL) { free(curpkgnamedep); goto out; } if (strcmp(pkgnamedep, curpkgnamedep) == 0) { pkgfound = true; if (strcmp(curver, pkgver) == 0) { free(curpkgnamedep); free(pkgnamedep); rv = EEXIST; goto out; } /* * if new dependency version is greater than current * one, store it. */ xbps_dbg_printf(xhp, "Missing pkgdep name matched, curver: %s newver: %s\n", curver, pkgver); if (xbps_cmpver(curver, pkgver) <= 0) { add_pkgdep = false; free(curpkgnamedep); free(pkgnamedep); rv = EEXIST; goto out; } update_pkgdep = true; } free(curpkgnamedep); free(pkgnamedep); if (pkgfound) break; idx++; } add_pkgdep = true; out: if (iter) xbps_object_iterator_release(iter); if (update_pkgdep) xbps_array_remove(mdeps, idx); if (add_pkgdep) { char *str; str = xbps_xasprintf("MISSING: %s", reqpkg); xbps_array_add_cstring(mdeps, str); free(str); } return rv; }
static int find_repo_deps(struct xbps_handle *xhp, xbps_array_t unsorted, /* array of unsorted deps */ xbps_array_t pkg_rdeps_array, /* current pkg rundeps array */ xbps_array_t pkg_provides, /* current pkg provides array */ const char *curpkg, /* current pkgver */ unsigned short *depth) /* max recursion depth */ { xbps_dictionary_t curpkgd = NULL; xbps_object_t obj; xbps_object_iterator_t iter; xbps_array_t curpkgrdeps = NULL, curpkgprovides = NULL; pkg_state_t state; const char *reqpkg, *pkgver_q, *reason = NULL; char *pkgname, *reqpkgname; int rv = 0; bool foundvpkg; if (*depth >= MAX_DEPTH) return ELOOP; /* * Iterate over the list of required run dependencies for * current package. */ iter = xbps_array_iterator(pkg_rdeps_array); assert(iter); while ((obj = xbps_object_iterator_next(iter))) { foundvpkg = false; reqpkg = xbps_string_cstring_nocopy(obj); if (xhp->flags & XBPS_FLAG_DEBUG) { xbps_dbg_printf(xhp, "%s", ""); for (unsigned short x = 0; x < *depth; x++) { xbps_dbg_printf_append(xhp, " "); } xbps_dbg_printf_append(xhp, "%s: requires dependency '%s': ", curpkg ? curpkg : " ", reqpkg); } if (((pkgname = xbps_pkgpattern_name(reqpkg)) == NULL) && ((pkgname = xbps_pkg_name(reqpkg)) == NULL)) { xbps_dbg_printf(xhp, "%s: can't guess pkgname for dependency: %s\n", curpkg, reqpkg); xbps_set_cb_state(xhp, XBPS_STATE_INVALID_DEP, ENXIO, NULL, "%s: can't guess pkgname for dependency '%s'", curpkg, reqpkg); rv = ENXIO; break; } /* * Pass 1: check if required dependency is provided as virtual * package via "provides", if true ignore dependency. */ if (pkg_provides && xbps_match_virtual_pkg_in_array(pkg_provides, reqpkg)) { xbps_dbg_printf_append(xhp, "%s is a vpkg provided by %s, ignored.\n", pkgname, curpkg); free(pkgname); continue; } /* * Pass 2: check if required dependency has been already * added in the transaction dictionary. */ if ((curpkgd = xbps_find_pkg_in_array(unsorted, reqpkg, NULL)) || (curpkgd = xbps_find_virtualpkg_in_array(xhp, unsorted, reqpkg, NULL))) { xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q); xbps_dbg_printf_append(xhp, " (%s queued)\n", pkgver_q); free(pkgname); continue; } /* * Pass 3: check if required dependency is already installed * and its version is fully matched. */ if ((curpkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) { if ((curpkgd = xbps_pkgdb_get_virtualpkg(xhp, pkgname))) { foundvpkg = true; } } if (curpkgd == NULL) { if (errno && errno != ENOENT) { /* error */ rv = errno; xbps_dbg_printf(xhp, "failed to find installed pkg for `%s': %s\n", reqpkg, strerror(rv)); free(pkgname); break; } free(pkgname); /* Required dependency not installed */ xbps_dbg_printf_append(xhp, "not installed.\n"); reason = "install"; state = XBPS_PKG_STATE_NOT_INSTALLED; } else { /* * Required dependency is installed, check if its version can * satisfy the requirements. */ xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q); /* Check its state */ if ((rv = xbps_pkg_state_dictionary(curpkgd, &state)) != 0) { free(pkgname); break; } if (foundvpkg && xbps_match_virtual_pkg_in_dict(curpkgd, reqpkg)) { /* * Check if required dependency is a virtual package and is satisfied * by an installed package. */ xbps_dbg_printf_append(xhp, "[virtual] satisfied by `%s'.\n", pkgver_q); free(pkgname); continue; } rv = xbps_pkgpattern_match(pkgver_q, reqpkg); if (rv == 0) { char *curpkgname; /* * The version requirement is not satisfied. */ curpkgname = xbps_pkg_name(pkgver_q); assert(curpkgname); if (strcmp(pkgname, curpkgname)) { xbps_dbg_printf_append(xhp, "not installed `%s (vpkg)'", pkgver_q); if (xbps_dictionary_get(curpkgd, "hold")) { xbps_dbg_printf_append(xhp, " on hold state! ignoring package.\n"); } else { xbps_dbg_printf_append(xhp, "\n"); reason = "install"; } } else { xbps_dbg_printf_append(xhp, "installed `%s', must be updated", pkgver_q); if (xbps_dictionary_get(curpkgd, "hold")) { xbps_dbg_printf_append(xhp, " on hold state! ignoring package.\n"); } else { xbps_dbg_printf_append(xhp, "\n"); reason = "update"; } } free(curpkgname); free(pkgname); } else if (rv == 1) { /* * The version requirement is satisfied. */ free(pkgname); rv = 0; if (state == XBPS_PKG_STATE_UNPACKED) { /* * Package matches the dependency pattern but was only unpacked, * configure pkg. */ xbps_dbg_printf_append(xhp, "installed `%s', must be configured.\n", pkgver_q); reason = "configure"; } else if (state == XBPS_PKG_STATE_INSTALLED) { /* * Package matches the dependency pattern and is fully installed, * skip to next one. */ xbps_dbg_printf_append(xhp, "installed `%s'.\n", pkgver_q); continue; } } else { /* error matching pkgpattern */ xbps_dbg_printf(xhp, "failed to match pattern %s with %s\n", reqpkg, pkgver_q); free(pkgname); break; } } if (xbps_dictionary_get(curpkgd, "hold")) { xbps_dbg_printf(xhp, "%s on hold state! ignoring package.\n", curpkg); continue; } /* * Pass 4: find required dependency in repository pool. * If dependency does not match add pkg into the missing * deps array and pass to next one. */ if (((curpkgd = xbps_rpool_get_pkg(xhp, reqpkg)) == NULL) && ((curpkgd = xbps_rpool_get_virtualpkg(xhp, reqpkg)) == NULL)) { /* pkg not found, there was some error */ if (errno && errno != ENOENT) { xbps_dbg_printf(xhp, "failed to find pkg for `%s' in rpool: %s\n", reqpkg, strerror(errno)); rv = errno; break; } rv = add_missing_reqdep(xhp, reqpkg); if (rv != 0 && rv != EEXIST) { xbps_dbg_printf(xhp, "`%s': add_missing_reqdep failed\n", reqpkg); break; } else if (rv == EEXIST) { xbps_dbg_printf(xhp, "`%s' missing dep already added.\n", reqpkg); rv = 0; continue; } else { xbps_dbg_printf(xhp, "`%s' added into the missing deps array.\n", reqpkg); continue; } } xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q); reqpkgname = xbps_pkg_name(pkgver_q); assert(reqpkgname); /* * Check dependency validity. */ pkgname = xbps_pkg_name(curpkg); assert(pkgname); if (strcmp(pkgname, reqpkgname) == 0) { xbps_dbg_printf_append(xhp, "[ignoring wrong dependency %s (depends on itself)]\n", reqpkg); xbps_remove_string_from_array(pkg_rdeps_array, reqpkg); free(pkgname); free(reqpkgname); continue; } free(pkgname); free(reqpkgname); /* * If package doesn't have rundeps, pass to the next one. */ curpkgrdeps = xbps_dictionary_get(curpkgd, "run_depends"); if (curpkgrdeps == NULL) { /* * Package is on repo, add it into the transaction dictionary. */ xbps_dictionary_set_cstring_nocopy(curpkgd, "transaction", reason); rv = xbps_transaction_store(xhp, unsorted, curpkgd, reason, true); if (rv != 0) { xbps_dbg_printf(xhp, "xbps_transaction_store failed for `%s': %s\n", reqpkg, strerror(rv)); break; } continue; } if (xhp->flags & XBPS_FLAG_DEBUG) { xbps_dbg_printf(xhp, "%s", ""); for (unsigned short x = 0; x < *depth; x++) { xbps_dbg_printf_append(xhp, " "); } xbps_dbg_printf_append(xhp, "%s: finding dependencies:\n", pkgver_q); } /* * Recursively find rundeps for current pkg dictionary. */ (*depth)++; curpkgprovides = xbps_dictionary_get(curpkgd, "provides"); rv = find_repo_deps(xhp, unsorted, curpkgrdeps, curpkgprovides, pkgver_q, depth); if (rv != 0) { xbps_dbg_printf(xhp, "Error checking %s for rundeps: %s\n", reqpkg, strerror(rv)); break; } /* * Package is on repo, add it into the transaction dictionary. */ xbps_dictionary_set_cstring_nocopy(curpkgd, "transaction", reason); rv = xbps_transaction_store(xhp, unsorted, curpkgd, reason, true); if (rv != 0) { xbps_dbg_printf(xhp, "xbps_transaction_store failed for `%s': %s\n", reqpkg, strerror(rv)); break; } } xbps_object_iterator_release(iter); (*depth)--; return rv; }
xbps_array_t xbps_find_pkg_obsoletes(struct xbps_handle *xhp, xbps_dictionary_t instd, xbps_dictionary_t newd) { xbps_array_t instfiles, newfiles, obsoletes; /* These are symlinks in Void and must not be removed */ const char *basesymlinks[] = { "./bin", "./sbin", "./lib", "./lib32", "./lib64", "./usr/lib32", "./usr/lib64", "./var/run", }; int rv = 0; assert(xbps_object_type(instd) == XBPS_TYPE_DICTIONARY); assert(xbps_object_type(newd) == XBPS_TYPE_DICTIONARY); obsoletes = xbps_array_create(); assert(obsoletes); instfiles = merge_filelist(instd); if (xbps_array_count(instfiles) == 0) { /* nothing to check if current pkg does not own any file */ xbps_object_release(instfiles); return obsoletes; } newfiles = merge_filelist(newd); /* * Iterate over files list from installed package. */ for (unsigned int i = 0; i < xbps_array_count(instfiles); i++) { xbps_object_t obj, obj2; xbps_string_t oldstr, newstr; struct stat st; uint64_t mtime = 0; const char *oldhash; char file[PATH_MAX]; bool found = false; obj = xbps_array_get(instfiles, i); if (xbps_object_type(obj) != XBPS_TYPE_DICTIONARY) { /* ignore unexistent files */ continue; } oldstr = xbps_dictionary_get(obj, "file"); if (oldstr == NULL) continue; snprintf(file, sizeof(file), ".%s", xbps_string_cstring_nocopy(oldstr)); if (xbps_dictionary_get_cstring_nocopy(obj, "sha256", &oldhash)) { rv = xbps_file_hash_check(file, oldhash); if (rv == ENOENT || rv == ERANGE) { /* Skip unexistent and files that do not * match the hash. */ continue; } } /* * Check if current file is available in new pkg filelist. */ for (unsigned int x = 0; x < xbps_array_count(newfiles); x++) { obj2 = xbps_array_get(newfiles, x); newstr = xbps_dictionary_get(obj2, "file"); assert(newstr); /* * Skip files with same path. */ if (xbps_string_equals(oldstr, newstr)) { found = true; break; } } if (found) { continue; } /* * Make sure to not remove any symlink of root directory. */ for (uint8_t x = 0; x < __arraycount(basesymlinks); x++) { if (strcmp(file, basesymlinks[x]) == 0) { found = true; xbps_dbg_printf(xhp, "[obsoletes] ignoring " "%s removal\n", file); break; } } if (found) { continue; } /* * Finally check if file mtime on disk matched what * the installed pkg has stored. */ if (xbps_dictionary_get_uint64(obj, "mtime", &mtime)) { if (lstat(file, &st) == -1) { xbps_dbg_printf(xhp, "[obsoletes] lstat failed " "for %s: %s\n", file, strerror(errno)); continue; } if (mtime != (uint64_t)st.st_mtime) continue; xbps_dbg_printf(xhp, "[obsoletes] %s: matched mtime, adding obsolete.\n", file); } /* * Obsolete found, add onto the array. */ xbps_dbg_printf(xhp, "found obsolete: %s\n", file); xbps_array_add_cstring(obsoletes, file); } xbps_object_release(instfiles); xbps_object_release(newfiles); return obsoletes; }