update-alternatives.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827
  1. /*
  2. * update-alternatives
  3. *
  4. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  5. * Copyright © 2000-2002 Wichert Akkerman <wakkerma@debian.org>
  6. * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
  8. * Copyright © 2009-2010 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <stdarg.h>
  30. #include <stdbool.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <dirent.h>
  36. #include <time.h>
  37. #include <setjmp.h>
  38. #include <assert.h>
  39. #include <locale.h>
  40. #include <ctype.h>
  41. #include <limits.h>
  42. #include <dpkg/macros.h>
  43. #include <dpkg/i18n.h>
  44. /* Global variables: */
  45. #define PROGNAME "update-alternatives"
  46. static const char *altdir = SYSCONFDIR "/alternatives";
  47. static const char *admdir;
  48. static const char *prog_path = "update-alternatives";
  49. /* Action to perform */
  50. static const char *action = NULL;
  51. static const char *log_file = LOGDIR "/alternatives.log";
  52. /* Skip alternatives properly configured in auto mode (for --config) */
  53. static int opt_skip_auto = 0;
  54. static int opt_verbose = 0;
  55. static int opt_force = 0;
  56. /*
  57. * Functions.
  58. */
  59. static void
  60. version(void)
  61. {
  62. printf(_("Debian %s version %s.\n"), PROGNAME, VERSION);
  63. printf("\n");
  64. printf(_(
  65. "This is free software; see the GNU General Public License version 2 or\n"
  66. "later for copying conditions. There is NO warranty.\n"));
  67. }
  68. static void
  69. usage(void)
  70. {
  71. printf(_(
  72. "Usage: %s [<option> ...] <command>\n"
  73. "\n"), PROGNAME);
  74. printf(_(
  75. "Commands:\n"
  76. " --install <link> <name> <path> <priority>\n"
  77. " [--slave <link> <name> <path>] ...\n"
  78. " add a group of alternatives to the system.\n"
  79. " --remove <name> <path> remove <path> from the <name> group alternative.\n"
  80. " --remove-all <name> remove <name> group from the alternatives system.\n"
  81. " --auto <name> switch the master link <name> to automatic mode.\n"
  82. " --display <name> display information about the <name> group.\n"
  83. " --query <name> machine parseable version of --display <name>.\n"
  84. " --list <name> display all targets of the <name> group.\n"
  85. " --get-selections list master alternative names and their status.\n"
  86. " --set-selections read alternative status from standard input.\n"
  87. " --config <name> show alternatives for the <name> group and ask the\n"
  88. " user to select which one to use.\n"
  89. " --set <name> <path> set <path> as alternative for <name>.\n"
  90. " --all call --config on all alternatives.\n"
  91. "\n"));
  92. printf(_(
  93. "<link> is the symlink pointing to %s/<name>.\n"
  94. " (e.g. /usr/bin/pager)\n"
  95. "<name> is the master name for this link group.\n"
  96. " (e.g. pager)\n"
  97. "<path> is the location of one of the alternative target files.\n"
  98. " (e.g. /usr/bin/less)\n"
  99. "<priority> is an integer; options with higher numbers have higher priority in\n"
  100. " automatic mode.\n"
  101. "\n"), altdir);
  102. printf(_(
  103. "Options:\n"
  104. " --altdir <directory> change the alternatives directory.\n"
  105. " --admindir <directory> change the administrative directory.\n"
  106. " --log <file> change the log file.\n"
  107. " --force allow replacing files with alternative links.\n"
  108. " --skip-auto skip prompt for alternatives correctly configured\n"
  109. " in automatic mode (relevant for --config only)\n"
  110. " --verbose verbose operation, more output.\n"
  111. " --quiet quiet operation, minimal output.\n"
  112. " --help show this help message.\n"
  113. " --version show the version.\n"
  114. ));
  115. }
  116. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1)
  117. error(char const *fmt, ...)
  118. {
  119. va_list args;
  120. fprintf(stderr, "%s: %s: ", PROGNAME, _("error"));
  121. va_start(args, fmt);
  122. vfprintf(stderr, fmt, args);
  123. va_end(args);
  124. fprintf(stderr, "\n");
  125. exit(2);
  126. }
  127. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1)
  128. syserr(char const *fmt, ...)
  129. {
  130. va_list args;
  131. fprintf(stderr, "%s: %s: ", PROGNAME, _("error"));
  132. va_start(args, fmt);
  133. vfprintf(stderr, fmt, args);
  134. va_end(args);
  135. fprintf(stderr, ": %s\n", strerror(errno));
  136. exit(2);
  137. }
  138. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1)
  139. badusage(char const *fmt, ...)
  140. {
  141. va_list args;
  142. fprintf(stderr, "%s: ", PROGNAME);
  143. va_start(args, fmt);
  144. vfprintf(stderr, fmt, args);
  145. va_end(args);
  146. fprintf(stderr, "\n\n");
  147. fprintf(stderr, _("Use '%s --help' for program usage information."),
  148. PROGNAME);
  149. fprintf(stderr, "\n");
  150. exit(2);
  151. }
  152. static void DPKG_ATTR_PRINTF(1)
  153. warning(char const *fmt, ...)
  154. {
  155. va_list args;
  156. if (opt_verbose < 0)
  157. return;
  158. fprintf(stderr, "%s: %s: ", PROGNAME, _("warning"));
  159. va_start(args, fmt);
  160. vfprintf(stderr, fmt, args);
  161. va_end(args);
  162. fprintf(stderr, "\n");
  163. }
  164. static void DPKG_ATTR_PRINTF(1)
  165. debug(char const *fmt, ...)
  166. {
  167. #if 0
  168. va_list args;
  169. fprintf(stderr, "DEBUG: ");
  170. va_start(args, fmt);
  171. vfprintf(stderr, fmt, args);
  172. va_end(args);
  173. fprintf(stderr, "\n");
  174. #endif
  175. }
  176. static void DPKG_ATTR_PRINTF(1)
  177. verbose(char const *fmt, ...)
  178. {
  179. va_list args;
  180. if (opt_verbose < 1)
  181. return;
  182. printf("%s: ", PROGNAME);
  183. va_start(args, fmt);
  184. vprintf(fmt, args);
  185. va_end(args);
  186. printf("\n");
  187. }
  188. static void DPKG_ATTR_PRINTF(1)
  189. info(char const *fmt, ...)
  190. {
  191. va_list args;
  192. if (opt_verbose < 0)
  193. return;
  194. printf("%s: ", PROGNAME);
  195. va_start(args, fmt);
  196. vprintf(fmt, args);
  197. va_end(args);
  198. printf("\n");
  199. }
  200. static void DPKG_ATTR_PRINTF(1)
  201. pr(char const *fmt, ...)
  202. {
  203. va_list args;
  204. va_start(args, fmt);
  205. vprintf(fmt, args);
  206. va_end(args);
  207. printf("\n");
  208. }
  209. static void *
  210. xmalloc(size_t size)
  211. {
  212. void *ptr;
  213. ptr = malloc(size);
  214. if (!ptr)
  215. error(_("malloc failed (%zu bytes)"), size);
  216. return ptr;
  217. }
  218. static char *
  219. xstrdup(const char *str)
  220. {
  221. char *new_str;
  222. if (!str)
  223. return NULL;
  224. new_str = strdup(str);
  225. if (!new_str)
  226. error(_("failed to allocate memory"));
  227. return new_str;
  228. }
  229. static char *
  230. areadlink(const char *linkname)
  231. {
  232. struct stat st;
  233. char *buf;
  234. ssize_t size;
  235. /* Allocate required memory to store the value of the symlink */
  236. if (lstat(linkname, &st))
  237. return NULL;
  238. if (!S_ISLNK(st.st_mode)) {
  239. errno = EINVAL;
  240. return NULL;
  241. }
  242. buf = xmalloc(st.st_size + 1);
  243. /* Read it and terminate the string properly */
  244. size = readlink(linkname, buf, st.st_size);
  245. if (size == -1) {
  246. int saved_errno = errno;
  247. free(buf);
  248. errno = saved_errno;
  249. return NULL;
  250. }
  251. buf[size] = '\0';
  252. return buf;
  253. }
  254. static char *
  255. xreadlink(const char *linkname)
  256. {
  257. char *buf;
  258. buf = areadlink(linkname);
  259. if (buf == NULL)
  260. syserr(_("unable to read link '%.255s'"), linkname);
  261. return buf;
  262. }
  263. static char * DPKG_ATTR_VPRINTF(1)
  264. xvasprintf(const char *fmt, va_list args)
  265. {
  266. char *str;
  267. if (vasprintf(&str, fmt, args) < 0)
  268. error(_("failed to allocate memory"));
  269. return str;
  270. }
  271. static char * DPKG_ATTR_PRINTF(1)
  272. xasprintf(const char *fmt, ...)
  273. {
  274. va_list args;
  275. char *str;
  276. va_start(args, fmt);
  277. str = xvasprintf(fmt, args);
  278. va_end(args);
  279. return str;
  280. }
  281. static void
  282. set_action(const char *new_action)
  283. {
  284. if (action)
  285. badusage(_("two commands specified: --%s and --%s"),
  286. action, new_action);
  287. action = new_action;
  288. }
  289. static const char *
  290. admindir_init(void)
  291. {
  292. const char *basedir, *basedir_env;
  293. /* Try to get the admindir from an environment variable, usually set
  294. * by the system package manager. */
  295. basedir_env = getenv(ADMINDIR_ENVVAR);
  296. if (basedir_env)
  297. basedir = basedir_env;
  298. else
  299. basedir = ADMINDIR;
  300. return xasprintf("%s/%s", basedir, "alternatives");
  301. }
  302. static FILE *fh_log = NULL;
  303. static void DPKG_ATTR_PRINTF(1)
  304. log_msg(const char *fmt, ...)
  305. {
  306. va_list args;
  307. if (fh_log == NULL) {
  308. fh_log = fopen(log_file, "a");
  309. if (fh_log == NULL && errno != EACCES)
  310. syserr(_("cannot append to '%s'"), log_file);
  311. }
  312. if (fh_log) {
  313. char timestamp[64];
  314. time_t now;
  315. time(&now);
  316. strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S",
  317. localtime(&now));
  318. fprintf(fh_log, "%s %s: ", PROGNAME, timestamp);
  319. va_start(args, fmt);
  320. vfprintf(fh_log, fmt, args);
  321. va_end(args);
  322. fprintf(fh_log, "\n");
  323. }
  324. }
  325. static int
  326. spawn(const char *prog, const char *args[])
  327. {
  328. pid_t pid, dead_pid;
  329. int status;
  330. pid = fork();
  331. if (pid == -1)
  332. error(_("fork failed"));
  333. if (pid == 0) {
  334. execvp(prog, (char *const *)args);
  335. syserr(_("unable to execute %s (%s)"), prog, prog);
  336. }
  337. while ((dead_pid = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
  338. if (dead_pid != pid)
  339. error(_("wait for subprocess %s failed"), prog);
  340. return status;
  341. }
  342. static bool
  343. rename_mv(const char *src, const char *dst)
  344. {
  345. const char *args[] = { "mv", src, dst, NULL };
  346. int rc;
  347. if (rename(src, dst) == 0)
  348. return true;
  349. if (errno == ENOENT)
  350. return false;
  351. rc = spawn("mv", args);
  352. if (WIFEXITED(rc) && WEXITSTATUS(rc) == 0)
  353. return true;
  354. return false;
  355. }
  356. static void
  357. checked_symlink(const char *filename, const char *linkname)
  358. {
  359. if (symlink(filename, linkname))
  360. syserr(_("error creating symbolic link '%.255s'"), linkname);
  361. }
  362. static void
  363. checked_mv(const char *src, const char *dst)
  364. {
  365. if (!rename_mv(src, dst))
  366. syserr(_("unable to install '%.250s' as '%.250s'"), src, dst);
  367. }
  368. static void
  369. checked_rm(const char *f)
  370. {
  371. if (!unlink(f))
  372. return;
  373. if (errno != ENOENT)
  374. syserr(_("unable to remove '%s'"), f);
  375. }
  376. static void DPKG_ATTR_PRINTF(1)
  377. checked_rm_args(const char *fmt, ...)
  378. {
  379. va_list args;
  380. char *path;
  381. va_start(args, fmt);
  382. path = xvasprintf(fmt, args);
  383. va_end(args);
  384. checked_rm(path);
  385. free(path);
  386. }
  387. /*
  388. * OBJECTS
  389. */
  390. struct fileset {
  391. struct fileset *next;
  392. char *master_file;
  393. int priority;
  394. struct slave_file {
  395. struct slave_file *next;
  396. char *name;
  397. char *file;
  398. } *slaves;
  399. };
  400. static struct fileset *
  401. fileset_new(const char *master_file, int prio)
  402. {
  403. struct fileset *fs;
  404. fs = xmalloc(sizeof(*fs));
  405. fs->next = NULL;
  406. fs->master_file = xstrdup(master_file);
  407. fs->priority = prio;
  408. fs->slaves = NULL;
  409. return fs;
  410. }
  411. static void
  412. fileset_free(struct fileset *fs)
  413. {
  414. struct slave_file *slave, *next;
  415. free(fs->master_file);
  416. for (slave = fs->slaves; slave; slave = next) {
  417. next = slave->next;
  418. free(slave->name);
  419. free(slave->file);
  420. free(slave);
  421. }
  422. free(fs);
  423. }
  424. static void
  425. fileset_add_slave(struct fileset *fs, const char *name, const char *file)
  426. {
  427. struct slave_file *sl, *cur, *prev = NULL;
  428. /* Replace existing first */
  429. for (cur = fs->slaves; cur; cur = cur->next) {
  430. if (strcmp(cur->name, name) == 0) {
  431. free(cur->file);
  432. cur->file = xstrdup(file);
  433. return;
  434. }
  435. prev = cur;
  436. }
  437. /* Otherwise add new at the end */
  438. sl = xmalloc(sizeof(*sl));
  439. sl->next = NULL;
  440. sl->name = xstrdup(name);
  441. sl->file = xstrdup(file);
  442. if (prev)
  443. prev->next = sl;
  444. else
  445. fs->slaves = sl;
  446. }
  447. static const char *
  448. fileset_get_slave(struct fileset *fs, const char *name)
  449. {
  450. struct slave_file *slave;
  451. for (slave = fs->slaves; slave; slave = slave->next) {
  452. if (strcmp(slave->name, name) == 0)
  453. return slave->file;
  454. }
  455. return NULL;
  456. }
  457. static bool
  458. fileset_has_slave(struct fileset *fs, const char *name)
  459. {
  460. const char *file = fileset_get_slave(fs, name);
  461. if (file == NULL)
  462. return false;
  463. return file[0] != '\0';
  464. }
  465. static bool
  466. fileset_can_install_slave(struct fileset *fs, const char *slave_name)
  467. {
  468. struct stat st;
  469. /* Decide whether the slave alternative must be setup */
  470. if (fileset_has_slave(fs, slave_name)) {
  471. const char *slave = fileset_get_slave(fs, slave_name);
  472. errno = 0;
  473. if (stat(slave, &st) == -1 && errno != ENOENT)
  474. syserr(_("cannot stat file '%s'"), slave);
  475. if (errno == 0)
  476. return true;
  477. }
  478. return false;
  479. }
  480. struct slave_link {
  481. struct slave_link *next;
  482. char *name;
  483. char *link;
  484. bool updated;
  485. };
  486. struct commit_operation {
  487. struct commit_operation *next;
  488. enum opcode {
  489. OPCODE_NOP,
  490. OPCODE_RM,
  491. OPCODE_MV,
  492. } opcode;
  493. char *arg_a;
  494. char *arg_b;
  495. };
  496. enum alternative_update_reason {
  497. ALT_UPDATE_NO,
  498. ALT_UPDATE_SLAVE_CHANGED,
  499. ALT_UPDATE_LINK_BROKEN,
  500. };
  501. struct alternative {
  502. char *master_name;
  503. char *master_link;
  504. char *current;
  505. enum alternative_status {
  506. ALT_ST_UNKNOWN,
  507. ALT_ST_AUTO,
  508. ALT_ST_MANUAL,
  509. } status;
  510. struct slave_link *slaves;
  511. struct fileset *choices;
  512. struct commit_operation *commit_ops;
  513. int ref_count;
  514. bool modified;
  515. bool known_current;
  516. };
  517. static void
  518. slave_link_free(struct slave_link *slave)
  519. {
  520. free(slave->name);
  521. free(slave->link);
  522. free(slave);
  523. }
  524. static void
  525. commit_operation_free(struct commit_operation *commit_op)
  526. {
  527. free(commit_op->arg_a);
  528. free(commit_op->arg_b);
  529. free(commit_op);
  530. }
  531. static struct alternative *
  532. alternative_new(const char *name)
  533. {
  534. struct alternative *alt;
  535. alt = xmalloc(sizeof(*alt));
  536. alt->master_name = xstrdup(name);
  537. alt->master_link = NULL;
  538. alt->current = NULL;
  539. alt->status = ALT_ST_UNKNOWN;
  540. alt->slaves = NULL;
  541. alt->choices = NULL;
  542. alt->commit_ops = NULL;
  543. alt->modified = false;
  544. alt->known_current = false;
  545. alt->ref_count = 1;
  546. return alt;
  547. }
  548. static inline void
  549. alternative_ref(struct alternative *a)
  550. {
  551. a->ref_count++;
  552. }
  553. static inline bool
  554. alternative_unref(struct alternative *a)
  555. {
  556. return --a->ref_count == 0;
  557. }
  558. static void
  559. alternative_choices_free(struct alternative *a)
  560. {
  561. struct fileset *fs;
  562. if (a->choices)
  563. a->modified = true;
  564. while (a->choices) {
  565. fs = a->choices;
  566. a->choices = fs->next;
  567. fileset_free(fs);
  568. }
  569. }
  570. static void
  571. alternative_commit_operations_free(struct alternative *a)
  572. {
  573. struct commit_operation *op;
  574. while (a->commit_ops) {
  575. op = a->commit_ops;
  576. a->commit_ops = op->next;
  577. commit_operation_free(op);
  578. }
  579. }
  580. static void
  581. alternative_reset(struct alternative *alt)
  582. {
  583. struct slave_link *slave;
  584. free(alt->current);
  585. alt->current = NULL;
  586. free(alt->master_link);
  587. alt->master_link = NULL;
  588. while (alt->slaves) {
  589. slave = alt->slaves;
  590. alt->slaves = slave->next;
  591. slave_link_free(slave);
  592. }
  593. alternative_choices_free(alt);
  594. alternative_commit_operations_free(alt);
  595. alt->modified = false;
  596. alt->known_current = false;
  597. }
  598. static void
  599. alternative_free(struct alternative *alt)
  600. {
  601. if (!alternative_unref(alt))
  602. return;
  603. alternative_reset(alt);
  604. free(alt->master_name);
  605. free(alt);
  606. }
  607. static int
  608. alternative_choices_count(struct alternative *alt)
  609. {
  610. struct fileset *fs;
  611. int count = 0;
  612. for (fs = alt->choices; fs; fs = fs->next)
  613. count++;
  614. return count;
  615. }
  616. static int
  617. alternative_slaves_count(struct alternative *alt)
  618. {
  619. struct slave_link *sl;
  620. int count = 0;
  621. for (sl = alt->slaves; sl; sl = sl->next)
  622. count++;
  623. return count;
  624. }
  625. static int
  626. compare_fileset(const void *va, const void *vb)
  627. {
  628. const struct fileset *a = *(const struct fileset **)va;
  629. const struct fileset *b = *(const struct fileset **)vb;
  630. assert(a && a->master_file);
  631. assert(b && b->master_file);
  632. return strcmp(a->master_file, b->master_file);
  633. }
  634. static int
  635. compare_slave_link(const void *va, const void *vb)
  636. {
  637. const struct slave_link *a = *(const struct slave_link **)va;
  638. const struct slave_link *b = *(const struct slave_link **)vb;
  639. assert(a && a->name);
  640. assert(b && b->name);
  641. return strcmp(a->name, b->name);
  642. }
  643. static void
  644. alternative_sort_choices(struct alternative *a)
  645. {
  646. int count, i;
  647. struct fileset **table, *fs;
  648. count = alternative_choices_count(a);
  649. if (count < 2) /* Nothing to sort */
  650. return;
  651. /* Store objects in a table instead of a linked list */
  652. table = xmalloc(sizeof(fs) * count);
  653. for (fs = a->choices, i = 0; fs; fs = fs->next) {
  654. assert(fs->master_file);
  655. table[i++] = fs;
  656. }
  657. qsort(table, count, sizeof(fs), compare_fileset);
  658. /* Rewrite the linked list from the sorted table */
  659. a->choices = fs = table[0];
  660. table[count - 1]->next = NULL;
  661. for (i = 1; i < count; fs = fs->next, i++)
  662. fs->next = table[i];
  663. free(table);
  664. }
  665. static void
  666. alternative_sort_slaves(struct alternative *a)
  667. {
  668. int count, i;
  669. struct slave_link **table, *sl;
  670. count = alternative_slaves_count(a);
  671. if (count < 2) /* Nothing to sort */
  672. return;
  673. /* Store objects in a table instead of a linked list */
  674. table = xmalloc(sizeof(sl) * count);
  675. for (sl = a->slaves, i = 0; sl; sl = sl->next, i++) {
  676. table[i] = sl;
  677. }
  678. qsort(table, count, sizeof(sl), compare_slave_link);
  679. /* Rewrite the linked list from the sorted table */
  680. a->slaves = sl = table[0];
  681. table[count - 1]->next = NULL;
  682. for (i = 1; i < count; sl = sl->next, i++)
  683. sl->next = table[i];
  684. free(table);
  685. }
  686. static struct fileset *
  687. alternative_get_fileset(struct alternative *a, const char *file)
  688. {
  689. struct fileset *fs;
  690. for (fs = a->choices; fs; fs = fs->next)
  691. if (strcmp(fs->master_file, file) == 0)
  692. return fs;
  693. return NULL;
  694. }
  695. static struct slave_link *
  696. alternative_get_slave(struct alternative *a, const char *name)
  697. {
  698. struct slave_link *sl;
  699. for (sl = a->slaves; sl; sl = sl->next)
  700. if (strcmp(sl->name, name) == 0)
  701. return sl;
  702. return NULL;
  703. }
  704. static bool
  705. alternative_has_slave(struct alternative *a, const char *name)
  706. {
  707. return alternative_get_slave(a, name) != NULL;
  708. }
  709. static bool
  710. alternative_has_choice(struct alternative *a, const char *file)
  711. {
  712. return alternative_get_fileset(a, file) != NULL;
  713. }
  714. static void
  715. alternative_add_choice(struct alternative *a, struct fileset *fs)
  716. {
  717. struct fileset *cur, *prev = NULL;
  718. /* Replace if already existing */
  719. for (cur = a->choices; cur; cur = cur->next) {
  720. if (strcmp(cur->master_file, fs->master_file) == 0) {
  721. fs->next = cur->next;
  722. fileset_free(cur);
  723. if (prev)
  724. prev->next = fs;
  725. else
  726. a->choices = fs;
  727. /* XXX: Be smarter in detecting change? */
  728. a->modified = true;
  729. return;
  730. }
  731. prev = cur;
  732. }
  733. /* Otherwise add at the end */
  734. if (prev == NULL)
  735. a->choices = fs;
  736. else
  737. prev->next = fs;
  738. fs->next = NULL;
  739. a->modified = true;
  740. }
  741. static struct slave_link *
  742. alternative_add_slave(struct alternative *a,
  743. const char *slave_name, const char *slave_link)
  744. {
  745. struct slave_link *sl, *new;
  746. /* Replace if already existing */
  747. for (sl = a->slaves; sl; sl = sl->next) {
  748. if (strcmp(sl->name, slave_name) == 0) {
  749. free(sl->link);
  750. sl->link = xstrdup(slave_link);
  751. return sl;
  752. }
  753. if (sl->next == NULL)
  754. break;
  755. }
  756. /* Otherwise create new and add at the end */
  757. new = xmalloc(sizeof(*new));
  758. new->name = xstrdup(slave_name);
  759. new->link = xstrdup(slave_link);
  760. new->updated = false;
  761. new->next = NULL;
  762. if (sl)
  763. sl->next = new;
  764. else
  765. a->slaves = new;
  766. return new;
  767. }
  768. static void
  769. alternative_copy_slave(struct alternative *a, struct slave_link *sl)
  770. {
  771. struct slave_link *sl_new;
  772. sl_new = alternative_add_slave(a, sl->name, sl->link);
  773. sl_new->updated = sl->updated;
  774. }
  775. static const char *
  776. alternative_status_string(enum alternative_status status)
  777. {
  778. return (status == ALT_ST_AUTO) ? "auto" : "manual";
  779. }
  780. static const char *
  781. alternative_status_describe(enum alternative_status status)
  782. {
  783. return (status == ALT_ST_AUTO) ? _("auto mode") : _("manual mode");
  784. }
  785. static void
  786. alternative_set_status(struct alternative *a, enum alternative_status status)
  787. {
  788. if (a->status == ALT_ST_UNKNOWN || status != a->status)
  789. a->modified = true;
  790. if (a->status != ALT_ST_UNKNOWN && status != a->status)
  791. log_msg("status of link group %s set to %s", a->master_link,
  792. alternative_status_string(status));
  793. a->status = status;
  794. }
  795. static void
  796. alternative_set_link(struct alternative *a, const char *linkname)
  797. {
  798. if (a->master_link == NULL || strcmp(linkname, a->master_link) != 0)
  799. a->modified = true;
  800. free(a->master_link);
  801. a->master_link = xstrdup(linkname);
  802. }
  803. static bool
  804. alternative_remove_choice(struct alternative *a, const char *file)
  805. {
  806. struct fileset *fs, *fs_prev;
  807. fs_prev = NULL;
  808. for (fs = a->choices; fs; fs = fs->next) {
  809. if (strcmp(fs->master_file, file) != 0) {
  810. fs_prev = fs;
  811. continue;
  812. }
  813. if (fs_prev)
  814. fs_prev->next = fs->next;
  815. else
  816. a->choices = fs->next;
  817. fileset_free(fs);
  818. a->modified = true;
  819. return true;
  820. }
  821. return false;
  822. }
  823. /*
  824. * Alternatives Database Load/Store functions.
  825. */
  826. enum altdb_flags {
  827. ALTDB_LAX_PARSER = 1 << 0,
  828. ALTDB_WARN_PARSER = 1 << 1,
  829. };
  830. struct altdb_context {
  831. FILE *fh;
  832. char *filename;
  833. enum altdb_flags flags;
  834. bool modified;
  835. void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  836. (*bad_format)(struct altdb_context *, const char *format, ...);
  837. jmp_buf on_error;
  838. };
  839. static int
  840. altdb_filter_namelist(const struct dirent *entry)
  841. {
  842. if (strcmp(entry->d_name, ".") == 0 ||
  843. strcmp(entry->d_name, "..") == 0 ||
  844. (strlen(entry->d_name) > strlen(ALT_TMP_EXT) &&
  845. strcmp(entry->d_name + strlen(entry->d_name) -
  846. strlen(ALT_TMP_EXT), ALT_TMP_EXT) == 0))
  847. return 0;
  848. return 1;
  849. }
  850. static int
  851. altdb_get_namelist(struct dirent ***table)
  852. {
  853. int count;
  854. count = scandir(admdir, table, altdb_filter_namelist, alphasort);
  855. if (count < 0)
  856. syserr(_("cannot scan directory '%.255s'"), admdir);
  857. return count;
  858. }
  859. static void
  860. altdb_free_namelist(struct dirent **table, int n)
  861. {
  862. while (n--)
  863. free(table[n]);
  864. free(table);
  865. }
  866. static char *
  867. altdb_get_line(struct altdb_context *ctx, const char *name)
  868. {
  869. char *buf, *line;
  870. size_t len, bufsz, i;
  871. bufsz = 1024;
  872. buf = xmalloc(bufsz);
  873. for (i = 0; true; i += strlen(line)) {
  874. errno = 0;
  875. line = fgets(buf + i, bufsz - i, ctx->fh);
  876. if (line) {
  877. if (strlen(buf) < bufsz - 1 || buf[bufsz - 2] == '\n')
  878. break;
  879. /* Need more space */
  880. bufsz *= 2;
  881. buf = realloc(buf, bufsz);
  882. if (!buf)
  883. error(_("failed to allocate memory"));
  884. continue;
  885. }
  886. if (feof(ctx->fh))
  887. ctx->bad_format(ctx, _("unexpected end of file while trying "
  888. "to read %s"), name);
  889. ctx->bad_format(ctx, _("while reading %s: %s"),
  890. name, strerror(errno));
  891. }
  892. len = strlen(buf);
  893. if (len == 0 || buf[len - 1] != '\n') {
  894. ctx->bad_format(ctx, _("line not terminated while trying "
  895. "to read %s"), name);
  896. }
  897. line[len - 1] = '\0';
  898. return buf;
  899. }
  900. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  901. altdb_parse_error(struct altdb_context *ctx, const char *format, ...)
  902. {
  903. char *msg;
  904. va_list args;
  905. va_start(args, format);
  906. msg = xvasprintf(format, args);
  907. va_end(args);
  908. error(_("%s corrupt: %s"), ctx->filename, msg);
  909. }
  910. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  911. altdb_parse_stop(struct altdb_context *ctx, const char *format, ...)
  912. {
  913. longjmp(ctx->on_error, 1);
  914. }
  915. static void
  916. altdb_print_line(struct altdb_context *ctx, const char *line)
  917. {
  918. if (strchr(line, '\n') != NULL)
  919. error(_("newlines prohibited in update-alternatives files (%s)"),
  920. line);
  921. if (fprintf(ctx->fh, "%s\n", line) < (int) strlen(line) + 1)
  922. syserr(_("unable to write file '%s'"), ctx->filename);
  923. }
  924. static bool
  925. alternative_parse_slave(struct alternative *a, struct altdb_context *ctx)
  926. {
  927. char *name, *linkname;
  928. struct slave_link *sl;
  929. name = altdb_get_line(ctx, _("slave name"));
  930. if (!strlen(name)) { /* End of list */
  931. free(name);
  932. return false;
  933. }
  934. sl = alternative_get_slave(a, name);
  935. if (sl) {
  936. free(name);
  937. ctx->bad_format(ctx, _("duplicate slave name %s"), sl->name);
  938. }
  939. linkname = altdb_get_line(ctx, _("slave link"));
  940. if (strcmp(linkname, a->master_link) == 0) {
  941. free(linkname);
  942. free(name);
  943. ctx->bad_format(ctx, _("slave link same as main link %s"),
  944. a->master_link);
  945. }
  946. for (sl = a->slaves; sl; sl = sl->next) {
  947. if (strcmp(linkname, sl->link) == 0) {
  948. free(linkname);
  949. free(name);
  950. ctx->bad_format(ctx, _("duplicate slave link %s"),
  951. sl->link);
  952. }
  953. }
  954. alternative_add_slave(a, name, linkname);
  955. free(linkname);
  956. free(name);
  957. return true;
  958. }
  959. static bool
  960. alternative_parse_fileset(struct alternative *a, struct altdb_context *ctx)
  961. {
  962. struct fileset *fs;
  963. struct slave_link *sl;
  964. struct stat st;
  965. char *master_file;
  966. master_file = altdb_get_line(ctx, _("master file"));
  967. if (!strlen(master_file)) { /* End of list */
  968. free(master_file);
  969. return false;
  970. }
  971. fs = alternative_get_fileset(a, master_file);
  972. if (fs)
  973. ctx->bad_format(ctx, _("duplicate path %s"), master_file);
  974. if (stat(master_file, &st)) {
  975. char *junk;
  976. if (errno != ENOENT)
  977. syserr(_("cannot stat file '%s'"), master_file);
  978. /* File not found - remove. */
  979. if (ctx->flags & ALTDB_WARN_PARSER)
  980. warning(_("alternative %s (part of link group %s) "
  981. "doesn't exist; removing from list of "
  982. "alternatives"), master_file, a->master_name);
  983. junk = altdb_get_line(ctx, _("priority"));
  984. free(junk);
  985. for (sl = a->slaves; sl; sl = sl->next) {
  986. junk = altdb_get_line(ctx, _("slave file"));
  987. free(junk);
  988. }
  989. ctx->modified = true;
  990. } else {
  991. char *prio_str, *prio_end;
  992. long prio;
  993. prio_str = altdb_get_line(ctx, _("priority"));
  994. errno = 0;
  995. prio = strtol(prio_str, &prio_end, 10);
  996. /* XXX: Leak master_file/prio_str on non-fatal error */
  997. if (prio_str == prio_end || *prio_end != '\0')
  998. ctx->bad_format(ctx, _("priority of %s: %s"),
  999. master_file, prio_str);
  1000. if (prio < INT_MIN || prio > INT_MAX || errno == ERANGE)
  1001. ctx->bad_format(ctx,
  1002. _("priority of %s is out of range: %s"),
  1003. master_file, prio_str);
  1004. free(prio_str);
  1005. fs = fileset_new(master_file, prio);
  1006. for (sl = a->slaves; sl; sl = sl->next) {
  1007. char *slave_file = altdb_get_line(ctx, _("slave file"));
  1008. fileset_add_slave(fs, sl->name, slave_file);
  1009. free(slave_file);
  1010. }
  1011. alternative_add_choice(a, fs);
  1012. }
  1013. free(master_file);
  1014. return true;
  1015. }
  1016. static bool
  1017. alternative_load(struct alternative *a, enum altdb_flags flags)
  1018. {
  1019. struct altdb_context ctx;
  1020. struct stat st;
  1021. char *status;
  1022. char *master_link;
  1023. /* Initialize parse context */
  1024. if (setjmp(ctx.on_error)) {
  1025. if (ctx.fh)
  1026. fclose(ctx.fh);
  1027. free(ctx.filename);
  1028. alternative_reset(a);
  1029. return false;
  1030. }
  1031. ctx.modified = false;
  1032. ctx.flags = flags;
  1033. if (flags & ALTDB_LAX_PARSER)
  1034. ctx.bad_format = altdb_parse_stop;
  1035. else
  1036. ctx.bad_format = altdb_parse_error;
  1037. ctx.filename = xasprintf("%s/%s", admdir, a->master_name);
  1038. /* Open the alternative file. */
  1039. ctx.fh = fopen(ctx.filename, "r");
  1040. if (ctx.fh == NULL) {
  1041. if (errno == ENOENT)
  1042. return false;
  1043. syserr(_("unable to open file '%s'"), ctx.filename);
  1044. }
  1045. /* Verify the alternative is not empty. */
  1046. if (fstat(fileno(ctx.fh), &st) == -1)
  1047. syserr(_("cannot stat file '%s'"), ctx.filename);
  1048. if (st.st_size == 0)
  1049. return false;
  1050. /* Start parsing mandatory attributes (link+status) of the alternative */
  1051. alternative_reset(a);
  1052. status = altdb_get_line(&ctx, _("status"));
  1053. if (strcmp(status, "auto") != 0 && strcmp(status, "manual") != 0)
  1054. ctx.bad_format(&ctx, _("invalid status"));
  1055. alternative_set_status(a, (strcmp(status, "auto") == 0) ?
  1056. ALT_ST_AUTO : ALT_ST_MANUAL);
  1057. free(status);
  1058. master_link = altdb_get_line(&ctx, _("master link"));
  1059. alternative_set_link(a, master_link);
  1060. free(master_link);
  1061. /* Parse the description of the slaves links of the alternative */
  1062. while (alternative_parse_slave(a, &ctx));
  1063. /* Parse the available choices in the alternative */
  1064. while (alternative_parse_fileset(a, &ctx)) ;
  1065. /* Close database file */
  1066. if (fclose(ctx.fh))
  1067. syserr(_("unable to close file '%s'"), ctx.filename);
  1068. free(ctx.filename);
  1069. /* Initialize the modified field which has been erroneously changed
  1070. * by the various alternative_(add|set)_* calls:
  1071. * false unless a choice has been auto-cleaned */
  1072. a->modified = ctx.modified;
  1073. return true;
  1074. }
  1075. static void
  1076. alternative_save(struct alternative *a)
  1077. {
  1078. struct altdb_context ctx;
  1079. struct slave_link *sl, *sl_prev;
  1080. struct fileset *fs;
  1081. char *filenew, *file;
  1082. /* Cleanup unused slaves before writing admin file. */
  1083. sl_prev = NULL;
  1084. for (sl = a->slaves; sl; sl_prev = sl, sl = sl->next) {
  1085. bool has_slave = false;
  1086. for (fs = a->choices; fs; fs = fs->next) {
  1087. if (fileset_has_slave(fs, sl->name)) {
  1088. has_slave = true;
  1089. break;
  1090. }
  1091. }
  1092. if (!has_slave) {
  1093. struct slave_link *sl_rm;
  1094. verbose(_("discarding obsolete slave link %s (%s)"),
  1095. sl->name, sl->link);
  1096. if (sl_prev)
  1097. sl_prev->next = sl->next;
  1098. else
  1099. a->slaves = sl->next;
  1100. sl_rm = sl;
  1101. sl = sl_prev ? sl_prev : a->slaves;
  1102. slave_link_free(sl_rm);
  1103. if (!sl)
  1104. break; /* No other slave left. */
  1105. }
  1106. }
  1107. /* Sort entries */
  1108. alternative_sort_slaves(a);
  1109. alternative_sort_choices(a);
  1110. /* Write admin file. */
  1111. file = xasprintf("%s/%s", admdir, a->master_name);
  1112. filenew = xasprintf("%s" ALT_TMP_EXT, file);
  1113. ctx.filename = filenew;
  1114. ctx.fh = fopen(ctx.filename, "w");
  1115. if (ctx.fh == NULL)
  1116. syserr(_("unable to create file '%s'"), ctx.filename);
  1117. altdb_print_line(&ctx, alternative_status_string(a->status));
  1118. altdb_print_line(&ctx, a->master_link);
  1119. for (sl = a->slaves; sl; sl = sl->next) {
  1120. altdb_print_line(&ctx, sl->name);
  1121. altdb_print_line(&ctx, sl->link);
  1122. }
  1123. altdb_print_line(&ctx, "");
  1124. for (fs = a->choices; fs; fs = fs->next) {
  1125. char *prio;
  1126. altdb_print_line(&ctx, fs->master_file);
  1127. prio = xasprintf("%d", fs->priority);
  1128. altdb_print_line(&ctx, prio);
  1129. free(prio);
  1130. for (sl = a->slaves; sl; sl = sl->next) {
  1131. if (fileset_has_slave(fs, sl->name))
  1132. altdb_print_line(&ctx,
  1133. fileset_get_slave(fs, sl->name));
  1134. else
  1135. altdb_print_line(&ctx, "");
  1136. }
  1137. }
  1138. altdb_print_line(&ctx, "");
  1139. /* Close database file */
  1140. if (fflush(ctx.fh))
  1141. syserr(_("unable to flush file '%s'"), ctx.filename);
  1142. if (fsync(fileno(ctx.fh)))
  1143. syserr(_("unable to sync file '%s'"), ctx.filename);
  1144. if (fclose(ctx.fh))
  1145. syserr(_("unable to close file '%s'"), ctx.filename);
  1146. /* Put in place atomically. */
  1147. checked_mv(filenew, file);
  1148. free(filenew);
  1149. free(file);
  1150. }
  1151. static const char *
  1152. alternative_set_current(struct alternative *a, char *new_choice)
  1153. {
  1154. a->known_current = true;
  1155. a->current = new_choice;
  1156. return new_choice;
  1157. }
  1158. static const char *
  1159. alternative_get_current(struct alternative *a)
  1160. {
  1161. struct stat st;
  1162. char *curlink;
  1163. char *file;
  1164. if (a->known_current)
  1165. return a->current;
  1166. curlink = xasprintf("%s/%s", altdir, a->master_name);
  1167. if (lstat(curlink, &st)) {
  1168. if (errno == ENOENT) {
  1169. free(curlink);
  1170. return alternative_set_current(a, NULL);
  1171. }
  1172. syserr(_("cannot stat file '%s'"), curlink);
  1173. }
  1174. file = xreadlink(curlink);
  1175. free(curlink);
  1176. return alternative_set_current(a, file);
  1177. }
  1178. static struct fileset *
  1179. alternative_get_best(struct alternative *a)
  1180. {
  1181. struct fileset *fs, *best;
  1182. const char *current;
  1183. current = alternative_get_current(a);
  1184. if (current)
  1185. best = alternative_get_fileset(a, current);
  1186. else
  1187. best = NULL;
  1188. if (best == NULL)
  1189. best = a->choices;
  1190. for (fs = a->choices; fs; fs = fs->next)
  1191. if (fs->priority > best->priority)
  1192. best = fs;
  1193. return best;
  1194. }
  1195. static void
  1196. alternative_display_query(struct alternative *a)
  1197. {
  1198. struct fileset *best, *fs;
  1199. struct slave_link *sl;
  1200. const char *current;
  1201. pr("Name: %s", a->master_name);
  1202. pr("Link: %s", a->master_link);
  1203. if (alternative_slaves_count(a) > 0) {
  1204. pr("Slaves:");
  1205. for (sl = a->slaves; sl; sl = sl->next)
  1206. pr(" %s %s", sl->name, sl->link);
  1207. }
  1208. pr("Status: %s", alternative_status_string(a->status));
  1209. best = alternative_get_best(a);
  1210. if (best)
  1211. pr("Best: %s", best->master_file);
  1212. current = alternative_get_current(a);
  1213. pr("Value: %s", current ? current : "none");
  1214. for (fs = a->choices; fs; fs = fs->next) {
  1215. printf("\n");
  1216. pr("Alternative: %s", fs->master_file);
  1217. pr("Priority: %d", fs->priority);
  1218. if (alternative_slaves_count(a) == 0)
  1219. continue;
  1220. pr("Slaves:");
  1221. for (sl = a->slaves; sl; sl = sl->next) {
  1222. if (fileset_has_slave(fs, sl->name))
  1223. pr(" %s %s", sl->name,
  1224. fileset_get_slave(fs, sl->name));
  1225. }
  1226. }
  1227. }
  1228. static void
  1229. alternative_display_user(struct alternative *a)
  1230. {
  1231. const char *current;
  1232. struct fileset *fs;
  1233. struct slave_link *sl;
  1234. pr("%s - %s", a->master_name, alternative_status_describe(a->status));
  1235. fs = alternative_get_best(a);
  1236. if (fs)
  1237. pr(_(" link best version is %s"), fs->master_file);
  1238. else
  1239. pr(_(" link best version not available"));
  1240. current = alternative_get_current(a);
  1241. if (current) {
  1242. pr(_(" link currently points to %s"), current);
  1243. } else {
  1244. pr(_(" link currently absent"));
  1245. }
  1246. pr(_(" link %s is %s"), a->master_name, a->master_link);
  1247. for (sl = a->slaves; sl; sl = sl->next)
  1248. pr(_(" slave %s is %s"), sl->name, sl->link);
  1249. for (fs = a->choices; fs; fs = fs->next) {
  1250. pr(_("%s - priority %d"), fs->master_file, fs->priority);
  1251. for (sl = a->slaves; sl; sl = sl->next) {
  1252. if (fileset_has_slave(fs, sl->name))
  1253. pr(_(" slave %s: %s"), sl->name,
  1254. fileset_get_slave(fs, sl->name));
  1255. }
  1256. }
  1257. }
  1258. static void
  1259. alternative_display_list(struct alternative *a)
  1260. {
  1261. struct fileset *fs;
  1262. for (fs = a->choices; fs; fs = fs->next)
  1263. pr("%s", fs->master_file);
  1264. }
  1265. static void
  1266. alternative_print_choice(struct alternative *a, enum alternative_status status,
  1267. struct fileset *fs, int idx, int len)
  1268. {
  1269. const char *current = alternative_get_current(a);
  1270. int mark;
  1271. if (a->status == status &&
  1272. current && strcmp(current, fs->master_file) == 0)
  1273. mark = '*';
  1274. else
  1275. mark = ' ';
  1276. pr("%c %-12d %-*s % -10d %s", mark, idx, len,
  1277. fs->master_file, fs->priority, alternative_status_describe(status));
  1278. }
  1279. static char *
  1280. alternative_select_choice(struct alternative *a)
  1281. {
  1282. const char *current;
  1283. char *ret, selection[_POSIX_PATH_MAX];
  1284. struct fileset *best, *fs;
  1285. int n_choices;
  1286. int len, idx;
  1287. n_choices = alternative_choices_count(a);
  1288. current = alternative_get_current(a);
  1289. best = alternative_get_best(a);
  1290. assert(best);
  1291. len = 15;
  1292. for (fs = a->choices; fs; fs = fs->next)
  1293. len = max(len, (int)strlen(fs->master_file) + 1);
  1294. for (;;) {
  1295. pr(P_("There is %d choice for the alternative %s (providing %s).",
  1296. "There are %d choices for the alternative %s (providing %s).",
  1297. n_choices), n_choices, a->master_name, a->master_link);
  1298. printf("\n");
  1299. pr(" %-12.12s %-*.*s %-10.10s %s", _("Selection"), len, len,
  1300. _("Path"), _("Priority"), _("Status"));
  1301. pr("------------------------------------------------------------");
  1302. idx = 0;
  1303. alternative_print_choice(a, ALT_ST_AUTO, best, idx++, len);
  1304. for (fs = a->choices; fs; fs = fs->next, idx++)
  1305. alternative_print_choice(a, ALT_ST_MANUAL, fs, idx, len);
  1306. printf("\n");
  1307. printf(_("Press <enter> to keep the current choice[*], "
  1308. "or type selection number: "));
  1309. ret = fgets(selection, sizeof(selection), stdin);
  1310. if (ret == NULL || strlen(selection) == 0) {
  1311. return NULL;
  1312. }
  1313. selection[strlen(selection) - 1] = '\0';
  1314. if (strlen(selection) == 0)
  1315. return xstrdup(current);
  1316. errno = 0;
  1317. idx = strtol(selection, &ret, 10);
  1318. if (idx >= 0 && errno == 0 && *ret == '\0') {
  1319. /* Look up by index */
  1320. if (idx == 0) {
  1321. alternative_set_status(a, ALT_ST_AUTO);
  1322. return xstrdup(best->master_file);
  1323. }
  1324. idx--;
  1325. for (fs = a->choices; idx && fs; idx--)
  1326. fs = fs->next;
  1327. if (fs) {
  1328. alternative_set_status(a, ALT_ST_MANUAL);
  1329. return xstrdup(fs->master_file);
  1330. }
  1331. } else {
  1332. /* Look up by name */
  1333. fs = alternative_get_fileset(a, selection);
  1334. if (fs) {
  1335. alternative_set_status(a, ALT_ST_MANUAL);
  1336. return xstrdup(selection);
  1337. }
  1338. }
  1339. }
  1340. }
  1341. static char *
  1342. alternative_config(struct alternative *a, const char *current_choice)
  1343. {
  1344. char *new_choice = NULL;
  1345. if (alternative_choices_count(a) == 0) {
  1346. pr(_("There is no program which provides %s."),
  1347. a->master_name);
  1348. pr(_("Nothing to configure."));
  1349. } else if (opt_skip_auto && a->status == ALT_ST_AUTO) {
  1350. alternative_display_user(a);
  1351. } else if (alternative_choices_count(a) == 1 &&
  1352. a->status == ALT_ST_AUTO &&
  1353. current_choice != NULL) {
  1354. pr(_("There is only one alternative in link group %s (providing %s): %s"),
  1355. a->master_name, a->master_link, current_choice);
  1356. pr(_("Nothing to configure."));
  1357. } else {
  1358. new_choice = alternative_select_choice(a);
  1359. }
  1360. return new_choice;
  1361. }
  1362. static void
  1363. alternative_add_commit_op(struct alternative *a, enum opcode opcode,
  1364. const char *arg_a, const char *arg_b)
  1365. {
  1366. struct commit_operation *op, *cur;
  1367. op = xmalloc(sizeof(*op));
  1368. op->opcode = opcode;
  1369. op->arg_a = xstrdup(arg_a);
  1370. op->arg_b = xstrdup(arg_b);
  1371. op->next = NULL;
  1372. /* Add at the end */
  1373. cur = a->commit_ops;
  1374. while (cur && cur->next)
  1375. cur = cur->next;
  1376. if (cur)
  1377. cur->next = op;
  1378. else
  1379. a->commit_ops = op;
  1380. }
  1381. static void
  1382. alternative_commit(struct alternative *a)
  1383. {
  1384. struct commit_operation *op;
  1385. for (op = a->commit_ops; op; op = op->next) {
  1386. switch (op->opcode) {
  1387. case OPCODE_NOP:
  1388. break;
  1389. case OPCODE_RM:
  1390. checked_rm(op->arg_a);
  1391. break;
  1392. case OPCODE_MV:
  1393. checked_mv(op->arg_a, op->arg_b);
  1394. break;
  1395. }
  1396. }
  1397. alternative_commit_operations_free(a);
  1398. }
  1399. enum alternative_path_status {
  1400. ALT_PATH_SYMLINK,
  1401. ALT_PATH_MISSING,
  1402. ALT_PATH_OTHER,
  1403. };
  1404. static enum alternative_path_status
  1405. alternative_path_classify(const char *linkname)
  1406. {
  1407. struct stat st;
  1408. errno = 0;
  1409. if (lstat(linkname, &st) == -1) {
  1410. if (errno != ENOENT)
  1411. syserr(_("cannot stat file '%s'"), linkname);
  1412. return ALT_PATH_MISSING;
  1413. } else if (S_ISLNK(st.st_mode)) {
  1414. return ALT_PATH_SYMLINK;
  1415. } else {
  1416. return ALT_PATH_OTHER;
  1417. }
  1418. }
  1419. static bool
  1420. alternative_path_can_remove(const char *linkname)
  1421. {
  1422. if (opt_force)
  1423. return true;
  1424. if (alternative_path_classify(linkname) == ALT_PATH_OTHER)
  1425. return false;
  1426. else
  1427. return true;
  1428. }
  1429. static bool
  1430. alternative_path_needs_update(const char *linkname, const char *filename)
  1431. {
  1432. char *linktarget;
  1433. bool update;
  1434. if (opt_force)
  1435. return true;
  1436. switch (alternative_path_classify(linkname)) {
  1437. case ALT_PATH_SYMLINK:
  1438. linktarget = xreadlink(linkname);
  1439. if (strcmp(linktarget, filename) == 0)
  1440. update = false;
  1441. else
  1442. update = true;
  1443. free(linktarget);
  1444. return update;
  1445. case ALT_PATH_OTHER:
  1446. warning(_("not replacing %s with a link"), linkname);
  1447. return false;
  1448. case ALT_PATH_MISSING:
  1449. default:
  1450. return true;
  1451. }
  1452. }
  1453. static void
  1454. alternative_prepare_install_single(struct alternative *a, const char *name,
  1455. const char *linkname, const char *file)
  1456. {
  1457. char *fntmp, *fn;
  1458. /* Create link in /etc/alternatives. */
  1459. fntmp = xasprintf("%s/%s" ALT_TMP_EXT, altdir, name);
  1460. fn = xasprintf("%s/%s", altdir, name);
  1461. checked_rm(fntmp);
  1462. checked_symlink(file, fntmp);
  1463. alternative_add_commit_op(a, OPCODE_MV, fntmp, fn);
  1464. free(fntmp);
  1465. if (alternative_path_needs_update(linkname, fn)) {
  1466. /* Create alternative link. */
  1467. fntmp = xasprintf("%s" ALT_TMP_EXT, linkname);
  1468. checked_rm(fntmp);
  1469. checked_symlink(fn, fntmp);
  1470. alternative_add_commit_op(a, OPCODE_MV, fntmp, linkname);
  1471. free(fntmp);
  1472. }
  1473. free(fn);
  1474. }
  1475. static void
  1476. alternative_prepare_install(struct alternative *a, const char *choice)
  1477. {
  1478. struct slave_link *sl;
  1479. struct fileset *fs;
  1480. fs = alternative_get_fileset(a, choice);
  1481. if (fs == NULL)
  1482. error(_("can't install unknown choice %s"), choice);
  1483. /* Take care of master alternative */
  1484. alternative_prepare_install_single(a, a->master_name, a->master_link,
  1485. choice);
  1486. /* Take care of slaves alternatives */
  1487. for (sl = a->slaves; sl; sl = sl->next) {
  1488. char *fn;
  1489. if (fileset_can_install_slave(fs, sl->name)) {
  1490. alternative_prepare_install_single(a, sl->name,
  1491. sl->link, fileset_get_slave(fs, sl->name));
  1492. continue;
  1493. }
  1494. /* Slave can't be installed */
  1495. if (fileset_has_slave(fs, sl->name))
  1496. warning(_("skip creation of %s because associated "
  1497. "file %s (of link group %s) doesn't exist"),
  1498. sl->link, fileset_get_slave(fs, sl->name),
  1499. a->master_name);
  1500. /* Drop unused slave. */
  1501. fn = xasprintf("%s/%s", altdir, sl->name);
  1502. if (alternative_path_can_remove(sl->link))
  1503. alternative_add_commit_op(a, OPCODE_RM, sl->link, NULL);
  1504. else
  1505. warning(_("not removing %s since it's not a symlink"),
  1506. sl->link);
  1507. alternative_add_commit_op(a, OPCODE_RM, fn, NULL);
  1508. free(fn);
  1509. }
  1510. }
  1511. static void
  1512. alternative_remove_files(struct alternative *a)
  1513. {
  1514. struct slave_link *sl;
  1515. checked_rm_args("%s" ALT_TMP_EXT, a->master_link);
  1516. if (alternative_path_can_remove(a->master_link))
  1517. checked_rm(a->master_link);
  1518. checked_rm_args("%s/%s" ALT_TMP_EXT, altdir, a->master_name);
  1519. checked_rm_args("%s/%s", altdir, a->master_name);
  1520. for (sl = a->slaves; sl; sl = sl->next) {
  1521. checked_rm_args("%s" ALT_TMP_EXT, sl->link);
  1522. if (alternative_path_can_remove(sl->link))
  1523. checked_rm(sl->link);
  1524. checked_rm_args("%s/%s" ALT_TMP_EXT, altdir, sl->name);
  1525. checked_rm_args("%s/%s", altdir, sl->name);
  1526. }
  1527. /* Drop admin file */
  1528. checked_rm_args("%s/%s", admdir, a->master_name);
  1529. }
  1530. static const char *
  1531. alternative_remove(struct alternative *a, const char *current_choice,
  1532. const char *path)
  1533. {
  1534. const char *new_choice = NULL;
  1535. if (alternative_has_choice(a, path))
  1536. alternative_remove_choice(a, path);
  1537. else
  1538. verbose(_("alternative %s for %s not registered; not removing"),
  1539. path, a->master_name);
  1540. if (current_choice && strcmp(current_choice, path) == 0) {
  1541. struct fileset *best;
  1542. /* Current choice is removed. */
  1543. if (a->status == ALT_ST_MANUAL) {
  1544. /* And it was manual, switch to auto. */
  1545. info(_("removing manually selected alternative "
  1546. "- switching %s to auto mode"),
  1547. a->master_name);
  1548. alternative_set_status(a, ALT_ST_AUTO);
  1549. }
  1550. best = alternative_get_best(a);
  1551. if (best)
  1552. new_choice = best->master_file;
  1553. }
  1554. return new_choice;
  1555. }
  1556. static bool
  1557. alternative_has_broken_slave(struct slave_link *sl, struct fileset *fs)
  1558. {
  1559. if (fileset_can_install_slave(fs, sl->name)) {
  1560. char *wanted;
  1561. char *sl_altlnk, *sl_current;
  1562. /* Verify link -> /etc/alternatives/foo */
  1563. sl_altlnk = areadlink(sl->link);
  1564. if (!sl_altlnk)
  1565. return true;
  1566. wanted = xasprintf("%s/%s", altdir, sl->name);
  1567. if (strcmp(sl_altlnk, wanted) != 0) {
  1568. free(wanted);
  1569. free(sl_altlnk);
  1570. return true;
  1571. }
  1572. free(sl_altlnk);
  1573. /* Verify /etc/alternatives/foo -> file */
  1574. sl_current = areadlink(wanted);
  1575. free(wanted);
  1576. if (!sl_current)
  1577. return true;
  1578. if (strcmp(sl_current, fileset_get_slave(fs, sl->name)) != 0) {
  1579. free(sl_current);
  1580. return true;
  1581. }
  1582. free(sl_current);
  1583. } else {
  1584. char *sl_altlnk;
  1585. /* Slave link must not exist. */
  1586. if (alternative_path_classify(sl->link) != ALT_PATH_MISSING)
  1587. return true;
  1588. sl_altlnk = xasprintf("%s/%s", altdir, sl->name);
  1589. if (alternative_path_classify(sl_altlnk) != ALT_PATH_MISSING) {
  1590. free(sl_altlnk);
  1591. return true;
  1592. }
  1593. free(sl_altlnk);
  1594. }
  1595. return false;
  1596. }
  1597. static enum alternative_update_reason
  1598. alternative_needs_update(struct alternative *a)
  1599. {
  1600. enum alternative_update_reason reason = ALT_UPDATE_NO;
  1601. const char *current;
  1602. char *altlnk, *wanted;
  1603. struct fileset *fs;
  1604. struct slave_link *sl;
  1605. /* Check master link */
  1606. altlnk = areadlink(a->master_link);
  1607. if (!altlnk)
  1608. return ALT_UPDATE_LINK_BROKEN;
  1609. wanted = xasprintf("%s/%s", altdir, a->master_name);
  1610. if (strcmp(altlnk, wanted) != 0) {
  1611. free(wanted);
  1612. free(altlnk);
  1613. return ALT_UPDATE_LINK_BROKEN;
  1614. }
  1615. free(wanted);
  1616. free(altlnk);
  1617. /* Stop if we have an unmanaged alternative */
  1618. current = alternative_get_current(a);
  1619. if (current == NULL)
  1620. return ALT_UPDATE_LINK_BROKEN;
  1621. fs = alternative_get_fileset(a, current);
  1622. /* Stop if we do not have the choice. */
  1623. if (fs == NULL)
  1624. return ALT_UPDATE_NO;
  1625. /* Check slaves */
  1626. for (sl = a->slaves; sl; sl = sl->next) {
  1627. if (alternative_has_broken_slave(sl, fs)) {
  1628. if (sl->updated)
  1629. reason = ALT_UPDATE_SLAVE_CHANGED;
  1630. else
  1631. return ALT_UPDATE_LINK_BROKEN;
  1632. }
  1633. }
  1634. return reason;
  1635. }
  1636. struct alternative_map {
  1637. struct alternative_map *next;
  1638. const char *key;
  1639. struct alternative *item;
  1640. };
  1641. static struct alternative_map *
  1642. alternative_map_new(const char *key, struct alternative *a)
  1643. {
  1644. struct alternative_map *am;
  1645. am = xmalloc(sizeof(*am));
  1646. am->next = NULL;
  1647. am->key = key;
  1648. am->item = a;
  1649. return am;
  1650. }
  1651. static struct alternative *
  1652. alternative_map_find(struct alternative_map *am, const char *key)
  1653. {
  1654. for (; am; am = am->next)
  1655. if (am->key && strcmp(am->key, key) == 0)
  1656. return am->item;
  1657. return NULL;
  1658. }
  1659. static void
  1660. alternative_map_add(struct alternative_map *am, const char *key,
  1661. struct alternative *a)
  1662. {
  1663. if (am->key == NULL) {
  1664. am->key = key;
  1665. am->item = a;
  1666. } else {
  1667. struct alternative_map *new = alternative_map_new(key, a);
  1668. while (am->next)
  1669. am = am->next;
  1670. am->next = new;
  1671. }
  1672. }
  1673. static void
  1674. alternative_map_load_names(struct alternative_map *alt_map_obj)
  1675. {
  1676. struct dirent **table;
  1677. int i, count;
  1678. count = altdb_get_namelist(&table);
  1679. for (i = 0; i < count; i++) {
  1680. struct alternative *a_new = alternative_new(table[i]->d_name);
  1681. if (!alternative_load(a_new, ALTDB_LAX_PARSER)) {
  1682. alternative_free(a_new);
  1683. continue;
  1684. }
  1685. alternative_map_add(alt_map_obj, a_new->master_name, a_new);
  1686. }
  1687. altdb_free_namelist(table, count);
  1688. }
  1689. static void
  1690. alternative_map_load_tree(struct alternative_map *alt_map_links,
  1691. struct alternative_map *alt_map_parent)
  1692. {
  1693. struct dirent **table;
  1694. int i, count;
  1695. count = altdb_get_namelist(&table);
  1696. for (i = 0; i < count; i++) {
  1697. struct slave_link *sl;
  1698. struct alternative *a_new = alternative_new(table[i]->d_name);
  1699. if (!alternative_load(a_new, ALTDB_LAX_PARSER)) {
  1700. alternative_free(a_new);
  1701. continue;
  1702. }
  1703. alternative_map_add(alt_map_links, a_new->master_link, a_new);
  1704. alternative_ref(a_new);
  1705. alternative_map_add(alt_map_parent, a_new->master_name, a_new);
  1706. for (sl = a_new->slaves; sl; sl = sl->next) {
  1707. alternative_ref(a_new);
  1708. alternative_map_add(alt_map_links, sl->link, a_new);
  1709. alternative_ref(a_new);
  1710. alternative_map_add(alt_map_parent, sl->name, a_new);
  1711. }
  1712. }
  1713. altdb_free_namelist(table, count);
  1714. }
  1715. static void
  1716. alternative_map_free(struct alternative_map *am)
  1717. {
  1718. struct alternative_map *am_next;
  1719. while (am) {
  1720. am_next = am->next;
  1721. if (am->item)
  1722. alternative_free(am->item);
  1723. free(am);
  1724. am = am_next;
  1725. }
  1726. }
  1727. static const char *
  1728. alternative_set_manual(struct alternative *a, const char *path)
  1729. {
  1730. const char *new_choice = NULL;
  1731. if (alternative_has_choice(a, path))
  1732. new_choice = path;
  1733. else
  1734. error(_("alternative %s for %s not registered; "
  1735. "not setting"), path, a->master_name);
  1736. alternative_set_status(a, ALT_ST_MANUAL);
  1737. return new_choice;
  1738. }
  1739. static const char *
  1740. alternative_set_auto(struct alternative *a)
  1741. {
  1742. const char *new_choice = NULL;
  1743. alternative_set_status(a, ALT_ST_AUTO);
  1744. if (alternative_choices_count(a) == 0)
  1745. pr(_("There is no program which provides %s."),
  1746. a->master_name);
  1747. else
  1748. new_choice = alternative_get_best(a)->master_file;
  1749. return new_choice;
  1750. }
  1751. static const char *
  1752. get_argv_string(int argc, char **argv)
  1753. {
  1754. static char string[2048];
  1755. size_t cur_len;
  1756. int i;
  1757. string[0] = '\0';
  1758. cur_len = 0;
  1759. for (i = 1; i < argc; i++) {
  1760. size_t arg_len = strlen(argv[i]);
  1761. if (cur_len + arg_len + 2 > sizeof(string))
  1762. break;
  1763. if (cur_len) {
  1764. strcpy(string + cur_len, " ");
  1765. cur_len++;
  1766. }
  1767. strcpy(string + cur_len, argv[i]);
  1768. cur_len += arg_len;
  1769. }
  1770. return string;
  1771. }
  1772. static void
  1773. alternative_select_mode(struct alternative *a, const char *current_choice)
  1774. {
  1775. if (current_choice) {
  1776. /* Detect manually modified alternative, switch to manual. */
  1777. if (!alternative_has_choice(a, current_choice)) {
  1778. struct stat st;
  1779. errno = 0;
  1780. if (stat(current_choice, &st) == -1 && errno != ENOENT)
  1781. syserr(_("cannot stat file '%s'"), current_choice);
  1782. if (errno == ENOENT) {
  1783. warning(_("%s/%s is dangling; it will be updated "
  1784. "with best choice"), altdir, a->master_name);
  1785. alternative_set_status(a, ALT_ST_AUTO);
  1786. } else if (a->status != ALT_ST_MANUAL) {
  1787. warning(_("%s/%s has been changed (manually or by "
  1788. "a script); switching to manual "
  1789. "updates only"), altdir, a->master_name);
  1790. alternative_set_status(a, ALT_ST_MANUAL);
  1791. }
  1792. }
  1793. } else {
  1794. /* Lack of alternative link => automatic mode. */
  1795. verbose(_("setting up automatic selection of %s"),
  1796. a->master_name);
  1797. alternative_set_status(a, ALT_ST_AUTO);
  1798. }
  1799. }
  1800. static void
  1801. alternative_evolve_slave(struct alternative *a, const char *cur_choice,
  1802. struct slave_link *sl, struct fileset *fs)
  1803. {
  1804. struct slave_link *sl_old;
  1805. struct stat st;
  1806. char *new_file = NULL;
  1807. const char *old, *new;
  1808. sl_old = alternative_get_slave(a, sl->name);
  1809. if (sl_old == NULL) {
  1810. sl->updated = true;
  1811. return;
  1812. }
  1813. old = sl_old->link;
  1814. new = sl->link;
  1815. if (cur_choice && strcmp(cur_choice, fs->master_file) == 0) {
  1816. new_file = xstrdup(fileset_get_slave(fs, sl->name));
  1817. } else {
  1818. char *lnk;
  1819. lnk = xasprintf("%s/%s", altdir, sl->name);
  1820. new_file = areadlink(lnk);
  1821. free(lnk);
  1822. }
  1823. if (strcmp(old, new) != 0 &&
  1824. alternative_path_classify(old) == ALT_PATH_SYMLINK) {
  1825. bool rename_link = false;
  1826. if (new_file) {
  1827. errno = 0;
  1828. if (stat(new_file, &st) == -1 && errno != ENOENT)
  1829. syserr(_("cannot stat file '%s'"),
  1830. new_file);
  1831. rename_link = (errno == 0);
  1832. }
  1833. if (rename_link) {
  1834. info(_("renaming %s slave link from %s to %s"),
  1835. sl->name, old, new);
  1836. checked_mv(old, new);
  1837. } else {
  1838. checked_rm(old);
  1839. }
  1840. sl->updated = true;
  1841. }
  1842. free(new_file);
  1843. }
  1844. static void
  1845. alternative_evolve(struct alternative *a, struct alternative *b,
  1846. const char *cur_choice, struct fileset *fs)
  1847. {
  1848. struct slave_link *sl;
  1849. bool is_link;
  1850. is_link = alternative_path_classify(a->master_link) == ALT_PATH_SYMLINK;
  1851. if (is_link && strcmp(a->master_link, b->master_link) != 0) {
  1852. info(_("renaming %s link from %s to %s"), b->master_name,
  1853. a->master_link, b->master_link);
  1854. checked_mv(a->master_link, b->master_link);
  1855. }
  1856. alternative_set_link(a, b->master_link);
  1857. /* Check if new slaves have been added, or existing
  1858. * ones renamed. */
  1859. for (sl = b->slaves; sl; sl = sl->next) {
  1860. alternative_evolve_slave(a, cur_choice, sl, fs);
  1861. alternative_copy_slave(a, sl);
  1862. }
  1863. }
  1864. static void
  1865. alternative_update(struct alternative *a,
  1866. const char *current_choice, const char *new_choice)
  1867. {
  1868. enum alternative_update_reason reason;
  1869. /* No choice left, remove everything. */
  1870. if (!alternative_choices_count(a)) {
  1871. log_msg("link group %s fully removed", a->master_name);
  1872. alternative_remove_files(a);
  1873. return;
  1874. }
  1875. /* New choice wanted. */
  1876. if (new_choice &&
  1877. (!current_choice || strcmp(new_choice, current_choice) != 0)) {
  1878. log_msg("link group %s updated to point to %s", a->master_name,
  1879. new_choice);
  1880. if (a->status == ALT_ST_AUTO)
  1881. info(_("using %s to provide %s (%s) in auto mode"),
  1882. new_choice, a->master_link, a->master_name);
  1883. else
  1884. info(_("using %s to provide %s (%s) in manual mode"),
  1885. new_choice, a->master_link, a->master_name);
  1886. debug("prepare_install(%s)", new_choice);
  1887. alternative_prepare_install(a, new_choice);
  1888. } else if ((reason = alternative_needs_update(a))) {
  1889. if (reason == ALT_UPDATE_SLAVE_CHANGED) {
  1890. log_msg("link group %s updated with changed slaves",
  1891. a->master_name);
  1892. info(_("updating alternative %s "
  1893. "because link group %s has changed slave links"),
  1894. current_choice, a->master_name);
  1895. } else {
  1896. log_msg("auto-repair link group %s", a->master_name);
  1897. warning(_("forcing reinstallation of alternative %s "
  1898. "because link group %s is broken"),
  1899. current_choice, a->master_name);
  1900. }
  1901. if (current_choice && !alternative_has_choice(a, current_choice)) {
  1902. struct fileset *best = alternative_get_best(a);
  1903. warning(_("current alternative %s is unknown, "
  1904. "switching to %s for link group %s"),
  1905. current_choice, best->master_file,
  1906. a->master_name);
  1907. current_choice = best->master_file;
  1908. alternative_set_status(a, ALT_ST_AUTO);
  1909. }
  1910. if (current_choice)
  1911. alternative_prepare_install(a, current_choice);
  1912. }
  1913. /* Save administrative file if needed. */
  1914. if (a->modified) {
  1915. debug("%s is modified and will be saved", a->master_name);
  1916. alternative_save(a);
  1917. }
  1918. /* Replace all symlinks in one pass. */
  1919. alternative_commit(a);
  1920. }
  1921. static void
  1922. alternative_config_all(void)
  1923. {
  1924. struct alternative_map *alt_map_obj;
  1925. struct alternative_map *am;
  1926. alt_map_obj = alternative_map_new(NULL, NULL);
  1927. alternative_map_load_names(alt_map_obj);
  1928. for (am = alt_map_obj; am && am->item; am = am->next) {
  1929. const char *current_choice;
  1930. char *new_choice;
  1931. current_choice = alternative_get_current(am->item);
  1932. alternative_select_mode(am->item, current_choice);
  1933. new_choice = alternative_config(am->item, current_choice);
  1934. alternative_update(am->item, current_choice, new_choice);
  1935. free(new_choice);
  1936. }
  1937. alternative_map_free(alt_map_obj);
  1938. }
  1939. static void
  1940. alternative_get_selections(void)
  1941. {
  1942. struct alternative_map *alt_map_obj;
  1943. struct alternative_map *am;
  1944. alt_map_obj = alternative_map_new(NULL, NULL);
  1945. alternative_map_load_names(alt_map_obj);
  1946. for (am = alt_map_obj; am && am->item; am = am->next) {
  1947. const char *current;
  1948. current = alternative_get_current(am->item);
  1949. printf("%-30s %-8s %s\n", am->key,
  1950. alternative_status_string(am->item->status),
  1951. current ? current : "");
  1952. }
  1953. alternative_map_free(alt_map_obj);
  1954. }
  1955. static void
  1956. alternative_set_selection(struct alternative_map *all, const char *name,
  1957. const char *status, const char *choice)
  1958. {
  1959. struct alternative *a;
  1960. debug("set_selection(%s, %s, %s)", name, status, choice);
  1961. a = alternative_map_find(all, name);
  1962. if (a) {
  1963. const char *new_choice = NULL;
  1964. if (strcmp(status, "auto") == 0) {
  1965. new_choice = alternative_set_auto(a);
  1966. } else if (alternative_has_choice(a, choice)) {
  1967. new_choice = alternative_set_manual(a, choice);
  1968. } else {
  1969. pr(_("Alternative %s unchanged because choice "
  1970. "%s is not available."), name, choice);
  1971. }
  1972. if (new_choice) {
  1973. const char *current_choice;
  1974. current_choice = alternative_get_current(a);
  1975. alternative_select_mode(a, current_choice);
  1976. alternative_update(a, current_choice, new_choice);
  1977. }
  1978. } else {
  1979. pr(_("Skip unknown alternative %s."), name);
  1980. }
  1981. }
  1982. static void
  1983. alternative_set_selections(FILE *input, const char *desc)
  1984. {
  1985. struct alternative_map *alt_map_obj;
  1986. alt_map_obj = alternative_map_new(NULL, NULL);
  1987. alternative_map_load_names(alt_map_obj);
  1988. for (;;) {
  1989. char line[1024], *res, *name, *status, *choice;
  1990. size_t len, i;
  1991. errno = 0;
  1992. /* Can't use scanf("%s %s %s") because choice can
  1993. * contain a space */
  1994. res = fgets(line, sizeof(line), input);
  1995. if (res == NULL && errno) {
  1996. syserr(_("read error in %.250s"), desc);
  1997. } else if (res == NULL) {
  1998. break;
  1999. }
  2000. len = strlen(line);
  2001. if (len == 0 || line[len - 1] != '\n') {
  2002. error(_("line too long or not terminated while "
  2003. "trying to read %s"), desc);
  2004. }
  2005. line[len - 1] = '\0';
  2006. len--;
  2007. /* Delimit name string in line */
  2008. i = 0;
  2009. name = line;
  2010. while (i < len && !isblank(line[i]))
  2011. i++;
  2012. if (i >= len) {
  2013. printf("[%s %s] ", PROGNAME, "--set-selections");
  2014. pr(_("Skip invalid line: %s"), line);
  2015. continue;
  2016. }
  2017. line[i++] = '\0';
  2018. while (i < len && isblank(line[i]))
  2019. i++;
  2020. /* Delimit status string in line */
  2021. status = line + i;
  2022. while (i < len && !isblank(line[i]))
  2023. i++;
  2024. if (i >= len) {
  2025. printf("[%s %s] ", PROGNAME, "--set-selections");
  2026. pr(_("Skip invalid line: %s"), line);
  2027. continue;
  2028. }
  2029. line[i++] = '\0';
  2030. while (i < len && isblank(line[i]))
  2031. i++;
  2032. /* Delimit choice string in the line */
  2033. if (i >= len) {
  2034. printf("[%s %s] ", PROGNAME, "--set-selections");
  2035. pr(_("Skip invalid line: %s"), line);
  2036. continue;
  2037. }
  2038. choice = line + i;
  2039. printf("[%s %s] ", PROGNAME, "--set-selections");
  2040. alternative_set_selection(alt_map_obj, name, status, choice);
  2041. }
  2042. alternative_map_free(alt_map_obj);
  2043. }
  2044. static void
  2045. alternative_check_name(const char *name)
  2046. {
  2047. if (strpbrk(name, "/ \t"))
  2048. error(_("alternative name (%s) must not contain '/' "
  2049. "and spaces"), name);
  2050. }
  2051. static void
  2052. alternative_check_link(const char *linkname)
  2053. {
  2054. if (linkname[0] != '/')
  2055. error(_("alternative link is not absolute as it should be: %s"),
  2056. linkname);
  2057. }
  2058. static void
  2059. alternative_check_path(const char *file)
  2060. {
  2061. if (!file || file[0] != '/')
  2062. error(_("alternative path is not absolute as it should be: %s"),
  2063. file);
  2064. }
  2065. /**
  2066. * Check the alternative installation arguments.
  2067. *
  2068. * That the caller doesn't mix links between alternatives, doesn't mix
  2069. * alternatives between slave/master, and that the various parameters
  2070. * are fine.
  2071. */
  2072. static void
  2073. alternative_check_install_args(struct alternative *inst_alt,
  2074. struct fileset *fileset)
  2075. {
  2076. struct alternative_map *alt_map_links, *alt_map_parent;
  2077. struct alternative *found;
  2078. struct slave_link *sl;
  2079. struct stat st;
  2080. alternative_check_name(inst_alt->master_name);
  2081. alternative_check_link(inst_alt->master_link);
  2082. alternative_check_path(fileset->master_file);
  2083. /* Load information about all alternatives to check for mistakes. */
  2084. alt_map_links = alternative_map_new(NULL, NULL);
  2085. alt_map_parent = alternative_map_new(NULL, NULL);
  2086. alternative_map_load_tree(alt_map_links, alt_map_parent);
  2087. found = alternative_map_find(alt_map_parent, inst_alt->master_name);
  2088. if (found && strcmp(found->master_name, inst_alt->master_name) != 0) {
  2089. error(_("alternative %s can't be master: it is a slave of %s"),
  2090. inst_alt->master_name, found->master_name);
  2091. }
  2092. found = alternative_map_find(alt_map_links, inst_alt->master_link);
  2093. if (found && strcmp(found->master_name, inst_alt->master_name) != 0) {
  2094. found = alternative_map_find(alt_map_parent,
  2095. found->master_name);
  2096. error(_("alternative link %s is already managed by %s"),
  2097. inst_alt->master_link, found->master_name);
  2098. }
  2099. if (stat(fileset->master_file, &st) == -1) {
  2100. if (errno == ENOENT)
  2101. error(_("alternative path %s doesn't exist"),
  2102. fileset->master_file);
  2103. else
  2104. syserr(_("cannot stat file '%s'"), fileset->master_file);
  2105. }
  2106. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  2107. const char *file = fileset_get_slave(fileset, sl->name);
  2108. alternative_check_name(sl->name);
  2109. alternative_check_link(sl->link);
  2110. alternative_check_path(file);
  2111. found = alternative_map_find(alt_map_parent, sl->name);
  2112. if (found &&
  2113. strcmp(found->master_name, inst_alt->master_name) != 0) {
  2114. if (strcmp(found->master_name, sl->name) == 0)
  2115. error(_("alternative %s can't be slave of %s: "
  2116. "it is a master alternative"),
  2117. sl->name, inst_alt->master_name);
  2118. else
  2119. error(_("alternative %s can't be slave of %s: "
  2120. "it is a slave of %s"),
  2121. sl->name, inst_alt->master_name,
  2122. found->master_name);
  2123. }
  2124. found = alternative_map_find(alt_map_links, sl->link);
  2125. if (found &&
  2126. strcmp(found->master_name, inst_alt->master_name) != 0) {
  2127. error(_("alternative link %s is already "
  2128. "managed by %s"), sl->link,
  2129. found->master_name);
  2130. }
  2131. if (found) {
  2132. struct slave_link *sl2;
  2133. for (sl2 = found->slaves; sl2; sl2 = sl2->next)
  2134. if (strcmp(sl2->link, sl->link) == 0)
  2135. break;
  2136. if (sl2 && strcmp(sl2->name, sl->name) != 0)
  2137. error(_("alternative link %s is already "
  2138. "managed by %s (slave of %s)"),
  2139. sl->link, sl2->name,
  2140. found->master_name);
  2141. }
  2142. }
  2143. alternative_map_free(alt_map_links);
  2144. alternative_map_free(alt_map_parent);
  2145. }
  2146. /*
  2147. * Main program
  2148. */
  2149. #define MISSING_ARGS(nb) (argc < i + nb + 1)
  2150. int
  2151. main(int argc, char **argv)
  2152. {
  2153. /* Alternative worked on. */
  2154. struct alternative *a = NULL;
  2155. /* Alternative to install. */
  2156. struct alternative *inst_alt = NULL;
  2157. /* Set of files to install in the alternative. */
  2158. struct fileset *fileset = NULL;
  2159. /* Path of alternative we are offering. */
  2160. char *path = NULL;
  2161. const char *current_choice = NULL;
  2162. const char *new_choice = NULL;
  2163. bool modifies_alt = false;
  2164. bool modifies_sys = false;
  2165. int i = 0;
  2166. setlocale(LC_ALL, "");
  2167. bindtextdomain(PACKAGE, LOCALEDIR);
  2168. textdomain(PACKAGE);
  2169. admdir = admindir_init();
  2170. if (setvbuf(stdout, NULL, _IONBF, 0))
  2171. syserr("setvbuf failed");
  2172. prog_path = argv[0];
  2173. for (i = 1; i < argc; i++) {
  2174. if (strstr(argv[i], "--") != argv[i]) {
  2175. error(_("unknown argument '%s'"), argv[i]);
  2176. } else if (strcmp("--help", argv[i]) == 0) {
  2177. usage();
  2178. exit(0);
  2179. } else if (strcmp("--version", argv[i]) == 0) {
  2180. version();
  2181. exit(0);
  2182. } else if (strcmp("--verbose", argv[i]) == 0) {
  2183. opt_verbose++;
  2184. } else if (strcmp("--quiet", argv[i]) == 0) {
  2185. opt_verbose--;
  2186. } else if (strcmp("--install", argv[i]) == 0) {
  2187. char *prio_str, *prio_end;
  2188. long prio;
  2189. set_action("install");
  2190. if (MISSING_ARGS(4))
  2191. badusage(_("--install needs <link> <name> "
  2192. "<path> <priority>"));
  2193. prio_str = argv[i + 4];
  2194. if (strcmp(argv[i+1], argv[i+3]) == 0)
  2195. badusage(_("<link> and <path> can't be the same"));
  2196. errno = 0;
  2197. prio = strtol(prio_str, &prio_end, 10);
  2198. if (prio_str == prio_end || *prio_end != '\0')
  2199. badusage(_("priority must be an integer"));
  2200. if (prio < INT_MIN || prio > INT_MAX || errno == ERANGE)
  2201. badusage(_("priority is out of range"));
  2202. a = alternative_new(argv[i + 2]);
  2203. inst_alt = alternative_new(argv[i + 2]);
  2204. alternative_set_status(inst_alt, ALT_ST_AUTO);
  2205. alternative_set_link(inst_alt, argv[i + 1]);
  2206. fileset = fileset_new(argv[i + 3], prio);
  2207. i += 4;
  2208. } else if (strcmp("--remove", argv[i]) == 0 ||
  2209. strcmp("--set", argv[i]) == 0) {
  2210. set_action(argv[i] + 2);
  2211. if (MISSING_ARGS(2))
  2212. badusage(_("--%s needs <name> <path>"), argv[i] + 2);
  2213. a = alternative_new(argv[i + 1]);
  2214. path = xstrdup(argv[i + 2]);
  2215. alternative_check_name(a->master_name);
  2216. alternative_check_path(path);
  2217. i += 2;
  2218. } else if (strcmp("--display", argv[i]) == 0 ||
  2219. strcmp("--query", argv[i]) == 0 ||
  2220. strcmp("--auto", argv[i]) == 0 ||
  2221. strcmp("--config", argv[i]) == 0 ||
  2222. strcmp("--list", argv[i]) == 0 ||
  2223. strcmp("--remove-all", argv[i]) == 0) {
  2224. set_action(argv[i] + 2);
  2225. if (MISSING_ARGS(1))
  2226. badusage(_("--%s needs <name>"), argv[i] + 2);
  2227. a = alternative_new(argv[i + 1]);
  2228. alternative_check_name(a->master_name);
  2229. i++;
  2230. } else if (strcmp("--all", argv[i]) == 0 ||
  2231. strcmp("--get-selections", argv[i]) == 0 ||
  2232. strcmp("--set-selections", argv[i]) == 0) {
  2233. set_action(argv[i] + 2);
  2234. } else if (strcmp("--slave", argv[i]) == 0) {
  2235. const char *slink, *sname, *spath;
  2236. struct slave_link *sl;
  2237. if (action == NULL ||
  2238. (action && strcmp(action, "install") != 0))
  2239. badusage(_("--slave only allowed with --install"));
  2240. if (MISSING_ARGS(3))
  2241. badusage(_("--slave needs <link> <name> <path>"));
  2242. slink = argv[i + 1];
  2243. sname = argv[i + 2];
  2244. spath = argv[i + 3];
  2245. if (strcmp(slink, spath) == 0)
  2246. badusage(_("<link> and <path> can't be the same"));
  2247. if (strcmp(inst_alt->master_name, sname) == 0)
  2248. badusage(_("name %s is both primary and slave"),
  2249. sname);
  2250. if (strcmp(slink, inst_alt->master_link) == 0)
  2251. badusage(_("link %s is both primary and slave"),
  2252. slink);
  2253. if (alternative_has_slave(inst_alt, sname))
  2254. badusage(_("duplicate slave name %s"), sname);
  2255. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  2256. const char *linkname = sl->link;
  2257. if (linkname == NULL)
  2258. linkname = "";
  2259. if (strcmp(linkname, slink) == 0)
  2260. badusage(_("duplicate slave link %s"),
  2261. slink);
  2262. }
  2263. alternative_add_slave(inst_alt, sname, slink);
  2264. fileset_add_slave(fileset, sname, spath);
  2265. i+= 3;
  2266. } else if (strcmp("--log", argv[i]) == 0) {
  2267. if (MISSING_ARGS(1))
  2268. badusage(_("--%s needs a <file> argument"), "log");
  2269. log_file = argv[i + 1];
  2270. i++;
  2271. } else if (strcmp("--altdir", argv[i]) == 0) {
  2272. if (MISSING_ARGS(1))
  2273. badusage(_("--%s needs a <directory> argument"), "log");
  2274. altdir = argv[i + 1];
  2275. i++;
  2276. } else if (strcmp("--admindir", argv[i]) == 0) {
  2277. if (MISSING_ARGS(1))
  2278. badusage(_("--%s needs a <directory> argument"), "log");
  2279. admdir = argv[i + 1];
  2280. i++;
  2281. } else if (strcmp("--skip-auto", argv[i]) == 0) {
  2282. opt_skip_auto = 1;
  2283. } else if (strcmp("--force", argv[i]) == 0) {
  2284. opt_force = 1;
  2285. } else {
  2286. badusage(_("unknown option '%s'"), argv[i]);
  2287. }
  2288. }
  2289. if (!action)
  2290. badusage(_("need --display, --query, --list, --get-selections, "
  2291. "--config, --set, --set-selections, --install, "
  2292. "--remove, --all, --remove-all or --auto"));
  2293. /* The following actions might modify the current alternative. */
  2294. if (strcmp(action, "set") == 0 ||
  2295. strcmp(action, "auto") == 0 ||
  2296. strcmp(action, "config") == 0 ||
  2297. strcmp(action, "remove") == 0 ||
  2298. strcmp(action, "remove-all") == 0 ||
  2299. strcmp(action, "install") == 0)
  2300. modifies_alt = true;
  2301. /* The following actions might modify the system somehow. */
  2302. if (modifies_alt ||
  2303. strcmp(action, "all") == 0 ||
  2304. strcmp(action, "set-selections") == 0)
  2305. modifies_sys = true;
  2306. if (strcmp(action, "install") == 0)
  2307. alternative_check_install_args(inst_alt, fileset);
  2308. if (strcmp(action, "display") == 0 ||
  2309. strcmp(action, "query") == 0 ||
  2310. strcmp(action, "list") == 0 ||
  2311. strcmp(action, "set") == 0 ||
  2312. strcmp(action, "auto") == 0 ||
  2313. strcmp(action, "config") == 0 ||
  2314. strcmp(action, "remove-all") == 0) {
  2315. /* Load the alternative info, stop on failure. */
  2316. if (!alternative_load(a, ALTDB_WARN_PARSER))
  2317. error(_("no alternatives for %s"), a->master_name);
  2318. } else if (strcmp(action, "remove") == 0) {
  2319. /* FIXME: Be consistent for now with the case when we
  2320. * try to remove a non-existing path from an existing
  2321. * link group file. */
  2322. if (!alternative_load(a, ALTDB_WARN_PARSER)) {
  2323. verbose(_("no alternatives for %s"), a->master_name);
  2324. exit(0);
  2325. }
  2326. } else if (strcmp(action, "install") == 0) {
  2327. /* Load the alternative info, ignore failures. */
  2328. alternative_load(a, ALTDB_WARN_PARSER);
  2329. }
  2330. if (modifies_sys)
  2331. log_msg("run with %s", get_argv_string(argc, argv));
  2332. if (modifies_alt) {
  2333. current_choice = alternative_get_current(a);
  2334. alternative_select_mode(a, current_choice);
  2335. }
  2336. /* Handle actions. */
  2337. if (strcmp(action, "all") == 0) {
  2338. alternative_config_all();
  2339. } else if (strcmp(action, "get-selections") == 0) {
  2340. alternative_get_selections();
  2341. } else if (strcmp(action, "set-selections") == 0) {
  2342. alternative_set_selections(stdin, _("<standard input>"));
  2343. } else if (strcmp(action, "display") == 0) {
  2344. alternative_display_user(a);
  2345. } else if (strcmp(action, "query") == 0) {
  2346. alternative_display_query(a);
  2347. } else if (strcmp(action, "list") == 0) {
  2348. alternative_display_list(a);
  2349. } else if (strcmp(action, "set") == 0) {
  2350. new_choice = alternative_set_manual(a, path);
  2351. } else if (strcmp(action, "auto") == 0) {
  2352. new_choice = alternative_set_auto(a);
  2353. } else if (strcmp(action, "config") == 0) {
  2354. new_choice = alternative_config(a, current_choice);
  2355. } else if (strcmp(action, "remove") == 0) {
  2356. new_choice = alternative_remove(a, current_choice, path);
  2357. } else if (strcmp(action, "remove-all") == 0) {
  2358. alternative_choices_free(a);
  2359. } else if (strcmp(action, "install") == 0) {
  2360. if (a->master_link) {
  2361. /* Alternative already exists, check if anything got
  2362. * updated. */
  2363. alternative_evolve(a, inst_alt, current_choice, fileset);
  2364. } else {
  2365. /* Alternative doesn't exist, create from parameters. */
  2366. alternative_free(a);
  2367. a = inst_alt;
  2368. }
  2369. alternative_add_choice(a, fileset);
  2370. if (a->status == ALT_ST_AUTO) {
  2371. new_choice = alternative_get_best(a)->master_file;
  2372. } else {
  2373. verbose(_("automatic updates of %s/%s are disabled; "
  2374. "leaving it alone"), altdir, a->master_name);
  2375. verbose(_("to return to automatic updates use "
  2376. "'%s --auto %s'"), PROGNAME, a->master_name);
  2377. }
  2378. }
  2379. if (modifies_alt)
  2380. alternative_update(a, current_choice, new_choice);
  2381. return 0;
  2382. }