xref: /OK3568_Linux_fs/buildroot/support/download/scp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/usr/bin/env bash
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun# We want to catch any unexpected failure, and exit immediately
4*4882a593Smuzhiyunset -e
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun# Download helper for scp, to be called from the download wrapper script
7*4882a593Smuzhiyun#
8*4882a593Smuzhiyun# Options:
9*4882a593Smuzhiyun#   -q          Be quiet.
10*4882a593Smuzhiyun#   -o FILE     Copy to local file FILE.
11*4882a593Smuzhiyun#   -f FILE     Copy from remote file FILE.
12*4882a593Smuzhiyun#   -u URI      Download file at URI.
13*4882a593Smuzhiyun#
14*4882a593Smuzhiyun# Environment:
15*4882a593Smuzhiyun#   SCP       : the scp command to call
16*4882a593Smuzhiyun
17*4882a593Smuzhiyunquiet=
18*4882a593Smuzhiyunwhile getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
19*4882a593Smuzhiyun    case "${OPT}" in
20*4882a593Smuzhiyun    q)  quiet=-q;;
21*4882a593Smuzhiyun    o)  output="${OPTARG}";;
22*4882a593Smuzhiyun    f)  filename="${OPTARG}";;
23*4882a593Smuzhiyun    u)  uri="${OPTARG}";;
24*4882a593Smuzhiyun    :)  printf "option '%s' expects a mandatory argument\n" "${OPTARG}"; exit 1;;
25*4882a593Smuzhiyun    \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
26*4882a593Smuzhiyun    esac
27*4882a593Smuzhiyundone
28*4882a593Smuzhiyun
29*4882a593Smuzhiyunshift $((OPTIND-1)) # Get rid of our options
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun# Caller needs to single-quote its arguments to prevent them from
32*4882a593Smuzhiyun# being expanded a second time (in case there are spaces in them)
33*4882a593Smuzhiyun_scp() {
34*4882a593Smuzhiyun    if [ -z "${quiet}" ]; then
35*4882a593Smuzhiyun        printf '%s ' ${SCP} "${@}"; printf '\n'
36*4882a593Smuzhiyun    fi
37*4882a593Smuzhiyun    _plain_scp "$@"
38*4882a593Smuzhiyun}
39*4882a593Smuzhiyun# Note: please keep command below aligned with what is printed above
40*4882a593Smuzhiyun_plain_scp() {
41*4882a593Smuzhiyun    eval ${SCP} "${@}"
42*4882a593Smuzhiyun}
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun# Remove any scheme prefix
45*4882a593Smuzhiyunuri="${uri##scp://}"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun_scp ${quiet} "${@}" "'${uri}/${filename}'" "'${output}'"
48