common 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. function_check() {
  41. local function=$1
  42. declare -f -F "$function" > /dev/null
  43. }
  44. variable_check() {
  45. local variable=$1
  46. test ! -z "${!variable}"
  47. }
  48. arguments_list() {
  49. local argument
  50. for argument in "$@"
  51. do
  52. printf '%s\n' "$argument"
  53. done
  54. }
  55. download_wrapper() {
  56. local download_dir="$1"
  57. shift
  58. local uris=($@)
  59. # TODO: Add support for curl, in addition
  60. # to wget, for compatibility reasons
  61. wget_options=(
  62. --config=/dev/null
  63. --secure-protocol=PFS
  64. --directory-prefix="${download_dir}"
  65. --continue
  66. --
  67. )
  68. wget "${wget_options[@]}" "${uris[@]}"
  69. }
  70. diff_patch_file() {
  71. local repository_path="$1"
  72. local patch_file_path="$2"
  73. # TODO: Improve handling of filenames to avoid gotchas w/ \n, \t, etc.
  74. local filename_in_diff="$(sed -rne 's/^-{3}\s+([^ \r\n]*).*/\1/p' "${patch_file_path}")"
  75. local source_file_path
  76. if ! ( grep -E '^-{3}.*/' "${patch_file_path}" >/dev/null 2>&1 ); then
  77. source_file_path="${repository_path}/${filename_in_diff}"
  78. else
  79. source_file_path="${repository_path}/${filename_in_diff##*/}"
  80. fi
  81. patch "${source_file_path}" "${patch_file_path}"
  82. }
  83. path_wildcard_expand() {
  84. local path=$@
  85. # Evaluation fails with unescaped whitespaces.
  86. path=$( printf '%s\n' "$path" | sed "s/ /\\\ /g" )
  87. eval "arguments_list "$path""
  88. }
  89. file_checksum_create() {
  90. local path=$1
  91. local checksum_path="$path.$CHECKSUM"
  92. local name=$( basename "$path" )
  93. local directory_path=$( dirname "$path" )
  94. (
  95. cd "$directory_path"
  96. sha256sum "$name" > "$checksum_path"
  97. )
  98. }
  99. file_checksum_check() {
  100. local path=$1
  101. local checksum_path="$path.$CHECKSUM"
  102. local name=$( basename "$path" )
  103. local directory_path=$( dirname "$path" )
  104. if ! [ -f "$checksum_path" ]
  105. then
  106. printf '%s\n' 'Could not verify file checksum!' >&2
  107. return 1
  108. fi
  109. (
  110. cd "$directory_path"
  111. sha256sum -c "$checksum_path"
  112. )
  113. }
  114. file_signature_create() {
  115. local path=$1
  116. local signature_path="$path.$DSIG"
  117. if [ -z "$RELEASE_KEY" ]
  118. then
  119. return 0
  120. fi
  121. gpg --default-key "$RELEASE_KEY" --armor --output "$signature_path" --detach-sign --yes "$path"
  122. }
  123. file_signature_check() {
  124. local path=$1
  125. local signature_path="$path.$DSIG"
  126. if ! [ -f "$signature_path" ]
  127. then
  128. printf '%s\n' 'Could not verify file signature!' >&2
  129. return 1
  130. fi
  131. gpg --armor --verify "$signature_path" "$path"
  132. }
  133. file_verification_create() {
  134. local path=$1
  135. file_checksum_create "$path"
  136. file_signature_create "$path"
  137. }
  138. file_verification_check() {
  139. local path=$1
  140. file_checksum_check "$path"
  141. file_signature_check "$path"
  142. }
  143. file_exists_check() {
  144. local path=$1
  145. test -f "$path"
  146. }
  147. directory_filled_check() {
  148. local path=$1
  149. if [ -z "$( ls -A "$path" 2> /dev/null )" ]
  150. then
  151. return 1
  152. else
  153. return 0
  154. fi
  155. }
  156. archive_files_create() {
  157. local source_path="$1"
  158. local directory="$(basename "${source_path}")"
  159. local tarfiles_path="${source_path}/${DOTTARFILES}"
  160. local revision_path="${source_path}/${DOTREVISION}"
  161. local version_path="${source_path}/${DOTVERSION}"
  162. local epoch_path="${source_path}/${DOTEPOCH}"
  163. local rnd_seed_path="${source_path}/${DOTRNDSEED}"
  164. # Files in "${tarfiles_path}" are NUL terminated.
  165. # `tr '\0' '\n'` for human-readable output.
  166. if git_check "${source_path}"; then
  167. git_files "${source_path}" > "${tarfiles_path}"
  168. printf '%s\0' "${DOTTARFILES}" >> "${tarfiles_path}"
  169. else
  170. find "${source_path}" -print0 | env LC_ALL='C.UTF-8' sort -z | sed -z "1d;s,^${source_path}/\\?,,;/^${DOTTARFILES}\$/d" > "${tarfiles_path}"
  171. fi
  172. for dotfile in "${revision_path}" \
  173. "${version_path}" \
  174. "${epoch_path}" \
  175. "${rnd_seed_path}"
  176. do
  177. if [[ -f "${dotfile}" ]]; then
  178. printf '%s\0' ".${dotfile##*.}" >> "${tarfiles_path}"
  179. fi
  180. done
  181. }
  182. archive_files_date() {
  183. local source_path="$1"
  184. local epoch_path="${source_path}/${DOTEPOCH}"
  185. if [[ -n "${SOURCE_DATE_EPOCH}" ]]; then
  186. find "${source_path}" -execdir touch --no-dereference --date="@${SOURCE_DATE_EPOCH}" {} +
  187. fi
  188. }
  189. archive_create() {
  190. local archive_path="$1"
  191. local source_path="$2"
  192. local directory="$3"
  193. local tarfiles_path="${source_path}/${DOTTARFILES}"
  194. local directory_path="$(dirname "${archive_path}")"
  195. mkdir -p "${directory_path}"
  196. if [[ -z "${directory}" ]]; then
  197. directory="$(basename "${source_path}")"
  198. fi
  199. archive_files_create "${source_path}"
  200. archive_files_date "${source_path}"
  201. local tar_options=(
  202. --create
  203. --xz
  204. --file="${archive_path}"
  205. --files-from="${tarfiles_path}"
  206. --transform="s,^,${directory}/,S"
  207. --no-recursion
  208. --warning=no-filename-with-nuls
  209. --null
  210. --owner=0
  211. --group=0
  212. --numeric-owner
  213. )
  214. (
  215. cd "${source_path}"
  216. tar "${tar_options[@]}"
  217. )
  218. }
  219. archive_extract() {
  220. local archive_path="$1"
  221. local destination_path="$2"
  222. if [[ -z "${destination_path}" ]]; then
  223. destination_path="$(dirname "${archive_path}")"
  224. fi
  225. tar -xf "${archive_path}" -ps -C "${destination_path}"
  226. }
  227. rootfs_files_create() {
  228. local source_path="$1"
  229. local directory="$(basename "${source_path}")"
  230. local tarfiles_path="${source_path}/${DOTTARFILES}"
  231. # Files in "${tarfiles_path}" are NUL terminated.
  232. # `tr '\0' '\n'` for human-readable output.
  233. execute_root find "${source_path}" -print0 | env LC_ALL='C.UTF-8' sort -z | sed -z "1d;s,^${source_path}/\\?,,;/^${DOTTARFILES}\$/d" > "${tarfiles_path}"
  234. }
  235. rootfs_files_date() {
  236. local source_path="$1"
  237. local epoch_path="${source_path}/${DOTEPOCH}"
  238. if [[ -n "${SOURCE_DATE_EPOCH}" ]]; then
  239. execute_root find "${source_path}" -execdir touch --no-dereference --date="@${SOURCE_DATE_EPOCH}" {} +
  240. fi
  241. }
  242. rootfs_create() {
  243. local rootfs_path="$1"
  244. local source_path="$2"
  245. local directory="$3"
  246. local tarfiles_path="${source_path}/${DOTTARFILES}"
  247. local directory_path="$(dirname "${rootfs_path}")"
  248. mkdir -p "${directory_path}"
  249. if [[ -z "${directory}" ]]; then
  250. directory="$(basename "${source_path}")"
  251. fi
  252. rootfs_files_create "${source_path}"
  253. rootfs_files_date "${source_path}"
  254. local tar_options=(
  255. --create
  256. --xz
  257. --file="${rootfs_path}"
  258. --files-from="${tarfiles_path}"
  259. --no-recursion
  260. --warning=no-filename-with-nuls
  261. --null
  262. --owner=0
  263. --group=0
  264. --numeric-owner
  265. )
  266. (
  267. cd "${source_path}"
  268. execute_root tar "${tar_options[@]}"
  269. )
  270. execute_root chmod 644 "${rootfs_path}"
  271. execute_root chown "${USER}:${USER}" "${rootfs_path}"
  272. }
  273. requirements() {
  274. local requirement
  275. local requirement_path
  276. for requirement in "$@"
  277. do
  278. requirement_path=$( which "$requirement" || true )
  279. if [ -z "$requirement_path" ]
  280. then
  281. printf '%s\n' "Missing requirement: $requirement" >&2
  282. exit 1
  283. fi
  284. done
  285. }
  286. requirements_root() {
  287. local requirement
  288. local requirement_path
  289. for requirement in "$@"
  290. do
  291. # We need to keep stdout output to show the command.
  292. requirement_path=$( execute_root which "$requirement" || true )
  293. if [ -z "$requirement_path" ]
  294. then
  295. printf '%s\n' "Missing requirement: $requirement" >&2
  296. exit 1
  297. fi
  298. done
  299. }
  300. arguments_concat() {
  301. local delimiter=$1
  302. shift
  303. local concat
  304. for argument in "$@"
  305. do
  306. if ! [ -z "$concat" ]
  307. then
  308. concat="$concat""$delimiter""$argument"
  309. else
  310. concat="$argument"
  311. fi
  312. done
  313. printf '%s\n' "$concat"
  314. }
  315. execute_root() {
  316. local sudo=$( which sudo 2> /dev/null || true )
  317. local arguments
  318. printf '%s' 'Running command as root: ' >&2
  319. printf '%b\n' "$*" >&2
  320. if ! [ -z "$sudo" ]
  321. then
  322. sudo "$@"
  323. else
  324. # Quote arguments for eval through su.
  325. arguments=$( printf '%q ' "$@" )
  326. su -c "$arguments"
  327. fi
  328. }