package-manager-call 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/sh
  2. . /usr/share/libubox/jshn.sh
  3. action=$1
  4. shift
  5. if [ -f /usr/bin/apk ]; then
  6. ipkg_bin="apk"
  7. else
  8. ipkg_bin="opkg"
  9. fi
  10. case "$action" in
  11. list-installed)
  12. if [ $ipkg_bin = "apk" ]; then
  13. $ipkg_bin list -I --full 2>/dev/null
  14. else
  15. cat /usr/lib/opkg/status
  16. fi
  17. ;;
  18. list-available)
  19. if [ $ipkg_bin = "apk" ]; then
  20. $ipkg_bin list --full 2>/dev/null
  21. else
  22. lists_dir=$(sed -rne 's#^lists_dir \S+ (\S+)#\1#p' /etc/opkg.conf /etc/opkg/*.conf 2>/dev/null | tail -n 1)
  23. find "${lists_dir:-/usr/lib/opkg/lists}" -type f '!' -name '*.sig' | xargs -r gzip -cd
  24. fi
  25. ;;
  26. install|update|upgrade|remove)
  27. (
  28. cmd="$ipkg_bin"
  29. # APK have command renamed
  30. if [ $ipkg_bin = "apk" ]; then
  31. case "$action" in
  32. install)
  33. action="add"
  34. ;;
  35. update)
  36. action="update"
  37. ;;
  38. upgrade)
  39. action="upgrade"
  40. ;;
  41. remove)
  42. action="del"
  43. ;;
  44. esac
  45. fi
  46. # APK have --autoremove enabled by default and
  47. # --force-removal-of-dependent-packages as -r option
  48. if [ $ipkg_bin = "apk" ]; then
  49. while [ -n "$1" ]; do
  50. case "$1" in
  51. --force-removal-of-dependent-packages)
  52. cmd="$cmd -r"
  53. shift
  54. ;;
  55. --force-overwrite)
  56. cmd="$cmd $1"
  57. shift
  58. ;;
  59. -*)
  60. shift
  61. ;;
  62. *)
  63. break
  64. ;;
  65. esac
  66. done
  67. else
  68. while [ -n "$1" ]; do
  69. case "$1" in
  70. --autoremove|--force-overwrite|--force-removal-of-dependent-packages)
  71. ipkg_bin="$apk $1"
  72. shift
  73. ;;
  74. -*)
  75. shift
  76. ;;
  77. *)
  78. break
  79. ;;
  80. esac
  81. done
  82. fi
  83. if flock -x 200; then
  84. pkmcmd="$cmd $action $@"
  85. $cmd $action "$@" </dev/null >/tmp/ipkg.out 2>/tmp/ipkg.err
  86. code=$?
  87. stdout=$(cat /tmp/ipkg.out)
  88. stderr=$(cat /tmp/ipkg.err)
  89. else
  90. code=255
  91. stderr="Failed to acquire lock"
  92. fi
  93. json_init
  94. json_add_int code $code
  95. [ -n "$pkmcmd" ] && json_add_string pkmcmd "$pkmcmd"
  96. [ -n "$stdout" ] && json_add_string stdout "$stdout"
  97. [ -n "$stderr" ] && json_add_string stderr "$stderr"
  98. json_dump
  99. ) 200>/tmp/ipkg.lock
  100. rm -f /tmp/ipkg.lock /tmp/ipkg.err /tmp/ipkg.out
  101. ;;
  102. *)
  103. echo "Usage: $0 {list-installed|list-available|update}" >&2
  104. echo " $0 {install|upgrade|remove} pkg[ pkg...]" >&2
  105. exit 1
  106. ;;
  107. esac