xref: /OK3568_Linux_fs/buildroot/support/download/dl-wrapper (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/usr/bin/env bash
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun# This script is a wrapper to the other download backends.
4*4882a593Smuzhiyun# Its role is to ensure atomicity when saving downloaded files
5*4882a593Smuzhiyun# back to BR2_DL_DIR, and not clutter BR2_DL_DIR with partial,
6*4882a593Smuzhiyun# failed downloads.
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun# To avoid cluttering BR2_DL_DIR, we download to a trashable
9*4882a593Smuzhiyun# location, namely in $(BUILD_DIR).
10*4882a593Smuzhiyun# Then, we move the downloaded file to a temporary file in the
11*4882a593Smuzhiyun# same directory as the final output file.
12*4882a593Smuzhiyun# This allows us to finally atomically rename it to its final
13*4882a593Smuzhiyun# name.
14*4882a593Smuzhiyun# If anything goes wrong, we just remove all the temporaries
15*4882a593Smuzhiyun# created so far.
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun# We want to catch any unexpected failure, and exit immediately.
18*4882a593Smuzhiyunset -e
19*4882a593Smuzhiyun
20*4882a593Smuzhiyunexport BR_BACKEND_DL_GETOPTS=":hc:d:o:n:N:H:ru:qf:e"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyuncheck_kgithub() {
23*4882a593Smuzhiyun    if wget -q --delete-after \
24*4882a593Smuzhiyun        https://raw.kgithub.com/git/git/master/README.md; then
25*4882a593Smuzhiyun        if git ls-remote https://kgithub.com/git/git &>/dev/null; then
26*4882a593Smuzhiyun            return 0
27*4882a593Smuzhiyun        fi
28*4882a593Smuzhiyun    fi
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun    if grep -q kgithub /etc/hosts; then
31*4882a593Smuzhiyun        echo "Oops! The kgithub is down!"
32*4882a593Smuzhiyun        return 1
33*4882a593Smuzhiyun    fi
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun    echo "Your DNS doesn't support kgithub.com"
36*4882a593Smuzhiyun    echo "Please modify it:"
37*4882a593Smuzhiyun    echo "sudo sed -i '\$a 43.154.68.204\tkgithub.com' /etc/hosts"
38*4882a593Smuzhiyun    echo "sudo sed -i '\$a 43.155.83.75\traw.kgithub.com objects.githubusercontent.kgithub.com' /etc/hosts"
39*4882a593Smuzhiyun    return 1
40*4882a593Smuzhiyun}
41*4882a593Smuzhiyun
42*4882a593Smuzhiyunmain() {
43*4882a593Smuzhiyun    local OPT OPTARG
44*4882a593Smuzhiyun    local backend output hfile recurse quiet rc use_kgithub
45*4882a593Smuzhiyun    local -a uris
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun    # Parse our options; anything after '--' is for the backend
48*4882a593Smuzhiyun    while getopts ":c:d:D:o:n:N:H:rf:u:q" OPT; do
49*4882a593Smuzhiyun        case "${OPT}" in
50*4882a593Smuzhiyun        c)  cset="${OPTARG}";;
51*4882a593Smuzhiyun        d)  dl_dir="${OPTARG}";;
52*4882a593Smuzhiyun        D)  old_dl_dir="${OPTARG}";;
53*4882a593Smuzhiyun        o)  output="${OPTARG}";;
54*4882a593Smuzhiyun        n)  raw_base_name="${OPTARG}";;
55*4882a593Smuzhiyun        N)  base_name="${OPTARG}";;
56*4882a593Smuzhiyun        H)  hfile="${OPTARG}";;
57*4882a593Smuzhiyun        r)  recurse="-r";;
58*4882a593Smuzhiyun        f)  filename="${OPTARG}";;
59*4882a593Smuzhiyun        u)  uris+=( "${OPTARG}" );;
60*4882a593Smuzhiyun        q)  quiet="-q";;
61*4882a593Smuzhiyun        :)  error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
62*4882a593Smuzhiyun        \?) error "unknown option '%s'\n" "${OPTARG}";;
63*4882a593Smuzhiyun        esac
64*4882a593Smuzhiyun    done
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun    # Forget our options, and keep only those for the backend
67*4882a593Smuzhiyun    shift $((OPTIND-1))
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun    if [ -z "${output}" ]; then
70*4882a593Smuzhiyun        error "no output specified, use -o\n"
71*4882a593Smuzhiyun    fi
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun    # Legacy handling: check if the file already exists in the global
74*4882a593Smuzhiyun    # download directory. If it does, hard-link it. If it turns out it
75*4882a593Smuzhiyun    # was an incorrect download, we'd still check it below anyway.
76*4882a593Smuzhiyun    # If we can neither link nor copy, fallback to doing a download.
77*4882a593Smuzhiyun    # NOTE! This is not atomic, is subject to TOCTTOU, but the whole
78*4882a593Smuzhiyun    # dl-wrapper runs under an flock, so we're safe.
79*4882a593Smuzhiyun    if [ ! -e "${output}" -a -e "${old_dl_dir}/${filename}" ]; then
80*4882a593Smuzhiyun        ln "${old_dl_dir}/${filename}" "${output}" || \
81*4882a593Smuzhiyun        cp "${old_dl_dir}/${filename}" "${output}" || \
82*4882a593Smuzhiyun        true
83*4882a593Smuzhiyun    fi
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun    # If the output file already exists and:
86*4882a593Smuzhiyun    # - there's no .hash file: do not download it again and exit promptly
87*4882a593Smuzhiyun    # - matches all its hashes: do not download it again and exit promptly
88*4882a593Smuzhiyun    # - fails at least one of its hashes: force a re-download
89*4882a593Smuzhiyun    # - there's no hash (but a .hash file): consider it a hard error
90*4882a593Smuzhiyun    if [ -e "${output}" ]; then
91*4882a593Smuzhiyun        if support/download/check-hash ${quiet} "${hfile}" "${output}" "${output##*/}"; then
92*4882a593Smuzhiyun            exit 0
93*4882a593Smuzhiyun        elif [ ${?} -ne 2 ]; then
94*4882a593Smuzhiyun            # Do not remove the file, otherwise it might get re-downloaded
95*4882a593Smuzhiyun            # from a later location (i.e. primary -> upstream -> mirror).
96*4882a593Smuzhiyun            # Do not print a message, check-hash already did.
97*4882a593Smuzhiyun            exit 1
98*4882a593Smuzhiyun        fi
99*4882a593Smuzhiyun        rm -f "${output}"
100*4882a593Smuzhiyun        warn "Re-downloading '%s'...\n" "${output##*/}"
101*4882a593Smuzhiyun    fi
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun    if [ -z "$BR2_NO_KGITHUB" ] && echo "${uris[@]}" | grep -wq github.com; then
104*4882a593Smuzhiyun        if ! git ls-remote https://github.com/git/git &>/dev/null; then
105*4882a593Smuzhiyun            echo -e "\e[35m"
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun            echo "Unable to access github.com! Trying kgithub now..."
108*4882a593Smuzhiyun            if check_kgithub; then
109*4882a593Smuzhiyun                echo "Using kgithub instead..."
110*4882a593Smuzhiyun                echo "Setup a VPN or export BR2_NO_KGITHUB=1 to disable this."
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun                use_kgithub=1
113*4882a593Smuzhiyun            fi
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun            echo -e "\e[0m"
116*4882a593Smuzhiyun        fi
117*4882a593Smuzhiyun    fi
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun    # Look through all the uris that we were given to download the package
120*4882a593Smuzhiyun    # source
121*4882a593Smuzhiyun    download_and_check=0
122*4882a593Smuzhiyun    rc=1
123*4882a593Smuzhiyun    for uri in "${uris[@]}"; do
124*4882a593Smuzhiyun        backend_urlencode="${uri%%+*}"
125*4882a593Smuzhiyun        backend="${backend_urlencode%|*}"
126*4882a593Smuzhiyun        case "${backend}" in
127*4882a593Smuzhiyun            git|svn|cvs|bzr|file|scp|hg) ;;
128*4882a593Smuzhiyun            *) backend="wget" ;;
129*4882a593Smuzhiyun        esac
130*4882a593Smuzhiyun        uri=${uri#*+}
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun        urlencode=${backend_urlencode#*|}
133*4882a593Smuzhiyun        # urlencode must be "urlencode"
134*4882a593Smuzhiyun        [ "${urlencode}" != "urlencode" ] && urlencode=""
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun        if [ "$use_kgithub" ]; then
137*4882a593Smuzhiyun            uri=${uri/\/\/github.com\//\/\/kgithub.com\/}
138*4882a593Smuzhiyun            uri=${uri/\/\/raw.githubusercontent.com\//\/\/raw.kgithub.com\/}
139*4882a593Smuzhiyun        fi
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun        # tmpd is a temporary directory in which backends may store
142*4882a593Smuzhiyun        # intermediate by-products of the download.
143*4882a593Smuzhiyun        # tmpf is the file in which the backends should put the downloaded
144*4882a593Smuzhiyun        # content.
145*4882a593Smuzhiyun        # tmpd is located in $(BUILD_DIR), so as not to clutter the (precious)
146*4882a593Smuzhiyun        # $(BR2_DL_DIR)
147*4882a593Smuzhiyun        # We let the backends create tmpf, so they are able to set whatever
148*4882a593Smuzhiyun        # permission bits they want (although we're only really interested in
149*4882a593Smuzhiyun        # the executable bit.)
150*4882a593Smuzhiyun        tmpd="$(mktemp -d "${BUILD_DIR}/.${output##*/}.XXXXXX")"
151*4882a593Smuzhiyun        tmpf="${tmpd}/output"
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun        # Helpers expect to run in a directory that is *really* trashable, so
154*4882a593Smuzhiyun        # they are free to create whatever files and/or sub-dirs they might need.
155*4882a593Smuzhiyun        # Doing the 'cd' here rather than in all backends is easier.
156*4882a593Smuzhiyun        cd "${tmpd}"
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun        # If the backend fails, we can just remove the content of the temporary
159*4882a593Smuzhiyun        # directory to remove all the cruft it may have left behind, and try
160*4882a593Smuzhiyun        # the next URI until it succeeds. Once out of URI to try, we need to
161*4882a593Smuzhiyun        # cleanup and exit.
162*4882a593Smuzhiyun        if ! "${OLDPWD}/support/download/${backend}" \
163*4882a593Smuzhiyun                $([ -n "${urlencode}" ] && printf %s '-e') \
164*4882a593Smuzhiyun                -c "${cset}" \
165*4882a593Smuzhiyun                -d "${dl_dir}" \
166*4882a593Smuzhiyun                -n "${raw_base_name}" \
167*4882a593Smuzhiyun                -N "${base_name}" \
168*4882a593Smuzhiyun                -f "${filename}" \
169*4882a593Smuzhiyun                -u "${uri}" \
170*4882a593Smuzhiyun                -o "${tmpf}" \
171*4882a593Smuzhiyun                ${quiet} ${recurse} -- "${@}"
172*4882a593Smuzhiyun        then
173*4882a593Smuzhiyun            # cd back to keep path coherence
174*4882a593Smuzhiyun            cd "${OLDPWD}"
175*4882a593Smuzhiyun            rm -rf "${tmpd}"
176*4882a593Smuzhiyun            continue
177*4882a593Smuzhiyun        fi
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun        # cd back to free the temp-dir, so we can remove it later
180*4882a593Smuzhiyun        cd "${OLDPWD}"
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun        # Check if the downloaded file is sane, and matches the stored hashes
183*4882a593Smuzhiyun        # for that file
184*4882a593Smuzhiyun        if support/download/check-hash ${quiet} "${hfile}" "${tmpf}" "${output##*/}"; then
185*4882a593Smuzhiyun            rc=0
186*4882a593Smuzhiyun        else
187*4882a593Smuzhiyun            if [ ${?} -ne 3 ]; then
188*4882a593Smuzhiyun                rm -rf "${tmpd}"
189*4882a593Smuzhiyun                continue
190*4882a593Smuzhiyun            fi
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun            # the hash file exists and there was no hash to check the file
193*4882a593Smuzhiyun            # against
194*4882a593Smuzhiyun            rc=1
195*4882a593Smuzhiyun        fi
196*4882a593Smuzhiyun        download_and_check=1
197*4882a593Smuzhiyun        break
198*4882a593Smuzhiyun    done
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun    # We tried every URI possible, none seems to work or to check against the
201*4882a593Smuzhiyun    # available hash. *ABORT MISSION*
202*4882a593Smuzhiyun    if [ "${download_and_check}" -eq 0 ]; then
203*4882a593Smuzhiyun        rm -rf "${tmpd}"
204*4882a593Smuzhiyun        exit 1
205*4882a593Smuzhiyun    fi
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun    # tmp_output is in the same directory as the final output, so we can
208*4882a593Smuzhiyun    # later move it atomically.
209*4882a593Smuzhiyun    tmp_output="$(mktemp "${output}.XXXXXX")"
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun    # 'mktemp' creates files with 'go=-rwx', so the files are not accessible
212*4882a593Smuzhiyun    # to users other than the one doing the download (and root, of course).
213*4882a593Smuzhiyun    # This can be problematic when a shared BR2_DL_DIR is used by different
214*4882a593Smuzhiyun    # users (e.g. on a build server), where all users may write to the shared
215*4882a593Smuzhiyun    # location, since other users would not be allowed to read the files
216*4882a593Smuzhiyun    # another user downloaded.
217*4882a593Smuzhiyun    # So, we restore the 'go' access rights to a more sensible value, while
218*4882a593Smuzhiyun    # still abiding by the current user's umask. We must do that before the
219*4882a593Smuzhiyun    # final 'mv', so just do it now.
220*4882a593Smuzhiyun    # Some backends (cp and scp) may create executable files, so we need to
221*4882a593Smuzhiyun    # carry the executable bit if needed.
222*4882a593Smuzhiyun    [ -x "${tmpf}" ] && new_mode=755 || new_mode=644
223*4882a593Smuzhiyun    new_mode=$(printf "%04o" $((0${new_mode} & ~0$(umask))))
224*4882a593Smuzhiyun    chmod ${new_mode} "${tmp_output}"
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun    # We must *not* unlink tmp_output, otherwise there is a small window
227*4882a593Smuzhiyun    # during which another download process may create the same tmp_output
228*4882a593Smuzhiyun    # name (very, very unlikely; but not impossible.)
229*4882a593Smuzhiyun    # Using 'cp' is not reliable, since 'cp' may unlink the destination file
230*4882a593Smuzhiyun    # if it is unable to open it with O_WRONLY|O_TRUNC; see:
231*4882a593Smuzhiyun    #   http://pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
232*4882a593Smuzhiyun    # Since the destination filesystem can be anything, it might not support
233*4882a593Smuzhiyun    # O_TRUNC, so 'cp' would unlink it first.
234*4882a593Smuzhiyun    # Use 'cat' and append-redirection '>>' to save to the final location,
235*4882a593Smuzhiyun    # since that is the only way we can be 100% sure of the behaviour.
236*4882a593Smuzhiyun    if ! cat "${tmpf}" >>"${tmp_output}"; then
237*4882a593Smuzhiyun        rm -rf "${tmpd}" "${tmp_output}"
238*4882a593Smuzhiyun        exit 1
239*4882a593Smuzhiyun    fi
240*4882a593Smuzhiyun    rm -rf "${tmpd}"
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun    # tmp_output and output are on the same filesystem, so POSIX guarantees
243*4882a593Smuzhiyun    # that 'mv' is atomic, because it then uses rename() that POSIX mandates
244*4882a593Smuzhiyun    # to be atomic, see:
245*4882a593Smuzhiyun    #   http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
246*4882a593Smuzhiyun    if ! mv -f "${tmp_output}" "${output}"; then
247*4882a593Smuzhiyun        rm -f "${tmp_output}"
248*4882a593Smuzhiyun        exit 1
249*4882a593Smuzhiyun    fi
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun    return ${rc}
252*4882a593Smuzhiyun}
253*4882a593Smuzhiyun
254*4882a593Smuzhiyuntrace()  { local msg="${1}"; shift; printf "%s: ${msg}" "${my_name}" "${@}"; }
255*4882a593Smuzhiyunwarn()   { trace "${@}" >&2; }
256*4882a593SmuzhiyunerrorN() { local ret="${1}"; shift; warn "${@}"; exit ${ret}; }
257*4882a593Smuzhiyunerror()  { errorN 1 "${@}"; }
258*4882a593Smuzhiyun
259*4882a593Smuzhiyunmy_name="${0##*/}"
260*4882a593Smuzhiyunmain "${@}"
261