summaryrefslogtreecommitdiff
path: root/bin/create-build-support-package
blob: d1dd998f460d076f8c6ae6c24fa998fd5a8ac7b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/sh

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

# shellcheck disable=SC2016
usage() {
  >&2 echo 'usage:'
  >&2 echo '  create-build-support-package [options] --from $pkgname-$epoch:$pkgver-$pkgrel.$sub_pkgrel [--] $file1 $file2 $file3 ...'
  >&2 echo '    create $pkgname-shim containing'
  >&2 echo '    $file1, $file2, ... from the original package'
  >&2 echo '  create-build-support-package [options] --shim $pkgfile'
  >&2 echo '    insert $pkgfile into $repo_arch/build-support/$pkgname-shim'
  >&2 echo ''
  >&2 echo 'possible options:'
  >&2 echo '  -a|--arch $repo_arch: (required)'
  >&2 echo '    Operate on repository of given architecture.'
  >&2 echo '  -h|--help:'
  >&2 echo '    Show this help and exit.'
  >&2 echo '  -w|--wait:'
  >&2 echo '    Wait for lock if necessary.'
  [ -z "$1" ] && exit 1 || exit "$1"
}

eval set -- "$(
  getopt -o a:hw \
    --long arch: \
    --long from: \
    --long help \
    --long shim: \
    --long wait \
    -n "$(basename "$0")" -- "$@" || \
    echo usage
  )"

repo_arch=''
shim_package=''
source_package=''
wait_for_lock='-n'
while true; do
  case "$1" in
    -a|--arch)
      if [ -n "${repo_arch}" ]; then
        >&2 prinf -- '-a|--arch given more than once.\n'
        usage
      fi
      shift
      repo_arch="$1"
    ;;
    --from)
      if [ -n "${source_package}" ]; then
        >&2 prinf -- '--from given more than once.\n'
        usage
      fi
      shift
      source_package="$1"
    ;;
    -h|--help)
      usage 0
    ;;
    --shim)
      if [ -n "${shim_package}" ]; then
        >&2 prinf -- '--shim given more than once.\n'
        usage
      fi
      shift
      shim_package="$1"
    ;;
    -w|--wait)
      wait_for_lock=''
    ;;
    --)
      shift
      break
    ;;
    *)
      >&2 printf 'Whoops, forgot to implement option "%s" internally.\n' \
        "$1"
      exit 42
    ;;
  esac
  shift
done

if [ -n "${source_package}" ] \
  && [ -n "${shim_package}" ]; then
  >&2 echo 'Conflicting parameters: --from and --shim'
  usage
fi

exec 9> "${sanity_check_lock_file}"
verbose_flock -s ${wait_for_lock} 9

tmp_dir=$(mktemp -d 'tmp.create-build-support.XXXXXXXXXX')
# shellcheck disable=SC2064
trap "rm -rf --one-file-system '${tmp_dir:?}'" EXIT

