common 6.8 KB

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