+ /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
+ so we can't save ourself from removing important packages
+ At this point we (should) have extracted the .control file, so it
+ would be a good idea to reload the data in it, and set the Essential
+ state in *pkg. From now on the Essential is back in status file and
+ we can protect again.
+ We should operate this way:
+ fopen the file ( pkg->dest->root_dir/pkg->name.control )
+ check for "Essential" in it
+ set the value in pkg->essential.
+ This new routine could be useful also for every other flag
+ Pigi: 16/03/2004 */
+ set_flags_from_control(conf, pkg) ;
+
+ opkg_message(conf, OPKG_DEBUG, " Calling pkg_write_filelist from %s\n", __FUNCTION__);
+ err = pkg_write_filelist(conf, pkg);
+ if (err)
+ return err;
+
+ /* XXX: FEATURE: opkg should identify any files which existed
+ before installation and which were overwritten, (see
+ check_data_file_clashes()). What it must do is remove any such
+ files from the filelist of the old package which provided the
+ file. Otherwise, if the old package were removed at some point
+ it would break the new package. Removing the new package will
+ also break the old one, but this cannot be helped since the old
+ package's file has already been deleted. This is the importance
+ of check_data_file_clashes(), and only allowing opkg to install
+ a clashing package with a user force. */
+
+ return 0;
+}
+
+static int
+user_prefers_old_conffile(const char *file_name, const char *backup)
+{
+ char *response;
+ const char *short_file_name;
+
+ short_file_name = strrchr(file_name, '/');
+ if (short_file_name) {
+ short_file_name++;
+ } else {
+ short_file_name = file_name;
+ }
+
+ while (1) {
+ response = get_user_response(" Configuration file '%s'\n"
+ " ==> File on system created by you or by a script.\n"
+ " ==> File also in package provided by package maintainer.\n"
+ " What would you like to do about it ? Your options are:\n"
+ " Y or I : install the package maintainer's version\n"
+ " N or O : keep your currently-installed version\n"
+ " D : show the differences between the versions (if diff is installed)\n"
+ " The default action is to keep your current version.\n"
+ " *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
+
+ if (response == NULL)
+ return 1;
+
+ if (strcmp(response, "y") == 0
+ || strcmp(response, "i") == 0
+ || strcmp(response, "yes") == 0) {
+ free(response);
+ return 0;
+ }
+
+ if (strcmp(response, "d") == 0) {
+ const char *argv[] = {"diff", "-u", backup, file_name, NULL};
+ xsystem(argv);
+ printf(" [Press ENTER to continue]\n");
+ response = file_read_line_alloc(stdin);
+ free(response);
+ continue;
+ }
+
+ free(response);
+ return 1;
+ }
+}
+
+static int
+resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg)
+{
+ conffile_list_elt_t *iter;
+ conffile_t *cf;
+ char *cf_backup;
+ char *md5sum;
+
+ if (conf->noaction) return 0;
+
+ for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
+ char *root_filename;
+ cf = (conffile_t *)iter->data;
+ root_filename = root_filename_alloc(conf, cf->name);
+
+ /* Might need to initialize the md5sum for each conffile */
+ if (cf->value == NULL) {
+ cf->value = file_md5sum_alloc(root_filename);
+ }
+
+ if (!file_exists(root_filename)) {
+ free(root_filename);
+ continue;
+ }
+
+ cf_backup = backup_filename_alloc(root_filename);
+
+
+ if (file_exists(cf_backup)) {
+ /* Let's compute md5 to test if files are changed */
+ md5sum = file_md5sum_alloc(cf_backup);
+ if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
+ if (conf->force_maintainer) {
+ opkg_message(conf, OPKG_NOTICE, "Conffile %s using maintainer's setting.\n", cf_backup);
+ } else if (conf->force_defaults
+ || user_prefers_old_conffile(root_filename, cf_backup) ) {
+ rename(cf_backup, root_filename);
+ }
+ }
+ unlink(cf_backup);
+ if (md5sum)
+ free(md5sum);
+ }
+
+ free(cf_backup);
+ free(root_filename);
+ }
+
+ return 0;
+}
+
+
+opkg_error_t
+opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
+{
+ int cmp, err = 0;
+ pkg_t *old, *new;
+ char *old_version, *new_version;
+
+ opkg_message(conf, OPKG_DEBUG2, " Getting old from pkg_hash_fetch \n" );
+ old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
+ if ( old )
+ opkg_message(conf, OPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n", old->version );
+
+ opkg_message(conf, OPKG_DEBUG2, " Getting new from pkg_hash_fetch \n" );
+ new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name, &err);
+ if ( new )
+ opkg_message(conf, OPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n", new->version );
+
+/* Pigi Basically here is broken the version stuff.
+ What's happening is that nothing provide the version to differents
+ functions, so the returned struct is always the latest.
+ That's why the install by name don't work.
+*/
+ opkg_message(conf, OPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
+
+ if ( old )
+ opkg_message(conf, OPKG_DEBUG2, " old %s ", old->version );
+ if ( new )
+ opkg_message(conf, OPKG_DEBUG2, " new %s ", new->version );
+ opkg_message(conf, OPKG_DEBUG2, " \n");
+
+ if (new == NULL) {
+ if (err)
+ return err;
+ else
+ return OPKG_PKG_HAS_NO_CANDIDATE;
+ }
+
+ new->state_flag |= SF_USER;
+ if (old) {
+ old_version = pkg_version_str_alloc(old);
+ new_version = pkg_version_str_alloc(new);
+
+ cmp = pkg_compare_versions(old, new);
+ if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
+ opkg_message(conf, OPKG_DEBUG, " Forcing downgrade \n");
+ cmp = -1 ; /* then we force opkg to downgrade */
+ /* We need to use a value < 0 because in the 0 case we are asking to */
+ /* reinstall, and some check could fail asking the "force-reinstall" option */
+ }
+ opkg_message(conf, OPKG_DEBUG,
+ "Comparing visible versions of pkg %s:"
+ "\n\t%s is installed "
+ "\n\t%s is available "
+ "\n\t%d was comparison result\n",
+ pkg_name, old_version, new_version, cmp);
+ if (cmp == 0 && !conf->force_reinstall) {
+ opkg_message(conf, OPKG_NOTICE,
+ "Package %s (%s) installed in %s is up to date.\n",
+ old->name, old_version, old->dest->name);
+ free(old_version);
+ free(new_version);
+ return 0;
+ } else if (cmp > 0) {
+ opkg_message(conf, OPKG_NOTICE,
+ "Not downgrading package %s on %s from %s to %s.\n",
+ old->name, old->dest->name, old_version, new_version);
+ free(old_version);
+ free(new_version);
+ return 0;
+ } else if (cmp < 0) {
+ new->dest = old->dest;
+ old->state_want = SW_DEINSTALL; /* Here probably the problem for bug 1277 */
+ }
+ free(old_version);
+ free(new_version);
+ }
+
+ /* XXX: CLEANUP: The error code of opkg_install_by_name is really
+ supposed to be an opkg_error_t, but opkg_install_pkg could
+ return any kind of integer, (might be errno from a syscall,
+ etc.). This is a real mess and will need to be cleaned up if
+ anyone ever wants to make a nice libopkg. */
+
+ opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
+ return opkg_install_pkg(conf, new,0);
+}
+
+/**
+ * @brief Really install a pkg_t
+ */
+int
+opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
+{
+ int err = 0;
+ int message = 0;
+ pkg_t *old_pkg = NULL;
+ pkg_vec_t *replacees;
+ abstract_pkg_t *ab_pkg = NULL;
+ int old_state_flag;
+ char* file_md5;
+#ifdef HAVE_SHA256
+ char* file_sha256;
+#endif
+
+ if ( from_upgrade )
+ message = 1; /* Coming from an upgrade, and should change the output message */
+
+ if (!pkg) {
+ opkg_message(conf, OPKG_ERROR,
+ "INTERNAL ERROR: null pkg passed to opkg_install_pkg\n");
+ return OPKG_INSTALL_ERR_INTERNAL;
+ }
+
+ opkg_message(conf, OPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
+
+ if (!pkg_arch_supported(conf, pkg)) {
+ opkg_message(conf, OPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
+ pkg->architecture, pkg->name);
+ return OPKG_INSTALL_ERR_INTERNAL;
+ }
+ if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
+ err = satisfy_dependencies_for(conf, pkg);
+ if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
+
+ opkg_message(conf, OPKG_NOTICE,
+ "Package %s is already installed in %s.\n",
+ pkg->name, pkg->dest->name);
+ return 0;
+ }
+
+ if (pkg->dest == NULL) {
+ pkg->dest = conf->default_dest;
+ }
+
+ old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
+
+ err = opkg_install_check_downgrade(conf, pkg, old_pkg, message);
+ if (err) { return OPKG_INSTALL_ERR_NO_DOWNGRADE; }
+
+ pkg->state_want = SW_INSTALL;
+ if (old_pkg){
+ old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
+ }
+
+
+ /* Abhaya: conflicts check */
+ err = check_conflicts_for(conf, pkg);
+ if (err) { return OPKG_INSTALL_ERR_CONFLICTS; }
+
+ /* this setup is to remove the upgrade scenario in the end when
+ installing pkg A, A deps B & B deps on A. So both B and A are
+ installed. Then A's installation is started resulting in an
+ uncecessary upgrade */
+ if (pkg->state_status == SS_INSTALLED
+ && conf->force_reinstall == 0) return 0;
+
+ err = verify_pkg_installable(conf, pkg);
+ if (err) { return OPKG_INSTALL_ERR_NO_SPACE; }
+
+ if (pkg->local_filename == NULL) {
+ err = opkg_download_pkg(conf, pkg, conf->tmp_dir);
+ if (err) {
+ opkg_message(conf, OPKG_ERROR,
+ "Failed to download %s. Perhaps you need to run 'opkg update'?\n",
+ pkg->name);
+ return OPKG_INSTALL_ERR_DOWNLOAD;
+ }
+ }
+
+ /* check that the repository is valid */
+ #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
+ char *list_file_name, *sig_file_name, *lists_dir;
+
+ /* check to ensure the package has come from a repository */
+ if (conf->check_signature && pkg->src)
+ {
+ sprintf_alloc (&lists_dir, "%s",
+ (conf->restrict_to_default_dest)
+ ? conf->default_dest->lists_dir
+ : conf->lists_dir);
+ sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
+ sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
+
+ if (file_exists (sig_file_name))
+ {
+ if (opkg_verify_file (conf, list_file_name, sig_file_name)){
+ opkg_message(conf, OPKG_ERROR, "Failed to verify the signature of: %s\n",
+ list_file_name);
+ return OPKG_INSTALL_ERR_SIGNATURE;
+ }
+ }else{
+ opkg_message(conf, OPKG_ERROR, "Signature file is missing. "
+ "Perhaps you need to run 'opkg update'?\n");
+ return OPKG_INSTALL_ERR_SIGNATURE;
+ }
+
+ free (lists_dir);
+ free (list_file_name);
+ free (sig_file_name);
+ }
+ #endif
+
+ /* Check for md5 values */
+ if (pkg->md5sum)
+ {
+ file_md5 = file_md5sum_alloc(pkg->local_filename);
+ if (file_md5 && strcmp(file_md5, pkg->md5sum))
+ {
+ opkg_message(conf, OPKG_ERROR,
+ "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
+ pkg->name);
+ free(file_md5);
+ return OPKG_INSTALL_ERR_MD5;
+ }
+ if (file_md5)
+ free(file_md5);
+ }
+
+#ifdef HAVE_SHA256
+ /* Check for sha256 value */
+ if(pkg->sha256sum)
+ {
+ file_sha256 = file_sha256sum_alloc(pkg->local_filename);
+ if (file_sha256 && strcmp(file_sha256, pkg->sha256sum))
+ {
+ opkg_message(conf, OPKG_ERROR,
+ "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
+ pkg->name);
+ free(file_sha256);
+ return OPKG_INSTALL_ERR_SHA256;
+ }
+ if (file_sha256)
+ free(file_sha256);
+ }
+#endif
+
+ if (pkg->tmp_unpack_dir == NULL) {
+ unpack_pkg_control_files(conf, pkg);
+ }