if [ -n "${source_package}" ]; then
  # create shim_package from source_package

  if [ $# -eq 0 ]; then
    usage 1
  fi

  pkg=$(
    # shellcheck disable=SC2016
    {
      printf 'SELECT DISTINCT '
      mysql_package_name_query
      printf ' FROM `binary_packages`'
      mysql_join_binary_packages_binary_packages_in_repositories
      mysql_join_binary_packages_in_repositories_repositories
      mysql_join_repositories_architectures '' 'r_a'
      mysql_join_binary_packages_architectures
      printf ' WHERE `repositories`.`is_on_master_mirror`'
      printf ' AND `r_a`.`name`=from_base64("%s")' \
        "$(
          printf '%s' "${repo_arch}" \
            | base64 -w0
        )"
      printf ' AND '
      mysql_package_name_query '' 'XX' \
        | sed '
          s/,"-",`XX.*\.pkg\.tar\.xz"//
        '
      printf '=from_base64("%s")' \
        "$(
          printf '%s' "${source_package}" \
            | base64 -w0
        )"
    } \
      | mysql_run_query \
      | tr '\t' ' '
  )

  if [ -z "${pkg}" ]; then
    >&2 printf 'Could not find %s/%s in the database.\n' \
      "${repo_arch}" "${source_package}"
    exit 1
  fi

  if printf '%s\n' "${pkg}" \
    | wc -l \
    | grep -vqxF '1'; then
    >&2 printf 'Package %s/%s was found multiple times:\n' \
      "${repo_arch}" "${source_package}"
    >&2 printf '%s\n' "${pkg}"
    exit 1
  fi

  new_pkg="${pkg%-*-*-*}"
  new_pkg="${new_pkg}-shim${pkg#${new_pkg}}"

  failsafe_rsync \
    "${master_mirror_rsync_directory}/pool/${pkg}" \
    "${tmp_dir}/"

  mkdir "${tmp_dir}/content"
  bsdtar -C "${tmp_dir}/content" -xf "${tmp_dir}/${pkg}" \
    '.BUILDINFO' \
    '.PKGINFO' \
    "$@"

  # TODO: mangle customizable meta-infos here (new provides, keep some
  # depends)
  sed -i '
    /^pkgname = / s/ \S\+$/\0-shim\nprovides =\0/
    /^depend = /d
  ' "${tmp_dir}/content/.PKGINFO"

  # from /usr/bin/makepkg
  list_package_files() {
    (
      cd "${tmp_dir}/content"
      find . -path './.*' \! -name '.'; find . \! -path './.*' \! -name '.' \
        | LC_ALL=C sort
    ) \
      | sed -e 's|^\./||' \
      | tr '\n' '\0'
  }

  list_package_files \
    | LANG=C bsdtar -C "${tmp_dir}/content" -cnf - --format=mtree \
      --options='!all,use-set,type,uid,gid,mode,time,size,md5,sha256,link' \
      --null --files-from - --exclude .MTREE \
    | gzip -c -f -n \
    > "${tmp_dir}/content/.MTREE"

  list_package_files \
    | LANG=C bsdtar -C "${tmp_dir}/content" -cnf - --null --files-from - \
    | xz -c -z - \
    > "${tmp_dir}/${new_pkg}"

  rm -rf --one-file-system "${tmp_dir:?}/content"
  mv "${tmp_dir}/${new_pkg}" "${work_dir}/"
  >&2 printf 'Please sign %s with a build slave key and run\n' \
    "${work_dir}/${new_pkg}"
  # shellcheck disable=SC2016
  >&2 printf 'create-build-support-package --arch %s --shim %s\n' \
    "${repo_arch}" \
    "${work_dir}/${new_pkg}"

elif [ -n "${shim_package}" ]; then
  # insert shim_package into [build-support]

  if [ $# -ne 0 ]; then
    >&2 echo 'Too many arguments'
    usage
  fi

  if [ ! -r "${shim_package}" ]; then
    >&2 printf 'Cannot open file %s\n' \
      "${shim_package}"
    exit 1
  fi

  if ! gpg --batch --status-fd 1 -q --homedir /etc/pacman.d/gnupg --verify \
    "${shim_package}.sig" \
    "${shim_package}" \
    2> /dev/null \
    | cut -d' ' -f2 \
    | grep -qxF TRUST_FULLY; then
    >&2 printf '%s has invalid signature\n' \
      "${shim_package}"
    exit 1
  fi

  exec 8> "${package_database_lock_file}"
  verbose_flock ${wait_for_lock} 8

  original_pkg_id=$(
    # shellcheck disable=SC2016
    {
      printf 'SELECT DISTINCT `binary_packages`.`id`'
      printf ' FROM `binary_packages`'
      mysql_join_binary_packages_architectures
      printf ' WHERE '
      mysql_package_name_query \
        | sed 's/pkgname`,"/\0-shim/'
      printf '=from_base64("%s")' \
        "$(
          printf '%s' "${shim_package##*/}" \
            | base64 -w0
        )"
    } | \
      mysql_run_query
  )
  if [ -z "${original_pkg_id}" ]; then
    >&2 printf 'could not find the original package to %s.\n' \
      "${shim_package##*/}"
    exit 1
  fi

  # TODO: check signature, retrieve db, insert package into db, copy
  # row in `binary_packages`, insert row into
  # `binary_packages_in_repositories`, add package to pool/, send db

else
  >&2 echo 'Need either --from or --shim.'
  usage
fi