update-alternatives.c 66 KB

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