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
|
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This tool contains functions that are to be used to handle/enable funsize
# Author: Mihai Tabara
#
HOOK=
AWS_BUCKET_NAME=
LOCAL_CACHE_DIR=
# Don't cache files smaller than this, as it's slower with S3
# Bug 1437473
CACHE_THRESHOLD=500000
S3_CACHE_HITS=0
S3_CACHE_MISSES=0
getsha512(){
openssl sha512 "${1}" | awk '{print $2}'
}
print_usage(){
echo "$(basename "$0") [-S S3-BUCKET-NAME] [-c LOCAL-CACHE-DIR-PATH] [-g] [-u] PATH-FROM-URL PATH-TO-URL PATH-PATCH"
echo "Script that saves/retrieves from cache presumptive patches as args"
echo ""
echo "-A SERVER-URL - host where to send the files"
echo "-c LOCAL-CACHE-DIR-PATH local path to which patches are cached"
echo "-g pre hook - tests whether patch already in cache"
echo "-u post hook - upload patch to cache for future use"
echo ""
echo "PATH-FROM-URL : path on disk for source file"
echo "PATH-TO-URL : path on disk for destination file"
echo "PATH-PATCH : path on disk for patch between source and destination"
}
upload_patch(){
if [ "$(stat -c "%s" "$2")" -lt ${CACHE_THRESHOLD} ]
then
return 0
fi
sha_from=$(getsha512 "$1")
sha_to=$(getsha512 "$2")
patch_path="$3"
patch_filename="$(basename "$3")"
# save to local cache first
if [ -n "$LOCAL_CACHE_DIR" ]; then
local_cmd="mkdir -p "$LOCAL_CACHE_DIR/$sha_from""
if $local_cmd >&2; then
cp -avf "${patch_path}" "$LOCAL_CACHE_DIR/$sha_from/$sha_to"
echo "${patch_path} saved on local cache."
fi
fi
if [ -n "${AWS_BUCKET_NAME}" ]; then
BUCKET_PATH="s3://${AWS_BUCKET_NAME}${sha_from}/${sha_to}/${patch_filename}"
if aws s3 cp "${patch_path}" "${BUCKET_PATH}"; then
echo "${patch_path} saved on s://${AWS_BUCKET_NAME}"
return 0
fi
echo "${patch_path} failed to be uploaded to s3://${AWS_BUCKET_NAME}"
return 1
fi
return 0
}
get_patch(){
# $1 and $2 are the /path/to/filename
if [ "$(stat -c "%s" "$2")" -lt ${CACHE_THRESHOLD} ]
then
return 1
fi
sha_from=$(getsha512 "$1")
sha_to=$(getsha512 "$2")
destination_file="$3"
s3_filename="$(basename "$3")"
# Try to retrieve from local cache first.
if [ -n "$LOCAL_CACHE_DIR" ]; then
if [ -r "$LOCAL_CACHE_DIR/$sha_from/$sha_to" ]; then
cp -avf "$LOCAL_CACHE_DIR/$sha_from/$sha_to" "$destination_file"
echo "Successful retrieved ${destination_file} from local cache."
return 0
fi
fi
# If not in the local cache, we might find it remotely.
if [ -n "${AWS_BUCKET_NAME}" ]; then
BUCKET_PATH="s3://${AWS_BUCKET_NAME}${sha_from}/${sha_to}/${s3_filename}"
if aws s3 ls "${BUCKET_PATH}"; then
((S3_CACHE_HITS++))
echo "s3 cache hit for ${s3_filename} (${S3_CACHE_HITS} total hits)"
if aws s3 cp "${BUCKET_PATH}" "${destination_file}"; then
echo "Successful retrieved ${destination_file} from s3://${AWS_BUCKET_NAME}"
return 0
else
echo "Failed to retrieve ${destination_file} from s3://${AWS_BUCKET_NAME}"
return 1
fi
# Not found, fall through to default error
else
((S3_CACHE_MISSES++))
echo "s3 cache miss for ${s3_filename} (${S3_CACHE_MISSES} total misses)"
fi
fi
return 1
}
OPTIND=1
while getopts ":S:c:gu" option; do
case $option in
S)
# This will probably be bucketname/path/prefix but we can use it either way
AWS_BUCKET_NAME="$OPTARG"
# Ensure trailing slash is there.
if [[ ! $AWS_BUCKET_NAME =~ .*/$ ]]; then
AWS_BUCKET_NAME="${AWS_BUCKET_NAME}/"
fi
;;
c)
LOCAL_CACHE_DIR="$OPTARG"
;;
g)
HOOK="PRE"
;;
u)
HOOK="POST"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
print_usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
print_usage
exit 1
;;
*)
echo "Unimplemented option: -$OPTARG" >&2
print_usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ "$HOOK" == "PRE" ]; then
get_patch "$1" "$2" "$3"
elif [ "$HOOK" == "POST" ]; then
upload_patch "$1" "$2" "$3"
fi
|