common 7.9 KB

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