make_incremental_update.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/bin/bash
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. #
  6. # This tool generates incremental update packages for the update system.
  7. # Author: Darin Fisher
  8. #
  9. . $(dirname "$0")/common.sh
  10. # -----------------------------------------------------------------------------
  11. print_usage() {
  12. notice "Usage: $(basename $0) [OPTIONS] ARCHIVE FROMDIR TODIR"
  13. notice ""
  14. notice "The differences between FROMDIR and TODIR will be stored in ARCHIVE."
  15. notice ""
  16. notice "Options:"
  17. notice " -h show this help text"
  18. notice " -f clobber this file in the installation"
  19. notice " Must be a path to a file to clobber in the partial update."
  20. notice ""
  21. }
  22. check_for_forced_update() {
  23. force_list="$1"
  24. forced_file_chk="$2"
  25. local f
  26. if [ "$forced_file_chk" = "precomplete" ]; then
  27. ## "true" *giggle*
  28. return 0;
  29. fi
  30. if [ "$forced_file_chk" = "Contents/Resources/precomplete" ]; then
  31. ## "true" *giggle*
  32. return 0;
  33. fi
  34. if [ "$forced_file_chk" = "removed-files" ]; then
  35. ## "true" *giggle*
  36. return 0;
  37. fi
  38. if [ "$forced_file_chk" = "Contents/Resources/removed-files" ]; then
  39. ## "true" *giggle*
  40. return 0;
  41. fi
  42. if [ "$forced_file_chk" = "chrome.manifest" ]; then
  43. ## "true" *giggle*
  44. return 0;
  45. fi
  46. if [ "$forced_file_chk" = "Contents/Resources/chrome.manifest" ]; then
  47. ## "true" *giggle*
  48. return 0;
  49. fi
  50. if [ "${forced_file_chk##*.}" = "chk" ]; then
  51. ## "true" *giggle*
  52. return 0;
  53. fi
  54. for f in $force_list; do
  55. #echo comparing $forced_file_chk to $f
  56. if [ "$forced_file_chk" = "$f" ]; then
  57. ## "true" *giggle*
  58. return 0;
  59. fi
  60. done
  61. ## 'false'... because this is bash. Oh yay!
  62. return 1;
  63. }
  64. if [ $# = 0 ]; then
  65. print_usage
  66. exit 1
  67. fi
  68. requested_forced_updates='Contents/MacOS/firefox'
  69. while getopts "hf:" flag
  70. do
  71. case "$flag" in
  72. h) print_usage; exit 0
  73. ;;
  74. f) requested_forced_updates="$requested_forced_updates $OPTARG"
  75. ;;
  76. ?) print_usage; exit 1
  77. ;;
  78. esac
  79. done
  80. # -----------------------------------------------------------------------------
  81. let arg_start=$OPTIND-1
  82. shift $arg_start
  83. archive="$1"
  84. olddir="$2"
  85. newdir="$3"
  86. # Prevent the workdir from being inside the targetdir so it isn't included in
  87. # the update mar.
  88. if [ $(echo "$newdir" | grep -c '\/$') = 1 ]; then
  89. # Remove the /
  90. newdir=$(echo "$newdir" | sed -e 's:\/$::')
  91. fi
  92. workdir="$newdir.work"
  93. updatemanifestv2="$workdir/updatev2.manifest"
  94. updatemanifestv3="$workdir/updatev3.manifest"
  95. archivefiles="updatev2.manifest updatev3.manifest"
  96. mkdir -p "$workdir"
  97. # Generate a list of all files in the target directory.
  98. pushd "$olddir"
  99. if test $? -ne 0 ; then
  100. exit 1
  101. fi
  102. list_files oldfiles
  103. list_dirs olddirs
  104. popd
  105. pushd "$newdir"
  106. if test $? -ne 0 ; then
  107. exit 1
  108. fi
  109. if [ ! -f "precomplete" ]; then
  110. if [ ! -f "Contents/Resources/precomplete" ]; then
  111. notice "precomplete file is missing!"
  112. exit 1
  113. fi
  114. fi
  115. list_dirs newdirs
  116. list_files newfiles
  117. popd
  118. # Add the type of update to the beginning of the update manifests.
  119. notice ""
  120. notice "Adding type instruction to update manifests"
  121. > $updatemanifestv2
  122. > $updatemanifestv3
  123. notice " type partial"
  124. echo "type \"partial\"" >> $updatemanifestv2
  125. echo "type \"partial\"" >> $updatemanifestv3
  126. notice ""
  127. notice "Adding file patch and add instructions to update manifests"
  128. num_oldfiles=${#oldfiles[*]}
  129. remove_array=
  130. num_removes=0
  131. for ((i=0; $i<$num_oldfiles; i=$i+1)); do
  132. f="${oldfiles[$i]}"
  133. # If this file exists in the new directory as well, then check if it differs.
  134. if [ -f "$newdir/$f" ]; then
  135. if check_for_add_if_not_update "$f"; then
  136. # The full workdir may not exist yet, so create it if necessary.
  137. mkdir -p `dirname "$workdir/$f"`
  138. $BZIP2 -cz9 "$newdir/$f" > "$workdir/$f"
  139. copy_perm "$newdir/$f" "$workdir/$f"
  140. make_add_if_not_instruction "$f" "$updatemanifestv3"
  141. archivefiles="$archivefiles \"$f\""
  142. continue 1
  143. fi
  144. if check_for_forced_update "$requested_forced_updates" "$f"; then
  145. # The full workdir may not exist yet, so create it if necessary.
  146. mkdir -p `dirname "$workdir/$f"`
  147. $BZIP2 -cz9 "$newdir/$f" > "$workdir/$f"
  148. copy_perm "$newdir/$f" "$workdir/$f"
  149. make_add_instruction "$f" "$updatemanifestv2" "$updatemanifestv3" 1
  150. archivefiles="$archivefiles \"$f\""
  151. continue 1
  152. fi
  153. if ! diff "$olddir/$f" "$newdir/$f" > /dev/null; then
  154. # Compute both the compressed binary diff and the compressed file, and
  155. # compare the sizes. Then choose the smaller of the two to package.
  156. dir=$(dirname "$workdir/$f")
  157. mkdir -p "$dir"
  158. notice "diffing \"$f\""
  159. # MBSDIFF_HOOK represents the communication interface with funsize and,
  160. # if enabled, caches the intermediate patches for future use and
  161. # compute avoidance
  162. #
  163. # An example of MBSDIFF_HOOK env variable could look like this:
  164. # export MBSDIFF_HOOK="myscript.sh -A https://funsize/api -c /home/user"
  165. # where myscript.sh has the following usage:
  166. # myscript.sh -A SERVER-URL [-c LOCAL-CACHE-DIR-PATH] [-g] [-u] \
  167. # PATH-FROM-URL PATH-TO-URL PATH-PATCH SERVER-URL
  168. #
  169. # Note: patches are bzipped stashed in funsize to gain more speed
  170. # if service is not enabled then default to old behavior
  171. if [ -z "$MBSDIFF_HOOK" ]; then
  172. $MBSDIFF "$olddir/$f" "$newdir/$f" "$workdir/$f.patch"
  173. $BZIP2 -z9 "$workdir/$f.patch"
  174. else
  175. # if service enabled then check patch existence for retrieval
  176. if $MBSDIFF_HOOK -g "$olddir/$f" "$newdir/$f" "$workdir/$f.patch.bz2"; then
  177. notice "file \"$f\" found in funsize, diffing skipped"
  178. else
  179. # if not found already - compute it and cache it for future use
  180. $MBSDIFF "$olddir/$f" "$newdir/$f" "$workdir/$f.patch"
  181. $BZIP2 -z9 "$workdir/$f.patch"
  182. $MBSDIFF_HOOK -u "$olddir/$f" "$newdir/$f" "$workdir/$f.patch.bz2"
  183. fi
  184. fi
  185. $BZIP2 -cz9 "$newdir/$f" > "$workdir/$f"
  186. copy_perm "$newdir/$f" "$workdir/$f"
  187. patchfile="$workdir/$f.patch.bz2"
  188. patchsize=$(get_file_size "$patchfile")
  189. fullsize=$(get_file_size "$workdir/$f")
  190. if [ $patchsize -lt $fullsize ]; then
  191. make_patch_instruction "$f" "$updatemanifestv2" "$updatemanifestv3"
  192. mv -f "$patchfile" "$workdir/$f.patch"
  193. rm -f "$workdir/$f"
  194. archivefiles="$archivefiles \"$f.patch\""
  195. else
  196. make_add_instruction "$f" "$updatemanifestv2" "$updatemanifestv3"
  197. rm -f "$patchfile"
  198. archivefiles="$archivefiles \"$f\""
  199. fi
  200. fi
  201. else
  202. # remove instructions are added after add / patch instructions for
  203. # consistency with make_incremental_updates.py
  204. remove_array[$num_removes]=$f
  205. (( num_removes++ ))
  206. fi
  207. done
  208. # Newly added files
  209. notice ""
  210. notice "Adding file add instructions to update manifests"
  211. num_newfiles=${#newfiles[*]}
  212. for ((i=0; $i<$num_newfiles; i=$i+1)); do
  213. f="${newfiles[$i]}"
  214. # If we've already tested this file, then skip it
  215. for ((j=0; $j<$num_oldfiles; j=$j+1)); do
  216. if [ "$f" = "${oldfiles[j]}" ]; then
  217. continue 2
  218. fi
  219. done
  220. dir=$(dirname "$workdir/$f")
  221. mkdir -p "$dir"
  222. $BZIP2 -cz9 "$newdir/$f" > "$workdir/$f"
  223. copy_perm "$newdir/$f" "$workdir/$f"
  224. if check_for_add_if_not_update "$f"; then
  225. make_add_if_not_instruction "$f" "$updatemanifestv3"
  226. else
  227. make_add_instruction "$f" "$updatemanifestv2" "$updatemanifestv3"
  228. fi
  229. archivefiles="$archivefiles \"$f\""
  230. done
  231. notice ""
  232. notice "Adding file remove instructions to update manifests"
  233. for ((i=0; $i<$num_removes; i=$i+1)); do
  234. f="${remove_array[$i]}"
  235. notice " remove \"$f\""
  236. echo "remove \"$f\"" >> $updatemanifestv2
  237. echo "remove \"$f\"" >> $updatemanifestv3
  238. done
  239. # Add remove instructions for any dead files.
  240. notice ""
  241. notice "Adding file and directory remove instructions from file 'removed-files'"
  242. append_remove_instructions "$newdir" "$updatemanifestv2" "$updatemanifestv3"
  243. notice ""
  244. notice "Adding directory remove instructions for directories that no longer exist"
  245. num_olddirs=${#olddirs[*]}
  246. for ((i=0; $i<$num_olddirs; i=$i+1)); do
  247. f="${olddirs[$i]}"
  248. # If this dir doesn't exist in the new directory remove it.
  249. if [ ! -d "$newdir/$f" ]; then
  250. notice " rmdir $f/"
  251. echo "rmdir \"$f/\"" >> $updatemanifestv2
  252. echo "rmdir \"$f/\"" >> $updatemanifestv3
  253. fi
  254. done
  255. $BZIP2 -z9 "$updatemanifestv2" && mv -f "$updatemanifestv2.bz2" "$updatemanifestv2"
  256. $BZIP2 -z9 "$updatemanifestv3" && mv -f "$updatemanifestv3.bz2" "$updatemanifestv3"
  257. mar_command="$MAR"
  258. if [[ -n $MOZ_PRODUCT_VERSION ]]
  259. then
  260. mar_command="$mar_command -V $MOZ_PRODUCT_VERSION"
  261. fi
  262. if [[ -n $MOZ_CHANNEL_ID ]]
  263. then
  264. mar_command="$mar_command -H $MOZ_CHANNEL_ID"
  265. fi
  266. mar_command="$mar_command -C \"$workdir\" -c output.mar"
  267. eval "$mar_command $archivefiles"
  268. mv -f "$workdir/output.mar" "$archive"
  269. # cleanup
  270. rm -fr "$workdir"
  271. notice ""
  272. notice "Finished"
  273. notice ""