aboutsummaryrefslogtreecommitdiff
path: root/paludis/syncers/dotar.in
blob: 9c62c336ed0cbadd3c924b1dffbd7fd87ead6223 (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
#!/bin/bash

source "${PALUDIS_EBUILD_DIR}/echo_functions.bash"

for f in ${PALUDIS_BASHRC_FILES}; do
    [[ -f "${f}" ]] && source "${f}"
done

LOCAL=
REMOTE=

FETCHER_OPTIONS=( )
DOUNPACK_OPTIONS=( )
RSYNC_OPTIONS=( )

while [[ $# -gt 0 ]]; do
    case "${1}" in

        --strip-components=*)
            DOUNPACK_OPTIONS[${#DOUNPACK_OPTIONS[@]}]=--tar-option="${1}"
            ;;

        --exclude-from=*)
            RSYNC_OPTIONS[${#RSYNC_OPTIONS[@]}]=--exclude-from
            RSYNC_OPTIONS[${#RSYNC_OPTIONS[@]}]="${1#*=}"
            ;;

        --fetcher-option=*)
            FETCHER_OPTIONS[${#RSYNC_OPTIONS[@]}]="${1#*=}"
            ;;

        --tar-option=*)
            DOUNPACK_OPTIONS[${#DOUNPACK_OPTIONS[@]}]="${1}"
            ;;

        --rsync-option=*)
            RSYNC_OPTIONS[${#RSYNC_OPTIONS[@]}]="${1#*=}"
            ;;

        --help)
            PROTO="${0##*/do}"
            if [[ "${PROTO}" == tar+file ]]; then
                echo "    URL syntax: tar+file:///PATH"
            elif [[ "${PROTO}" == tar+http || "${PROTO}" == tar+https || "${PROTO}" == tar+ftp ]]; then
                echo "    URL syntax: ${PROTO}://[USERNAME[:PASSWORD]@]SERVER[:PORT]/PATH"
            else
                # This syncer can handle any protocol for which there
                # is a fetcher.  The user can symlink/copy this syncer
                # to enable support for other protocols, but we can't
                # give them much help with the syntax....
                PROTO="${PROTO#tar+}"
                echo "    URL syntax: tar+${PROTO}://[$( tr [:lower:] [:upper:] <<<"${PROTO}" )-URL]"
            fi

            echo "    Options:"
            echo "        --exclude-from=FILE             Use FILE as a list of exclude patterns"
            echo "        --strip-components=N            Ignore the first N components of the"
            echo "                                          paths from the tarball"
            echo "        --fetcher-option=OPTION         Pass OPTION to the fetcher"
            echo "                                          (The standard fetchers do not"
            echo "                                           accept any options - this is only"
            echo "                                           useful with custom fetchers)"
            echo "        --rsync-option=OPTION           Pass OPTION to the local rsync"
            echo "        --tar-option=OPTION             Pass OPTION to tar"
            exit 0
            ;;

        --*)
            ewarn "${0}: unknown option '${1%%=*}'"
            ;;

        *)
            if [[ -z "${LOCAL}" ]]; then
                LOCAL="${1}"
            elif [[ -z "${REMOTE}" ]]; then
                REMOTE="${1}"
            else
                eerror "${0}: extra argument '${1}'"
                exit 1
            fi
            ;;

    esac
    shift
done

if [[ -z "${LOCAL}" ]]; then
    eerror "${0}: unspecified local repository directory"
    exit 1
elif [[ -z "${REMOTE}" ]]; then
    eerror "${0}: unspecified remote repository URL"
    exit 1
fi

URL="${REMOTE#tar+}"

PROTOCOL="${URL%%://*}"
if [[ -z "${PROTOCOL}" ]]; then
    eerror "'${REMOTE}' is not a valid URL"
    exit 1
fi

if [[ -e "${LOCAL}" && ! -d "${LOCAL}" ]]; then
    eerror "'${LOCAL}' exists but is not a directory"
    exit 1
fi

FETCHER=
FETCHER_NAME="do$( tr '[:upper:]' '[:lower:]' <<<"${PROTOCOL}" )"
for d in ${PALUDIS_FETCHER_DIRS:-${PALUDIS_EBUILD_DIR}/fetchers/}; do
    if [[ -x "${d}/${FETCHER_NAME}" ]]; then
        FETCHER="${d}/${FETCHER_NAME}"
        break
    fi
done

if [[ -z "${FETCHER}" ]]; then
    eerror "Could not find a fetcher for '${PROTOCOL}'"
    exit 1
fi

TEMP="$( mktemp -d -p /var/tmp paludis-tarsync-XXXXXX )" || exit $?
TARFILE="${URL##*/}"

"${FETCHER}" "${FETCHER_OPTIONS[@]}" "${URL}" "${TEMP}/${TARFILE}" || exit $?

UNPACKDIR="${TEMP}/repository"
mkdir "${UNPACKDIR}"
( cd "${UNPACKDIR}" && "${PALUDIS_EBUILD_DIR}/utils/dounpack" \
    "${DOUNPACK_OPTIONS[@]}" "${TEMP}/${TARFILE}" ) || exit $?

rm "${TEMP}/${TARFILE}"

if [[ -z "$( ls -A "${UNPACKDIR}" )" ]]; then
    eerror "No files were extracted (not an archive?)"
    rm -rf "${TEMP}"
    exit 1
fi

[[ -d "${LOCAL}" ]] || mkdir -p "${LOCAL}"
${RSYNC_WRAPPER} rsync --recursive --links --safe-links --perms --times \
    --compress --force --whole-file --delete --delete-after --stats \
    --timeout=180 --exclude=/distfiles --exclude=/packages --exclude=/local \
    --exclude=/.cache --progress "${RSYNC_OPTIONS[@]}" "${UNPACKDIR}/" "${LOCAL}/" || exit $?

rm -rf "${TEMP}"