blob: 0730203d087f035a7c7f30936558832d9a905ad4 (
plain) (
blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#! /bin/bash
# Recognizes tar archive extraction using the options listed below. Any other
# operations or options will cause the tar command to be executed directly,
# with no special processing.
declare -r short_opts=ajJvxzZ
declare -r short_opts_arg=CfI
declare -r -A long_opts=(
[--auto-compress]=1 [--bzip2]=1 [--compress]=1 [--extract]=1 [--file]=1
[--get]=1 [--gunzip]=1 [--gzip]=1 [--lzip]=1 [--lzma]=1 [--lzop]=1
[--no-auto-compress]=1 [--uncompress]=1 [--ungzip]=1 [--verbose]=1
[--xz]=1 [--zstd]=1
)
declare -r -A long_opts_arg=(
[--use-compress-program]=1
)
command=${0##*/}
original_args=("$@")
args=()
opt_extract=
opt_directory=
opt_file=
PATH=${PATH#*:}
traditional_to_unix() {
local opts=$1 args=()
shift
[[ "$opts" =~ ^[Acdrtux] ]] || return
while [ -n "$opts" ]; do
args+=("-${opts:0:1}")
if [[ "$opts" =~ ^[$short_opts_arg] ]]; then
[ $# -ge 1 ] || return 1
args+=("$1")
shift
else
[[ "$opts" =~ ^[$short_opts] ]] || return
fi
opts="${opts:1}"
done
printf "%q " "${args[@]}" "$@"
}
debug() {
echo "$@">&2
}
fallback() {
debug "falling back to $command"
exec "$command" "${original_args[@]}"
}
[ $# -gt 0 ] || fallback
if ! [[ "$1" =~ ^- ]]; then
args=$(traditional_to_unix "$@") || fallback
set -- $args
fi
set_opt_file() {
[ -n "$1" ] || fallback
opt_file=$1
}
set_opt_directory() {
[ -n "$1" ] || fallback
opt_directory=$1
}
short_opt() {
debug option "$1"
case "$1" in
-x)
opt_extract=1
;;
esac
args+=("$1")
}
short_opt_arg() {
debug option "$1" "$2"
case "$1" in
-C)
set_opt_directory "$2"
;;
-f)
set_opt_file "$2"
;;
*)
args+=("$1$2")
;;
esac
}
long_opt() {
debug option "$1" "$2"
case "$1" in
--extract|--get)
opt_extract=1
;;
esac
args+=("$1")
}
long_opt_arg() {
debug option "$1" "$2"
case "$1" in
--directory)
set_opt_directory "$2"
;;
--file)
set_opt_file "$2"
;;
*)
args+=("$1=$2")
;;
esac
}
while [ $# -gt 0 ]; do
if [[ "$1" =~ ^-- ]]; then
[ "$1" != -- ] || break
if [[ "$1" =~ ^(--.*)=(.*) ]]; then
[ -n "${long_opts_arg[${BASH_REMATCH[1]}]}" ] || fallback
long_opt_arg "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
shift
elif [ -n "${long_opts_arg[$1]}" ]; then
[ $# -ge 2 ] || fallback
long_opt_arg "$1" "$2"
shift 2
elif [ -n "${long_opts[$1]}" ]; then
long_opt "$1"
shift
else
fallback
fi
elif [[ "$1" =~ ^- ]]; then
[ "$1" != - ] || break
if [[ "$1" =~ ^(-[$short_opts])(.*) ]]; then
short_opt "${BASH_REMATCH[1]}"
shift
if [ -n "${BASH_REMATCH[2]}" ]; then
set -- "-${BASH_REMATCH[2]}" "$@"
fi
elif [[ "$1" =~ ^(-[$short_opts_arg])(.*) ]]; then
if [ -z "${BASH_REMATCH[2]}" ]; then
[ $# -ge 2 ] || fallback
short_opt_arg "${BASH_REMATCH[1]}" "$2"
shift 2
else
short_opt_arg "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
shift
fi
else
fallback
fi
else
break
fi
done
[ -n "$opt_extract" ] || fallback
! [[ "/${opt_directory:-.}/" =~ /\.\./|^// ]] || fallback
mkpath() {
if [ "$1" == . ]; then
echo "$2"
elif [ "$2" == . ]; then
echo "$1"
else
echo "$1/$2"
fi
}
if [ -n "$QUILT_WORKDIR" ]; then
cleanup() {
rm -rf "$tmpdir"
}
dir=${opt_directory:-.}
pattern=$(mkpath "$dir" ".$command.XXXXXX")
tmpdir=$(mktemp -d "$pattern" 2> /dev/null) || fallback
trap cleanup EXIT
opt_directory=$tmpdir
fi
[ -z "$opt_directory" ] || args+=("-C$opt_directory")
[ -z "$opt_file" ] || args+=("-f$opt_file")
args+=("$@")
"$command" "${args[@]}" || exit
if [ -n "$QUILT_WORKDIR" ]; then
workdir=$(mkpath "$QUILT_WORKDIR" "$dir")
shopt -s dotglob
files=( "$opt_directory"/* )
for file in "${files[@]##*/}"; do
if [ -e "$(mkpath "$dir" "$file")" -o \
-e "$(mkpath "$workdir" "$file")" ]; then
printf $"Refusing to overwrite %s\n" \
"$(mkpath "$dir" "$file")"
exit 1
fi
done
mkdir -p "$workdir" || exit
cp -dlR "${files[@]}" "$workdir/" || exit
mv "${files[@]}" "$dir/" || exit
fi
|