diff --git a/include/xbps_api_impl.h b/include/xbps_api_impl.h index 971cf06f..e18bb55f 100644 --- a/include/xbps_api_impl.h +++ b/include/xbps_api_impl.h @@ -94,11 +94,12 @@ void HIDDEN xbps_fetch_unset_cache_connection(void); * @private * From lib/package_config_files.c */ -int HIDDEN xbps_config_file_from_archive_entry(prop_dictionary_t, - prop_dictionary_t, - struct archive_entry *, - int *, - bool *); +int HIDDEN xbps_entry_is_a_conf_file(prop_dictionary_t, const char *); +int HIDDEN xbps_entry_install_conf_file(prop_dictionary_t, + struct archive_entry *, + const char *, + const char *, + const char *); /** * @private * From lib/plist.c diff --git a/lib/package_config_files.c b/lib/package_config_files.c index d4a25268..41e14fcd 100644 --- a/lib/package_config_files.c +++ b/lib/package_config_files.c @@ -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 -#include #include #include #include -#include #include #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- */ } 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; } diff --git a/lib/package_unpack.c b/lib/package_unpack.c index 915603dc..0e1f611f 100644 --- a/lib/package_unpack.c +++ b/lib/package_unpack.c @@ -91,33 +91,36 @@ set_extract_flags(int *flags, bool update) * the consumer. */ static int -unpack_archive_fini(struct archive *ar, - prop_dictionary_t pkg, +unpack_archive_fini(prop_dictionary_t pkg_repod, + struct archive *ar, const char *pkgname, const char *version) { prop_dictionary_t propsd, filesd, old_filesd; struct archive_entry *entry; size_t entry_idx = 0; - const char *rootdir, *entry_str, *transact; + const char *rootdir, *entry_pname, *transact; char *buf; int rv, flags, lflags; - bool preserve, skip_entry, update, replace_files_in_pkg_update; - bool props_plist_found, files_plist_found; + bool preserve, update, replace_files_in_pkg_update; assert(ar != NULL); - assert(pkg != NULL); + assert(pkg_repod != NULL); + assert(pkgname != NULL); + assert(version != NULL); - preserve = skip_entry = update = replace_files_in_pkg_update = false; - props_plist_found = files_plist_found = false; + preserve = update = replace_files_in_pkg_update = false; rootdir = xbps_get_rootdir(); flags = xbps_get_flags(); if (chdir(rootdir) == -1) return errno; - prop_dictionary_get_bool(pkg, "preserve", &preserve); - prop_dictionary_get_cstring_nocopy(pkg, "trans-action", &transact); + prop_dictionary_get_bool(pkg_repod, "preserve", &preserve); + prop_dictionary_get_cstring_nocopy(pkg_repod, + "trans-action", &transact); + assert(transasct != NULL); + if (strcmp(transact, "update") == 0) update = true; @@ -139,6 +142,7 @@ unpack_archive_fini(struct archive *ar, } } free(buf); + buf = xbps_xasprintf(".%s/metadata/%s/REMOVE", XBPS_META_PATH, pkgname); if (buf == NULL) @@ -152,59 +156,54 @@ unpack_archive_fini(struct archive *ar, } free(buf); } - /* * Process the archive files. */ while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) { - entry_str = archive_entry_pathname(entry); + entry_pname = archive_entry_pathname(entry); set_extract_flags(&lflags, update); - /* - * Run the pre INSTALL action if the file is there. - */ - if (strcmp("./INSTALL", entry_str) == 0) { + + if (strcmp("./INSTALL", entry_pname) == 0) { + /* + * Extract package INSTALL file into destination + * directory and execute the pre install action. + */ buf = xbps_xasprintf(".%s/metadata/%s/INSTALL", XBPS_META_PATH, pkgname); if (buf == NULL) { rv = ENOMEM; goto out; } - archive_entry_set_pathname(entry, buf); archive_entry_set_perm(entry, 0750); - if (archive_read_extract(ar, entry, lflags) != 0) { if ((rv = archive_errno(ar)) != EEXIST) { free(buf); goto out; } } - rv = xbps_file_exec(buf, "pre", pkgname, version, update ? "yes" : "no", NULL); + free(buf); if (rv != 0) { - free(buf); fprintf(stderr, "%s: preinst action target error %s\n", pkgname, strerror(errno)); goto out; } /* Pass to the next entry if successful */ - free(buf); entry_idx++; continue; - /* * Unpack metadata files in final directory. */ - } else if (strcmp("./REMOVE", entry_str) == 0) { + } else if (strcmp("./REMOVE", entry_pname) == 0) { buf = xbps_xasprintf(".%s/metadata/%s/REMOVE", XBPS_META_PATH, pkgname); if (buf == NULL) { rv = ENOMEM; goto out; } - archive_entry_set_pathname(entry, buf); free(buf); archive_entry_set_perm(entry, 0750); @@ -212,12 +211,11 @@ unpack_archive_fini(struct archive *ar, if ((rv = archive_errno(ar)) != EEXIST) goto out; } - /* Pass to next entry if successful */ entry_idx++; continue; - } else if (strcmp("./files.plist", entry_str) == 0) { + } else if (strcmp("./files.plist", entry_pname) == 0) { /* * Now we have a dictionary from the entry * in memory. Will be written to disk later, when @@ -228,23 +226,19 @@ unpack_archive_fini(struct archive *ar, rv = errno; goto out; } - /* Pass to next entry */ - files_plist_found = true; entry_idx++; continue; - } else if (strcmp("./props.plist", entry_str) == 0) { + } else if (strcmp("./props.plist", entry_pname) == 0) { buf = xbps_xasprintf(".%s/metadata/%s/%s", XBPS_META_PATH, pkgname, XBPS_PKGPROPS); if (buf == NULL) { rv = ENOMEM; goto out; } - archive_entry_set_pathname(entry, buf); free(buf); - if (archive_read_extract(ar, entry, lflags) != 0) { rv = archive_errno(ar); goto out; @@ -258,16 +252,14 @@ unpack_archive_fini(struct archive *ar, goto out; } /* Pass to next entry if successful */ - props_plist_found = true; entry_idx++; continue; } - /* * If XBPS_PKGFILES or XBPS_PKGPROPS weren't found * in the archive at this phase, skip all data. */ - if (!files_plist_found || !props_plist_found) { + if (propsd == NULL || filesd == NULL) { archive_read_data_skip(ar); /* * If we have processed 4 entries and the two @@ -280,25 +272,37 @@ unpack_archive_fini(struct archive *ar, entry_idx++; continue; } - /* * Handle configuration files. Check if current entry is * a configuration file and take action if required. Skip * packages that don't have the "conf_files" array in * the XBPS_PKGPROPS dictionary. */ - if (prop_dictionary_get(propsd, "conf_files")) { - if ((rv = xbps_config_file_from_archive_entry(filesd, - propsd, entry, &lflags, &skip_entry)) != 0) + rv = xbps_entry_is_a_conf_file(propsd, entry_pname); + if (rv == -1) { + /* error */ + goto out; + } else if (rv == 1) { + rv = xbps_entry_install_conf_file(filesd, + entry, entry_pname, pkgname, version); + if (rv == -1) { + /* error */ goto out; - - if (skip_entry) { + } else if (rv == 1) { + /* + * Configuration file should be installed. + */ + lflags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE; + lflags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; + } else { + /* + * Keep current configuration file + * as is now and pass to next entry. + */ archive_read_data_skip(ar); - skip_entry = false; continue; } } - /* * Account for the following scenario (real example): * @@ -318,7 +322,8 @@ unpack_archive_fini(struct archive *ar, * don't match the SHA256 hash and skips them. */ replace_files_in_pkg_update = false; - prop_dictionary_get_bool(pkg, "replace-files-in-pkg-update", + prop_dictionary_get_bool(pkg_repod, + "replace-files-in-pkg-update", &replace_files_in_pkg_update); if (replace_files_in_pkg_update) { lflags &= ~ARCHIVE_EXTRACT_NO_OVERWRITE; @@ -331,21 +336,21 @@ unpack_archive_fini(struct archive *ar, if (archive_read_extract(ar, entry, lflags) != 0) { rv = archive_errno(ar); if (rv && rv != EEXIST) { - fprintf(stderr, "ERROR: %s...exiting!\n", + fprintf(stderr, "ERROR: extracting `%s' " + "(%s) ...exiting!\n", entry_pname, archive_error_string(ar)); goto out; } else if (rv == EEXIST) { if (flags & XBPS_FLAG_VERBOSE) { fprintf(stderr, "WARNING: ignoring existent " - "path: %s\n", - archive_entry_pathname(entry)); + "entry: %s\n", entry_pname); } continue; } } if (flags & XBPS_FLAG_VERBOSE) - printf(" %s\n", archive_entry_pathname(entry)); + printf(" %s\n", entry_pname); } if ((rv = archive_errno(ar)) == 0) { @@ -402,19 +407,19 @@ out: } int -xbps_unpack_binary_pkg(prop_dictionary_t pkg) +xbps_unpack_binary_pkg(prop_dictionary_t pkg_repod) { const char *pkgname, *version; struct archive *ar = NULL; char *binfile = NULL; int pkg_fd, rv = 0; - assert(pkg != NULL); + assert(pkg_repod != NULL); - prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &pkgname); - prop_dictionary_get_cstring_nocopy(pkg, "version", &version); + prop_dictionary_get_cstring_nocopy(pkg_repod, "pkgname", &pkgname); + prop_dictionary_get_cstring_nocopy(pkg_repod, "version", &version); - binfile = xbps_get_binpkg_repo_uri(pkg); + binfile = xbps_get_binpkg_repo_uri(pkg_repod); if (binfile == NULL) return EINVAL; @@ -445,7 +450,7 @@ xbps_unpack_binary_pkg(prop_dictionary_t pkg) goto out; } - if ((rv = unpack_archive_fini(ar, pkg, pkgname, version)) != 0) + if ((rv = unpack_archive_fini(pkg_repod, ar, pkgname, version)) != 0) goto out; /*