summaryrefslogtreecommitdiffstats
path: root/testing/docker/funsize-update-generator/scripts/mbsdiff_hook.sh
blob: 0b677a5e9cd245bcd554771e7ee8785313b2b0c0 (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
#!/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=
SERVER_URL=
LOCAL_CACHE_DIR=

getsha512(){
    echo "$(openssl sha512 "${1}" | awk '{print $2}')"
}

print_usage(){
    echo "$(basename $0) -A SERVER-URL [-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(){
    sha_from=`getsha512 "$1"`
    sha_to=`getsha512 "$2"`
    patch_path="$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
    # The remote cache implementation is not used. The code is for usage
    # reference only.
     return 0

    # send it over to funsize
    cmd="curl -sSw %{http_code} -o /dev/null -X POST $SERVER_URL -F sha_from="$sha_from" -F sha_to="$sha_to" -F patch_file="@$patch_path""
    ret_code=`$cmd`

    if [ $ret_code -eq 200 ]; then
        echo "$patch_path Successful uploaded to funsize!"
        return 0
    fi

    echo "$patch_path Failed to be uploaded to funsize!"
    return 1
}

get_patch(){
    sha_from=`getsha512 "$1"`
    sha_to=`getsha512 "$2"`
    destination_file="$3"
    tmp_file="$destination_file.tmp"

    # try to retrieve from local cache first
    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
    else
        echo "File is not in the locale cache"
        return 1
    fi
    # The remote cache implementation is not used. The code is for usage
    # reference only.

    # if unsuccessful, try to retrieve from funsize
    cmd="curl -LsSGw %{http_code} $SERVER_URL/$sha_from/$sha_to -o $tmp_file"
    ret_code=`$cmd`

    if [ $ret_code -eq 200 ]; then
        mv "$tmp_file" "$destination_file"
        echo "Successful retrieved $destination_file from funsize!"
        return 0
    fi

    rm  -f "$tmp_file"
    echo "Failed to retrieve $destination_file from funsize!"
    return 1
}

OPTIND=1

while getopts ":A:c:gu" option; do
    case $option in
        A)
            SERVER_URL="$OPTARG"
            ;;
        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