common 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. BUILD_SYSTEM="libreboot"
  17. PROJECTS="projects"
  18. SOURCES="sources"
  19. BUILD="build"
  20. INSTALL="install"
  21. RELEASE="release"
  22. SYSTEMS="systems"
  23. IMAGES="images"
  24. TOOLS="tools"
  25. CONFIGS="configs"
  26. PATCHES="patches"
  27. TARGETS="targets"
  28. REVISION="revision"
  29. BLOBS="blobs"
  30. BLOBS_IGNORE="blobs-ignore"
  31. BLOBS_DISCOVER="blobs-discover"
  32. DOTEPOCH=".epoch"
  33. DOTRNDSEED=".rndseed"
  34. DOTVERSION=".version"
  35. DOTREVISION=".revision"
  36. DOTTARFILES=".tarfiles"
  37. ARCHIVE="tar.xz"
  38. CHECKSUM="sha256sum"
  39. DSIG="asc"
  40. CONFIG_SHELL="${CONFIG_SHELL:-$(which bash)}"
  41. EDITOR="${EDITOR:-$(which vi || true)}"
  42. TASKS="${TASKS:-1}"
  43. function_check() {
  44. local function=$1
  45. declare -f -F "$function" > /dev/null
  46. }
  47. variable_check() {
  48. local variable=$1
  49. test ! -z "${!variable}"
  50. }
  51. arguments_list() {
  52. local argument
  53. for argument in "$@"
  54. do
  55. printf '%s\n' "$argument"
  56. done
  57. }
  58. download_wrapper() {
  59. local download_dir="$1"
  60. shift
  61. local uris=($@)
  62. # TODO: Add support for curl, in addition
  63. # to wget, for compatibility reasons
  64. wget_options=(
  65. --config=/dev/null
  66. --secure-protocol=PFS
  67. --directory-prefix="${download_dir}"
  68. --continue
  69. --
  70. )
  71. wget "${wget_options[@]}" "${uris[@]}"
  72. }
  73. diff_patch_file() {
  74. local repository_path="$1"
  75. local patch_file_path="$2"
  76. # TODO: Improve handling of filenames to avoid gotchas w/ \n, \t, etc.
  77. local filename_in_diff="$(sed -rne 's/^-{3}\s+([^ \r\n]*).*/\1/p' "${patch_file_path}")"
  78. local source_file_path
  79. if ! ( grep -E '^-{3}.*/' "${patch_file_path}" >/dev/null 2>&1 ); then
  80. source_file_path="${repository_path}/${filename_in_diff}"
  81. else
  82. source_file_path="${repository_path}/${filename_in_diff##*/}"
  83. fi
  84. patch "${source_file_path}" "${patch_file_path}"
  85. }
  86. path_wildcard_expand() {
  87. local path=$@
  88. # Evaluation fails with unescaped whitespaces.
  89. path=$( printf '%s\n' "$path" | sed "s/ /\\\ /g" )
  90. eval "arguments_list "$path""
  91. }
  92. file_checksum_create() {
  93. local path=$1
  94. local checksum_path="$path.$CHECKSUM"
  95. local name=$( basename "$path" )
  96. local directory_path=$( dirname "$path" )
  97. (
  98. cd "$directory_path"
  99. sha256sum "$name" > "$checksum_path"
  100. )
  101. }
  102. file_checksum_check() {
  103. local path=$1
  104. local checksum_path="$path.$CHECKSUM"
  105. local name=$( basename "$path" )
  106. local directory_path=$( dirname "$path" )
  107. if ! [ -f "$checksum_path" ]
  108. then
  109. printf '%s\n' 'Could not verify file checksum!' >&2
  110. return 1
  111. fi
  112. (
  113. cd "$directory_path"
  114. sha256sum -c "$checksum_path"
  115. )
  116. }
  117. file_signature_create() {
  118. local path=$1
  119. local signature_path="$path.$DSIG"
  120. if [ -z "$RELEASE_KEY" ]
  121. then
  122. return 0
  123. fi
  124. gpg --default-key "$RELEASE_KEY" --armor --output "$signature_path" --detach-sign --yes "$path"
  125. }
  126. file_signature_check() {
  127. local path=$1
  128. local signature_path="$path.$DSIG"
  129. if ! [ -f "$signature_path" ]
  130. then
  131. printf '%s\n' 'Could not verify file signature!' >&2
  132. return 1
  133. fi
  134. gpg --armor --verify "$signature_path" "$path"
  135. }
  136. file_verification_create() {
  137. local path=$1
  138. file_checksum_create "$path"
  139. file_signature_create "$path"
  140. }
  141. file_verification_check() {
  142. local path=$1
  143. file_checksum_check "$path"
  144. file_signature_check "$path"
  145. }
  146. file_exists_check() {
  147. local path=$1
  148. test -f "$path"
  149. }
  150. directory_filled_check() {
  151. local path=$1
  152. if [ -z "$( ls -A "$path" 2> /dev/null )" ]
  153. then
  154. return 1
  155. else
  156. return 0
  157. fi
  158. }
  159. archive_files_create() {
  160. local source_path="$1"
  161. local directory="$(basename "${source_path}")"
  162. local tarfiles_path="${source_path}/${DOTTARFILES}"
  163. local revision_path="${source_path}/${DOTREVISION}"
  164. local version_path="${source_path}/${DOTVERSION}"
  165. local epoch_path="${source_path}/${DOTEPOCH}"
  166. local rnd_seed_path="${source_path}/${DOTRNDSEED}"
  167. # Files in "${tarfiles_path}" are NUL terminated.
  168. # `tr '\0' '\n'` for human-readable output.
  169. if git_check "${source_path}"; then
  170. git_files "${source_path}" > "${tarfiles_path}"
  171. printf '%s\0' "${DOTTARFILES}" >> "${tarfiles_path}"
  172. else
  173. find "${source_path}" -print0 | env LC_ALL='C.UTF-8' sort -z | sed -z "1d;s,^${source_path}/\\?,,;/^${DOTTARFILES}\$/d" > "${tarfiles_path}"
  174. fi
  175. for dotfile in "${revision_path}" \
  176. "${version_path}" \
  177. "${epoch_path}" \
  178. "${rnd_seed_path}"
  179. do
  180. if [[ -f "${dotfile}" ]]; then
  181. printf '%s\0' ".${dotfile##*.}" >> "${tarfiles_path}"
  182. fi
  183. done
  184. }
  185. archive_files_date() {
  186. local source_path="$1"
  187. local epoch_path="${source_path}/${DOTEPOCH}"
  188. if [[ -n "${SOURCE_DATE_EPOCH}" ]]; then
  189. find "${source_path}" -execdir touch --no-dereference --date="@${SOURCE_DATE_EPOCH}" {} +
  190. fi
  191. }
  192. archive_create() {
  193. local archive_path="$1"
  194. local source_path="$2"
  195. local directory="$3"
  196. local tarfiles_path="${source_path}/${DOTTARFILES}"
  197. local directory_path="$(dirname "${archive_path}")"
  198. mkdir -p "${directory_path}"
  199. if [[ -z "${directory}" ]]; then
  200. directory="$(basename "${source_path}")"
  201. fi
  202. archive_files_create "${source_path}"
  203. archive_files_date "${source_path}"
  204. local tar_options=(
  205. --create
  206. --xz
  207. --file="${archive_path}"
  208. --files-from="${tarfiles_path}"
  209. --transform="s,^,${directory}/,S"
  210. --no-recursion
  211. --warning=no-filename-with-nuls
  212. --null
  213. --owner=0
  214. --group=0
  215. --numeric-owner
  216. )
  217. (
  218. cd "${source_path}"
  219. tar "${tar_options[@]}"
  220. )
  221. }
  222. archive_extract() {
  223. local archive_path="$1"
  224. local destination_path="$2"
  225. if [[ -z "${destination_path}" ]]; then
  226. destination_path="$(dirname "${archive_path}")"
  227. fi
  228. tar -xf "${archive_path}" -ps -C "${destination_path}"
  229. }
  230. rootfs_files_create() {
  231. local source_path="$1"
  232. local directory="$(basename "${source_path}")"
  233. local tarfiles_path="${source_path}/${DOTTARFILES}"
  234. # Files in "${tarfiles_path}" are NUL terminated.
  235. # `tr '\0' '\n'` for human-readable output.
  236. execute_root find "${source_path}" -print0 | env LC_ALL='C.UTF-8' sort -z | sed -z "1d;s,^${source_path}/\\?,,;/^${DOTTARFILES}\$/d" > "${tarfiles_path}"
  237. }
  238. rootfs_files_date() {
  239. local source_path="$1"
  240. local epoch_path="${source_path}/${DOTEPOCH}"
  241. if [[ -n "${SOURCE_DATE_EPOCH}" ]]; then
  242. execute_root find "${source_path}" -execdir touch --no-dereference --date="@${SOURCE_DATE_EPOCH}" {} +
  243. fi
  244. }
  245. rootfs_create() {
  246. local rootfs_path="$1"
  247. local source_path="$2"
  248. local directory="$3"
  249. local tarfiles_path="${source_path}/${DOTTARFILES}"
  250. local directory_path="$(dirname "${rootfs_path}")"
  251. mkdir -p "${directory_path}"
  252. if [[ -z "${directory}" ]]; then
  253. directory="$(basename "${source_path}")"
  254. fi
  255. rootfs_files_create "${source_path}"
  256. rootfs_files_date "${source_path}"
  257. local tar_options=(
  258. --create
  259. --xz
  260. --file="${rootfs_path}"
  261. --files-from="${tarfiles_path}"
  262. --no-recursion
  263. --warning=no-filename-with-nuls
  264. --null
  265. --owner=0
  266. --group=0
  267. --numeric-owner
  268. )
  269. (
  270. cd "${source_path}"
  271. execute_root tar "${tar_options[@]}"
  272. )
  273. execute_root chmod 644 "${rootfs_path}"
  274. execute_root chown "${USER}:${USER}" "${rootfs_path}"
  275. }
  276. requirements() {
  277. local requirement
  278. local requirement_path
  279. for requirement in "$@"
  280. do
  281. requirement_path=$( which "$requirement" || true )
  282. if [ -z "$requirement_path" ]
  283. then
  284. printf '%s\n' "Missing requirement: $requirement" >&2
  285. exit 1
  286. fi
  287. done
  288. }
  289. requirements_root() {
  290. local requirement
  291. local requirement_path
  292. for requirement in "$@"
  293. do
  294. # We need to keep stdout output to show the command.
  295. requirement_path=$( execute_root which "$requirement" || true )
  296. if [ -z "$requirement_path" ]
  297. then
  298. printf '%s\n' "Missing requirement: $requirement" >&2
  299. exit 1
  300. fi
  301. done
  302. }
  303. arguments_concat() {
  304. local delimiter=$1
  305. shift
  306. local concat
  307. for argument in "$@"
  308. do
  309. if ! [ -z "$concat" ]
  310. then
  311. concat="$concat""$delimiter""$argument"
  312. else
  313. concat="$argument"
  314. fi
  315. done
  316. printf '%s\n' "$concat"
  317. }
  318. execute_root() {
  319. local sudo=$( which sudo 2> /dev/null || true )
  320. local arguments
  321. printf '%s' 'Running command as root: ' >&2
  322. printf '%b\n' "$*" >&2
  323. if ! [ -z "$sudo" ]
  324. then
  325. sudo "$@"
  326. else
  327. # Quote arguments for eval through su.
  328. arguments=$( printf '%q ' "$@" )
  329. su -c "$arguments"
  330. fi
  331. }