update-alternatives.c 66 KB

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