ipkg 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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/ipkg
  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\)'
  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. echo "Downloading $src ..."
  198. rm -f $IPKG_TMP/$src_file
  199. case "$src" in
  200. http://* | ftp://*)
  201. if ! wget --passive-ftp $proxyoption $proxyuser $proxypassword -P $IPKG_TMP $src; then
  202. echo "ipkg_download: ERROR: Failed to retrieve $src, returning $err"
  203. return 1
  204. fi
  205. mv $IPKG_TMP/$src_file $dest_dir/$dest_file 2>/dev/null
  206. ;;
  207. file:/* )
  208. ln -s `echo $src | sed 's/^file://'` $dest_dir/$dest_file 2>/dev/null
  209. ;;
  210. *)
  211. echo "DEBUG: $src"
  212. ;;
  213. esac
  214. echo "Done."
  215. return 0
  216. }
  217. ipkg_update() {
  218. if [ ! -e "$IPKG_LISTS_DIR" ]; then
  219. mkdir -p $IPKG_LISTS_DIR
  220. fi
  221. local err=
  222. for src_name in `ipkg_src_names`; do
  223. local src="`ipkg_src_byname $src_name`"
  224. if ! ipkg_download $src/Packages $IPKG_LISTS_DIR/$src_name; then
  225. echo "ipkg_update: Error downloading $src/Packages to $IPKG_LISTS_DIR/$src_name" >&2
  226. err=t
  227. else
  228. echo "Updated list of available packages in $IPKG_LISTS_DIR/$src_name"
  229. fi
  230. done
  231. [ -n "$err" ] && return 1
  232. return 0
  233. }
  234. ipkg_list() {
  235. for src in `ipkg_src_names`; do
  236. if ipkg_require_list $src; then
  237. # black magic...
  238. sed -ne "
  239. /^Package:/{
  240. s/^Package:[[:space:]]*\<\([a-z0-9.+-]*$1[a-z0-9.+-]*\).*/\1/
  241. h
  242. }
  243. /^Description:/{
  244. s/^Description:[[:space:]]*\(.*\)/\1/
  245. H
  246. g
  247. s/\\
  248. / - /
  249. p
  250. }
  251. " $IPKG_LISTS_DIR/$src
  252. fi
  253. done
  254. }
  255. ipkg_extract_paragraph() {
  256. local pkg="$1"
  257. sed -ne "/Package:[[:space:]]*$pkg[[:space:]]*\$/,/^\$/p"
  258. }
  259. ipkg_extract_field() {
  260. local field="$1"
  261. # blacker magic...
  262. sed -ne "
  263. : TOP
  264. /^$field:/{
  265. p
  266. n
  267. b FIELD
  268. }
  269. d
  270. : FIELD
  271. /^$/b TOP
  272. /^[^[:space:]]/b TOP
  273. p
  274. n
  275. b FIELD
  276. "
  277. }
  278. ipkg_extract_value() {
  279. sed -e "s/^[^:]*:[[:space:]]*//"
  280. }
  281. ipkg_require_list() {
  282. [ $# -lt 1 ] && return 1
  283. local src="$1"
  284. if [ ! -f "$IPKG_LISTS_DIR/$src" ]; then
  285. echo "ERROR: File not found: $IPKG_LISTS_DIR/$src" >&2
  286. echo " You probably want to run \`ipkg update'" >&2
  287. return 1
  288. fi
  289. return 0
  290. }
  291. ipkg_info() {
  292. for src in `ipkg_src_names`; do
  293. if ipkg_require_list $src; then
  294. case $# in
  295. 0)
  296. cat $IPKG_LISTS_DIR/$src
  297. ;;
  298. 1)
  299. ipkg_extract_paragraph $1 < $IPKG_LISTS_DIR/$src
  300. ;;
  301. *)
  302. ipkg_extract_paragraph $1 < $IPKG_LISTS_DIR/$src | ipkg_extract_field $2
  303. ;;
  304. esac
  305. fi
  306. done
  307. }
  308. ipkg_status_sd() {
  309. [ $# -lt 1 ] && return 0
  310. sd="$1"
  311. shift
  312. if [ -f $sd/status ]; then
  313. case $# in
  314. 0)
  315. cat $sd/status
  316. ;;
  317. 1)
  318. ipkg_extract_paragraph $1 < $sd/status
  319. ;;
  320. *)
  321. ipkg_extract_paragraph $1 < $sd/status | ipkg_extract_field $2
  322. ;;
  323. esac
  324. fi
  325. return 0
  326. }
  327. ipkg_status_all() {
  328. for sd in `ipkg_state_dirs`; do
  329. ipkg_status_sd $sd $*
  330. done
  331. }
  332. ipkg_status() {
  333. if [ -n "$DEST_NAME" ]; then
  334. ipkg_status_sd $IPKG_STATE_DIR $*
  335. else
  336. ipkg_status_all $*
  337. fi
  338. }
  339. ipkg_status_matching_sd() {
  340. local sd="$1"
  341. local re="$2"
  342. if [ -f $sd/status ]; then
  343. sed -ne "
  344. : TOP
  345. /^Package:/{
  346. s/^Package:[[:space:]]*//
  347. s/[[:space:]]*$//
  348. h
  349. }
  350. /$re/{
  351. g
  352. p
  353. b NEXT
  354. }
  355. d
  356. : NEXT
  357. /^$/b TOP
  358. n
  359. b NEXT
  360. " < $sd/status
  361. fi
  362. return 0
  363. }
  364. ipkg_status_matching_all() {
  365. for sd in `ipkg_state_dirs`; do
  366. ipkg_status_matching_sd $sd $*
  367. done
  368. }
  369. ipkg_status_matching() {
  370. if [ -n "$DEST_NAME" ]; then
  371. ipkg_status_matching_sd $IPKG_STATE_DIR $*
  372. else
  373. ipkg_status_matching_all $*
  374. fi
  375. }
  376. ipkg_status_installed_sd() {
  377. local sd="$1"
  378. local pkg="$2"
  379. ipkg_status_sd $sd $pkg Status | grep -q "Status: install ok installed"
  380. }
  381. ipkg_status_installed_all() {
  382. local ret=1
  383. for sd in `ipkg_state_dirs`; do
  384. if `ipkg_status_installed_sd $sd $*`; then
  385. ret=0
  386. fi
  387. done
  388. return $ret
  389. }
  390. ipkg_status_mentioned_sd() {
  391. local sd="$1"
  392. local pkg="$2"
  393. [ -n "`ipkg_status_sd $sd $pkg Status`" ]
  394. }
  395. ipkg_files() {
  396. local pkg="$1"
  397. if [ -n "$DEST_NAME" ]; then
  398. dests=$IPKG_ROOT
  399. else
  400. dests="`ipkg_dests_all`"
  401. fi
  402. for dest in $dests; do
  403. if [ -f $dest/$IPKG_DIR_PREFIX/info/$pkg.list ]; then
  404. dest_sed="`echo $dest | ipkg_protect_slashes`"
  405. sed -e "s/^/$dest_sed/" < $dest/$IPKG_DIR_PREFIX/info/$pkg.list
  406. fi
  407. done
  408. }
  409. ipkg_search() {
  410. local pattern="$1"
  411. for dest_name in `ipkg_dest_names`; do
  412. dest="`ipkg_dest_byname $dest_name`"
  413. dest_sed="`echo $dest | ipkg_protect_slashes`"
  414. set +o noglob
  415. local list_files="`ls -1 $dest/$IPKG_DIR_PREFIX/info/*.list 2>/dev/null`"
  416. set -o noglob
  417. for file in $list_files; do
  418. if sed "s/^/$dest_sed/" $file | grep -q $pattern; then
  419. local pkg="`echo $file | sed "s/^.*\/\(.*\)\.list/\1/"`"
  420. [ "$dest_name" != `ipkg_dest_default_name` ] && pkg="$pkg ($dest_name)"
  421. sed "s/^/$dest_sed/" $file | grep $pattern | sed "s/^/$pkg: /"
  422. fi
  423. done
  424. done
  425. }
  426. ipkg_status_remove_sd() {
  427. local sd="$1"
  428. local pkg="$2"
  429. if [ ! -f $sd/status ]; then
  430. mkdir -p $sd
  431. touch $sd/status
  432. fi
  433. sed -ne "/Package:[[:space:]]*$pkg[[:space:]]*\$/,/^\$/!p" < $sd/status > $sd/status.new
  434. mv $sd/status.new $sd/status
  435. }
  436. ipkg_status_remove_all() {
  437. for sd in `ipkg_state_dirs`; do
  438. ipkg_status_remove_sd $sd $*
  439. done
  440. }
  441. ipkg_status_remove() {
  442. if [ -n "$DEST_NAME" ]; then
  443. ipkg_status_remove_sd $IPKG_STATE_DIR $*
  444. else
  445. ipkg_status_remove_all $*
  446. fi
  447. }
  448. ipkg_status_update_sd() {
  449. local sd="$1"
  450. local pkg="$2"
  451. ipkg_status_remove_sd $sd $pkg
  452. ipkg_extract_field "$IPKG_STATUS_FIELDS" >> $sd/status
  453. echo "" >> $sd/status
  454. }
  455. ipkg_status_update() {
  456. ipkg_status_update_sd $IPKG_STATE_DIR $*
  457. }
  458. ipkg_unsatisfied_dependences() {
  459. local pkg=$1
  460. local deps="`ipkg_get_depends $pkg`"
  461. local remaining_deps=
  462. for dep in $deps; do
  463. local installed="`ipkg_get_installed $dep`"
  464. if [ "$installed" != "installed" ] ; then
  465. remaining_deps="$remaining_deps $dep"
  466. fi
  467. done
  468. ## echo "ipkg_unsatisfied_dependences pkg=$pkg $remaining_deps" > /dev/console
  469. echo $remaining_deps
  470. }
  471. ipkg_safe_pkg_name() {
  472. local pkg=$1
  473. local spkg="`echo pkg_$pkg | sed -e y/-+./___/`"
  474. echo $spkg
  475. }
  476. ipkg_set_depends() {
  477. local pkg=$1; shift
  478. local new_deps="$*"
  479. pkg="`ipkg_safe_pkg_name $pkg`"
  480. ## setvar ${pkg}_depends "$new_deps"
  481. echo $new_deps > $IPKG_TMP/${pkg}.depends
  482. }
  483. ipkg_get_depends() {
  484. local pkg=$1
  485. pkg="`ipkg_safe_pkg_name $pkg`"
  486. cat $IPKG_TMP/${pkg}.depends
  487. ## eval "echo \$${pkg}_depends"
  488. }
  489. ipkg_set_installed() {
  490. local pkg=$1
  491. pkg="`ipkg_safe_pkg_name $pkg`"
  492. echo installed > $IPKG_TMP/${pkg}.installed
  493. ## setvar ${pkg}_installed "installed"
  494. }
  495. ipkg_set_uninstalled() {
  496. local pkg=$1
  497. pkg="`ipkg_safe_pkg_name $pkg`"
  498. ### echo ipkg_set_uninstalled $pkg > /dev/console
  499. echo uninstalled > $IPKG_TMP/${pkg}.installed
  500. ## setvar ${pkg}_installed "uninstalled"
  501. }
  502. ipkg_get_installed() {
  503. local pkg=$1
  504. pkg="`ipkg_safe_pkg_name $pkg`"
  505. if [ -f $IPKG_TMP/${pkg}.installed ]; then
  506. cat $IPKG_TMP/${pkg}.installed
  507. fi
  508. ## eval "echo \$${pkg}_installed"
  509. }
  510. ipkg_depends() {
  511. local new_pkgs="$*"
  512. local all_deps=
  513. local installed_pkgs="`ipkg_status_matching_all 'Status:.*[[:space:]]installed'`"
  514. for pkg in $installed_pkgs; do
  515. ipkg_set_installed $pkg
  516. done
  517. while [ -n "$new_pkgs" ]; do
  518. all_deps="$all_deps $new_pkgs"
  519. local new_deps=
  520. for pkg in $new_pkgs; do
  521. if echo $pkg | grep -q '[^a-z0-9.+-]'; then
  522. echo "ipkg_depends: ERROR: Package name $pkg contains illegal characters (should be [a-z0-9.+-])" >&2
  523. return 1
  524. fi
  525. # TODO: Fix this. For now I am ignoring versions and alternations in dependencies.
  526. new_deps="$new_deps "`ipkg_info $pkg '\(Pre-\)\?Depends' | ipkg_extract_value | sed -e 's/([^)]*)//g
  527. s/\(|[[:space:]]*[a-z0-9.+-]\+[[:space:]]*\)\+//g
  528. s/,/ /g
  529. s/ \+/ /g'`
  530. ipkg_set_depends $pkg $new_deps
  531. done
  532. new_deps=`echo $new_deps | sed -e 's/[[:space:]]\+/\n/g' | sort | uniq`
  533. local maybe_new_pkgs=
  534. for pkg in $new_deps; do
  535. if ! echo $installed_pkgs | grep -q "\<$pkg\>"; then
  536. maybe_new_pkgs="$maybe_new_pkgs $pkg"
  537. fi
  538. done
  539. new_pkgs=
  540. for pkg in $maybe_new_pkgs; do
  541. if ! echo $all_deps | grep -q "\<$pkg\>"; then
  542. if [ -z "`ipkg_info $pkg`" ]; then
  543. echo "ipkg_depends: Warning: $pkg mentioned in dependency but no package found in $IPKG_LISTS_DIR" >&2
  544. ipkg_set_installed $pkg
  545. else
  546. new_pkgs="$new_pkgs $pkg"
  547. ipkg_set_uninstalled $pkg
  548. fi
  549. else
  550. ipkg_set_uninstalled $pkg
  551. fi
  552. done
  553. done
  554. echo $all_deps
  555. }
  556. ipkg_get_install_dest() {
  557. local dest="$1"
  558. shift
  559. local sd=$dest/$IPKG_DIR_PREFIX
  560. local info_dir=$sd/info
  561. local requested_pkgs="$*"
  562. local pkgs="`ipkg_depends $*`"
  563. mkdir -p $info_dir
  564. for pkg in $pkgs; do
  565. if ! ipkg_status_mentioned_sd $sd $pkg; then
  566. echo "Package: $pkg
  567. Status: install ok not-installed" | ipkg_status_update_sd $sd $pkg
  568. fi
  569. done
  570. ## mark the packages that we were directly requested to install as uninstalled
  571. for pkg in $requested_pkgs; do ipkg_set_uninstalled $pkg; done
  572. local new_pkgs=
  573. local pkgs_installed=0
  574. while [ -n "pkgs" ]; do
  575. curcheck=0
  576. ## echo "pkgs to install: {$pkgs}" > /dev/console
  577. for pkg in $pkgs; do
  578. curcheck="`expr $curcheck + 1`"
  579. local is_installed="`ipkg_get_installed $pkg`"
  580. if [ "$is_installed" = "installed" ]; then
  581. echo "$pkg is installed" > /dev/console
  582. continue
  583. fi
  584. local remaining_deps="`ipkg_unsatisfied_dependences $pkg`"
  585. if [ -n "$remaining_deps" ]; then
  586. new_pkgs="$new_pkgs $pkg"
  587. ### echo "Dependences not satisfied for $pkg: $remaining_deps"
  588. if [ $curcheck -ne `echo $pkgs|wc -w` ]; then
  589. continue
  590. fi
  591. fi
  592. local filename=
  593. for src in `ipkg_src_names`; do
  594. if ipkg_require_list $src; then
  595. filename="`ipkg_extract_paragraph $pkg < $IPKG_LISTS_DIR/$src | ipkg_extract_field Filename | ipkg_extract_value`"
  596. [ -n "$filename" ] && break
  597. fi
  598. done
  599. if [ -z "$filename" ]; then
  600. echo "ipkg_get_install: ERROR: Cannot find package $pkg in $IPKG_LISTS_DIR"
  601. echo "ipkg_get_install: Check the spelling and maybe run \`ipkg update'."
  602. ipkg_status_remove_sd $sd $pkg
  603. return 1;
  604. fi
  605. echo ""
  606. local tmp_pkg_file="$IPKG_TMP/"`ipkg_file_part $filename`
  607. if ! ipkg_download `ipkg_src_byname $src`/$filename $tmp_pkg_file; then
  608. echo "ipkg_get_install: Perhaps you need to run \`ipkg update'?"
  609. return 1
  610. fi
  611. if ! ipkg_install_file_dest $dest $tmp_pkg_file; then
  612. echo "ipkg_get_install: ERROR: Failed to install $tmp_pkg_file"
  613. echo "ipkg_get_install: I'll leave it there for you to try a manual installation"
  614. return 1
  615. fi
  616. ipkg_set_installed $pkg
  617. pkgs_installed="`expr $pkgs_installed + 1`"
  618. rm $tmp_pkg_file
  619. done
  620. ### echo "Installed $pkgs_installed package(s) this round"
  621. if [ $pkgs_installed -eq 0 ]; then
  622. if [ -z "$new_pkgs" ]; then
  623. break
  624. fi
  625. fi
  626. pkgs_installed=0
  627. pkgs="$new_pkgs"
  628. new_pkgs=
  629. curcheck=0
  630. done
  631. }
  632. ipkg_get_install() {
  633. ipkg_get_install_dest $IPKG_ROOT $*
  634. }
  635. ipkg_install_file_dest() {
  636. local dest="$1"
  637. local filename="$2"
  638. local sd=$dest/$IPKG_DIR_PREFIX
  639. local info_dir=$sd/info
  640. if [ ! -f "$filename" ]; then
  641. echo "ipkg_install_file: ERROR: File $filename not found"
  642. return 1
  643. fi
  644. local pkg="`ipkg_file_part $filename | sed 's/\([a-z0-9.+-]\+\)_.*/\1/'`"
  645. local ext="`echo $filename | sed 's/.*\.//'`"
  646. local pkg_extract_stdout
  647. if [ "$ext" = "ipk" ]; then
  648. pkg_extract_stdout="tar -xzOf"
  649. elif [ "$ext" = "deb" ]; then
  650. pkg_extract_stdout="ar p"
  651. else
  652. echo "ipkg_install_file: ERROR: File $filename has unknown extension $ext (not .ipk or .deb)"
  653. return 1
  654. fi
  655. # Check dependencies
  656. local depends="`ipkg_depends $pkg | sed -e "s/\<$pkg\>//"`"
  657. # Don't worry about deps that are scheduled for installation
  658. local missing_deps=
  659. for dep in $depends; do
  660. if ! ipkg_status_all $dep | grep -q 'Status:[[:space:]]install'; then
  661. missing_deps="$missing_deps $dep"
  662. fi
  663. done
  664. if [ ! -z "$missing_deps" ]; then
  665. if [ -n "$FORCE_DEPENDS" ]; then
  666. echo "ipkg_install_file: Warning: $pkg depends on the following uninstalled programs: $missing_deps"
  667. else
  668. echo "ipkg_install_file: ERROR: $pkg depends on the following uninstalled programs:
  669. $missing_deps"
  670. echo "ipkg_install_file: You may want to use \`ipkg install' to install these."
  671. return 1
  672. fi
  673. fi
  674. mkdir -p $IPKG_TMP/$pkg/control
  675. mkdir -p $IPKG_TMP/$pkg/data
  676. mkdir -p $info_dir
  677. if ! $pkg_extract_stdout $filename ./control.tar.gz | (cd $IPKG_TMP/$pkg/control; tar -xzf - ) ; then
  678. echo "ipkg_install_file: ERROR unpacking control.tar.gz from $filename"
  679. return 1
  680. fi
  681. if [ -n "$IPKG_OFFLINE_ROOT" ]; then
  682. if grep -q '^InstallsOffline:[[:space:]]*no' $IPKG_TMP/$pkg/control/control; then
  683. echo "*** Warning: Package $pkg may not be installed in offline mode"
  684. echo "*** Warning: Scheduling $filename for pending installation (installing into $IPKG_PENDING_DIR)"
  685. echo "Package: $pkg
  686. Status: install ok pending" | ipkg_status_update_sd $sd $pkg
  687. mkdir -p $IPKG_PENDING_DIR
  688. cp -f $filename $IPKG_PENDING_DIR
  689. rm -r $IPKG_TMP/$pkg/control
  690. rm -r $IPKG_TMP/$pkg/data
  691. rmdir $IPKG_TMP/$pkg
  692. return 0
  693. fi
  694. fi
  695. echo -n "Unpacking $pkg..."
  696. set +o noglob
  697. for file in $IPKG_TMP/$pkg/control/*; do
  698. local base_file="`ipkg_file_part $file`"
  699. mv $file $info_dir/$pkg.$base_file
  700. done
  701. set -o noglob
  702. rm -r $IPKG_TMP/$pkg/control
  703. if ! $pkg_extract_stdout $filename ./data.tar.gz | (cd $IPKG_TMP/$pkg/data; tar -xzf - ) ; then
  704. echo "ipkg_install_file: ERROR unpacking data.tar.gz from $filename"
  705. return 1
  706. fi
  707. echo "Done."
  708. echo -n "Configuring $pkg..."
  709. export PKG_ROOT=$dest
  710. if [ -x "$info_dir/$pkg.preinst" ]; then
  711. if ! $info_dir/$pkg.preinst install; then
  712. echo "$info_dir/$pkg.preinst failed. Aborting installation of $pkg"
  713. rm -rf $IPKG_TMP/$pkg/data
  714. rmdir $IPKG_TMP/$pkg
  715. return 1
  716. fi
  717. fi
  718. local old_conffiles="`ipkg_status_sd $sd $pkg Conffiles | ipkg_extract_value`"
  719. local new_conffiles=
  720. if [ -f "$info_dir/$pkg.conffiles" ]; then
  721. for conffile in `cat $info_dir/$pkg.conffiles`; do
  722. if [ -f "$dest/$conffile" ] && ! echo " $old_conffiles " | grep -q " $conffile "`md5sum $dest/$conffile | sed 's/ .*//'`; then
  723. local use_maintainers_conffile=
  724. if [ -z "$FORCE_DEFAULTS" ]; then
  725. while true; do
  726. echo -n "Configuration file \`$conffile'
  727. ==> File on system created by you or by a script.
  728. ==> File also in package provided by package maintainer.
  729. What would you like to do about it ? Your options are:
  730. Y or I : install the package maintainer's version
  731. N or O : keep your currently-installed version
  732. D : show the differences between the versions (if diff is installed)
  733. The default action is to keep your current version.
  734. *** `ipkg_file_part $conffile` (Y/I/N/O/D) [default=N] ? "
  735. read response
  736. case "$response" in
  737. [YyIi] | [Yy][Ee][Ss])
  738. use_maintainers_conffile=t
  739. break
  740. ;;
  741. [Dd])
  742. echo "
  743. diff -u $dest/$conffile $IPKG_TMP/$pkg/data/$conffile"
  744. diff -u $dest/$conffile $IPKG_TMP/$pkg/data/$conffile || true
  745. echo "[Press ENTER to continue]"
  746. read junk
  747. ;;
  748. *)
  749. break
  750. ;;
  751. esac
  752. done
  753. fi
  754. if [ -n "$use_maintainers_conffile" ]; then
  755. local md5sum="`md5sum $IPKG_TMP/$pkg/data/$conffile | sed 's/ .*//'`"
  756. new_conffiles="$new_conffiles $conffile $md5sum"
  757. else
  758. new_conffiles="$new_conffiles $conffile <custom>"
  759. rm $IPKG_TMP/$pkg/data/$conffile
  760. fi
  761. else
  762. md5sum="`md5sum $IPKG_TMP/$pkg/data/$conffile | sed 's/ .*//'`"
  763. new_conffiles="$new_conffiles $conffile $md5sum"
  764. fi
  765. done
  766. fi
  767. local owd="`pwd`"
  768. (cd $IPKG_TMP/$pkg/data/; tar cf - . | (cd $owd; cd $dest; tar xf -))
  769. rm -rf $IPKG_TMP/$pkg/data
  770. rmdir $IPKG_TMP/$pkg
  771. rm -f $info_dir/$pkg.list
  772. $pkg_extract_stdout $filename ./data.tar.gz | tar tzf - | sed -e 's/^\.//' > $info_dir/$pkg.list
  773. if [ -x "$info_dir/$pkg.postinst" ]; then
  774. $info_dir/$pkg.postinst configure
  775. fi
  776. if [ -n "$new_conffiles" ]; then
  777. new_conffiles='Conffiles: '`echo $new_conffiles | ipkg_protect_slashes`
  778. fi
  779. local sed_safe_offline_root="`echo ${IPKG_OFFLINE_ROOT} | ipkg_protect_slashes`"
  780. local sed_safe_root="`echo $dest | sed -e "s/^${sed_safe_offline_root}//" | ipkg_protect_slashes`"
  781. sed -e "s/\(Package:.*\)/\1\\
  782. Status: install ok installed\\
  783. Root: ${sed_safe_root}\\
  784. ${new_conffiles}/" $info_dir/$pkg.control | ipkg_status_update_sd $sd $pkg
  785. rm -f $info_dir/$pkg.control
  786. rm -f $info_dir/$pkg.conffiles
  787. rm -f $info_dir/$pkg.preinst
  788. rm -f $info_dir/$pkg.postinst
  789. echo "Done."
  790. }
  791. ipkg_install_file() {
  792. ipkg_install_file_dest $IPKG_ROOT $*
  793. }
  794. ipkg_install() {
  795. while [ $# -gt 0 ]; do
  796. local pkg="$1"
  797. shift
  798. case "$pkg" in
  799. http://* | ftp://*)
  800. local tmp_pkg_file="$IPKG_TMP/"`ipkg_file_part $pkg`
  801. if ipkg_download $pkg $tmp_pkg_file; then
  802. ipkg_install_file $tmp_pkg_file
  803. rm $tmp_pkg_file
  804. fi
  805. ;;
  806. file:/*.ipk | file://*.deb)
  807. local ipkg_filename="`echo $pkg|sed 's/^file://'`"
  808. ipkg_install_file $ipkg_filename
  809. ;;
  810. *.ipk | *.deb)
  811. ipkg_install_file $pkg
  812. ;;
  813. *)
  814. ipkg_get_install $pkg || true
  815. ;;
  816. esac
  817. done
  818. }
  819. ipkg_install_pending() {
  820. [ -n "$IPKG_OFFLINE_ROOT" ] && return 0
  821. if [ -d "$IPKG_PENDING_DIR" ]; then
  822. set +o noglob
  823. local pending="`ls -1d $IPKG_PENDING_DIR/*.ipk 2> /dev/null`" || true
  824. set -o noglob
  825. if [ -n "$pending" ]; then
  826. echo "The following packages in $IPKG_PENDING_DIR will now be installed:"
  827. echo $pending
  828. for filename in $pending; do
  829. if ipkg_install_file $filename; then
  830. rm $filename
  831. fi
  832. done
  833. fi
  834. fi
  835. return 0
  836. }
  837. ipkg_install_wanted() {
  838. local wanted="`ipkg_status_matching 'Status:[[:space:]]*install.*not-installed'`"
  839. if [ -n "$wanted" ]; then
  840. echo "The following package were previously requested but have not been installed:"
  841. echo $wanted
  842. if [ -n "$FORCE_DEFAULTS" ]; then
  843. echo "Installing them now."
  844. else
  845. echo -n "Install them now [Y/n] ? "
  846. read response
  847. case "$response" in
  848. [Nn] | [Nn][Oo])
  849. return 0
  850. ;;
  851. esac
  852. fi
  853. ipkg_install $wanted
  854. fi
  855. return 0
  856. }
  857. ipkg_upgrade_pkg() {
  858. local pkg="$1"
  859. local avail_ver="`ipkg_info $pkg Version | ipkg_extract_value | head -n 1`"
  860. is_installed=
  861. for dest_name in `ipkg_dest_names`; do
  862. local dest="`ipkg_dest_byname $dest_name`"
  863. local sd=$dest/$IPKG_DIR_PREFIX
  864. local inst_ver="`ipkg_status_sd $sd $pkg Version | ipkg_extract_value`"
  865. if [ -n "$inst_ver" ]; then
  866. is_installed=t
  867. if [ -z "$avail_ver" ]; then
  868. echo "Assuming locally installed package $pkg ($inst_ver) is up to date"
  869. return 0
  870. fi
  871. if [ "$avail_ver" = "$inst_ver" ]; then
  872. echo "Package $pkg ($inst_ver) installed in $dest_name is up to date"
  873. elif ipkg_is_upgrade "$avail_ver" "$inst_ver"; then
  874. echo "Upgrading $pkg ($dest_name) from $inst_ver to $avail_ver"
  875. ipkg_get_install_dest $dest $pkg
  876. else
  877. echo "Not downgrading package $pkg from $inst_ver to $avail_ver"
  878. fi
  879. fi
  880. done
  881. if [ -z "$is_installed" ]; then
  882. echo "Package $pkg does not appear to be installed"
  883. return 0
  884. fi
  885. }
  886. ipkg_upgrade() {
  887. if [ $# -lt 1 ]; then
  888. local pkgs="`ipkg_status_matching 'Status:.*[[:space:]]installed'`"
  889. else
  890. pkgs="$*"
  891. fi
  892. for pkg in $pkgs; do
  893. ipkg_upgrade_pkg $pkg
  894. done
  895. }
  896. ipkg_remove_pkg_dest() {
  897. local dest="$1"
  898. local pkg="$2"
  899. local sd=$dest/$IPKG_DIR_PREFIX
  900. local info_dir=$sd/info
  901. if ! ipkg_status_installed_sd $sd $pkg; then
  902. echo "ipkg_remove: Package $pkg does not appear to be installed in $dest"
  903. if ipkg_status_mentioned_sd $sd $pkg; then
  904. echo "Purging mention of $pkg from the ipkg database"
  905. ipkg_status_remove_sd $sd $pkg
  906. fi
  907. return 1
  908. fi
  909. echo "ipkg_remove: Removing $pkg... "
  910. local files="`cat $info_dir/$pkg.list`"
  911. export PKG_ROOT=$dest
  912. if [ -x "$info_dir/$pkg.prerm" ]; then
  913. $info_dir/$pkg.prerm remove
  914. fi
  915. local conffiles="`ipkg_status_sd $sd $pkg Conffiles | ipkg_extract_value`"
  916. local dirs_to_remove=
  917. for file in $files; do
  918. if [ -d "$dest/$file" ]; then
  919. dirs_to_remove="$dirs_to_remove $dest/$file"
  920. else
  921. if echo " $conffiles " | grep -q " $file "; then
  922. if echo " $conffiles " | grep -q " $file "`md5sum $dest/$file | sed 's/ .*//'`; then
  923. rm -f $dest/$file
  924. fi
  925. else
  926. rm -f $dest/$file
  927. fi
  928. fi
  929. done
  930. local removed_a_dir=t
  931. while [ -n "$removed_a_dir" ]; do
  932. removed_a_dir=
  933. local new_dirs_to_remove=
  934. for dir in $dirs_to_remove; do
  935. if rmdir $dir >/dev/null 2>&1; then
  936. removed_a_dir=t
  937. else
  938. new_dirs_to_remove="$new_dirs_to_remove $dir"
  939. fi
  940. done
  941. dirs_to_remove="$new_dirs_to_remove"
  942. done
  943. if [ -n "$dirs_to_remove" ]; then
  944. echo "ipkg_remove: Warning: Not removing the following directories since they are not empty:" >&2
  945. echo "$dirs_to_remove" | sed -e 's/\/[/]\+/\//g' >&2
  946. fi
  947. if [ -x "$info_dir/$pkg.postrm" ]; then
  948. $info_dir/$pkg.postrm remove
  949. fi
  950. ipkg_status_remove_sd $sd $pkg
  951. set +o noglob
  952. rm -f $info_dir/$pkg.*
  953. set -o noglob
  954. echo "Done."
  955. }
  956. ipkg_remove_pkg() {
  957. local pkg="$1"
  958. for dest in `ipkg_dests_all`; do
  959. local sd=$dest/$IPKG_DIR_PREFIX
  960. if ipkg_status_mentioned_sd $sd $pkg; then
  961. ipkg_remove_pkg_dest $dest $pkg
  962. fi
  963. done
  964. }
  965. ipkg_remove() {
  966. while [ $# -gt 0 ]; do
  967. local pkg="$1"
  968. shift
  969. if [ -n "$DEST_NAME" ]; then
  970. ipkg_remove_pkg_dest $IPKG_ROOT $pkg
  971. else
  972. ipkg_remove_pkg $pkg
  973. fi
  974. done
  975. }
  976. ###########
  977. # ipkg main
  978. ###########
  979. # Parse options
  980. while [ $# -gt 0 ]; do
  981. arg="$1"
  982. case $arg in
  983. -d | -dest)
  984. [ $# -gt 1 ] || ipkg_usage "option $arg requires an argument"
  985. DEST_NAME="$2"
  986. shift
  987. ;;
  988. -o | -offline)
  989. [ $# -gt 1 ] || ipkg_usage "option $arg requires an argument"
  990. IPKG_OFFLINE_ROOT="$2"
  991. shift
  992. ;;
  993. -force-depends)
  994. FORCE_DEPENDS=t
  995. ;;
  996. -force-defaults)
  997. FORCE_DEFAULTS=t
  998. ;;
  999. -*)
  1000. ipkg_usage "unknown option $arg"
  1001. ;;
  1002. *)
  1003. break
  1004. ;;
  1005. esac
  1006. shift
  1007. done
  1008. [ $# -lt 1 ] && ipkg_usage "ipkg must have one sub-command argument"
  1009. cmd="$1"
  1010. shift
  1011. ipkg_load_configuration
  1012. mkdir -p /tmp/ipkg
  1013. case "$cmd" in
  1014. update|upgrade|list|info|status|install_pending)
  1015. ;;
  1016. install|depends|remove|files|search)
  1017. [ $# -lt 1 ] && ipkg_usage "ERROR: the \`\`$cmd'' command requires an argument"
  1018. ;;
  1019. *)
  1020. echo "ERROR: unknown sub-command \`$cmd'"
  1021. ipkg_usage
  1022. ;;
  1023. esac
  1024. # Only install pending if we have an interactive sub-command
  1025. case "$cmd" in
  1026. upgrade|install)
  1027. ipkg_install_pending
  1028. ipkg_install_wanted
  1029. ;;
  1030. esac
  1031. ipkg_$cmd $*
  1032. for a in `ls $IPKG_TMP`; do
  1033. rm -rf $IPKG_TMP/$a
  1034. done