-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.subr
121 lines (100 loc) · 3.1 KB
/
fetch.subr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if [ ! "$_CBSD_FETCH_SUBR" ]; then
_CBSD_FETCH_SUBR=1
###
# Wrapper for fetch and dpv
# fetchme -u URL -o /tmp/out
# -d - try dpv - 0,1 (1 by default)
# use optional TF variable for output ( legacy from repo )
# require: cbsdsh as shell
# include: tools for humanize, DIALOG env for dpv
fetchme()
{
local _res
local _out="${TF}"
local _quiet=0
local _url=
local _TONULL
local _filename
local _fromrepo
local _fetch_current_retry
local _try_dpv=1
# global variable. Need for most dialog-based action
[ -n "${quiet}" ] && _quiet="${quiet}"
while getopts "d:u:q:o:" opt; do
case "${opt}" in
d) _try_dpv="${OPTARG}" ;;
u) _url="${OPTARG}" ;;
q) _quiet="${OPTARG}" ;;
o) _out="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${_url}" ] && echo "Empty URL" && return 1
# global settings
readconf fetch.conf
local _sizebyte=
local _humansize=
# We use size output for non-quiet and for dpv(1) mode
[ ${_quiet} -eq 0 ] && _sizebyte=$( ${FETCH_CMD} -qs ${_url} 2>/dev/null | ${AWK_CMD} '{printf $1}' )
# test for human
if [ -n "${_sizebyte}" ]; then
if ! is_number ${_sizebyte}; then
if conv2human "${_sizebyte}"; then
local _humansize="${convval}"
else
unset _sizebyte
fi
else
unset _sizebyte
fi
fi
[ ! -r "${DPV_CMD}" ] && _try_dpv=0
[ -z "${DIALOG}" ] && _try_dpv=0
if [ ${_quiet} -eq 1 ]; then
_try_dpv=0
fetch_opts="${fetch_opts} -q"
fi
[ ${_try_dpv} -eq 0 ] && unset DPV_CMD
_filename=$( ${BASENAME_CMD} ${_url} 2>&1 )
[ -z "${_filename}" ] && _filename="Resource"
_fromrepo=$( echo ${_url} | ${CUT_CMD} -d "/" -f 3 )
if [ -r ${_out}.lock ]; then
${ECHO} "${N1_COLOR} cbsd fetch: lock file exists, another instance is fetching the same? ${N2_COLOR}${_out}.lock${N0_COLOR}"
return 1
fi
[ -z ${fetch_max_retry} ] && fetch_max_retry=0
if is_number ${fetch_max_retry}; then
# not number?
${ECHO} "${N1_COLOR}fetch_max_retry not number: ${N2_COLOR}${fetch_max_retry}${N1_COLOR}? reset to 0${N0_COLOR}"
fetch_max_retry=0
fi
if [ ${fetch_max_retry} -eq 0 ]; then
_fetch_current_retry=-1
else
_fetch_current_retry=0
fi
_res=1
while [ ${_fetch_current_retry} -lt ${fetch_max_retry} ]; do
_fetch_current_retry=$(( _fetch_current_retry + 1 ))
if [ -n "${DPV_CMD}" -a ${_try_dpv} -eq 1 ]; then
${LOCKF_CMD} -s -t0 ${_out}.lock ${FETCH_CMD} ${fetch_opts} -q -o '-' ${_url} | ${DPV_CMD} -b "${product}" -p "fetching from ${_fromrepo}:" -t " fetching " -x "${CAT_CMD} > ${_out}" ${_sizebyte}:${_filename}
# check for size, cause "fetch | dpv " is always true
if [ -s "${_out}" ]; then
_res=0
else
${RM_CMD} -f ${_out}
_res=1
fi
else
[ -n "${_humansize}" ] && ${ECHO} "${N1_COLOR}retrieve ${H5_COLOR}${_filename}${N1_COLOR} from ${H3_COLOR}${_fromrepo}${N1_COLOR}, size: ${N2_COLOR}${_humansize}${N0_COLOR}"
${LOCKF_CMD} -s -t0 ${_out}.lock ${FETCH_CMD} ${fetch_opts} -o ${_out} ${_url} ${_TONULL}
_res=$?
fi
# success of interrupted
[ ${_res} -eq 0 ] && break
[ -z "${DPV_CMD}" ] && ${ECHO} "${N1_COLOR}retry: ${N2_COLOR}${_fetch_current_retry}/${fetch_max_retry}${N0_COLOR}"
done
return ${_res}
}
###
fi