ipkg 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #!/bin/sh
  2. # ipkg - the itsy package management system
  3. #
  4. # Copyright (C) 2001 Carl D. Worth
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. set -e
  16. # By default do not do globbing. Any command wanting globbing should
  17. # explicitly enable it first and disable it afterwards.
  18. set -o noglob
  19. ipkg_is_upgrade () {
  20. local A B a b
  21. A=$(echo $1 | sed -r "s/([0-9]+)[^[:alnum:]]*/ \1 /g").
  22. B=$(echo $2 | sed -r "s/([0-9]+)[^[:alnum:]]*/ \1 /g").
  23. while [ \! -z "$A" ] && [ \! -z "$B" ]; do {
  24. set $A; a=$1; shift; A=$*
  25. set $B; b=$1; shift; B=$*
  26. [ "$a" -lt "$b" ] 2>&- && return 1
  27. { [ "$a" -gt "$b" ] 2>&- || [ "$a" ">" "$b" ]; } && return
  28. }; done
  29. return 1
  30. }
  31. which md5sum 2>&1 >/dev/null || alias md5sum=md5
  32. ipkg_srcs() {
  33. local srcre="$1"
  34. sed -ne "s/^src[[:space:]]\+$srcre[[:space:]]\+//p" < $IPKG_CONF
  35. }
  36. ipkg_src_names() {
  37. sed -ne "s/^src[[:space:]]\+\([^[:space:]]\+\).*/\1/p" < $IPKG_CONF
  38. }
  39. ipkg_src_byname() {
  40. local src="$1"
  41. ipkg_srcs $src | head -n 1
  42. }
  43. ipkg_dests() {
  44. local destre="`echo $1 | ipkg_protect_slashes`"
  45. sed -ne "/^dest[[:space:]]\+$destre/{
  46. s/^dest[[:space:]]\+[^[:space:]]\+[[:space:]]\+//
  47. s/^/`echo $IPKG_OFFLINE_ROOT | ipkg_protect_slashes`/
  48. p
  49. }" < $IPKG_CONF
  50. }
  51. ipkg_dest_names() {
  52. sed -ne "s/^dest[[:space:]]\+\([^[:space:]]\+\).*/\1/p" < $IPKG_CONF
  53. }
  54. ipkg_dests_all() {
  55. ipkg_dests '.*'
  56. }
  57. ipkg_state_dirs() {
  58. ipkg_dests_all | sed "s|\$|/$IPKG_DIR_PREFIX|"
  59. }
  60. ipkg_dest_default() {
  61. ipkg_dests_all | head -n 1
  62. }
  63. ipkg_dest_default_name() {
  64. ipkg_dest_names | head -n 1
  65. }
  66. ipkg_dest_byname() {
  67. local dest="$1"
  68. ipkg_dests $dest | head -n 1
  69. }
  70. ipkg_option() {
  71. local option="$1"
  72. sed -ne "s/^option[[:space:]]\+$option[[:space:]]\+//p" < $IPKG_CONF
  73. }
  74. ipkg_load_configuration() {
  75. if [ -z "$IPKG_CONF_DIR" ]; then
  76. IPKG_CONF_DIR=/etc
  77. fi
  78. IPKG_CONF="$IPKG_CONF_DIR/ipkg.conf"
  79. if [ -z "$IPKG_OFFLINE_ROOT" ]; then
  80. IPKG_OFFLINE_ROOT="`ipkg_option offline_root`"
  81. fi
  82. # Export IPKG_OFFLINE_ROOT for use by update-alternatives
  83. export IPKG_OFFLINE_ROOT
  84. if [ -n "$DEST_NAME" ]; then
  85. IPKG_ROOT="`ipkg_dest_byname $DEST_NAME`"
  86. if [ -z "$IPKG_ROOT" ]; then
  87. if [ -d "$IPKG_OFFLINE_ROOT$DEST_NAME" ]; then
  88. IPKG_ROOT="$IPKG_OFFLINE_ROOT$DEST_NAME";
  89. else
  90. echo "ipkg: invalid destination specification: $DEST_NAME
  91. Valid destinations are directories or one of the dest names from $IPKG_CONF:" >&2
  92. ipkg_dest_names >&2
  93. return 1
  94. fi
  95. fi
  96. else
  97. IPKG_ROOT="`ipkg_dest_default`"
  98. fi
  99. # Global ipkg state directories
  100. IPKG_DIR_PREFIX=usr/lib/opkg
  101. IPKG_LISTS_DIR=$IPKG_OFFLINE_ROOT/$IPKG_DIR_PREFIX/lists
  102. IPKG_PENDING_DIR=$IPKG_OFFLINE_ROOT/$IPKG_DIR_PREFIX/pending
  103. if [ -z "$IPKG_TMP" ]; then
  104. IPKG_TMP=$IPKG_ROOT/tmp/ipkg
  105. fi
  106. [ -e "$IPKG_TMP" ] || mkdir -p $IPKG_TMP
  107. # Destination specific ipkg meta-data directory
  108. IPKG_STATE_DIR=$IPKG_ROOT/$IPKG_DIR_PREFIX
  109. # Proxy Support
  110. IPKG_PROXY_USERNAME="`ipkg_option proxy_username`"
  111. IPKG_PROXY_PASSWORD="`ipkg_option proxy_password`"
  112. IPKG_HTTP_PROXY="`ipkg_option http_proxy`"
  113. IPKG_FTP_PROXY="`ipkg_option ftp_proxy`"
  114. IPKG_NO_PROXY="`ipkg_option no_proxy`"
  115. if [ -n "$IPKG_HTTP_PROXY" ]; then
  116. export http_proxy="$IPKG_HTTP_PROXY"
  117. fi
  118. if [ -n "$IPKG_FTP_PROXY" ]; then
  119. export ftp_proxy="$IPKG_FTP_PROXY"
  120. fi
  121. if [ -n "$IPKG_NO_PROXY" ]; then
  122. export no_proxy="$IPKG_NO_PROXY"
  123. fi
  124. IPKG_STATUS_FIELDS='\(Package\|Status\|Essential\|Version\|Conffiles\|Root\|Architecture\)'
  125. }
  126. ipkg_usage() {
  127. [ $# -gt 0 ] && echo "ipkg: $*"
  128. echo "
  129. usage: ipkg [options...] sub-command [arguments...]
  130. where sub-command is one of:
  131. Package Manipulation:
  132. update Update list of available packages
  133. upgrade Upgrade all installed packages to latest version
  134. install <pkg> Download and install <pkg> (and dependencies)
  135. install <file.ipk> Install package <file.ipk>
  136. install <file.deb> Install package <file.deb>
  137. remove <pkg> Remove package <pkg>
  138. Informational Commands:
  139. list List available packages and descriptions
  140. files <pkg> List all files belonging to <pkg>
  141. search <file> Search for a packaging providing <file>
  142. info [pkg [<field>]] Display all/some info fields for <pkg> or all
  143. status [pkg [<field>]] Display all/some status fields for <pkg> or all
  144. depends <pkg> Print uninstalled package dependencies for <pkg>
  145. Options:
  146. -d <dest_name> Use <dest_name> as the the root directory for
  147. -dest <dest_name> package installation, removal, upgrading.
  148. <dest_name> should be a defined dest name from the
  149. configuration file, (but can also be a directory
  150. name in a pinch).
  151. -o <offline_root> Use <offline_root> as the root for offline installation.
  152. -offline <offline_root>
  153. Force Options (use when ipkg is too smart for its own good):
  154. -force-depends Make dependency checks warnings instead of errors
  155. -force-defaults Use default options for questions asked by ipkg.
  156. (no prompts). Note that this will not prevent
  157. package installation scripts from prompting.
  158. " >&2
  159. exit 1
  160. }
  161. ipkg_dir_part() {
  162. local dir="`echo $1 | sed -ne 's/\(.*\/\).*/\1/p'`"
  163. if [ -z "$dir" ]; then
  164. dir="./"
  165. fi
  166. echo $dir
  167. }
  168. ipkg_file_part() {
  169. echo $1 | sed 's/.*\///'
  170. }
  171. ipkg_protect_slashes() {
  172. sed -e 's/\//\\\//g'
  173. }
  174. ipkg_download() {
  175. local src="$1"
  176. local dest="$2"
  177. local src_file="`ipkg_file_part $src`"
  178. local dest_dir="`ipkg_dir_part $dest`"
  179. if [ -z "$dest_dir" ]; then
  180. dest_dir="$IPKG_TMP"
  181. fi
  182. local dest_file="`ipkg_file_part $dest`"
  183. if [ -z "$dest_file" ]; then
  184. dest_file="$src_file"
  185. fi
  186. # Proxy support
  187. local proxyuser=""
  188. local proxypassword=""
  189. local proxyoption=""
  190. if [ -n "$IPKG_PROXY_USERNAME" ]; then
  191. proxyuser="--proxy-user=\"$IPKG_PROXY_USERNAME\""
  192. proxypassword="--proxy-passwd=\"$IPKG_PROXY_PASSWORD\""
  193. fi
  194. if [ -n "$IPKG_PROXY_HTTP" -o -n "$IPKG_PROXY_FTP" ]; then
  195. proxyoption="--proxy=on"
  196. fi
  197. rm -f $IPKG_TMP/$src_file
  198. case "$src" in
  199. http://* | ftp://*)
  200. if ! wget --passive-ftp $proxyoption $proxyuser $proxypassword -P $IPKG_TMP $src; then
  201. echo "ipkg_download: ERROR: Failed to retrieve $src, returning $err" >&2
  202. return 1
  203. fi
  204. mv $IPKG_TMP/$src_file $dest_dir/$dest_file 2>/dev/null
  205. ;;
  206. file:/* )
  207. ln -s `echo $src | sed 's/^file://'` $dest_dir/$dest_file 2>/dev/null
  208. ;;
  209. *)
  210. echo "DEBUG: $src"
  211. ;;
  212. esac
  213. return 0
  214. }
  215. ipkg_update() {
  216. if [ ! -e "$IPKG_LISTS_DIR" ]; then
  217. mkdir -p $IPKG_LISTS_DIR
  218. fi
  219. local err=
  220. for src_name in `ipkg_src_names`; do
  221. local src="`ipkg_src_byname $src_name`"
  222. if ! ipkg_download $src/Packages $IPKG_LISTS_DIR/$src_name; then
  223. echo "ipkg_update: Error downloading $src/Packages to $IPKG_LISTS_DIR/$src_name" >&2
  224. err=t
  225. else
  226. echo "Updated list of available packages in $IPKG_LISTS_DIR/$src_name"
  227. fi
  228. done
  229. [ -n "$err" ] && return 1
  230. return 0
  231. }
  232. ipkg_list() {
  233. for src in `ipkg_src_names`; do
  234. if ipkg_require_list $src; then
  235. # black magic...
  236. sed -ne "
  237. /^Package:/{
  238. s/^Package:[[:space:]]*\<\([a-z0-9.+-]*$1[a-z0-9.+-]*\).*/\1/
  239. h
  240. }
  241. /^Description:/{
  242. s/^Description:[[:space:]]*\(.*\)/\1/
  243. H
  244. g
  245. s/\\
  246. / - /
  247. p
  248. }
  249. " $IPKG_LISTS_DIR/$src
  250. fi
  251. done
  252. }
  253. ipkg_extract_paragraph() {
  254. local pkg="$1"
  255. sed -ne "/Package:[[:space:]]*$pkg[[:space:]]*\$/,/^\$/p"
  256. }
  257. ipkg_extract_field() {
  258. local field="$1"
  259. # blacker magic...
  260. sed -ne "
  261. : TOP
  262. /^$field:/{
  263. p
  264. n
  265. b FIELD
  266. }
  267. d
  268. : FIELD
  269. /^$/b TOP
  270. /^[^[:space:]]/b TOP
  271. p
  272. n
  273. b FIELD
  274. "
  275. }
  276. ipkg_extract_value() {
  277. sed -e "s/^[^:]*:[[:space:]]*//"
  278. }
  279. ipkg_require_list() {
  280. [ $# -lt 1 ] && return 1
  281. local src="$1"
  282. if [ ! -f "$IPKG_LISTS_DIR/$src" ]; then
  283. echo "ERROR: File not found: $IPKG_LISTS_DIR/$src" >&2
  284. echo " You probably want to run \`ipkg update'" >&2
  285. return 1
  286. fi
  287. return 0
  288. }
  289. ipkg_info() {
  290. for src in `ipkg_src_names`; do
  291. if ipkg_require_list $src; then
  292. case $# in
  293. 0)
  294. cat $IPKG_LISTS_DIR/$src
  295. ;;
  296. 1)
  297. ipkg_extract_paragraph $1 < $IPKG_LISTS_DIR/$src
  298. ;;
  299. *)
  300. ipkg_extract_paragraph $1 < $IPKG_LISTS_DIR/$src | ipkg_extract_field $2
  301. ;;
  302. esac
  303. fi
  304. done
  305. }
  306. ipkg_status_sd() {
  307. [ $# -lt 1 ] && return 0
  308. sd="$1"
  309. shift
  310. if [ -f $sd/status ]; then
  311. case $# in
  312. 0)
  313. cat $sd/status
  314. ;;
  315. 1)
  316. ipkg_extract_paragraph $1 < $sd/status
  317. ;;
  318. *)
  319. ipkg_extract_paragraph $1 < $sd/status | ipkg_extract_field $2
  320. ;;
  321. esac
  322. fi
  323. return 0
  324. }
  325. ipkg_status_all() {
  326. for sd in `ipkg_state_dirs`; do
  327. ipkg_status_sd $sd $*
  328. done
  329. }
  330. ipkg_status() {
  331. if [ -n "$DEST_NAME" ]; then
  332. ipkg_status_sd $IPKG_STATE_DIR $*
  333. else
  334. ipkg_status_all $*
  335. fi
  336. }
  337. ipkg_status_matching_sd() {
  338. local sd="$1"
  339. local re="$2"
  340. if [ -f $sd/status ]; then
  341. sed -ne "
  342. : TOP
  343. /^Package:/{
  344. s/^Package:[[:space:]]*//
  345. s/[[:space:]]*$//
  346. h
  347. }
  348. /$re/{
  349. g
  350. p
  351. b NEXT
  352. }
  353. d
  354. : NEXT
  355. /^$/b TOP
  356. n
  357. b NEXT
  358. " < $sd/status
  359. fi
  360. return 0
  361. }
  362. ipkg_status_matching_all() {
  363. for sd in `ipkg_state_dirs`; do
  364. ipkg_status_matching_sd $sd $*
  365. done
  366. }
  367. ipkg_status_matching() {
  368. if [ -n "$DEST_NAME" ]; then
  369. ipkg_status_matching_sd $IPKG_STATE_DIR $*
  370. else
  371. ipkg_status_matching_all $*
  372. fi
  373. }
  374. ipkg_status_installed_sd() {
  375. local sd="$1"
  376. local pkg="$2"
  377. ipkg_status_sd $sd $pkg Status | grep -q "Status: install ok installed"
  378. }
  379. ipkg_status_installed_all() {
  380. local ret=1
  381. for sd in `ipkg_state_dirs`; do
  382. if `ipkg_status_installed_sd $sd $*`; then
  383. ret=0
  384. fi
  385. done
  386. return $ret
  387. }
  388. ipkg_status_mentioned_sd() {
  389. local sd="$1"
  390. local pkg="$2"
  391. [ -n "`ipkg_status_sd $sd $pkg Status`" ]
  392. }
  393. ipkg_files() {
  394. local pkg="$1"
  395. if [ -n "$DEST_NAME" ]; then
  396. dests=$IPKG_ROOT
  397. else
  398. dests="`ipkg_dests_all`"
  399. fi
  400. for dest in $dests; do
  401. if [ -f $dest/$IPKG_DIR_PREFIX/info/$pkg.list ]; then
  402. dest_sed="`echo $dest | ipkg_protect_slashes`"
  403. sed -e "s/^/$dest_sed/" < $dest/$IPKG_DIR_PREFIX/info/$pkg.list
  404. fi
  405. done
  406. }
  407. ipkg_search() {
  408. local pattern="$1"
  409. for dest_name in `ipkg_dest_names`; do
  410. dest="`ipkg_dest_byname $dest_name`"
  411. dest_sed="`echo $dest | ipkg_protect_slashes`"
  412. set +o noglob
  413. local list_files="`ls -1 $dest/$IPKG_DIR_PREFIX/info/*.list 2>/dev/null`"
  414. set -o noglob
  415. for file in $list_files; do
  416. if sed "s/^/$dest_sed/" $file | grep -q $pattern; then
  417. local pkg="`echo $file | sed "s/^.*\/\(.*\)\.list/\1/"`"
  418. [ "$dest_name" != `ipkg_dest_default_name` ] && pkg="$pkg ($dest_name)"
  419. sed "s/^/$dest_sed/" $file | grep $pattern | sed "s/^/$pkg: /"
  420. fi
  421. done
  422. done
  423. }
  424. ipkg_status_remove_sd() {
  425. local sd="$1"
  426. local pkg="$2"
  427. if [ ! -f $sd/status ]; then
  428. mkdir -p $sd
  429. touch $sd/status
  430. fi
  431. sed -ne "/Package:[[:space:]]*$pkg[[:space:]]*\$/,/^\$/!p" < $sd/status > $sd/status.new
  432. mv $sd/status.new $sd/status
  433. }
  434. ipkg_status_remove_all() {
  435. for sd in `ipkg_state_dirs`; do
  436. ipkg_status_remove_sd $sd $*
  437. done
  438. }
  439. ipkg_status_remove() {
  440. if [ -n "$DEST_NAME" ]; then
  441. ipkg_status_remove_sd $IPKG_STATE_DIR $*
  442. else
  443. ipkg_status_remove_all $*
  444. fi
  445. }
  446. ipkg_status_update_sd() {
  447. local sd="$1"
  448. local pkg="$2"
  449. ipkg_status_remove_sd $sd $pkg
  450. ipkg_extract_field "$IPKG_STATUS_FIELDS" >> $sd/status
  451. echo "" >> $sd/status
  452. }
  453. ipkg_status_update() {
  454. ipkg_status_update_sd $IPKG_STATE_DIR $*
  455. }
  456. ipkg_unsatisfied_dependences() {
  457. local pkg=$1
  458. local deps="`ipkg_get_depends $pkg`"
  459. local remaining_deps=
  460. for dep in $deps; do
  461. local installed="`ipkg_get_installed $dep`"
  462. if [ "$installed" != "installed" ] ; then
  463. remaining_deps="$remaining_deps $dep"
  464. fi
  465. done
  466. ## echo "ipkg_unsatisfied_dependences pkg=$pkg $remaining_deps" > /dev/console
  467. echo $remaining_deps
  468. }
  469. ipkg_safe_pkg_name() {
  470. local pkg=$1
  471. local spkg="`echo pkg_$pkg | sed -e y/-+./___/`"
  472. echo $spkg
  473. }
  474. ipkg_set_depends() {
  475. local pkg=$1; shift
  476. local new_deps="$*"
  477. pkg="`ipkg_safe_pkg_name $pkg`"
  478. ## setvar ${pkg}_depends "$new_deps"
  479. echo $new_deps > $IPKG_TMP/${pkg}.depends
  480. }
  481. ipkg_get_depends() {
  482. local pkg=$1
  483. pkg="`ipkg_safe_pkg_name $pkg`"
  484. cat $IPKG_TMP/${pkg}.depends
  485. ## eval "echo \$${pkg}_depends"
  486. }
  487. ipkg_set_installed() {
  488. local pkg=$1
  489. pkg="`ipkg_safe_pkg_name $pkg`"
  490. echo installed > $IPKG_TMP/${pkg}.installed
  491. ## setvar ${pkg}_installed "installed"
  492. }
  493. ipkg_set_uninstalled() {
  494. local pkg=$1
  495. pkg="`ipkg_safe_pkg_name $pkg`"
  496. ### echo ipkg_set_uninstalled $pkg > /dev/console
  497. echo uninstalled > $IPKG_TMP/${pkg}.installed
  498. ## setvar ${pkg}_installed "uninstalled"
  499. }
  500. ipkg_get_installed() {
  501. local pkg=$1
  502. pkg="`ipkg_safe_pkg_name $pkg`"
  503. if [ -f $IPKG_TMP/${pkg}.installed ]; then
  504. cat $IPKG_TMP/${pkg}.installed
  505. fi
  506. ## eval "echo \$${pkg}_installed"
  507. }
  508. ipkg_depends() {
  509. local new_pkgs="$*"
  510. local all_deps=
  511. local installed_pkgs="`ipkg_status_matching_all 'Status:.*[[:space:]]installed'`"
  512. for pkg in $installed_pkgs; do
  513. ipkg_set_installed $pkg
  514. done
  515. while [ -n "$new_pkgs" ]; do
  516. all_deps="$all_deps $new_pkgs"
  517. local new_deps=
  518. for pkg in $new_pkgs; do
  519. if echo $pkg | grep -q '[^A-Za-z0-9.+-]'; then
  520. echo "ipkg_depends: ERROR: Package name $pkg contains illegal characters (should be [A-Za-z0-9.+-])" >&2
  521. return 1
  522. fi
  523. # TODO: Fix this. For now I am ignoring versions and alternations in dependencies.
  524. new_deps="$new_deps "`ipkg_info $pkg '\(Pre-\)\?Depends' | ipkg_extract_value | sed -e 's/([^)]*)//g
  525. s/\(|[[:space:]]*[a-z0-9.+-]\+[[:space:]]*\)\+//g
  526. s/,/ /g
  527. s/ \+/ /g'`
  528. ipkg_set_depends $pkg $new_deps
  529. done
  530. new_deps=`echo $new_deps | sed -e 's/[[:space:]]\+/\n/g' | sort | uniq`
  531. local maybe_new_pkgs=
  532. for pkg in $new_deps; do
  533. if ! echo $installed_pkgs | grep -q "\<$pkg\>"; then
  534. maybe_new_pkgs="$maybe_new_pkgs $pkg"
  535. fi
  536. done
  537. new_pkgs=
  538. for pkg in $maybe_new_pkgs; do
  539. if ! echo $all_deps | grep -q "\<$pkg\>"; then
  540. if [ -z "`ipkg_info $pkg`" ]; then
  541. echo "ipkg_depends: Warning: $pkg mentioned in dependency but no package found in $IPKG_LISTS_DIR" >&2
  542. ipkg_set_installed $pkg
  543. else
  544. new_pkgs="$new_pkgs $pkg"
  545. ipkg_set_uninstalled $pkg
  546. fi
  547. else
  548. ipkg_set_uninstalled $pkg
  549. fi
  550. done
  551. done
  552. echo $all_deps
  553. }
  554. ipkg_get_install_dest() {
  555. local dest="$1"
  556. shift
  557. local sd=$dest/$IPKG_DIR_PREFIX
  558. local info_dir=$sd/info
  559. local requested_pkgs="$*"
  560. local pkgs="`ipkg_depends $*`"
  561. mkdir -p $info_dir
  562. for pkg in $pkgs; do
  563. if ! ipkg_status_mentioned_sd $sd $pkg; then
  564. echo "Package: $pkg
  565. Status: install ok not-installed" | ipkg_status_update_sd $sd $pkg
  566. fi
  567. done
  568. ## mark the packages that we were directly requested to install as uninstalled
  569. for pkg in $requested_pkgs; do ipkg_set_uninstalled $pkg; done
  570. local new_pkgs=
  571. local pkgs_installed=0
  572. while [ -n "pkgs" ]; do
  573. curcheck=0
  574. ## echo "pkgs to install: {$pkgs}" > /dev/console
  575. for pkg in $pkgs; do
  576. curcheck="`expr $curcheck + 1`"
  577. local is_installed="`ipkg_get_installed $pkg`"
  578. if [ "$is_installed" = "installed" ]; then
  579. echo "$pkg is installed"
  580. continue
  581. fi
  582. local remaining_deps="`ipkg_unsatisfied_dependences $pkg`"
  583. if [ -n "$remaining_deps" ]; then
  584. new_pkgs="$new_pkgs $pkg"
  585. ### echo "Dependences not satisfied for $pkg: $remaining_deps"
  586. if [ $curcheck -ne `echo $pkgs|wc -w` ]; then
  587. continue
  588. fi
  589. fi
  590. local filename=
  591. for src in `ipkg_src_names`; do
  592. if ipkg_require_list $src; then
  593. filename="`ipkg_extract_paragraph $pkg < $IPKG_LISTS_DIR/$src | ipkg_extract_field Filename | ipkg_extract_value`"
  594. [ -n "$filename" ] && break
  595. fi
  596. done
  597. if [ -z "$filename" ]; then
  598. echo "ipkg_get_install: ERROR: Cannot find package $pkg in $IPKG_LISTS_DIR" >&2
  599. echo "ipkg_get_install: Check the spelling and maybe run \`ipkg update'." >&2
  600. ipkg_status_remove_sd $sd $pkg
  601. return 1;
  602. fi
  603. local tmp_pkg_file="$IPKG_TMP/"`ipkg_file_part $filename`
  604. if ! ipkg_download `ipkg_src_byname $src`/$filename $tmp_pkg_file; then
  605. echo "ipkg_get_install: Perhaps you need to run \`ipkg update'?"
  606. return 1
  607. fi
  608. if ! ipkg_install_file_dest $dest $tmp_pkg_file; then
  609. echo "ipkg_get_install: ERROR: Failed to install $tmp_pkg_file" >&2
  610. echo "ipkg_get_install: I'll leave it there for you to try a manual installation" >&2
  611. return 1
  612. fi
  613. ipkg_set_installed $pkg
  614. pkgs_installed="`expr $pkgs_installed + 1`"
  615. rm $tmp_pkg_file
  616. done
  617. ### echo "Installed $pkgs_installed package(s) this round"
  618. if [ $pkgs_installed -eq 0 ]; then
  619. if [ -z "$new_pkgs" ]; then
  620. break
  621. fi
  622. fi
  623. pkgs_installed=0
  624. pkgs="$new_pkgs"
  625. new_pkgs=
  626. curcheck=0
  627. done
  628. }
  629. ipkg_get_install() {
  630. ipkg_get_install_dest $IPKG_ROOT $*
  631. }
  632. ipkg_install_file_dest() {
  633. local dest="$1"
  634. local filename="$2"
  635. local sd=$dest/$IPKG_DIR_PREFIX
  636. local info_dir=$sd/info
  637. if [ ! -f "$filename" ]; then
  638. echo "ipkg_install_file: ERROR: File $filename not found" >&2
  639. return 1
  640. fi
  641. local pkg="`ipkg_file_part $filename | sed 's/\([a-z0-9.+-]\+\)_.*/\1/'`"
  642. local ext="`echo $filename | sed 's/.*\.//'`"
  643. local pkg_extract_stdout
  644. if [ "$ext" = "ipk" ]; then
  645. pkg_extract_stdout="tar -xzOf"
  646. elif [ "$ext" = "deb" ]; then
  647. pkg_extract_stdout="ar p"
  648. else
  649. echo "ipkg_install_file: ERROR: File $filename has unknown extension $ext (not .ipk or .deb)" >&2
  650. return 1
  651. fi
  652. # Check dependencies
  653. local depends="`ipkg_depends $pkg | sed -e "s/\<$pkg\>//"`"
  654. # Don't worry about deps that are scheduled for installation
  655. local missing_deps=
  656. for dep in $depends; do
  657. if ! ipkg_status_all $dep | grep -q 'Status:[[:space:]]install'; then
  658. missing_deps="$missing_deps $dep"
  659. fi
  660. done
  661. if [ ! -z "$missing_deps" ]; then
  662. if [ -n "$FORCE_DEPENDS" ]; then
  663. echo "ipkg_install_file: Warning: $pkg depends on the following uninstalled programs: $missing_deps" >&2
  664. else
  665. echo "ipkg_install_file: ERROR: $pkg depends on the following uninstalled programs:
  666. $missing_deps" >&2
  667. echo "ipkg_install_file: You may want to use \`ipkg install' to install these." >&2
  668. return 1
  669. fi
  670. fi
  671. mkdir -p $IPKG_TMP/$pkg/control
  672. mkdir -p $IPKG_TMP/$pkg/data
  673. mkdir -p $info_dir
  674. if ! $pkg_extract_stdout $filename ./control.tar.gz | (cd $IPKG_TMP/$pkg/control; zcat | tar -xf - ) ; then
  675. echo "ipkg_install_file: ERROR unpacking control.tar.gz from $filename" >&2
  676. return 1
  677. fi
  678. if [ -n "$IPKG_OFFLINE_ROOT" ]; then
  679. if grep -q '^InstallsOffline:[[:space:]]*no' $IPKG_TMP/$pkg/control/control; then
  680. echo "*** Warning: Package $pkg may not be installed in offline mode" >&2
  681. echo "*** Warning: Scheduling $filename for pending installation (installing into $IPKG_PENDING_DIR)" >&2
  682. echo "Package: $pkg
  683. Status: install ok pending" | ipkg_status_update_sd $sd $pkg
  684. mkdir -p $IPKG_PENDING_DIR
  685. cp -f $filename $IPKG_PENDING_DIR
  686. rm -r $IPKG_TMP/$pkg/control
  687. rm -r $IPKG_TMP/$pkg/data
  688. rmdir $IPKG_TMP/$pkg
  689. return 0
  690. fi
  691. fi
  692. printf "Unpacking $pkg..."
  693. set +o noglob
  694. for file in $IPKG_TMP/$pkg/control/*; do
  695. local base_file="`ipkg_file_part $file`"
  696. mv $file $info_dir/$pkg.$base_file
  697. done
  698. set -o noglob
  699. rm -r $IPKG_TMP/$pkg/control
  700. if ! $pkg_extract_stdout $filename ./data.tar.gz | (cd $IPKG_TMP/$pkg/data; zcat | tar -xf - ) ; then
  701. echo "ipkg_install_file: ERROR unpacking data.tar.gz from $filename" >&2
  702. return 1
  703. fi
  704. echo "Done."
  705. printf "Configuring $pkg..."
  706. export PKG_ROOT=$dest
  707. if [ -x "$info_dir/$pkg.preinst" ]; then
  708. if ! $info_dir/$pkg.preinst install; then
  709. echo "$info_dir/$pkg.preinst failed. Aborting installation of $pkg"
  710. rm -rf $IPKG_TMP/$pkg/data
  711. rmdir $IPKG_TMP/$pkg
  712. return 1
  713. fi
  714. fi
  715. local old_conffiles="`ipkg_status_sd $sd $pkg Conffiles | ipkg_extract_value`"
  716. local new_conffiles=
  717. if [ -f "$info_dir/$pkg.conffiles" ]; then
  718. for conffile in `cat $info_dir/$pkg.conffiles`; do
  719. if [ -f "$dest/$conffile" ] && ! echo " $old_conffiles " | grep -q " $conffile "`md5sum $dest/$conffile | sed 's/ .*//'`; then
  720. local use_maintainers_conffile=
  721. if [ -z "$FORCE_DEFAULTS" ]; then
  722. while true; do
  723. printf "Configuration file \`$conffile'
  724. ==> File on system created by you or by a script.
  725. ==> File also in package provided by package maintainer.
  726. What would you like to do about it ? Your options are:
  727. Y or I : install the package maintainer's version
  728. N or O : keep your currently-installed version
  729. D : show the differences between the versions (if diff is installed)
  730. The default action is to keep your current version.
  731. *** `ipkg_file_part $conffile` (Y/I/N/O/D) [default=N] ? "
  732. read response
  733. case "$response" in
  734. [YyIi] | [Yy][Ee][Ss])
  735. use_maintainers_conffile=t
  736. break
  737. ;;
  738. [Dd])
  739. echo "
  740. diff -u $dest/$conffile $IPKG_TMP/$pkg/data/$conffile"
  741. diff -u $dest/$conffile $IPKG_TMP/$pkg/data/$conffile || true
  742. echo "[Press ENTER to continue]"
  743. read junk
  744. ;;
  745. *)
  746. break
  747. ;;
  748. esac
  749. done
  750. fi
  751. if [ -n "$use_maintainers_conffile" ]; then
  752. local md5sum="`md5sum $IPKG_TMP/$pkg/data/$conffile | sed 's/ .*//'`"
  753. new_conffiles="$new_conffiles $conffile $md5sum"
  754. else
  755. new_conffiles="$new_conffiles $conffile <custom>"
  756. rm $IPKG_TMP/$pkg/data/$conffile
  757. fi
  758. else
  759. md5sum="`md5sum $IPKG_TMP/$pkg/data/$conffile | sed 's/ .*//'`"
  760. new_conffiles="$new_conffiles $conffile $md5sum"
  761. fi
  762. done
  763. fi
  764. local owd="`pwd`"
  765. (cd $IPKG_TMP/$pkg/data/; tar cf - . | (cd $owd; cd $dest; tar xf -))
  766. rm -rf $IPKG_TMP/$pkg/data
  767. rmdir $IPKG_TMP/$pkg
  768. rm -f $info_dir/$pkg.list
  769. $pkg_extract_stdout $filename ./data.tar.gz | zcat | tar tf - | sed -e '/\/$/d; s/^\.//' > $info_dir/$pkg.list
  770. if [ -x "$info_dir/$pkg.postinst" ]; then
  771. $info_dir/$pkg.postinst configure
  772. fi
  773. if [ -n "$new_conffiles" ]; then
  774. new_conffiles='Conffiles: '`echo $new_conffiles | ipkg_protect_slashes`
  775. fi
  776. local sed_safe_offline_root="`echo ${IPKG_OFFLINE_ROOT} | ipkg_protect_slashes`"
  777. local sed_safe_root="`echo $dest | sed -e "s/^${sed_safe_offline_root}//" | ipkg_protect_slashes`"
  778. sed -e "s/\(Package:.*\)/\1\\
  779. Status: install ok installed\\
  780. Root: ${sed_safe_root}\\
  781. ${new_conffiles}/" $info_dir/$pkg.control | ipkg_status_update_sd $sd $pkg
  782. rm -f $info_dir/$pkg.control
  783. rm -f $info_dir/$pkg.conffiles
  784. rm -f $info_dir/$pkg.preinst
  785. rm -f $info_dir/$pkg.postinst
  786. echo "Done."
  787. }
  788. ipkg_install_file() {
  789. ipkg_install_file_dest $IPKG_ROOT $*
  790. }
  791. ipkg_install() {
  792. while [ $# -gt 0 ]; do
  793. local pkg="$1"
  794. shift
  795. case "$pkg" in
  796. http://* | ftp://*)
  797. local tmp_pkg_file="$IPKG_TMP/"`ipkg_file_part $pkg`
  798. if ipkg_download $pkg $tmp_pkg_file; then
  799. ipkg_install_file $tmp_pkg_file
  800. rm $tmp_pkg_file
  801. fi
  802. ;;
  803. file:/*.ipk | file://*.deb)
  804. local ipkg_filename="`echo $pkg|sed 's/^file://'`"
  805. ipkg_install_file $ipkg_filename
  806. ;;
  807. *.ipk | *.deb)
  808. ipkg_install_file $pkg
  809. ;;
  810. *)
  811. ipkg_get_install $pkg || true
  812. ;;
  813. esac
  814. done
  815. }
  816. ipkg_install_pending() {
  817. [ -n "$IPKG_OFFLINE_ROOT" ] && return 0
  818. if [ -d "$IPKG_PENDING_DIR" ]; then
  819. set +o noglob
  820. local pending="`ls -1d $IPKG_PENDING_DIR/*.ipk 2> /dev/null`" || true
  821. set -o noglob
  822. if [ -n "$pending" ]; then
  823. echo "The following packages in $IPKG_PENDING_DIR will now be installed:"
  824. echo $pending
  825. for filename in $pending; do
  826. if ipkg_install_file $filename; then
  827. rm $filename
  828. fi
  829. done
  830. fi
  831. fi
  832. return 0
  833. }
  834. ipkg_install_wanted() {
  835. local wanted="`ipkg_status_matching 'Status:[[:space:]]*install.*not-installed'`"
  836. if [ -n "$wanted" ]; then
  837. echo "The following package were previously requested but have not been installed:"
  838. echo $wanted
  839. if [ -n "$FORCE_DEFAULTS" ]; then
  840. echo "Installing them now."
  841. else
  842. printf "Install them now [Y/n] ? "
  843. read response
  844. case "$response" in
  845. [Nn] | [Nn][Oo])
  846. return 0
  847. ;;
  848. esac
  849. fi
  850. ipkg_install $wanted
  851. fi
  852. return 0
  853. }
  854. ipkg_upgrade_pkg() {
  855. local pkg="$1"
  856. local avail_ver="`ipkg_info $pkg Version | ipkg_extract_value | head -n 1`"
  857. is_installed=
  858. for dest_name in `ipkg_dest_names`; do
  859. local dest="`ipkg_dest_byname $dest_name`"
  860. local sd=$dest/$IPKG_DIR_PREFIX
  861. local inst_ver="`ipkg_status_sd $sd $pkg Version | ipkg_extract_value`"
  862. if [ -n "$inst_ver" ]; then
  863. is_installed=t
  864. if [ -z "$avail_ver" ]; then
  865. echo "Assuming locally installed package $pkg ($inst_ver) is up to date"
  866. return 0
  867. fi
  868. if [ "$avail_ver" = "$inst_ver" ]; then
  869. echo "Package $pkg ($inst_ver) installed in $dest_name is up to date"
  870. elif ipkg_is_upgrade "$avail_ver" "$inst_ver"; then
  871. echo "Upgrading $pkg ($dest_name) from $inst_ver to $avail_ver"
  872. ipkg_get_install_dest $dest $pkg
  873. else
  874. echo "Not downgrading package $pkg from $inst_ver to $avail_ver"
  875. fi
  876. fi
  877. done
  878. if [ -z "$is_installed" ]; then
  879. echo "Package $pkg does not appear to be installed"
  880. return 0
  881. fi
  882. }
  883. ipkg_upgrade() {
  884. if [ $# -lt 1 ]; then
  885. local pkgs="`ipkg_status_matching 'Status:.*[[:space:]]installed'`"
  886. else
  887. pkgs="$*"
  888. fi
  889. for pkg in $pkgs; do
  890. ipkg_upgrade_pkg $pkg
  891. done
  892. }
  893. ipkg_remove_pkg_dest() {
  894. local dest="$1"
  895. local pkg="$2"
  896. local sd=$dest/$IPKG_DIR_PREFIX
  897. local info_dir=$sd/info
  898. if ! ipkg_status_installed_sd $sd $pkg; then
  899. echo "ipkg_remove: Package $pkg does not appear to be installed in $dest"
  900. if ipkg_status_mentioned_sd $sd $pkg; then
  901. echo "Purging mention of $pkg from the ipkg database"
  902. ipkg_status_remove_sd $sd $pkg
  903. fi
  904. return 1
  905. fi
  906. echo "ipkg_remove: Removing $pkg... "
  907. local files="`cat $info_dir/$pkg.list`"
  908. export PKG_ROOT=$dest
  909. if [ -x "$info_dir/$pkg.prerm" ]; then
  910. $info_dir/$pkg.prerm remove
  911. fi
  912. local conffiles="`ipkg_status_sd $sd $pkg Conffiles | ipkg_extract_value`"
  913. local dirs_to_remove=
  914. for file in $files; do
  915. if [ -d "$dest/$file" ]; then
  916. dirs_to_remove="$dirs_to_remove $dest/$file"
  917. else
  918. if echo " $conffiles " | grep -q " $file "; then
  919. if echo " $conffiles " | grep -q " $file "`md5sum $dest/$file | sed 's/ .*//'`; then
  920. rm -f $dest/$file
  921. fi
  922. else
  923. rm -f $dest/$file
  924. fi
  925. fi
  926. done
  927. local removed_a_dir=t
  928. while [ -n "$removed_a_dir" ]; do
  929. removed_a_dir=
  930. local new_dirs_to_remove=
  931. for dir in $dirs_to_remove; do
  932. if rmdir $dir >/dev/null 2>&1; then
  933. removed_a_dir=t
  934. else
  935. new_dirs_to_remove="$new_dirs_to_remove $dir"
  936. fi
  937. done
  938. dirs_to_remove="$new_dirs_to_remove"
  939. done
  940. if [ -n "$dirs_to_remove" ]; then
  941. echo "ipkg_remove: Warning: Not removing the following directories since they are not empty:" >&2
  942. echo "$dirs_to_remove" | sed -e 's/\/[/]\+/\//g' >&2
  943. fi
  944. if [ -x "$info_dir/$pkg.postrm" ]; then
  945. $info_dir/$pkg.postrm remove
  946. fi
  947. ipkg_status_remove_sd $sd $pkg
  948. set +o noglob
  949. rm -f $info_dir/$pkg.*
  950. set -o noglob
  951. echo "Done."
  952. }
  953. ipkg_remove_pkg() {
  954. local pkg="$1"
  955. for dest in `ipkg_dests_all`; do
  956. local sd=$dest/$IPKG_DIR_PREFIX
  957. if ipkg_status_mentioned_sd $sd $pkg; then
  958. ipkg_remove_pkg_dest $dest $pkg
  959. fi
  960. done
  961. }
  962. ipkg_remove() {
  963. while [ $# -gt 0 ]; do
  964. local pkg="$1"
  965. shift
  966. if [ -n "$DEST_NAME" ]; then
  967. ipkg_remove_pkg_dest $IPKG_ROOT $pkg
  968. else
  969. ipkg_remove_pkg $pkg
  970. fi
  971. done
  972. }
  973. ###########
  974. # ipkg main
  975. ###########
  976. # Parse options
  977. while [ $# -gt 0 ]; do
  978. arg="$1"
  979. case $arg in
  980. -d | -dest)
  981. [ $# -gt 1 ] || ipkg_usage "option $arg requires an argument"
  982. DEST_NAME="$2"
  983. shift
  984. ;;
  985. -o | -offline)
  986. [ $# -gt 1 ] || ipkg_usage "option $arg requires an argument"
  987. IPKG_OFFLINE_ROOT="$2"
  988. shift
  989. ;;
  990. -force-depends)
  991. FORCE_DEPENDS=t
  992. ;;
  993. -force-defaults)
  994. FORCE_DEFAULTS=t
  995. ;;
  996. -*)
  997. ipkg_usage "unknown option $arg"
  998. ;;
  999. *)
  1000. break
  1001. ;;
  1002. esac
  1003. shift
  1004. done
  1005. [ $# -lt 1 ] && ipkg_usage "ipkg must have one sub-command argument"
  1006. cmd="$1"
  1007. shift
  1008. ipkg_load_configuration
  1009. mkdir -p /tmp/ipkg
  1010. case "$cmd" in
  1011. update|upgrade|list|info|status|install_pending)
  1012. ;;
  1013. install|depends|remove|files|search)
  1014. [ $# -lt 1 ] && ipkg_usage "ERROR: the \`\`$cmd'' command requires an argument" >&2
  1015. ;;
  1016. *)
  1017. echo "ERROR: unknown sub-command \`$cmd'" >&2
  1018. ipkg_usage
  1019. ;;
  1020. esac
  1021. # Only install pending if we have an interactive sub-command
  1022. case "$cmd" in
  1023. upgrade|install)
  1024. ipkg_install_pending
  1025. ipkg_install_wanted
  1026. ;;
  1027. esac
  1028. ipkg_$cmd $*
  1029. for a in `ls $IPKG_TMP`; do
  1030. rm -rf $IPKG_TMP/$a
  1031. done