| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2008 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <locale.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <signal.h> |
| 30 |
#include <glib/gi18n.h> |
| 31 |
#include <packagekit-glib2/packagekit.h> |
| 32 |
#include <packagekit-glib2/packagekit-private.h> |
| 33 |
|
| 34 |
#define PK_MAX_PATH_LEN 1023 |
| 35 |
|
| 36 |
typedef enum { |
| 37 |
PK_CNF_POLICY_RUN, |
| 38 |
PK_CNF_POLICY_INSTALL, |
| 39 |
PK_CNF_POLICY_ASK, |
| 40 |
PK_CNF_POLICY_WARN, |
| 41 |
PK_CNF_POLICY_UNKNOWN |
| 42 |
} PkCnfPolicy; |
| 43 |
|
| 44 |
typedef struct { |
| 45 |
PkCnfPolicy single_match; |
| 46 |
PkCnfPolicy multiple_match; |
| 47 |
PkCnfPolicy single_install; |
| 48 |
PkCnfPolicy multiple_install; |
| 49 |
gboolean software_source_search; |
| 50 |
gboolean similar_name_search; |
| 51 |
gchar **locations; |
| 52 |
guint max_search_time; |
| 53 |
} PkCnfPolicyConfig; |
| 54 |
|
| 55 |
static PkTask *task = NULL; |
| 56 |
static GCancellable *cancellable = NULL; |
| 57 |
|
| 58 |
/* bash reserved code */ |
| 59 |
#define EXIT_COMMAND_NOT_FOUND 127 |
| 60 |
|
| 61 |
/** |
| 62 |
* |
| 63 |
* Swizzle ordering, e.g. amke -> make |
| 64 |
**/ |
| 65 |
static void |
| 66 |
pk_cnf_find_alternatives_swizzle (const gchar *cmd, guint len, GPtrArray *array) |
| 67 |
{ |
| 68 |
guint i; |
| 69 |
gchar *possible; |
| 70 |
gchar swap; |
| 71 |
|
| 72 |
/* */ |
| 73 |
for (i = 0; i < len-1; i++) { |
| 74 |
possible = g_strdup (cmd); |
| 75 |
swap = possible[i]; |
| 76 |
possible[i] = possible[i+1]; |
| 77 |
possible[i+1] = swap; |
| 78 |
g_ptr_array_add (array, possible); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* |
| 84 |
* Replace some easily confused chars, e.g. gnome-power-managir to gnome-power-manager |
| 85 |
**/ |
| 86 |
static void |
| 87 |
pk_cnf_find_alternatives_replace (const gchar *cmd, guint len, GPtrArray *array) |
| 88 |
{ |
| 89 |
guint i; |
| 90 |
gchar *possible; |
| 91 |
gchar temp; |
| 92 |
|
| 93 |
/* replace some easily confused chars */ |
| 94 |
for (i = 0; i < len; i++) { |
| 95 |
temp = cmd[i]; |
| 96 |
if (temp == 'i') { |
| 97 |
possible = g_strdup (cmd); |
| 98 |
possible[i] = 'e'; |
| 99 |
g_ptr_array_add (array, possible); |
| 100 |
} |
| 101 |
if (temp == 'e') { |
| 102 |
possible = g_strdup (cmd); |
| 103 |
possible[i] = 'i'; |
| 104 |
g_ptr_array_add (array, possible); |
| 105 |
} |
| 106 |
if (temp == 'i') { |
| 107 |
possible = g_strdup (cmd); |
| 108 |
possible[i] = 'o'; |
| 109 |
g_ptr_array_add (array, possible); |
| 110 |
} |
| 111 |
if (temp == 'c') { |
| 112 |
possible = g_strdup (cmd); |
| 113 |
possible[i] = 's'; |
| 114 |
g_ptr_array_add (array, possible); |
| 115 |
} |
| 116 |
if (temp == 's') { |
| 117 |
possible = g_strdup (cmd); |
| 118 |
possible[i] = 'c'; |
| 119 |
g_ptr_array_add (array, possible); |
| 120 |
} |
| 121 |
if (temp == 's') { |
| 122 |
possible = g_strdup (cmd); |
| 123 |
possible[i] = 'z'; |
| 124 |
g_ptr_array_add (array, possible); |
| 125 |
} |
| 126 |
if (temp == 'z') { |
| 127 |
possible = g_strdup (cmd); |
| 128 |
possible[i] = 's'; |
| 129 |
g_ptr_array_add (array, possible); |
| 130 |
} |
| 131 |
if (temp == 'k') { |
| 132 |
possible = g_strdup (cmd); |
| 133 |
possible[i] = 'c'; |
| 134 |
g_ptr_array_add (array, possible); |
| 135 |
} |
| 136 |
if (temp == 'c') { |
| 137 |
possible = g_strdup (cmd); |
| 138 |
possible[i] = 'k'; |
| 139 |
g_ptr_array_add (array, possible); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* |
| 146 |
* Truncate first and last char, so lshall -> lshal |
| 147 |
**/ |
| 148 |
static void |
| 149 |
pk_cnf_find_alternatives_truncate (const gchar *cmd, guint len, GPtrArray *array) |
| 150 |
{ |
| 151 |
guint i; |
| 152 |
gchar *possible; |
| 153 |
|
| 154 |
/* truncate last char */ |
| 155 |
possible = g_strdup (cmd); |
| 156 |
possible[len-1] = '\0'; |
| 157 |
g_ptr_array_add (array, possible); |
| 158 |
|
| 159 |
/* truncate first char */ |
| 160 |
possible = g_strdup (cmd); |
| 161 |
for (i = 0; i < len-1; i++) |
| 162 |
possible[i] = possible[i+1]; |
| 163 |
possible[len-1] = '\0'; |
| 164 |
g_ptr_array_add (array, possible); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* |
| 169 |
* Remove double chars, e.g. gnome-power-manaager -> gnome-power-manager |
| 170 |
**/ |
| 171 |
static void |
| 172 |
pk_cnf_find_alternatives_remove_double (const gchar *cmd, guint len, GPtrArray *array) |
| 173 |
{ |
| 174 |
guint i, j; |
| 175 |
gchar *possible; |
| 176 |
|
| 177 |
for (i = 1; i < len; i++) { |
| 178 |
if (cmd[i-1] == cmd[i]) { |
| 179 |
possible = g_strdup (cmd); |
| 180 |
for (j = i; j < len; j++) |
| 181 |
possible[j] = possible[j+1]; |
| 182 |
possible[len-1] = '\0'; |
| 183 |
g_ptr_array_add (array, possible); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* |
| 190 |
* Fix British spellings, e.g. colourdiff -> colordiff |
| 191 |
**/ |
| 192 |
static void |
| 193 |
pk_cnf_find_alternatives_locale (const gchar *cmd, guint len, GPtrArray *array) |
| 194 |
{ |
| 195 |
guint i, j; |
| 196 |
gchar *possible; |
| 197 |
|
| 198 |
for (i = 1; i < len; i++) { |
| 199 |
if (cmd[i-1] == 'o' && cmd[i] == 'u') { |
| 200 |
possible = g_strdup (cmd); |
| 201 |
for (j = i; j < len; j++) |
| 202 |
possible[j] = possible[j+1]; |
| 203 |
possible[len-1] = '\0'; |
| 204 |
g_ptr_array_add (array, possible); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* |
| 211 |
* Suggest Linux commands for Solaris commands |
| 212 |
**/ |
| 213 |
static void |
| 214 |
pk_cnf_find_alternatives_solaris (const gchar *cmd, guint len, GPtrArray *array) |
| 215 |
{ |
| 216 |
const gchar *tmp; |
| 217 |
g_autoptr(GHashTable) hash = NULL; |
| 218 |
|
| 219 |
hash = g_hash_table_new (g_str_hash, g_str_equal); |
| 220 |
g_hash_table_insert (hash, (gpointer) "smuser", (gpointer) "usermod"); |
| 221 |
g_hash_table_insert (hash, (gpointer) "logins", (gpointer) "usermod"); |
| 222 |
g_hash_table_insert (hash, (gpointer) "adb", (gpointer) "gdb"); |
| 223 |
g_hash_table_insert (hash, (gpointer) "add_drv", (gpointer) "modprobe"); |
| 224 |
g_hash_table_insert (hash, (gpointer) "modload", (gpointer) "modprobe"); |
| 225 |
g_hash_table_insert (hash, (gpointer) "modunload", (gpointer) "modprobe"); |
| 226 |
g_hash_table_insert (hash, (gpointer) "rem_drv", (gpointer) "modprobe"); |
| 227 |
g_hash_table_insert (hash, (gpointer) "audit", (gpointer) "auditctl"); |
| 228 |
g_hash_table_insert (hash, (gpointer) "auditreduce", (gpointer) "auditctl"); |
| 229 |
g_hash_table_insert (hash, (gpointer) "cfgadm", (gpointer) "lsmod"); |
| 230 |
g_hash_table_insert (hash, (gpointer) "clri", (gpointer) "fsck"); |
| 231 |
g_hash_table_insert (hash, (gpointer) "fsdb", (gpointer) "fsck"); |
| 232 |
g_hash_table_insert (hash, (gpointer) "volcheck", (gpointer) "fsck"); |
| 233 |
g_hash_table_insert (hash, (gpointer) "crle", (gpointer) "ldconfig"); |
| 234 |
g_hash_table_insert (hash, (gpointer) "devfsadm", (gpointer) "udevtrigger"); |
| 235 |
g_hash_table_insert (hash, (gpointer) "devlinks", (gpointer) "ln"); |
| 236 |
g_hash_table_insert (hash, (gpointer) "dfshares", (gpointer) "exportfs"); |
| 237 |
g_hash_table_insert (hash, (gpointer) "share", (gpointer) "exportfs"); |
| 238 |
g_hash_table_insert (hash, (gpointer) "shareall", (gpointer) "exportfs"); |
| 239 |
g_hash_table_insert (hash, (gpointer) "dladm", (gpointer) "ifconfig"); |
| 240 |
g_hash_table_insert (hash, (gpointer) "kstat", (gpointer) "ifconfig"); |
| 241 |
g_hash_table_insert (hash, (gpointer) "dtrace", (gpointer) "stap"); |
| 242 |
g_hash_table_insert (hash, (gpointer) "eeprom", (gpointer) "hwclock"); |
| 243 |
g_hash_table_insert (hash, (gpointer) "fcinfo", (gpointer) "lspci"); |
| 244 |
g_hash_table_insert (hash, (gpointer) "prtfru", (gpointer) "lspci"); |
| 245 |
g_hash_table_insert (hash, (gpointer) "fmthard", (gpointer) "fdisk"); |
| 246 |
g_hash_table_insert (hash, (gpointer) "format", (gpointer) "fdisk"); |
| 247 |
g_hash_table_insert (hash, (gpointer) "prtvtoc", (gpointer) "fdisk"); |
| 248 |
g_hash_table_insert (hash, (gpointer) "installboot", (gpointer) "mkbootdisk"); |
| 249 |
g_hash_table_insert (hash, (gpointer) "installpatch", (gpointer) "yum"); |
| 250 |
g_hash_table_insert (hash, (gpointer) "patchaddpkgadd", (gpointer) "yum"); |
| 251 |
g_hash_table_insert (hash, (gpointer) "pkgchk", (gpointer) "yum"); |
| 252 |
g_hash_table_insert (hash, (gpointer) "pkginfo", (gpointer) "yum"); |
| 253 |
g_hash_table_insert (hash, (gpointer) "pkgrm", (gpointer) "yum"); |
| 254 |
g_hash_table_insert (hash, (gpointer) "prodreg", (gpointer) "yum"); |
| 255 |
g_hash_table_insert (hash, (gpointer) "showrev", (gpointer) "yum"); |
| 256 |
g_hash_table_insert (hash, (gpointer) "isainfo", (gpointer) "uname"); |
| 257 |
g_hash_table_insert (hash, (gpointer) "luxadm", (gpointer) "systool"); |
| 258 |
g_hash_table_insert (hash, (gpointer) "mkfile", (gpointer) "touch"); |
| 259 |
g_hash_table_insert (hash, (gpointer) "mpathadm", (gpointer) "multipath"); |
| 260 |
g_hash_table_insert (hash, (gpointer) "stmsboot", (gpointer) "multipath"); |
| 261 |
g_hash_table_insert (hash, (gpointer) "ndd", (gpointer) "modinfo"); |
| 262 |
g_hash_table_insert (hash, (gpointer) "newfs", (gpointer) "mkfs"); |
| 263 |
g_hash_table_insert (hash, (gpointer) "pbind", (gpointer) "taskset"); |
| 264 |
g_hash_table_insert (hash, (gpointer) "pldd", (gpointer) "ldd"); |
| 265 |
g_hash_table_insert (hash, (gpointer) "praudit", (gpointer) "auditctl"); |
| 266 |
g_hash_table_insert (hash, (gpointer) "prstat", (gpointer) "ps"); |
| 267 |
g_hash_table_insert (hash, (gpointer) "prtconf", (gpointer) "dmesg"); |
| 268 |
g_hash_table_insert (hash, (gpointer) "psrinfo", (gpointer) "dmidecode"); |
| 269 |
g_hash_table_insert (hash, (gpointer) "sysdef", (gpointer) "dmidecode"); |
| 270 |
g_hash_table_insert (hash, (gpointer) "ptree", (gpointer) "pstree"); |
| 271 |
g_hash_table_insert (hash, (gpointer) "snoop", (gpointer) "tcpdump"); |
| 272 |
g_hash_table_insert (hash, (gpointer) "sotruss", (gpointer) "strace"); |
| 273 |
g_hash_table_insert (hash, (gpointer) "truss", (gpointer) "strace"); |
| 274 |
g_hash_table_insert (hash, (gpointer) "svcadm", (gpointer) "service"); |
| 275 |
g_hash_table_insert (hash, (gpointer) "svcs", (gpointer) "service"); |
| 276 |
g_hash_table_insert (hash, (gpointer) "swap", (gpointer) "swapon"); |
| 277 |
g_hash_table_insert (hash, (gpointer) "trapstat", (gpointer) "oprofile"); |
| 278 |
|
| 279 |
/* find anything that matches exactly */ |
| 280 |
tmp = g_hash_table_lookup (hash, cmd); |
| 281 |
if (tmp != NULL) |
| 282 |
g_ptr_array_add (array, g_strdup (tmp)); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* |
| 287 |
* Remove double chars, e.g. Lshal -> lshal |
| 288 |
**/ |
| 289 |
static void |
| 290 |
pk_cnf_find_alternatives_case (const gchar *cmd, guint len, GPtrArray *array) |
| 291 |
{ |
| 292 |
guint i; |
| 293 |
gchar *possible; |
| 294 |
gchar temp; |
| 295 |
|
| 296 |
for (i = 0; i < len; i++) { |
| 297 |
temp = g_ascii_tolower (cmd[i]); |
| 298 |
if (temp != cmd[i]) { |
| 299 |
possible = g_strdup (cmd); |
| 300 |
possible[i] = temp; |
| 301 |
g_ptr_array_add (array, possible); |
| 302 |
} |
| 303 |
temp = g_ascii_toupper (cmd[i]); |
| 304 |
if (temp != cmd[i]) { |
| 305 |
possible = g_strdup (cmd); |
| 306 |
possible[i] = temp; |
| 307 |
g_ptr_array_add (array, possible); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/* all lower */ |
| 312 |
possible = g_strdup (cmd); |
| 313 |
for (i = 0; i < len; i++) |
| 314 |
possible[i] = g_ascii_tolower (cmd[i]); |
| 315 |
if (strcmp (possible, cmd) != 0) |
| 316 |
g_ptr_array_add (array, possible); |
| 317 |
else |
| 318 |
g_free (possible); |
| 319 |
|
| 320 |
/* all upper */ |
| 321 |
possible = g_strdup (cmd); |
| 322 |
for (i = 0; i < len; i++) |
| 323 |
possible[i] = g_ascii_toupper (cmd[i]); |
| 324 |
if (strcmp (possible, cmd) != 0) |
| 325 |
g_ptr_array_add (array, possible); |
| 326 |
else |
| 327 |
g_free (possible); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* |
| 332 |
* Generate a list of commands it might be |
| 333 |
**/ |
| 334 |
static GPtrArray * |
| 335 |
pk_cnf_find_alternatives (const gchar *cmd, guint len) |
| 336 |
{ |
| 337 |
GPtrArray *array; |
| 338 |
const gchar *cmdt; |
| 339 |
const gchar *cmdt2; |
| 340 |
guint i, j; |
| 341 |
gchar buffer_bin[PK_MAX_PATH_LEN+1]; |
| 342 |
gchar buffer_sbin[PK_MAX_PATH_LEN+1]; |
| 343 |
gboolean ret; |
| 344 |
g_autoptr(GPtrArray) possible = NULL; |
| 345 |
g_autoptr(GPtrArray) unique = NULL; |
| 346 |
|
| 347 |
array = g_ptr_array_new_with_free_func (g_free); |
| 348 |
possible = g_ptr_array_new_with_free_func (g_free); |
| 349 |
unique = g_ptr_array_new (); |
| 350 |
pk_cnf_find_alternatives_swizzle (cmd, len, possible); |
| 351 |
pk_cnf_find_alternatives_replace (cmd, len, possible); |
| 352 |
if (len > 3) |
| 353 |
pk_cnf_find_alternatives_truncate (cmd, len, possible); |
| 354 |
pk_cnf_find_alternatives_remove_double (cmd, len, possible); |
| 355 |
pk_cnf_find_alternatives_case (cmd, len, possible); |
| 356 |
pk_cnf_find_alternatives_locale (cmd, len, possible); |
| 357 |
pk_cnf_find_alternatives_solaris (cmd, len, possible); |
| 358 |
|
| 359 |
/* remove duplicates using a helper array */ |
| 360 |
for (i = 0; i < possible->len; i++) { |
| 361 |
cmdt = g_ptr_array_index (possible, i); |
| 362 |
ret = TRUE; |
| 363 |
for (j=0; j<unique->len; j++) { |
| 364 |
cmdt2 = g_ptr_array_index (unique, j); |
| 365 |
if (strcmp (cmdt, cmdt2) == 0) { |
| 366 |
ret = FALSE; |
| 367 |
break; |
| 368 |
} |
| 369 |
} |
| 370 |
/* only add if not duplicate */ |
| 371 |
if (ret) |
| 372 |
g_ptr_array_add (unique, (gpointer) cmdt); |
| 373 |
} |
| 374 |
|
| 375 |
/* ITS4: ignore, source is constant size */ |
| 376 |
strncpy (buffer_bin, "/usr/bin/", PK_MAX_PATH_LEN); |
| 377 |
|
| 378 |
/* ITS4: ignore, source is constant size */ |
| 379 |
strncpy (buffer_sbin, "/usr/sbin/", PK_MAX_PATH_LEN); |
| 380 |
|
| 381 |
/* remove any that exist (fast path) */ |
| 382 |
for (i = 0; i < unique->len; i++) { |
| 383 |
cmdt = g_ptr_array_index (unique, i); |
| 384 |
|
| 385 |
/* ITS4: ignore, size is checked */ |
| 386 |
strncpy (&buffer_bin[9], cmdt, PK_MAX_PATH_LEN-9); |
| 387 |
|
| 388 |
/* ITS4: ignore, size is checked */ |
| 389 |
strncpy (&buffer_sbin[10], cmdt, PK_MAX_PATH_LEN-10); |
| 390 |
|
| 391 |
/* does file exist in bindir (common case) */ |
| 392 |
ret = g_file_test (buffer_bin, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_EXECUTABLE); |
| 393 |
if (ret) { |
| 394 |
g_ptr_array_add (array, g_strdup (cmdt)); |
| 395 |
continue; |
| 396 |
} |
| 397 |
|
| 398 |
/* does file exist in sbindir */ |
| 399 |
ret = g_file_test (buffer_sbin, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_EXECUTABLE); |
| 400 |
if (ret) |
| 401 |
g_ptr_array_add (array, g_strdup (cmdt)); |
| 402 |
} |
| 403 |
return array; |
| 404 |
} |
| 405 |
|
| 406 |
static void |
| 407 |
pk_cnf_progress_cb (PkProgress *progress, PkProgressType type, gpointer data) |
| 408 |
{ |
| 409 |
PkStatusEnum status; |
| 410 |
const gchar *text = NULL; |
| 411 |
|
| 412 |
/* status */ |
| 413 |
if (type != PK_PROGRESS_TYPE_STATUS) |
| 414 |
return; |
| 415 |
|
| 416 |
g_object_get (progress, |
| 417 |
"status", &status, |
| 418 |
NULL); |
| 419 |
|
| 420 |
switch (status) { |
| 421 |
case PK_STATUS_ENUM_SETUP: |
| 422 |
case PK_STATUS_ENUM_FINISHED: |
| 423 |
case PK_STATUS_ENUM_QUERY: |
| 424 |
break; |
| 425 |
case PK_STATUS_ENUM_DOWNLOAD_REPOSITORY: |
| 426 |
/* TRANSLATORS: downloading repo data so we can search */ |
| 427 |
text = _("Downloading details about the software repositories."); |
| 428 |
break; |
| 429 |
case PK_STATUS_ENUM_DOWNLOAD_FILELIST: |
| 430 |
/* TRANSLATORS: downloading file lists so we can search */ |
| 431 |
text = _("Downloading filelists (this may take some time to complete)."); |
| 432 |
break; |
| 433 |
case PK_STATUS_ENUM_WAITING_FOR_LOCK: |
| 434 |
/* TRANSLATORS: waiting for native lock */ |
| 435 |
text = _("Waiting for package manager lock."); |
| 436 |
break; |
| 437 |
case PK_STATUS_ENUM_LOADING_CACHE: |
| 438 |
/* TRANSLATORS: loading package cache so we can search */ |
| 439 |
text = _("Loading list of packages."); |
| 440 |
break; |
| 441 |
default: |
| 442 |
/* fallback to default */ |
| 443 |
text = pk_status_enum_to_localised_text (status); |
| 444 |
} |
| 445 |
|
| 446 |
/* print to screen, still one line */ |
| 447 |
if (text != NULL) |
| 448 |
g_print ("\n * %s... ", text); |
| 449 |
} |
| 450 |
|
| 451 |
static gboolean |
| 452 |
pk_cnf_cancel_cb (GCancellable *_cancellable) |
| 453 |
{ |
| 454 |
g_warning ("Cancelling request"); |
| 455 |
g_cancellable_cancel (cancellable); |
| 456 |
return FALSE; |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Find software we could install |
| 461 |
**/ |
| 462 |
static gchar ** |
| 463 |
pk_cnf_find_available (const gchar *cmd, guint max_search_time) |
| 464 |
{ |
| 465 |
PkPackage *item; |
| 466 |
gchar **package_ids = NULL; |
| 467 |
const gchar *prefixes[] = {"/usr/bin", "/usr/sbin", "/bin", "/sbin", NULL}; |
| 468 |
gchar **values = NULL; |
| 469 |
g_autoptr(GError) error = NULL; |
| 470 |
GPtrArray *array = NULL; |
| 471 |
guint i; |
| 472 |
guint len; |
| 473 |
PkBitfield filters; |
| 474 |
PkResults *results = NULL; |
| 475 |
PkError *error_code = NULL; |
| 476 |
guint cancel_id; |
| 477 |
|
| 478 |
/* create new array of full paths */ |
| 479 |
len = g_strv_length ((gchar **)prefixes); |
| 480 |
values = g_new0 (gchar *, len + 1); |
| 481 |
for (i = 0; prefixes[i] != NULL; i++) |
| 482 |
values[i] = g_build_filename (prefixes[i], cmd, NULL); |
| 483 |
|
| 484 |
/* only allow searching for a limited amount of time */ |
| 485 |
cancel_id = g_timeout_add (max_search_time, |
| 486 |
(GSourceFunc) pk_cnf_cancel_cb, |
| 487 |
cancellable); |
| 488 |
g_source_set_name_by_id (cancel_id, "[PkCommandNotFound] cancel"); |
| 489 |
|
| 490 |
/* do search */ |
| 491 |
filters = pk_bitfield_from_enums (PK_FILTER_ENUM_NOT_INSTALLED, |
| 492 |
PK_FILTER_ENUM_NEWEST, |
| 493 |
PK_FILTER_ENUM_ARCH, -1); |
| 494 |
results = pk_client_search_files (PK_CLIENT(task), filters, values, cancellable, |
| 495 |
NULL, NULL, &error); |
| 496 |
if (results == NULL) { |
| 497 |
if (!g_error_matches (error, PK_CLIENT_ERROR, PK_CLIENT_ERROR_INVALID_INPUT)) { |
| 498 |
/* TRANSLATORS: we failed to find the package, this shouldn't happen */ |
| 499 |
g_printerr ("%s: %s\n", _("Failed to search for file"), error->message); |
| 500 |
} |
| 501 |
goto out; |
| 502 |
} |
| 503 |
|
| 504 |
/* check error code */ |
| 505 |
error_code = pk_results_get_error_code (results); |
| 506 |
if (error_code != NULL) { |
| 507 |
if (pk_error_get_code (error_code) == PK_ERROR_ENUM_TRANSACTION_CANCELLED) { |
| 508 |
g_debug ("The search was cancelled as it was taking too long"); |
| 509 |
} else { |
| 510 |
/* TRANSLATORS: the transaction failed in a way we could not expect */ |
| 511 |
g_printerr ("%s: %s, %s\n", _("Getting the list of files failed"), |
| 512 |
pk_error_enum_to_string (pk_error_get_code (error_code)), |
| 513 |
pk_error_get_details (error_code)); |
| 514 |
goto out; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
/* get the packages returned */ |
| 519 |
array = pk_results_get_package_array (results); |
| 520 |
package_ids = g_new0 (gchar *, array->len+1); |
| 521 |
for (i = 0; i < array->len; i++) { |
| 522 |
item = g_ptr_array_index (array, i); |
| 523 |
package_ids[i] = g_strdup (pk_package_get_id (item)); |
| 524 |
} |
| 525 |
out: |
| 526 |
if (cancel_id > 0) |
| 527 |
g_source_remove (cancel_id); |
| 528 |
if (error_code != NULL) |
| 529 |
g_object_unref (error_code); |
| 530 |
if (results != NULL) |
| 531 |
g_object_unref (results); |
| 532 |
if (array != NULL) |
| 533 |
g_ptr_array_unref (array); |
| 534 |
g_strfreev (values); |
| 535 |
return package_ids; |
| 536 |
} |
| 537 |
|
| 538 |
static PkCnfPolicy |
| 539 |
pk_cnf_get_policy_from_string (const gchar *policy_text) |
| 540 |
{ |
| 541 |
if (policy_text == NULL) |
| 542 |
return PK_CNF_POLICY_UNKNOWN; |
| 543 |
if (strcmp (policy_text, "run") == 0) |
| 544 |
return PK_CNF_POLICY_RUN; |
| 545 |
if (strcmp (policy_text, "ask") == 0) |
| 546 |
return PK_CNF_POLICY_ASK; |
| 547 |
if (strcmp (policy_text, "warn") == 0) |
| 548 |
return PK_CNF_POLICY_WARN; |
| 549 |
return PK_CNF_POLICY_UNKNOWN; |
| 550 |
} |
| 551 |
|
| 552 |
static PkCnfPolicy |
| 553 |
pk_cnf_get_policy_from_file (GKeyFile *file, const gchar *key) |
| 554 |
{ |
| 555 |
PkCnfPolicy policy; |
| 556 |
g_autofree gchar *policy_text = NULL; |
| 557 |
g_autoptr(GError) error = NULL; |
| 558 |
|
| 559 |
/* get from file */ |
| 560 |
policy_text = g_key_file_get_string (file, "CommandNotFound", key, &error); |
| 561 |
if (policy_text == NULL) { |
| 562 |
g_warning ("failed to get key %s: %s", key, error->message); |
| 563 |
} |
| 564 |
|
| 565 |
/* convert to enum */ |
| 566 |
policy = pk_cnf_get_policy_from_string (policy_text); |
| 567 |
return policy; |
| 568 |
} |
| 569 |
|
| 570 |
static PkCnfPolicyConfig * |
| 571 |
pk_cnf_get_config (void) |
| 572 |
{ |
| 573 |
GKeyFile *file; |
| 574 |
gboolean ret; |
| 575 |
g_autofree gchar *path = NULL; |
| 576 |
g_autoptr(GError) error = NULL; |
| 577 |
PkCnfPolicyConfig *config; |
| 578 |
|
| 579 |
/* create */ |
| 580 |
config = g_new0 (PkCnfPolicyConfig, 1); |
| 581 |
|
| 582 |
/* set defaults if the conf file is not found */ |
| 583 |
config->single_match = PK_CNF_POLICY_UNKNOWN; |
| 584 |
config->multiple_match = PK_CNF_POLICY_UNKNOWN; |
| 585 |
config->single_install = PK_CNF_POLICY_UNKNOWN; |
| 586 |
config->multiple_install = PK_CNF_POLICY_UNKNOWN; |
| 587 |
config->software_source_search = TRUE; |
| 588 |
config->similar_name_search = TRUE; |
| 589 |
config->locations = NULL; |
| 590 |
config->max_search_time = 5000; |
| 591 |
|
| 592 |
/* load file */ |
| 593 |
file = g_key_file_new (); |
| 594 |
path = g_build_filename (SYSCONFDIR, "PackageKit", "CommandNotFound.conf", NULL); |
| 595 |
ret = g_key_file_load_from_file (file, path, G_KEY_FILE_NONE, &error); |
| 596 |
if (!ret) { |
| 597 |
g_printerr ("failed to load config file: %s\n", error->message); |
| 598 |
goto out; |
| 599 |
} |
| 600 |
|
| 601 |
/* get data */ |
| 602 |
config->single_match = pk_cnf_get_policy_from_file (file, "SingleMatch"); |
| 603 |
config->multiple_match = pk_cnf_get_policy_from_file (file, "MultipleMatch"); |
| 604 |
config->single_install = pk_cnf_get_policy_from_file (file, "SingleInstall"); |
| 605 |
config->multiple_install = pk_cnf_get_policy_from_file (file, "MultipleInstall"); |
| 606 |
config->software_source_search = g_key_file_get_boolean (file, "CommandNotFound", "SoftwareSourceSearch", NULL); |
| 607 |
config->similar_name_search = g_key_file_get_boolean (file, "CommandNotFound", "SimilarNameSearch", NULL); |
| 608 |
config->locations = g_key_file_get_string_list (file, "CommandNotFound", "SearchLocations", NULL, NULL); |
| 609 |
config->max_search_time = g_key_file_get_integer (file, "CommandNotFound", "MaxSearchTime", NULL); |
| 610 |
|
| 611 |
/* fallback */ |
| 612 |
if (config->locations == NULL) { |
| 613 |
g_warning ("not found SearchLocations, using fallback"); |
| 614 |
config->locations = g_strsplit ("/usr/bin;/usr/sbin", ";", -1); |
| 615 |
} |
| 616 |
if (config->max_search_time == 0) { |
| 617 |
g_warning ("not found MaxSearchTime, using fallback"); |
| 618 |
config->max_search_time = 2000; |
| 619 |
} |
| 620 |
out: |
| 621 |
g_key_file_free (file); |
| 622 |
return config; |
| 623 |
} |
| 624 |
|
| 625 |
static gint |
| 626 |
pk_cnf_spawn_command (const gchar *exec, gchar **arguments, guint arguments_count) |
| 627 |
{ |
| 628 |
g_autofree gchar **argv = NULL; |
| 629 |
gint exit_status = EXIT_FAILURE; |
| 630 |
g_autoptr(GError) error = NULL; |
| 631 |
|
| 632 |
/* ensure program starts on a fresh line */ |
| 633 |
g_print ("\n"); |
| 634 |
|
| 635 |
argv = g_new0 (gchar *, arguments_count + 2); |
| 636 |
argv[0] = (gchar*) exec; |
| 637 |
for (guint i = 0; i <= arguments_count; i++) { |
| 638 |
argv[1+i] = arguments[i]; |
| 639 |
} |
| 640 |
if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_CHILD_INHERITS_STDIN, NULL, NULL, |
| 641 |
NULL, NULL, &exit_status, &error)) { |
| 642 |
/* TRANSLATORS: we failed to launch the executable, the error follows */ |
| 643 |
g_printerr ("%s '%s': %s\n", _("Failed to launch:"), exec, error->message); |
| 644 |
} |
| 645 |
return exit_status; |
| 646 |
} |
| 647 |
|
| 648 |
static gboolean |
| 649 |
pk_cnf_install_package_id (const gchar *package_id) |
| 650 |
{ |
| 651 |
g_autoptr(GError) error = NULL; |
| 652 |
g_autoptr(PkError) error_code = NULL; |
| 653 |
g_autoptr(PkResults) results = NULL; |
| 654 |
g_auto(GStrv) package_ids = NULL; |
| 655 |
|
| 656 |
/* do install */ |
| 657 |
package_ids = pk_package_ids_from_id (package_id); |
| 658 |
results = pk_task_install_packages_sync (task, package_ids, cancellable, |
| 659 |
(PkProgressCallback) pk_cnf_progress_cb, NULL, &error); |
| 660 |
if (results == NULL) { |
| 661 |
/* TRANSLATORS: we failed to install the package */ |
| 662 |
g_printerr ("%s: %s\n", _("Failed to install packages"), error->message); |
| 663 |
return FALSE; |
| 664 |
} |
| 665 |
|
| 666 |
/* check error code */ |
| 667 |
error_code = pk_results_get_error_code (results); |
| 668 |
if (error_code != NULL) { |
| 669 |
/* TRANSLATORS: the transaction failed in a way we could not expect */ |
| 670 |
g_printerr ("%s: %s, %s\n", _("The transaction failed"), pk_error_enum_to_string (pk_error_get_code (error_code)), pk_error_get_details (error_code)); |
| 671 |
return FALSE; |
| 672 |
} |
| 673 |
return TRUE; |
| 674 |
} |
| 675 |
|
| 676 |
static void |
| 677 |
pk_cnf_sigint_handler (int sig) |
| 678 |
{ |
| 679 |
g_debug ("Handling SIGINT"); |
| 680 |
|
| 681 |
/* restore default ASAP, as the cancel might hang */ |
| 682 |
signal (SIGINT, SIG_DFL); |
| 683 |
|
| 684 |
/* hopefully, cancel client */ |
| 685 |
g_cancellable_cancel (cancellable); |
| 686 |
|
| 687 |
/* kill ourselves */ |
| 688 |
g_debug ("Retrying SIGINT"); |
| 689 |
kill (getpid (), SIGINT); |
| 690 |
} |
| 691 |
|
| 692 |
static gboolean |
| 693 |
pk_cnf_is_backend_fast_enough_to_do_search (void) |
| 694 |
{ |
| 695 |
gboolean ret = FALSE; |
| 696 |
gchar *backend; |
| 697 |
GError *error = NULL; |
| 698 |
PkControl *control = NULL; |
| 699 |
|
| 700 |
/* Initialize PkControl */ |
| 701 |
control = pk_control_new (); |
| 702 |
ret = pk_control_get_properties (control, cancellable, &error); |
| 703 |
if (!ret) { |
| 704 |
/* failed to contact the daemon */ |
| 705 |
g_error_free (error); |
| 706 |
goto out; |
| 707 |
} |
| 708 |
|
| 709 |
/* Find backend name */ |
| 710 |
g_object_get (control, "backend-name", &backend, NULL); |
| 711 |
|
| 712 |
/* Current list of too slow backends */ |
| 713 |
if (g_strcmp0 (backend, "yum") == 0) { |
| 714 |
ret = FALSE; |
| 715 |
goto out; |
| 716 |
} |
| 717 |
out: |
| 718 |
if (control != NULL) |
| 719 |
g_object_unref(control); |
| 720 |
return ret; |
| 721 |
} |
| 722 |
|
| 723 |
int |
| 724 |
main (int argc, char *argv[]) |
| 725 |
{ |
| 726 |
gboolean ret; |
| 727 |
PkCnfPolicyConfig *config = NULL; |
| 728 |
guint i; |
| 729 |
guint len; |
| 730 |
gchar *text; |
| 731 |
const gchar *possible; |
| 732 |
gchar **parts; |
| 733 |
guint retval = EXIT_COMMAND_NOT_FOUND; |
| 734 |
const gchar *shell = "bash"; |
| 735 |
const gchar *env_shell; |
| 736 |
g_autofree gchar *shell_to_free = NULL; |
| 737 |
g_autoptr(GPtrArray) array = NULL; |
| 738 |
g_auto(GStrv) package_ids = NULL; |
| 739 |
|
| 740 |
setlocale (LC_ALL, ""); |
| 741 |
bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR); |
| 742 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 743 |
textdomain (GETTEXT_PACKAGE); |
| 744 |
|
| 745 |
/* don't show debugging, unless VERBOSE is specified */ |
| 746 |
pk_debug_add_log_domain (G_LOG_DOMAIN); |
| 747 |
|
| 748 |
/* no input */ |
| 749 |
if (argv[1] == NULL) |
| 750 |
goto out; |
| 751 |
|
| 752 |
/* do stuff on ctrl-c */ |
| 753 |
signal (SIGINT, pk_cnf_sigint_handler); |
| 754 |
|
| 755 |
/* get policy config */ |
| 756 |
config = pk_cnf_get_config (); |
| 757 |
task = PK_TASK(pk_task_text_new ()); |
| 758 |
g_object_set (task, |
| 759 |
"cache-age", G_MAXUINT, |
| 760 |
"interactive", FALSE, |
| 761 |
"background", FALSE, |
| 762 |
NULL); |
| 763 |
cancellable = g_cancellable_new (); |
| 764 |
|
| 765 |
/* get length */ |
| 766 |
len = strlen (argv[1]); |
| 767 |
if (len < 1) |
| 768 |
goto out; |
| 769 |
|
| 770 |
/* invalid */ |
| 771 |
if (g_strcmp0 (argv[1], ".") == 0 || g_strcmp0 (argv[1], "..") == 0) |
| 772 |
goto out; |
| 773 |
|
| 774 |
env_shell = g_getenv ("SHELL"); |
| 775 |
if (env_shell != NULL) |
| 776 |
shell = shell_to_free = g_path_get_basename (env_shell); |
| 777 |
|
| 778 |
/* TRANSLATORS: the prefix of all the output telling the user |
| 779 |
* why it's not executing. NOTE: this is lowercase to mimic |
| 780 |
* the style of bash itself -- apologies */ |
| 781 |
g_printerr ("%s: %s: %s...\n", shell, argv[1], _("command not found")); |
| 782 |
|
| 783 |
/* ignore one char mistakes */ |
| 784 |
if (len < 2) |
| 785 |
goto out; |
| 786 |
|
| 787 |
/* user is not allowing CNF to do anything useful */ |
| 788 |
if (!config->software_source_search && |
| 789 |
!config->similar_name_search) { |
| 790 |
goto out; |
| 791 |
} |
| 792 |
|
| 793 |
/* generate swizzles */ |
| 794 |
if (config->similar_name_search) |
| 795 |
array = pk_cnf_find_alternatives (argv[1], len); |
| 796 |
|
| 797 |
/* one exact possibility */ |
| 798 |
if (array != NULL && array->len == 1) { |
| 799 |
possible = g_ptr_array_index (array, 0); |
| 800 |
if (config->single_match == PK_CNF_POLICY_WARN) { |
| 801 |
/* TRANSLATORS: tell the user what we think the command is */ |
| 802 |
g_printerr ("%s '%s'\n", _("Similar command is:"), possible); |
| 803 |
goto out; |
| 804 |
} |
| 805 |
|
| 806 |
/* run */ |
| 807 |
if (config->single_match == PK_CNF_POLICY_RUN) { |
| 808 |
retval = pk_cnf_spawn_command (possible, &argv[2], argc - 2); |
| 809 |
goto out; |
| 810 |
} |
| 811 |
|
| 812 |
/* ask */ |
| 813 |
if (config->single_match == PK_CNF_POLICY_ASK) { |
| 814 |
/* TRANSLATORS: Ask the user if we should run the similar command */ |
| 815 |
text = g_strdup_printf ("%s %s", _("Run similar command:"), possible); |
| 816 |
ret = pk_console_get_prompt (text, TRUE); |
| 817 |
if (ret) |
| 818 |
retval = pk_cnf_spawn_command (possible, &argv[2], argc - 2); |
| 819 |
g_free (text); |
| 820 |
} |
| 821 |
goto out; |
| 822 |
|
| 823 |
/* multiple choice */ |
| 824 |
} else if (array != NULL && array->len > 1) { |
| 825 |
if (config->multiple_match == PK_CNF_POLICY_WARN) { |
| 826 |
/* TRANSLATORS: show the user a list of commands that they could have meant */ |
| 827 |
g_printerr ("%s:\n", _("Similar commands are:")); |
| 828 |
for (i = 0; i < array->len; i++) { |
| 829 |
possible = g_ptr_array_index (array, i); |
| 830 |
g_printerr ("'%s'\n", possible); |
| 831 |
} |
| 832 |
|
| 833 |
/* ask */ |
| 834 |
} else if (config->multiple_match == PK_CNF_POLICY_ASK) { |
| 835 |
/* TRANSLATORS: show the user a list of commands we could run */ |
| 836 |
g_printerr ("%s:\n", _("Similar commands are:")); |
| 837 |
for (i = 0; i < array->len; i++) { |
| 838 |
possible = g_ptr_array_index (array, i); |
| 839 |
g_printerr ("%i\t'%s'\n", i+1, possible); |
| 840 |
} |
| 841 |
|
| 842 |
/* TRANSLATORS: ask the user to choose a file to run */ |
| 843 |
i = pk_console_get_number (_("Please choose a command to run"), array->len); |
| 844 |
g_assert (i < array->len); |
| 845 |
|
| 846 |
/* run command */ |
| 847 |
possible = g_ptr_array_index (array, i); |
| 848 |
retval = pk_cnf_spawn_command (possible, &argv[2], argc - 2); |
| 849 |
} |
| 850 |
goto out; |
| 851 |
|
| 852 |
/* only search using PackageKit if configured to do so */ |
| 853 |
} else if (config->software_source_search && |
| 854 |
pk_cnf_is_backend_fast_enough_to_do_search ()) { |
| 855 |
package_ids = pk_cnf_find_available (argv[1], config->max_search_time); |
| 856 |
if (package_ids == NULL) |
| 857 |
goto out; |
| 858 |
len = g_strv_length (package_ids); |
| 859 |
if (len == 1) { |
| 860 |
parts = pk_package_id_split (package_ids[0]); |
| 861 |
if (config->single_install == PK_CNF_POLICY_WARN) { |
| 862 |
/* TRANSLATORS: tell the user what package provides the command */ |
| 863 |
g_printerr ("%s '%s'\n", _("The package providing this file is:"), parts[PK_PACKAGE_ID_NAME]); |
| 864 |
goto out; |
| 865 |
} |
| 866 |
|
| 867 |
/* ask */ |
| 868 |
if (config->single_install == PK_CNF_POLICY_ASK) { |
| 869 |
/* TRANSLATORS: as the user if we want to install a package to provide the command */ |
| 870 |
text = g_strdup_printf (_("Install package '%s' to provide command '%s'?"), parts[PK_PACKAGE_ID_NAME], argv[1]); |
| 871 |
ret = pk_console_get_prompt (text, FALSE); |
| 872 |
g_free (text); |
| 873 |
if (ret) { |
| 874 |
ret = pk_cnf_install_package_id (package_ids[0]); |
| 875 |
if (ret) |
| 876 |
retval = pk_cnf_spawn_command (argv[1], &argv[2], argc - 2); |
| 877 |
} |
| 878 |
g_print ("\n"); |
| 879 |
goto out; |
| 880 |
} |
| 881 |
|
| 882 |
/* install */ |
| 883 |
if (config->single_install == PK_CNF_POLICY_INSTALL) { |
| 884 |
ret = pk_cnf_install_package_id (package_ids[0]); |
| 885 |
if (ret) |
| 886 |
retval = pk_cnf_spawn_command (argv[1], &argv[2], argc - 2); |
| 887 |
} |
| 888 |
g_strfreev (parts); |
| 889 |
goto out; |
| 890 |
} else if (len > 1) { |
| 891 |
if (config->multiple_install == PK_CNF_POLICY_WARN) { |
| 892 |
/* TRANSLATORS: Show the user a list of packages that provide this command */ |
| 893 |
g_printerr ("%s\n", _("Packages providing this file are:")); |
| 894 |
for (i = 0; package_ids[i] != NULL; i++) { |
| 895 |
parts = pk_package_id_split (package_ids[i]); |
| 896 |
g_printerr ("'%s'\n", parts[PK_PACKAGE_ID_NAME]); |
| 897 |
g_strfreev (parts); |
| 898 |
} |
| 899 |
|
| 900 |
/* ask */ |
| 901 |
} else if (config->multiple_install == PK_CNF_POLICY_ASK) { |
| 902 |
/* TRANSLATORS: Show the user a list of packages that they can install to provide this command */ |
| 903 |
g_printerr ("%s:\n", _("Suitable packages are:")); |
| 904 |
for (i = 0; package_ids[i] != NULL; i++) { |
| 905 |
parts = pk_package_id_split (package_ids[i]); |
| 906 |
g_printerr ("%i\t'%s'\n", i+1, parts[PK_PACKAGE_ID_NAME]); |
| 907 |
g_strfreev (parts); |
| 908 |
} |
| 909 |
|
| 910 |
/* TRANSLATORS: ask the user to choose a file to install */ |
| 911 |
i = pk_console_get_number (_("Please choose a package to install"), len); |
| 912 |
if (i == 0) { |
| 913 |
g_printerr ("%s\n", _("User aborted selection")); |
| 914 |
goto out; |
| 915 |
} |
| 916 |
g_assert (i < len); |
| 917 |
|
| 918 |
/* run command */ |
| 919 |
ret = pk_cnf_install_package_id (package_ids[i - 1]); |
| 920 |
if (ret) |
| 921 |
retval = pk_cnf_spawn_command (argv[1], &argv[2], argc - 2); |
| 922 |
} |
| 923 |
goto out; |
| 924 |
} |
| 925 |
} |
| 926 |
out: |
| 927 |
if (task != NULL) |
| 928 |
g_object_unref (task); |
| 929 |
if (cancellable != NULL) |
| 930 |
g_object_unref (cancellable); |
| 931 |
if (config != NULL) { |
| 932 |
g_strfreev (config->locations); |
| 933 |
g_free (config); |
| 934 |
} |
| 935 |
return retval; |
| 936 |
} |
| 937 |
|