usm-spm-pacman 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #!/bin/sh
  2. # USM system-package-manager helper for pacman-based systems (Arch).
  3. #
  4. # Implements the USM SPM contract (see usm/README.md, "System package
  5. # manager integration") for Arch-style systems:
  6. #
  7. # usm-spm-pacman query <usm-ref>... -> contract JSON on STDOUT
  8. # usm-spm-pacman install <native-name>... -> contract JSONL events on STDOUT
  9. #
  10. # API choice: pure POSIX shell on purpose -- the arch base group ships
  11. # neither python3 nor jq, and the SPM bootstrap must stay at zero extra
  12. # packages. bsdtar (libarchive, a hard pacman dependency) decompresses
  13. # the repository databases under /var/lib/pacman/sync so provides and
  14. # file lists are parsed directly instead of shelling pattern queries
  15. # through pacman:
  16. #
  17. # lib: X -> the sync dbs' %PROVIDES% entries; makepkg auto-
  18. # generates soname provides ("libfoo.so=2-64"), so the
  19. # "=major-arch" suffix is stripped before matching;
  20. # file ownership of /usr/lib/X is the fallback
  21. # everything file-backed ->
  22. # the opt-in files databases (<repo>.files, fetched by
  23. # `pacman -Fy` -- the arch analogue of apt-file update)
  24. # are parsed into a path -> package map; without them,
  25. # only files of INSTALLED packages resolve (via
  26. # `pacman -Qo`), the same offline stance as the emerge
  27. # helper. No package-name heuristics are used.
  28. #
  29. # dependency-count / installed-dependency-count model "a solo install on
  30. # a from-scratch machine": the closure comes from `pacman -S --print`
  31. # run against a throwaway --dbpath whose local database is empty and
  32. # whose sync databases symlink the system's, so every dependency
  33. # resolves as an install; the overlap with the installed set
  34. # (`pacman -Qq`) is installed-dependency-count. If that run cannot
  35. # resolve, the counts degrade to a (1, n) estimate with a warning.
  36. #
  37. # pacman exposes no per-download progress on a non-tty stdout; it names
  38. # each package once ("(k/N) installing pkg" in older versions,
  39. # "installing pkg..." since pacman 7), so "package" events are emitted
  40. # at package granularity (progress 1.0) and begin.total comes from a
  41. # `pacman -S --print` pass against the current system. Install runs
  42. # with --needed, making repeated installs idempotent.
  43. #
  44. # No INSTALL_COMPANIONS table is needed: arch does not split toolchain
  45. # packages, glibc already ships the crt objects.
  46. #
  47. # The query subcommand never modifies system state: databases are read
  48. # from their normal locations, and the only writes land in a throwaway
  49. # temp directory. The install subcommand must run as root; --noconfirm
  50. # guarantees pacman never prompts interactively: unresolved targets or
  51. # bad signatures fail instead.
  52. #
  53. # Arch path table (merged-usr layout, no multiarch): /usr/bin (sbin is
  54. # a symlink), /usr/lib (lib64 is a symlink), /usr/include, /usr/share
  55. # (+/usr/share/pkgconfig), /usr/share/vala*/vapi.
  56. set -eu
  57. export LC_ALL=C
  58. PROG=usm-spm-pacman
  59. EXIT_OK=0
  60. EXIT_FAILURE=1
  61. EXIT_USAGE=2
  62. EXIT_RESOLVE=3
  63. EXIT_DOWNLOAD=4
  64. EXIT_TRANSACTION=5
  65. WORK=""
  66. SYNC_DIR=/var/lib/pacman/sync
  67. REPO_INDEXED=0
  68. cleanup() {
  69. if [ -n "$WORK" ]; then
  70. rm -rf "$WORK"
  71. fi
  72. }
  73. warn() {
  74. printf '%s: %s\n' "$PROG" "$*" >&2
  75. }
  76. die_usage() {
  77. printf 'usage: %s query <usm-ref>...\n' "$PROG" >&2
  78. printf ' %s install <native-name>...\n' "$PROG" >&2
  79. exit "$EXIT_USAGE"
  80. }
  81. emit() {
  82. printf '%s\n' "$1"
  83. }
  84. json_escape() {
  85. printf '%s' "$1" | awk '
  86. {
  87. s = $0
  88. gsub(/\\/, "\\\\", s)
  89. gsub(/"/, "\\\"", s)
  90. gsub(/\t/, "\\t", s)
  91. gsub(/[[:cntrl:]]/, "", s)
  92. print s
  93. }'
  94. }
  95. mkwork() {
  96. WORK=$(mktemp -d "${TMPDIR:-/tmp}/usm-spm-pacman.XXXXXX" 2>/dev/null) || {
  97. WORK="${TMPDIR:-/tmp}/usm-spm-pacman.$$"
  98. (umask 077 && mkdir "$WORK")
  99. }
  100. trap cleanup EXIT
  101. }
  102. not_found_add() {
  103. printf '%s\n' "$1" >>"$WORK/nf"
  104. }
  105. map_add() {
  106. printf '%s\t%s\n' "$1" "$2" >>"$WORK/map"
  107. }
  108. # Parse the sync/files databases ONCE per invocation into flat lookup
  109. # tables: $WORK/provides maps "soname-or-virtual<TAB>package" (the
  110. # "=major-arch" suffix is stripped from soname provides so lib: refs
  111. # carrying the full soname match exactly), $WORK/filemap maps
  112. # "path-without-leading-slash<TAB>package" from the %FILES% sections of
  113. # the .files databases. Both stay empty (and the lookups degrade to the
  114. # installed database) when the databases are missing.
  115. build_repo_index() {
  116. [ "$REPO_INDEXED" = 0 ] || return 0
  117. REPO_INDEXED=1
  118. : >"$WORK/provides"
  119. : >"$WORK/filemap"
  120. if ! command -v bsdtar >/dev/null 2>&1; then
  121. warn "bsdtar not found in PATH; repository lookups are unavailable"
  122. return 0
  123. fi
  124. _ri_any=0
  125. for _ri_db in "$SYNC_DIR"/*.db; do
  126. [ -f "$_ri_db" ] || continue
  127. _ri_any=1
  128. bsdtar -xOf "$_ri_db" 2>>"$WORK/index.err" | awk '
  129. /^%NAME%$/ { mode = "name"; next }
  130. /^%PROVIDES%$/ { mode = "prov"; next }
  131. /^%/ { mode = ""; next }
  132. NF == 0 { next }
  133. mode == "name" { pkg = $1; mode = ""; next }
  134. mode == "prov" && pkg != "" {
  135. p = $1
  136. sub(/=.*$/, "", p)
  137. print pkg "\t" p
  138. }
  139. ' >>"$WORK/provides" || true
  140. done
  141. for _ri_db in "$SYNC_DIR"/*.files; do
  142. [ -f "$_ri_db" ] || continue
  143. _ri_any=1
  144. bsdtar -xOf "$_ri_db" 2>>"$WORK/index.err" | awk '
  145. /^%NAME%$/ { mode = "name"; next }
  146. /^%FILES%$/ { mode = "files"; next }
  147. /^%/ { mode = ""; next }
  148. NF == 0 { next }
  149. mode == "name" { pkg = $1; mode = ""; next }
  150. mode == "files" && pkg != "" { print pkg "\t" $1 }
  151. ' >>"$WORK/filemap" || true
  152. done
  153. if [ "$_ri_any" != 1 ]; then
  154. warn "no databases under $SYNC_DIR (run pacman -Sy as root); repository lookups are unavailable"
  155. elif [ ! -s "$WORK/filemap" ]; then
  156. warn "no files databases under $SYNC_DIR (run pacman -Fy as root); only installed files resolve"
  157. fi
  158. }
  159. # makepkg turns soname "libc.so.6" into the provide "libc.so=6-64", so a
  160. # lib: ref's trailing ".<major>" is optionally stripped before matching:
  161. # "lib:libc.so.6" matches "libc.so" exactly as an unversioned
  162. # "lib:libc.so" would.
  163. repo_provider_owners() {
  164. awk -F'\t' -v p="$1" '
  165. {
  166. r = p
  167. sub(/\.[0-9]+$/, "", r)
  168. if ($2 == p || ($2 == r && r != p)) { print $1 }
  169. }' "$WORK/provides" 2>/dev/null
  170. }
  171. repo_file_owners() {
  172. awk -F'\t' -v p="$1" '$2 == p { print $1 }' "$WORK/filemap" 2>/dev/null
  173. }
  174. repo_file_prefix_owners() {
  175. # exact file OR any file below the directory prefix
  176. awk -F'\t' -v p="$1" '$2 == p || index($2, p "/") == 1 { print $1 }' \
  177. "$WORK/filemap" 2>/dev/null
  178. }
  179. # Map every owner name read from STDIN onto _om_ref; succeeds when at
  180. # least one name arrived.
  181. owners_to_map() {
  182. _om_ref=$1
  183. _om_any=0
  184. while IFS= read -r _om_nm; do
  185. [ -n "$_om_nm" ] || continue
  186. map_add "$_om_nm" "$_om_ref"
  187. _om_any=1
  188. done
  189. [ "$_om_any" = 1 ]
  190. }
  191. pacman_diag_line() {
  192. [ -r "$1" ] || return 0
  193. sed -n 's/^[[:space:]]*error:[[:space:]]*//p' "$1" | head -n 1
  194. }
  195. record_providers() {
  196. _rp_ref=$1
  197. _rp_prov=$2
  198. build_repo_index
  199. if repo_provider_owners "$_rp_prov" | sort -u | owners_to_map "$_rp_ref"; then
  200. return 0
  201. fi
  202. not_found_add "$_rp_ref"
  203. }
  204. record_file_owner() {
  205. _fo_ref=$1
  206. shift
  207. build_repo_index
  208. _fo_hit=0
  209. for _fo_p in "$@"; do
  210. _fo_names=
  211. if [ -s "$WORK/filemap" ]; then
  212. _fo_names=$(repo_file_owners "${_fo_p#/}" | sort -u)
  213. fi
  214. if [ -z "$_fo_names" ]; then
  215. # installed-database fallback, exact for present files only
  216. _fo_f=
  217. if [ -d "$_fo_p" ]; then
  218. _fo_f=$(find "$_fo_p" -type f 2>/dev/null | head -n 1) || _fo_f=
  219. elif [ -f "$_fo_p" ]; then
  220. _fo_f=$_fo_p
  221. fi
  222. if [ -n "$_fo_f" ]; then
  223. _fo_names=$(pacman -Qo -- "$_fo_f" 2>/dev/null \
  224. | awk '{ print $(NF-1) }') || _fo_names=
  225. fi
  226. fi
  227. if [ -n "$_fo_names" ]; then
  228. printf '%s\n' "$_fo_names" | owners_to_map "$_fo_ref" && _fo_hit=1
  229. fi
  230. done
  231. if [ "$_fo_hit" != 1 ]; then
  232. not_found_add "$_fo_ref"
  233. fi
  234. }
  235. record_dir_owner() {
  236. # Like record_file_owner, but the ref names a directory (inc:), so
  237. # any file at or below the prefix satisfies it.
  238. _do_ref=$1
  239. _do_p=$2
  240. build_repo_index
  241. if [ -s "$WORK/filemap" ]; then
  242. if repo_file_prefix_owners "${_do_p#/}" | sort -u | owners_to_map "$_do_ref"; then
  243. return 0
  244. fi
  245. fi
  246. record_file_owner "$_do_ref" "$_do_p"
  247. }
  248. handle_ref() {
  249. _hr_ref=$1
  250. _hr_type=${_hr_ref%%:*}
  251. _hr_res=${_hr_ref#*:}
  252. if [ "$_hr_type" = "$_hr_ref" ] || [ -z "$_hr_res" ]; then
  253. warn "resource type of \"$_hr_ref\" has no system-package-manager translation"
  254. not_found_add "$_hr_ref"
  255. return
  256. fi
  257. case $_hr_res in
  258. *[!A-Za-z0-9._+/-]*)
  259. warn "resource name of \"$_hr_ref\" has no system-package-manager translation"
  260. not_found_add "$_hr_ref"
  261. return
  262. ;;
  263. esac
  264. case $_hr_type in
  265. bin)
  266. record_file_owner "$_hr_ref" "/usr/bin/$_hr_res"
  267. ;;
  268. sbin)
  269. record_file_owner "$_hr_ref" "/usr/bin/$_hr_res" "/usr/sbin/$_hr_res"
  270. ;;
  271. lib|libres)
  272. # soname provides first (exact, suffix-stripped), file second
  273. build_repo_index
  274. if ! repo_provider_owners "$_hr_res" | sort -u | owners_to_map "$_hr_ref"; then
  275. record_file_owner "$_hr_ref" "/usr/lib/$_hr_res"
  276. fi
  277. ;;
  278. pc)
  279. record_file_owner "$_hr_ref" \
  280. "/usr/lib/pkgconfig/$_hr_res" "/usr/share/pkgconfig/$_hr_res"
  281. ;;
  282. libexec)
  283. record_file_owner "$_hr_ref" "/usr/libexec/$_hr_res"
  284. ;;
  285. gir)
  286. record_file_owner "$_hr_ref" "/usr/share/gir-1.0/$_hr_res"
  287. ;;
  288. typelib)
  289. record_file_owner "$_hr_ref" "/usr/lib/girepository-1.0/$_hr_res"
  290. ;;
  291. gio)
  292. record_file_owner "$_hr_ref" "/usr/lib/gio/modules/$_hr_res"
  293. ;;
  294. res)
  295. record_file_owner "$_hr_ref" "/usr/share/$_hr_res"
  296. ;;
  297. cfg)
  298. record_file_owner "$_hr_ref" "/etc/$_hr_res"
  299. ;;
  300. man)
  301. record_file_owner "$_hr_ref" "/usr/share/man/$_hr_res"
  302. ;;
  303. info)
  304. record_file_owner "$_hr_ref" "/usr/share/info/$_hr_res"
  305. ;;
  306. locale)
  307. record_file_owner "$_hr_ref" "/usr/share/locale/$_hr_res"
  308. ;;
  309. inc)
  310. record_dir_owner "$_hr_ref" "/usr/include/$_hr_res"
  311. ;;
  312. vapi)
  313. build_repo_index
  314. if [ -s "$WORK/filemap" ]; then
  315. : >"$WORK/vown"
  316. repo_file_owners "usr/share/vala/vapi/$_hr_res" >>"$WORK/vown"
  317. awk -F'\t' -v suf="/vapi/$_hr_res" \
  318. 'substr($2, 1, 15) == "usr/share/vala" &&
  319. substr($2, length($2) - length(suf) + 1) == suf { print $1 }' \
  320. "$WORK/filemap" >>"$WORK/vown"
  321. if sort -u "$WORK/vown" | owners_to_map "$_hr_ref"; then
  322. return 0
  323. fi
  324. fi
  325. set -- "/usr/share/vala/vapi/$_hr_res"
  326. for _hr_d in /usr/share/vala-*/vapi/"$_hr_res"; do
  327. if [ -f "$_hr_d" ]; then
  328. set -- "$@" "$_hr_d"
  329. fi
  330. done
  331. record_file_owner "$_hr_ref" "$@"
  332. ;;
  333. rootpath)
  334. record_file_owner "$_hr_ref" "/$_hr_res"
  335. ;;
  336. tag)
  337. _hr_t=${_hr_res%.tag}
  338. record_file_owner "$_hr_ref" \
  339. "/usr/share/usm-tags/$(printf '%s' "$_hr_t" | tr '.' '/').tag"
  340. ;;
  341. *)
  342. warn "resource type of \"$_hr_ref\" has no system-package-manager translation"
  343. not_found_add "$_hr_ref"
  344. return
  345. ;;
  346. esac
  347. }
  348. counts_for() {
  349. _cf_name=$1
  350. _cf_cached=
  351. if [ -s "$WORK/counts" ]; then
  352. _cf_cached=$(awk -F'\t' -v n="$_cf_name" '$1 == n { print $2 " " $3; exit }' "$WORK/counts") || _cf_cached=
  353. fi
  354. if [ -n "$_cf_cached" ]; then
  355. printf '%s\n' "$_cf_cached"
  356. return
  357. fi
  358. # Repo-only closure: a throwaway database whose empty local db makes
  359. # every package resolve as an install and whose sync symlink reuses
  360. # the system's repositories.
  361. _cf_root="$WORK/rootdb"
  362. if [ ! -d "$_cf_root" ]; then
  363. mkdir -p "$_cf_root/local"
  364. ln -s "$SYNC_DIR" "$_cf_root/sync" 2>/dev/null || true
  365. fi
  366. _cf_dep=0
  367. _cf_inst=0
  368. _cf_cl=$(pacman --dbpath "$_cf_root" -S --print --print-format '%n' \
  369. --noconfirm -- "$_cf_name" 2>"$WORK/cnt.err" | grep -E '^[A-Za-z0-9@._+-]+$' || true)
  370. if [ -n "$_cf_cl" ]; then
  371. printf '%s\n' "$_cf_cl" >"$WORK/cl"
  372. _cf_dep=$(awk 'END { print NR }' "$WORK/cl")
  373. if [ -s "$WORK/installed" ]; then
  374. _cf_inst=$(grep -x -F -f "$WORK/installed" "$WORK/cl" | awk 'END { print NR }') || _cf_inst=0
  375. fi
  376. else
  377. warn "could not resolve solo install of $_cf_name, estimating dependency counts"
  378. if grep -qx -F "$_cf_name" "$WORK/installed" 2>/dev/null; then
  379. _cf_dep=1
  380. _cf_inst=1
  381. else
  382. _cf_dep=1
  383. _cf_inst=0
  384. fi
  385. fi
  386. printf '%s\t%s\t%s\n' "$_cf_name" "$_cf_dep" "$_cf_inst" >>"$WORK/counts"
  387. printf '%s %s\n' "$_cf_dep" "$_cf_inst"
  388. }
  389. cmd_query() {
  390. [ $# -ge 1 ] || die_usage
  391. mkwork
  392. pacman -Qq >"$WORK/installed" 2>/dev/null || : >"$WORK/installed"
  393. : >"$WORK/map"
  394. : >"$WORK/nf"
  395. : >"$WORK/counts"
  396. : >"$WORK/seen"
  397. for _q_ref in "$@"; do
  398. if [ -n "$_q_ref" ] && grep -x -F -q "$_q_ref" "$WORK/seen" 2>/dev/null; then
  399. continue
  400. fi
  401. printf '%s\n' "$_q_ref" >>"$WORK/seen"
  402. handle_ref "$_q_ref"
  403. done
  404. _q_out='{"not-found":['
  405. _q_first=1
  406. while IFS= read -r _q_r; do
  407. [ -n "$_q_r" ] || continue
  408. if [ "$_q_first" = 1 ]; then _q_first=0; else _q_out="$_q_out,"; fi
  409. _q_out="$_q_out\"$(json_escape "$_q_r")\""
  410. done <"$WORK/nf"
  411. _q_out="$_q_out],\"packages\":["
  412. _q_first=1
  413. for _q_nm in $(awk -F'\t' '{ print $1 }' "$WORK/map" | sort -u); do
  414. if [ "$_q_first" = 1 ]; then _q_first=0; else _q_out="$_q_out,"; fi
  415. awk -F'\t' -v n="$_q_nm" '$1 == n { print $2 }' "$WORK/map" >"$WORK/tmpres"
  416. _q_res=""
  417. _q_rf=1
  418. while IFS= read -r _q_rr; do
  419. if [ "$_q_rf" = 1 ]; then _q_rf=0; else _q_res="$_q_res,"; fi
  420. _q_res="$_q_res\"$(json_escape "$_q_rr")\""
  421. done <"$WORK/tmpres"
  422. _q_cnts=$(counts_for "$_q_nm")
  423. _q_dep=${_q_cnts%% *}
  424. _q_inst=${_q_cnts##* }
  425. _q_out="$_q_out{\"name\":\"$(json_escape "$_q_nm")\",\"resources\":[${_q_res}],\"dependency-count\":$_q_dep,\"installed-dependency-count\":$_q_inst}"
  426. done
  427. _q_out="$_q_out]}"
  428. emit "$_q_out"
  429. exit "$EXIT_OK"
  430. }
  431. classify_failure() {
  432. if grep -Eqi 'target not found|could not satisfy dependencies|conflicting|unresolvable|failed to prepare transaction|hold onto' "$1" 2>/dev/null; then
  433. printf '%s\n' "$EXIT_RESOLVE"
  434. elif grep -Eqi 'network|download|retriev|signature|keyring|gpg|invalid or corrupted|checksum|integrity|connection|timed out|mirror' "$1" 2>/dev/null; then
  435. printf '%s\n' "$EXIT_DOWNLOAD"
  436. else
  437. printf '%s\n' "$EXIT_TRANSACTION"
  438. fi
  439. }
  440. cmd_install() {
  441. [ $# -ge 1 ] || die_usage
  442. for _i_n in "$@"; do
  443. case $_i_n in
  444. -*) die_usage ;;
  445. esac
  446. done
  447. mkwork
  448. _i_simrc=0
  449. _i_sim=$(pacman -S --needed --noconfirm --print --print-format '%n' \
  450. -- "$@" 2>"$WORK/sim.err") || _i_simrc=$?
  451. if [ "$_i_simrc" != 0 ]; then
  452. _i_msg=$(pacman_diag_line "$WORK/sim.err")
  453. [ -n "$_i_msg" ] || _i_msg="pacman --print failed with status $_i_simrc"
  454. emit "{\"type\":\"error\",\"message\":\"$(json_escape "$_i_msg")\"}"
  455. exit "$(classify_failure "$WORK/sim.err")"
  456. fi
  457. _i_total=$(printf '%s\n' "$_i_sim" | grep -Ec '^[A-Za-z0-9@._+-]+$') || _i_total=0
  458. emit "{\"type\":\"begin\",\"total\":$_i_total}"
  459. {
  460. # || keeps set -e from aborting the pipeline group before the
  461. # status reaches the rc file
  462. _i_status=0
  463. pacman -S --needed --noconfirm --noprogressbar -- "$@" \
  464. 2>"$WORK/inst.err" || _i_status=$?
  465. printf '%s\n' "$_i_status" >"$WORK/rc"
  466. } | tr '\r' '\n' | {
  467. _i_done=0
  468. _i_lastpkg=
  469. while IFS= read -r _i_line || [ -n "$_i_line" ]; do
  470. # pacman's transaction output names each package exactly
  471. # once: older versions redraw "(k/N) installing pkg" lines
  472. # (\r-flattened here), pacman 7 pipes plain "installing
  473. # pkg..." lines. Dedupe on the package name either way.
  474. case $_i_line in
  475. \(*\)\ *)
  476. _i_kn=${_i_line#"("}
  477. _i_kn=${_i_kn%%")"*}
  478. _i_rest=${_i_line#*") "}
  479. _i_verb=${_i_rest%% *}
  480. _i_pkg=${_i_rest#* }
  481. _i_pkg=${_i_pkg%% *}
  482. _i_k=${_i_kn%%/*}
  483. _i_n=${_i_kn##*/}
  484. ;;
  485. installing\ *|upgrading\ *|downgrading\ *|reinstalling\ *)
  486. _i_verb=${_i_line%% *}
  487. _i_pkg=${_i_line#* }
  488. _i_pkg=${_i_pkg%% *}
  489. _i_pkg=${_i_pkg%"..."}
  490. _i_k=$((_i_done + 1))
  491. _i_n=$_i_total
  492. ;;
  493. *)
  494. continue
  495. ;;
  496. esac
  497. case $_i_verb in
  498. installing|upgrading|downgrading|reinstalling)
  499. if [ "$_i_pkg" != "$_i_lastpkg" ]; then
  500. _i_lastpkg=$_i_pkg
  501. emit "{\"type\":\"package\",\"name\":\"$(json_escape "$_i_pkg")\",\"current\":$_i_k,\"total\":$_i_n,\"progress\":1.0}"
  502. emit "{\"type\":\"package-complete\",\"name\":\"$(json_escape "$_i_pkg")\"}"
  503. _i_done=$((_i_done + 1))
  504. fi
  505. ;;
  506. esac
  507. done
  508. printf '%s\n' "$_i_done" >"$WORK/done"
  509. }
  510. _i_rc=$(cat "$WORK/rc" 2>/dev/null) || _i_rc=1
  511. if [ "$_i_rc" = 0 ]; then
  512. _i_done=$(cat "$WORK/done" 2>/dev/null) || _i_done=0
  513. emit "{\"type\":\"complete\",\"status\":\"ok\",\"installed\":$_i_done}"
  514. exit "$EXIT_OK"
  515. fi
  516. _i_msg=$(pacman_diag_line "$WORK/inst.err")
  517. [ -n "$_i_msg" ] || _i_msg="pacman -S failed with status $_i_rc"
  518. emit "{\"type\":\"error\",\"message\":\"$(json_escape "$_i_msg")\"}"
  519. exit "$(classify_failure "$WORK/inst.err")"
  520. }
  521. main() {
  522. if [ $# -lt 1 ]; then
  523. die_usage
  524. fi
  525. if ! command -v pacman >/dev/null 2>&1; then
  526. if [ "$1" = install ]; then
  527. emit '{"type":"error","message":"pacman not found in PATH"}'
  528. exit "$EXIT_FAILURE"
  529. fi
  530. warn "pacman not found in PATH"
  531. exit "$EXIT_RESOLVE"
  532. fi
  533. _m_cmd=$1
  534. shift
  535. case $_m_cmd in
  536. query)
  537. cmd_query "$@"
  538. ;;
  539. install)
  540. cmd_install "$@"
  541. ;;
  542. -h|--help|help)
  543. printf 'usage: %s query <usm-ref>...\n' "$PROG"
  544. printf ' %s install <native-name>...\n' "$PROG"
  545. exit "$EXIT_OK"
  546. ;;
  547. *)
  548. die_usage
  549. ;;
  550. esac
  551. }
  552. main "$@"