1#!/bin/bash
2#
3#   file.sh - function for handling the download and extraction of source files
4#
5#   Copyright (c) 2015-2018 Pacman Development Team <pacman-dev@archlinux.org>
6#
7#   This program is free software; you can redistribute it and/or modify
8#   it under the terms of the GNU General Public License as published by
9#   the Free Software Foundation; either version 2 of the License, or
10#   (at your option) any later version.
11#
12#   This program is distributed in the hope that it will be useful,
13#   but WITHOUT ANY WARRANTY; without even the implied warranty of
14#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#   GNU General Public License for more details.
16#
17#   You should have received a copy of the GNU General Public License
18#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21[[ -n "$LIBMAKEPKG_SOURCE_FILE_SH" ]] && return
22LIBMAKEPKG_SOURCE_FILE_SH=1
23
24
25LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
26
27source "$LIBRARY/util/message.sh"
28source "$LIBRARY/util/pkgbuild.sh"
29
30
31download_file() {
32	local netfile=$1
33
34	local filepath=$(get_filepath "$netfile")
35	if [[ -n "$filepath" ]]; then
36		msg2 "$(gettext "Found %s")" "${filepath##*/}"
37		return
38	fi
39
40	local proto=$(get_protocol "$netfile")
41
42	# find the client we should use for this URL
43	local -a cmdline
44	IFS=' ' read -a cmdline < <(get_downloadclient "$proto")
45	(( ${#cmdline[@]} )) || exit
46
47	local filename=$(get_filename "$netfile")
48	local url=$(get_url "$netfile")
49
50	if [[ $proto = "scp" ]]; then
51		# scp downloads should not pass the protocol in the url
52		url="${url##*://}"
53	fi
54
55	msg2 "$(gettext "Downloading %s...")" "$filename"
56
57	# temporary download file, default to last component of the URL
58	local dlfile="${url##*/}"
59
60	# replace %o by the temporary dlfile if it exists
61	if [[ ${cmdline[*]} = *%o* ]]; then
62		dlfile=$filename.part
63		cmdline=("${cmdline[@]//%o/$dlfile}")
64	fi
65	# add the URL, either in place of %u or at the end
66	if [[ ${cmdline[*]} = *%u* ]]; then
67		cmdline=("${cmdline[@]//%u/$url}")
68	else
69		cmdline+=("$url")
70	fi
71
72	if ! command -- "${cmdline[@]}" >&2; then
73		[[ ! -s $dlfile ]] && rm -f -- "$dlfile"
74		error "$(gettext "Failure while downloading %s")" "$url"
75		plain "$(gettext "Aborting...")"
76		exit 1
77	fi
78
79	# rename the temporary download file to the final destination
80	if [[ $dlfile != "$filename" ]]; then
81		mv -f "$SRCDEST/$dlfile" "$SRCDEST/$filename"
82	fi
83}
84
85extract_file() {
86	local file=$1
87
88	local filepath=$(get_filepath "$file")
89	rm -f "$srcdir/${file}"
90	ln -s "$filepath" "$srcdir/"
91
92	if in_array "$file" "${noextract[@]}"; then
93		# skip source files in the noextract=() array
94		# these are marked explicitly to NOT be extracted
95		return 0
96	fi
97
98	# do not rely on extension for file type
99	local file_type=$(file -bizL -- "$file")
100	local ext=${file##*.}
101	local cmd=''
102	case "$file_type" in
103		*application/x-tar*|*application/zip*|*application/x-zip*|*application/x-cpio*)
104			cmd="bsdtar" ;;
105		*application/x-gzip*)
106			case "$ext" in
107				gz|z|Z) cmd="gzip" ;;
108				*) return;;
109			esac ;;
110		*application/x-bzip*)
111			case "$ext" in
112				bz2|bz) cmd="bzip2" ;;
113				*) return;;
114			esac ;;
115		*application/x-xz*)
116			case "$ext" in
117				xz) cmd="xz" ;;
118				*) return;;
119			esac ;;
120		*)
121			# See if bsdtar can recognize the file
122			if bsdtar -tf "$file" -q '*' &>/dev/null; then
123				cmd="bsdtar"
124			else
125				return 0
126			fi ;;
127	esac
128
129	local ret=0
130	msg2 "$(gettext "Extracting %s with %s")" "$file" "$cmd"
131	if [[ $cmd = "bsdtar" ]]; then
132		$cmd -xf "$file" || ret=$?
133	else
134		rm -f -- "${file%.*}"
135		$cmd -dcf -- "$file" > "${file%.*}" || ret=$?
136	fi
137	if (( ret )); then
138		error "$(gettext "Failed to extract %s")" "$file"
139		plain "$(gettext "Aborting...")"
140		exit 1
141	fi
142
143	if (( EUID == 0 )); then
144		# change perms of all source files to root user & root group
145		chown -R 0:0 "$srcdir"
146	fi
147}
148