/** * pk_backend_initialize: * This should only be run once per backend load, i.e. not every transaction */ void pk_backend_initialize (GKeyFile *conf, PkBackend *backend) { gboolean ret; GFile *file = NULL; GError *error = NULL; GKeyFile *key_file = NULL; gchar *config_file = NULL; GList *mounts; /* use logging */ pk_debug_add_log_domain (G_LOG_DOMAIN); pk_debug_add_log_domain ("Yum"); /* create private area */ priv = g_new0 (PkBackendYumPrivate, 1); g_debug ("backend: initialize"); priv->spawn = pk_backend_spawn_new (conf); pk_backend_spawn_set_filter_stderr (priv->spawn, pk_backend_stderr_cb); pk_backend_spawn_set_filter_stdout (priv->spawn, pk_backend_stdout_cb); pk_backend_spawn_set_name (priv->spawn, "yum"); pk_backend_spawn_set_allow_sigkill (priv->spawn, FALSE); /* coldplug the mounts */ priv->volume_monitor = g_volume_monitor_get (); mounts = g_volume_monitor_get_mounts (priv->volume_monitor); g_list_foreach (mounts, (GFunc) pk_backend_mount_add, NULL); g_list_foreach (mounts, (GFunc) g_object_unref, NULL); g_list_free (mounts); /* setup a file monitor on the repos directory */ file = g_file_new_for_path (YUM_REPOS_DIRECTORY); priv->monitor = g_file_monitor_directory (file, G_FILE_MONITOR_NONE, NULL, &error); if (priv->monitor != NULL) { g_signal_connect (priv->monitor, "changed", G_CALLBACK (pk_backend_yum_repos_changed_cb), backend); } else { g_warning ("failed to setup monitor: %s", error->message); g_error_free (error); } /* read the config file */ key_file = g_key_file_new (); config_file = g_build_filename (SYSCONFDIR, "PackageKit", "Yum.conf", NULL); g_debug ("loading configuration from %s", config_file); ret = g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_NONE, &error); if (!ret) { g_warning ("failed to load Yum.conf: %s", error->message); g_error_free (error); goto out; } out: g_free (config_file); if (key_file != NULL) g_key_file_free (key_file); if (file != NULL) g_object_unref (file); }
/** * pk_debug_post_parse_hook: */ static gboolean pk_debug_post_parse_hook (GOptionContext *context, GOptionGroup *group, gpointer data, GError **error) { /* verbose? */ pk_debug_add_log_domain (G_LOG_DOMAIN); _console = (isatty (fileno (stdout)) == 1); g_debug ("Verbose debugging %s (on console %i)", _verbose ? "enabled" : "disabled", _console); return TRUE; }
/** * main: **/ int main (int argc, char *argv[]) { gboolean ret; PkCnfPolicyConfig *config = NULL; guint i; guint len; gchar *text; const gchar *possible; gchar **parts; guint retval = EXIT_COMMAND_NOT_FOUND; _cleanup_ptrarray_unref_ GPtrArray *array = NULL; _cleanup_strv_free_ gchar **package_ids = NULL; setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); #if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 31) if (! g_thread_supported ()) g_thread_init (NULL); #endif #if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 35) g_type_init (); #endif /* don't show debugging, unless VERBOSE is specified */ pk_debug_add_log_domain (G_LOG_DOMAIN); /* no input */ if (argv[1] == NULL) goto out; /* do stuff on ctrl-c */ signal (SIGINT, pk_cnf_sigint_handler); /* get policy config */ config = pk_cnf_get_config (); task = PK_TASK(pk_task_text_new ()); g_object_set (task, "cache-age", G_MAXUINT, "interactive", FALSE, "background", FALSE, NULL); cancellable = g_cancellable_new (); /* get length */ len = strlen (argv[1]); if (len < 1) goto out; if (argv[1][0] == '.') goto out; /* TRANSLATORS: the prefix of all the output telling the user * why it's not executing. NOTE: this is lowercase to mimic * the style of bash itself -- apologies */ g_printerr ("bash: %s: %s...\n", argv[1], _("command not found")); /* user is not allowing CNF to do anything useful */ if (!config->software_source_search && !config->similar_name_search) { goto out; } /* generate swizzles */ if (config->similar_name_search) array = pk_cnf_find_alternatives (argv[1], len); /* one exact possibility */ if (array != NULL && array->len == 1) { possible = g_ptr_array_index (array, 0); if (config->single_match == PK_CNF_POLICY_WARN) { /* TRANSLATORS: tell the user what we think the command is */ g_printerr ("%s '%s'\n", _("Similar command is:"), possible); goto out; } /* run */ if (config->single_match == PK_CNF_POLICY_RUN) { retval = pk_cnf_spawn_command (possible, &argv[2]); goto out; } /* ask */ if (config->single_match == PK_CNF_POLICY_ASK) { /* TRANSLATORS: Ask the user if we should run the similar command */ text = g_strdup_printf ("%s %s", _("Run similar command:"), possible); ret = pk_console_get_prompt (text, TRUE); if (ret) retval = pk_cnf_spawn_command (possible, &argv[2]); g_free (text); } goto out; /* multiple choice */ } else if (array != NULL && array->len > 1) { if (config->multiple_match == PK_CNF_POLICY_WARN) { /* TRANSLATORS: show the user a list of commands that they could have meant */ g_printerr ("%s:\n", _("Similar commands are:")); for (i = 0; i < array->len; i++) { possible = g_ptr_array_index (array, i); g_printerr ("'%s'\n", possible); } /* ask */ } else if (config->multiple_match == PK_CNF_POLICY_ASK) { /* TRANSLATORS: show the user a list of commands we could run */ g_printerr ("%s:\n", _("Similar commands are:")); for (i = 0; i < array->len; i++) { possible = g_ptr_array_index (array, i); g_printerr ("%i\t'%s'\n", i+1, possible); } /* TRANSLATORS: ask the user to choose a file to run */ i = pk_console_get_number (_("Please choose a command to run"), array->len); /* run command */ possible = g_ptr_array_index (array, i); retval = pk_cnf_spawn_command (possible, &argv[2]); } goto out; /* only search using PackageKit if configured to do so */ } else if (config->software_source_search) { package_ids = pk_cnf_find_available (argv[1], config->max_search_time); if (package_ids == NULL) goto out; len = g_strv_length (package_ids); if (len == 1) { parts = pk_package_id_split (package_ids[0]); if (config->single_install == PK_CNF_POLICY_WARN) { /* TRANSLATORS: tell the user what package provides the command */ g_printerr ("%s '%s'\n", _("The package providing this file is:"), parts[PK_PACKAGE_ID_NAME]); goto out; } /* ask */ if (config->single_install == PK_CNF_POLICY_ASK) { /* TRANSLATORS: as the user if we want to install a package to provide the command */ text = g_strdup_printf (_("Install package '%s' to provide command '%s'?"), parts[PK_PACKAGE_ID_NAME], argv[1]); ret = pk_console_get_prompt (text, FALSE); g_free (text); if (ret) { ret = pk_cnf_install_package_id (package_ids[0]); if (ret) retval = pk_cnf_spawn_command (argv[1], &argv[2]); } g_print ("\n"); goto out; } /* install */ if (config->single_install == PK_CNF_POLICY_INSTALL) { ret = pk_cnf_install_package_id (package_ids[0]); if (ret) retval = pk_cnf_spawn_command (argv[1], &argv[2]); } g_strfreev (parts); goto out; } else if (len > 1) { if (config->multiple_install == PK_CNF_POLICY_WARN) { /* TRANSLATORS: Show the user a list of packages that provide this command */ g_printerr ("%s\n", _("Packages providing this file are:")); for (i = 0; package_ids[i] != NULL; i++) { parts = pk_package_id_split (package_ids[i]); g_printerr ("'%s'\n", parts[PK_PACKAGE_ID_NAME]); g_strfreev (parts); } /* ask */ } else if (config->multiple_install == PK_CNF_POLICY_ASK) { /* TRANSLATORS: Show the user a list of packages that they can install to provide this command */ g_printerr ("%s:\n", _("Suitable packages are:")); for (i = 0; package_ids[i] != NULL; i++) { parts = pk_package_id_split (package_ids[i]); g_printerr ("%i\t'%s'\n", i+1, parts[PK_PACKAGE_ID_NAME]); g_strfreev (parts); } /* TRANSLATORS: ask the user to choose a file to install */ i = pk_console_get_number (_("Please choose a package to install"), len); if (i == 0) { g_printerr ("%s\n", _("User aborted selection")); goto out; } /* run command */ ret = pk_cnf_install_package_id (package_ids[i - 1]); if (ret) retval = pk_cnf_spawn_command (argv[1], &argv[2]); } goto out; } } out: if (task != NULL) g_object_unref (task); if (cancellable != NULL) g_object_unref (cancellable); if (config != NULL) { g_strfreev (config->locations); g_free (config); } return retval; }