summaryrefslogtreecommitdiffstats
path: root/chrome/installer/mac/dirpatcher.sh
blob: 223e82c68636eea14ee142a5ea073aa78a60dc98 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/bin/bash -p

# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# usage: dirpatcher.sh old_dir patch_dir new_dir
#
# dirpatcher creates new_dir from patch_dir by decompressing and copying
# files, and using goobspatch to apply binary diffs to files in old_dir.
#
# dirpatcher performs the inverse operation to dirdiffer. For more details,
# consult dirdiffer.sh.
#
# Exit codes:
#  0  OK
#  1  Unknown failure
#  2  Incorrect number of parameters
#  3  Input directories do not exist or are not directories
#  4  Output directory already exists
#  5  Parent of output directory does not exist or is not a directory
#  6  An input or output directories contains another
#  7  Could not create output directory
#  8  File already exists in output directory
#  9  Found an irregular file (non-directory, file, or symbolic link) in input
# 10  Could not create symbolic link
# 11  Unrecognized file extension
# 12  Attempt to patch a nonexistent or non-regular file
# 13  Patch application failed
# 14  File decompression failed
# 15  File copy failed
# 16  Could not set mode (permissions)
# 17  Could not set modification time

set -eu

# Environment sanitization. Set a known-safe PATH. Clear environment variables
# that might impact the interpreter's operation. The |bash -p| invocation
# on the #! line takes the bite out of BASH_ENV, ENV, and SHELLOPTS (among
# other features), but clearing them here ensures that they won't impact any
# shell scripts used as utility programs. SHELLOPTS is read-only and can't be
# unset, only unexported.
export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
unset BASH_ENV CDPATH ENV GLOBIGNORE IFS POSIXLY_CORRECT
export -n SHELLOPTS

shopt -s dotglob nullglob

# find_tool looks for an executable file named |tool_name|:
#  - in the same directory as this script,
#  - if this script is located in a Chromium source tree, at the expected
#    Release output location in the Mac xcodebuild directory,
#  - as above, but in the Debug output location
# If found in any of the above locations, the script's path is output.
# Otherwise, this function outputs |tool_name| as a fallback, allowing it to
# be found (or not) by an ordinary ${PATH} search.
find_tool() {
  local tool_name="${1}"

  local script_dir
  script_dir="$(dirname "${0}")"

  local tool="${script_dir}/${tool_name}"
  if [[ -f "${tool}" ]] && [[ -x "${tool}" ]]; then
    echo "${tool}"
    return
  fi

  local script_dir_phys
  script_dir_phys="$(cd "${script_dir}" && pwd -P)"
  if [[ "${script_dir_phys}" =~ ^(.*)/src/chrome/installer/mac$ ]]; then
    tool="${BASH_REMATCH[1]}/src/xcodebuild/Release/${tool_name}"
    if [[ -f "${tool}" ]] && [[ -x "${tool}" ]]; then
      echo "${tool}"
      return
    fi

    tool="${BASH_REMATCH[1]}/src/xcodebuild/Debug/${tool_name}"
    if [[ -f "${tool}" ]] && [[ -x "${tool}" ]]; then
      echo "${tool}"
      return
    fi
  fi

  echo "${tool_name}"
}

ME="$(basename "${0}")"
readonly ME
GOOBSPATCH="$(find_tool goobspatch)"
readonly GOOBSPATCH
readonly BUNZIP2="bunzip2"
readonly GUNZIP="gunzip"
XZDEC="$(dirname "${0}")/xzdec"
readonly XZDEC
readonly GBS_SUFFIX='$gbs'
readonly BZ2_SUFFIX='$bz2'
readonly GZ_SUFFIX='$gz'
readonly XZ_SUFFIX='$xz'
readonly PLAIN_SUFFIX='$raw'

err() {
  local error="${1}"

  echo "${ME}: ${error}" >& 2
}

