update-alternatives.c 66 KB

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