1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/update-packaging/common.sh Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,264 @@ 1.4 +#!/bin/bash 1.5 +# This Source Code Form is subject to the terms of the Mozilla Public 1.6 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.8 + 1.9 +# 1.10 +# Code shared by update packaging scripts. 1.11 +# Author: Darin Fisher 1.12 +# 1.13 + 1.14 +# ----------------------------------------------------------------------------- 1.15 +QUIET=0 1.16 + 1.17 +# By default just assume that these tools exist on our path 1.18 +MAR=${MAR:-mar} 1.19 +BZIP2=${BZIP2:-bzip2} 1.20 +MBSDIFF=${MBSDIFF:-mbsdiff} 1.21 + 1.22 +# ----------------------------------------------------------------------------- 1.23 +# Helper routines 1.24 + 1.25 +notice() { 1.26 + echo "$*" 1>&2 1.27 +} 1.28 + 1.29 +verbose_notice() { 1.30 + if [ $QUIET -eq 0 ]; then 1.31 + notice "$*" 1.32 + fi 1.33 +} 1.34 + 1.35 +get_file_size() { 1.36 + info=($(ls -ln "$1")) 1.37 + echo ${info[4]} 1.38 +} 1.39 + 1.40 +copy_perm() { 1.41 + reference="$1" 1.42 + target="$2" 1.43 + 1.44 + if [ -x "$reference" ]; then 1.45 + chmod 0755 "$target" 1.46 + else 1.47 + chmod 0644 "$target" 1.48 + fi 1.49 +} 1.50 + 1.51 +make_add_instruction() { 1.52 + f="$1" 1.53 + filev2="$2" 1.54 + # The third param will be an empty string when a file add instruction is only 1.55 + # needed in the version 2 manifest. This only happens when the file has an 1.56 + # add-if-not instruction in the version 3 manifest. This is due to the 1.57 + # precomplete file prior to the version 3 manifest having a remove instruction 1.58 + # for this file so the file is removed before applying a complete update. 1.59 + filev3="$3" 1.60 + 1.61 + # Used to log to the console 1.62 + if [ $4 ]; then 1.63 + forced=" (forced)" 1.64 + else 1.65 + forced= 1.66 + fi 1.67 + 1.68 + is_extension=$(echo "$f" | grep -c 'distribution/extensions/.*/') 1.69 + if [ $is_extension = "1" ]; then 1.70 + # Use the subdirectory of the extensions folder as the file to test 1.71 + # before performing this add instruction. 1.72 + testdir=$(echo "$f" | sed 's/\(.*distribution\/extensions\/[^\/]*\)\/.*/\1/') 1.73 + verbose_notice " add-if \"$testdir\" \"$f\"" 1.74 + echo "add-if \"$testdir\" \"$f\"" >> "$filev2" 1.75 + if [ ! "$filev3" = "" ]; then 1.76 + echo "add-if \"$testdir\" \"$f\"" >> "$filev3" 1.77 + fi 1.78 + else 1.79 + verbose_notice " add \"$f\"$forced" 1.80 + echo "add \"$f\"" >> "$filev2" 1.81 + if [ ! "$filev3" = "" ]; then 1.82 + echo "add \"$f\"" >> "$filev3" 1.83 + fi 1.84 + fi 1.85 +} 1.86 + 1.87 +check_for_add_if_not_update() { 1.88 + add_if_not_file_chk="$1" 1.89 + 1.90 + if [ `basename $add_if_not_file_chk` = "channel-prefs.js" -o \ 1.91 + `basename $add_if_not_file_chk` = "update-settings.ini" ]; then 1.92 + ## "true" *giggle* 1.93 + return 0; 1.94 + fi 1.95 + ## 'false'... because this is bash. Oh yay! 1.96 + return 1; 1.97 +} 1.98 + 1.99 +check_for_add_to_manifestv2() { 1.100 + add_if_not_file_chk="$1" 1.101 + 1.102 + if [ `basename $add_if_not_file_chk` = "update-settings.ini" ]; then 1.103 + ## "true" *giggle* 1.104 + return 0; 1.105 + fi 1.106 + ## 'false'... because this is bash. Oh yay! 1.107 + return 1; 1.108 +} 1.109 + 1.110 +make_add_if_not_instruction() { 1.111 + f="$1" 1.112 + filev3="$2" 1.113 + 1.114 + verbose_notice " add-if-not \"$f\" \"$f\"" 1.115 + echo "add-if-not \"$f\" \"$f\"" >> "$filev3" 1.116 +} 1.117 + 1.118 +make_addsymlink_instruction() { 1.119 + link="$1" 1.120 + target="$2" 1.121 + filev2="$3" 1.122 + filev3="$4" 1.123 + 1.124 + verbose_notice " addsymlink: $link -> $target" 1.125 + echo "addsymlink \"$link\" \"$target\"" >> "$filev2" 1.126 + echo "addsymlink \"$link\" \"$target\"" >> "$filev3" 1.127 +} 1.128 + 1.129 +make_patch_instruction() { 1.130 + f="$1" 1.131 + filev2="$2" 1.132 + filev3="$3" 1.133 + 1.134 + is_extension=$(echo "$f" | grep -c 'distribution/extensions/.*/') 1.135 + if [ $is_extension = "1" ]; then 1.136 + # Use the subdirectory of the extensions folder as the file to test 1.137 + # before performing this add instruction. 1.138 + testdir=$(echo "$f" | sed 's/\(.*distribution\/extensions\/[^\/]*\)\/.*/\1/') 1.139 + verbose_notice " patch-if \"$testdir\" \"$f.patch\" \"$f\"" 1.140 + echo "patch-if \"$testdir\" \"$f.patch\" \"$f\"" >> "$filev2" 1.141 + echo "patch-if \"$testdir\" \"$f.patch\" \"$f\"" >> "$filev3" 1.142 + else 1.143 + verbose_notice " patch \"$f.patch\" \"$f\"" 1.144 + echo "patch \"$f.patch\" \"$f\"" >> "$filev2" 1.145 + echo "patch \"$f.patch\" \"$f\"" >> "$filev3" 1.146 + fi 1.147 +} 1.148 + 1.149 +append_remove_instructions() { 1.150 + dir="$1" 1.151 + filev2="$2" 1.152 + filev3="$3" 1.153 + 1.154 + if [ -f "$dir/removed-files" ]; then 1.155 + prefix= 1.156 + listfile="$dir/removed-files" 1.157 + elif [ -f "$dir/Contents/MacOS/removed-files" ]; then 1.158 + prefix=Contents/MacOS/ 1.159 + listfile="$dir/Contents/MacOS/removed-files" 1.160 + fi 1.161 + if [ -n "$listfile" ]; then 1.162 + # Map spaces to pipes so that we correctly handle filenames with spaces. 1.163 + files=($(cat "$listfile" | tr " " "|" | sort -r)) 1.164 + num_files=${#files[*]} 1.165 + for ((i=0; $i<$num_files; i=$i+1)); do 1.166 + # Map pipes back to whitespace and remove carriage returns 1.167 + f=$(echo ${files[$i]} | tr "|" " " | tr -d '\r') 1.168 + # Trim whitespace 1.169 + f=$(echo $f) 1.170 + # Exclude blank lines. 1.171 + if [ -n "$f" ]; then 1.172 + # Exclude comments 1.173 + if [ ! $(echo "$f" | grep -c '^#') = 1 ]; then 1.174 + # Normalize the path to the root of the Mac OS X bundle if necessary 1.175 + fixedprefix="$prefix" 1.176 + if [ $prefix ]; then 1.177 + if [ $(echo "$f" | grep -c '^\.\./') = 1 ]; then 1.178 + if [ $(echo "$f" | grep -c '^\.\./\.\./') = 1 ]; then 1.179 + f=$(echo $f | sed -e 's:^\.\.\/\.\.\/::') 1.180 + fixedprefix="" 1.181 + else 1.182 + f=$(echo $f | sed -e 's:^\.\.\/::') 1.183 + fixedprefix=$(echo "$prefix" | sed -e 's:[^\/]*\/$::') 1.184 + fi 1.185 + fi 1.186 + fi 1.187 + if [ $(echo "$f" | grep -c '\/$') = 1 ]; then 1.188 + verbose_notice " rmdir \"$fixedprefix$f\"" 1.189 + echo "rmdir \"$fixedprefix$f\"" >> "$filev2" 1.190 + echo "rmdir \"$fixedprefix$f\"" >> "$filev3" 1.191 + elif [ $(echo "$f" | grep -c '\/\*$') = 1 ]; then 1.192 + # Remove the * 1.193 + f=$(echo "$f" | sed -e 's:\*$::') 1.194 + verbose_notice " rmrfdir \"$fixedprefix$f\"" 1.195 + echo "rmrfdir \"$fixedprefix$f\"" >> "$filev2" 1.196 + echo "rmrfdir \"$fixedprefix$f\"" >> "$filev3" 1.197 + else 1.198 + verbose_notice " remove \"$fixedprefix$f\"" 1.199 + echo "remove \"$fixedprefix$f\"" >> "$filev2" 1.200 + echo "remove \"$fixedprefix$f\"" >> "$filev3" 1.201 + fi 1.202 + fi 1.203 + fi 1.204 + done 1.205 + fi 1.206 +} 1.207 + 1.208 +# List all files in the current directory, stripping leading "./" 1.209 +# Pass a variable name and it will be filled as an array. 1.210 +# To support Tor Browser updates, skip the following files: 1.211 +# TorBrowser/Data/Browser/profiles.ini 1.212 +# TorBrowser/Data/Browser/profile.default/bookmarks.html 1.213 +# TorBrowser/Data/Tor/torrc 1.214 +list_files() { 1.215 + count=0 1.216 + 1.217 + find . -type f \ 1.218 + ! -name "update.manifest" \ 1.219 + ! -name "updatev2.manifest" \ 1.220 + ! -name "updatev3.manifest" \ 1.221 + ! -name "temp-dirlist" \ 1.222 + ! -name "temp-filelist" \ 1.223 + | sed 's/\.\/\(.*\)/\1/' \ 1.224 + | sort -r > "temp-filelist" 1.225 + while read file; do 1.226 + if [ "$file" = "TorBrowser/Data/Browser/profiles.ini" -o \ 1.227 + "$file" = "TorBrowser/Data/Browser/profile.default/bookmarks.html" -o \ 1.228 + "$file" = "TorBrowser/Data/Tor/torrc" ]; then 1.229 + continue; 1.230 + fi 1.231 + eval "${1}[$count]=\"$file\"" 1.232 + (( count++ )) 1.233 + done < "temp-filelist" 1.234 + rm "temp-filelist" 1.235 +} 1.236 + 1.237 +# List all directories in the current directory, stripping leading "./" 1.238 +list_dirs() { 1.239 + count=0 1.240 + 1.241 + find . -type d \ 1.242 + ! -name "." \ 1.243 + ! -name ".." \ 1.244 + | sed 's/\.\/\(.*\)/\1/' \ 1.245 + | sort -r > "temp-dirlist" 1.246 + while read dir; do 1.247 + eval "${1}[$count]=\"$dir\"" 1.248 + (( count++ )) 1.249 + done < "temp-dirlist" 1.250 + rm "temp-dirlist" 1.251 +} 1.252 + 1.253 +# List all symbolic links in the current directory, stripping leading "./" 1.254 +list_symlinks() { 1.255 + count=0 1.256 + 1.257 + find . -type l \ 1.258 + | sed 's/\.\/\(.*\)/\1/' \ 1.259 + | sort -r > "temp-symlinklist" 1.260 + while read symlink; do 1.261 + target=$(readlink "$symlink") 1.262 + eval "${1}[$count]=\"$symlink\"" 1.263 + eval "${2}[$count]=\"$target\"" 1.264 + (( count++ )) 1.265 + done < "temp-symlinklist" 1.266 + rm "temp-symlinklist" 1.267 +}