declare -a g_cleanup
cleanup() {
  local status=${?}

  trap - EXIT
  trap '' HUP INT QUIT TERM

  if [[ ${status} -ge 128 ]]; then
    err "Caught signal $((${status} - 128))"
  fi

  if [[ "${#g_cleanup[@]}" -gt 0 ]]; then
    rm -rf "${g_cleanup[@]}"
  fi

  exit ${status}
}

copy_mode_and_time() {
  local patch_file="${1}"
  local new_file="${2}"

  local mode
  mode="$(stat "-f%OMp%OLp" "${patch_file}")"
  if ! chmod -h "${mode}" "${new_file}"; then
    exit 16
  fi

  if ! [[ -L "${new_file}" ]]; then
    # Symbolic link modification times can't be copied because there's no
    # shell tool that provides direct access to lutimes. Instead, the symbolic
    # link was created with rsync, which already copied the timestamp with
    # lutimes.
    if ! touch -r "${patch_file}" "${new_file}"; then
      exit 17
    fi
  fi
}

apply_patch() {
  local old_file="${1}"
  local patch_file="${2}"
  local new_file="${3}"
  local patcher="${4}"

  if [[ -L "${old_file}" ]] || ! [[ -f "${old_file}" ]]; then
    err "can't patch nonexistent or irregular file ${old_file}"
    exit 12
  fi

  if ! "${patcher}" "${old_file}" "${new_file}" "${patch_file}"; then
    err "couldn't create ${new_file} by applying ${patch_file} to ${old_file}"
    exit 13
  fi
}

decompress_file() {
  local old_file="${1}"
  local patch_file="${2}"
  local new_file="${3}"
  local decompressor="${4}"

  if ! "${decompressor}" -c < "${patch_file}" > "${new_file}"; then
    err "couldn't decompress ${patch_file} to ${new_file} with ${decompressor}"
    exit 14
  fi
}

copy_file() {
  local old_file="${1}"
  local patch_file="${2}"
  local new_file="${3}"
  local extra="${4}"

  if ! cp "${patch_file}" "${new_file}"; then
    exit 15
  fi
}

patch_file() {
  local old_file="${1}"
  local patch_file="${2}"
  local new_file="${3}"

  local operation extra strip_length

  if [[ "${patch_file: -${#GBS_SUFFIX}}" = "${GBS_SUFFIX}" ]]; then
    operation="apply_patch"
    extra="${GOOBSPATCH}"
    strip_length=${#GBS_SUFFIX}
  elif [[ "${patch_file: -${#BZ2_SUFFIX}}" = "${BZ2_SUFFIX}" ]]; then
    operation="decompress_file"
    extra="${BUNZIP2}"
    strip_length=${#BZ2_SUFFIX}
  elif [[ "${patch_file: -${#GZ_SUFFIX}}" = "${GZ_SUFFIX}" ]]; then
    operation="decompress_file"
    extra="${GUNZIP}"
    strip_length=${#GZ_SUFFIX}
  elif [[ "${patch_file: -${#XZ_SUFFIX}}" = "${XZ_SUFFIX}" ]]; then
    operation="decompress_file"
    extra="${XZDEC}"
    strip_length=${#XZ_SUFFIX}
  elif [[ "${patch_file: -${#PLAIN_SUFFIX}}" = "${PLAIN_SUFFIX}" ]]; then
    operation="copy_file"
    extra="patch"
    strip_length=${#PLAIN_SUFFIX}
  else
    err "don't know how to operate on ${patch_file}"
    exit 11
  fi

  old_file="${old_file:0:${#old_file} - ${strip_length}}"
  new_file="${new_file:0:${#new_file} - ${strip_length}}"

  if [[ -e "${new_file}" ]]; then
    err "${new_file} already exists"
    exit 8
  fi

  "${operation}" "${old_file}" "${patch_file}" "${new_file}" "${extra}"

  copy_mode_and_time "${patch_file}" "${new_file}"
}

patch_symlink() {
  local patch_file="${1}"
  local new_file="${2}"

  # local target
  # target="$(readlink "${patch_file}")"
  # ln -s "${target}" "${new_file}"

  # Use rsync instead of the above, as it's the only way to preserve the
  # timestamp of a symbolic link using shell tools.
  if ! rsync -lt "${patch_file}" "${new_file}"; then
    exit 10
  fi

  copy_mode_and_time "${patch_file}" "${new_file}"
}

patch_dir() {
  local old_dir="${1}"
  local patch_dir="${2}"
  local new_dir="${3}"

  if ! mkdir "${new_dir}"; then
    exit 7
  fi

  local patch_file
  for patch_file in "${patch_dir}/"*; do
    local file="${patch_file:${#patch_dir} + 1}"
    local old_file="${old_dir}/${file}"
    local new_file="${new_dir}/${file}"

    if [[ -e "${new_file}" ]]; then
      err "${new_file} already exists"
      exit 8
    fi

    if [[ -L "${patch_file}" ]]; then
      patch_symlink "${patch_file}" "${new_file}"
    elif [[ -d "${patch_file}" ]]; then
      patch_dir "${old_file}" "${patch_file}" "${new_file}"
    elif ! [[ -f "${patch_file}" ]]; then
      err "can't handle irregular file ${patch_file}"
      exit 9
    else
      patch_file "${old_file}" "${patch_file}" "${new_file}"
    fi
  done

  copy_mode_and_time "${patch_dir}" "${new_dir}"
}

# shell_safe_path ensures that |path| is safe to pass to tools as a
# command-line argument. If the first character in |path| is "-", "./" is
# prepended to it. The possibily-modified |path| is output.
shell_safe_path() {
  local path="${1}"
  if [[ "${path:0:1}" = "-" ]]; then
    echo "./${path}"
  else
    echo "${path}"
  fi
}

dirs_contained() {
  local dir1="${1}/"
  local dir2="${2}/"

  if [[ "${dir1:0:${#dir2}}" = "${dir2}" ]] ||
     [[ "${dir2:0:${#dir1}}" = "${dir1}" ]]; then
    return 0
  fi

  return 1
}

usage() {
  echo "usage: ${ME} old_dir patch_dir new_dir" >& 2
}

main() {
  local old_dir patch_dir new_dir
  old_dir="$(shell_safe_path "${1}")"
  patch_dir="$(shell_safe_path "${2}")"
  new_dir="$(shell_safe_path "${3}")"

  trap cleanup EXIT HUP INT QUIT TERM

  if ! [[ -d "${old_dir}" ]] || ! [[ -d "${patch_dir}" ]]; then
    err "old_dir and patch_dir must exist and be directories"
    usage
    exit 3
  fi

  if [[ -e "${new_dir}" ]]; then
    err "new_dir must not exist"
    usage
    exit 4
  fi

  local new_dir_parent
  new_dir_parent="$(dirname "${new_dir}")"
  if ! [[ -d "${new_dir_parent}" ]]; then
    err "new_dir parent directory must exist and be a directory"
    usage
    exit 5
  fi

  local old_dir_phys patch_dir_phys new_dir_parent_phys new_dir_phys
  old_dir_phys="$(cd "${old_dir}" && pwd -P)"
  patch_dir_phys="$(cd "${patch_dir}" && pwd -P)"
  new_dir_parent_phys="$(cd "${new_dir_parent}" && pwd -P)"
  new_dir_phys="${new_dir_parent_phys}/$(basename "${new_dir}")"

  if dirs_contained "${old_dir_phys}" "${patch_dir_phys}" ||
     dirs_contained "${old_dir_phys}" "${new_dir_phys}" ||
     dirs_contained "${patch_dir_phys}" "${new_dir_phys}"; then
    err "directories must not contain one another"
    usage
    exit 6
  fi

  g_cleanup+=("${new_dir}")

  patch_dir "${old_dir}" "${patch_dir}" "${new_dir}"

  unset g_cleanup[${#g_cleanup[@]}]
  trap - EXIT
}

if [[ ${#} -ne 3 ]]; then
  usage
  exit 2
fi

main "${@}"
exit ${?}