-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext.zsh
More file actions
234 lines (214 loc) · 7.1 KB
/
Copy pathext.zsh
File metadata and controls
234 lines (214 loc) · 7.1 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Universal extractor: ext <file> [destdir|.] [--flat] [--no-dir] [--force] [--list]
ext() {
emulate -L zsh
setopt pipefail no_unset
setopt no_nomatch
local archive="${1:-}"
[[ -z "$archive" ]] && { print -u2 "Usage: ext <archive> [destdir|.] [--flat] [--no-dir] [--force] [--list]"; return 2; }
[[ ! -f "$archive" ]] && { print -u2 "ext: file not found: $archive"; return 1; }
shift
local dest="" flat=0 nodir=0 force=0 list=0
if [[ "${1:-}" != --* && -n "${1:-}" ]]; then
dest="$1"
shift
fi
while (( $# )); do
case "$1" in
--flat) flat=1 ;;
--no-dir) nodir=1 ;;
--force) force=1 ;;
--list) list=1 ;;
--help|-h)
print "Usage: ext <archive> [destdir|.] [--flat] [--no-dir] [--force] [--list]"
return 0
;;
*) print -u2 "ext: unknown option: $1"; return 2 ;;
esac
shift
done
_need() { command -v "$1" >/dev/null 2>&1 || { print -u2 "ext: missing dependency: $1"; return 127; } }
_have() { command -v "$1" >/dev/null 2>&1 }
local base="${archive:t}"
local root="$base"
case "$base" in
(*.tar.gz|*.tgz) root="${base%.tar.gz}"; root="${root%.tgz}" ;;
(*.tar.bz2|*.tbz2) root="${base%.tar.bz2}"; root="${root%.tbz2}" ;;
(*.tar.xz|*.txz) root="${base%.tar.xz}"; root="${root%.txz}" ;;
(*.tar.zst|*.tzst) root="${base%.tar.zst}"; root="${root%.tzst}" ;;
(*.tar) root="${base%.tar}" ;;
(*.zip) root="${base%.zip}" ;;
(*.7z) root="${base%.7z}" ;;
(*.rar) root="${base%.rar}" ;;
(*.gz) root="${base%.gz}" ;;
(*.bz2) root="${base%.bz2}" ;;
(*.xz) root="${base%.xz}" ;;
(*.zst) root="${base%.zst}" ;;
(*) root="${base%.*}" ;;
esac
if (( nodir )); then
dest="."
elif [[ -z "$dest" ]]; then
case "$base" in
(*.gz|*.bz2|*.xz|*.zst) dest="." ;;
(*) dest="$root" ;;
esac
fi
local outdir="$dest"
if [[ "$outdir" != "." ]]; then
if [[ -e "$outdir" && $force -eq 0 ]]; then
local i=2
while [[ -e "${outdir}_${i}" ]]; do ((i++)); done
outdir="${outdir}_${i}"
fi
mkdir -p -- "$outdir" || { print -u2 "ext: cannot create dest: $outdir"; return 1; }
fi
_list_entries() {
local a="$1"
case "$a" in
(*.tar|*.tar.gz|*.tgz|*.tar.bz2|*.tbz2|*.tar.xz|*.txz|*.tar.zst|*.tzst)
_need tar || return 127
tar -tf -- "$a" 2>/dev/null || true
;;
(*.zip)
_need unzip || return 127
unzip -Z1 -- "$a" 2>/dev/null || true
;;
(*.7z|*.rar)
_need 7z || return 127
7z l -ba -- "$a" 2>/dev/null | awk 'NR>0{print $0}' \
| sed -n 's/^Path = //p' || true
;;
(*)
if _have file; then
local kind; kind="$(file -b -- "$a" 2>/dev/null || true)"
case "$kind" in
(*tar\ archive*) _need tar; tar -tf -- "$a" 2>/dev/null || true ;;
(*Zip\ archive*) _need unzip; unzip -Z1 -- "$a" 2>/dev/null || true ;;
(*7-zip\ archive*|*RAR\ archive*) _need 7z; 7z l -ba -- "$a" 2>/dev/null | sed -n 's/^Path = //p' || true ;;
esac
fi
;;
esac
}
# traversal check
_is_bad_path() {
local p="$1"
[[ "$p" == /* ]] && return 0
[[ "$p" == *"/../"* || "$p" == "../"* || "$p" == *"/.." ]] && return 0
return 1
}
local entries
entries="$(_list_entries "$archive")"
if [[ -n "$entries" ]]; then
local bad=0
local line
while IFS=$'\n' read -r line; do
[[ -z "$line" ]] && continue
if _is_bad_path "$line"; then
bad=1
break
fi
done <<< "$entries"
if (( list )); then
print -- "$entries"
(( bad )) && print -u2 "ext: WARNING: archive contains suspicious paths (absolute or .. traversal)."
return 0
fi
if (( bad )); then
print -u2 "ext: REFUSING: archive contains absolute/.. paths (zip-slip / path traversal)."
print -u2 "ext: run: ext \"$archive\" --list to inspect."
return 4
fi
else
(( list )) && { print -u2 "ext: cannot list entries for: $archive"; return 5; }
fi
local ok=1
case "$base" in
(*.tar.bz2|*.tbz2) _need tar; tar -xjf -- "$archive" -C "$outdir"; ok=0 ;;
(*.tar.gz|*.tgz) _need tar; tar -xzf -- "$archive" -C "$outdir"; ok=0 ;;
(*.tar.xz|*.txz) _need tar; tar -xJf -- "$archive" -C "$outdir"; ok=0 ;;
(*.tar.zst|*.tzst) _need tar; tar --zstd -xf -- "$archive" -C "$outdir"; ok=0 ;;
(*.tar) _need tar; tar -xf -- "$archive" -C "$outdir"; ok=0 ;;
(*.zip)
_need unzip
if (( flat )); then
unzip -q -j -- "$archive" -d "$outdir"; ok=0
else
unzip -q -- "$archive" -d "$outdir"; ok=0
fi
;;
(*.7z|*.rar)
_need 7z
if (( flat )); then
7z x -y -- "$archive" -o"$outdir"; ok=0
local f
for f in "$outdir"/**/*(.N); do
local tgt="$outdir/${f:t}"
if [[ "$f" != "$tgt" ]]; then
if [[ -e "$tgt" ]]; then
local j=2
while [[ -e "${tgt:r}_${j}${tgt:e:+.${tgt:e}}" ]]; do ((j++)); done
tgt="${tgt:r}_${j}${tgt:e:+.${tgt:e}}"
fi
mv -- "$f" "$tgt" 2>/dev/null || true
fi
done
rmdir -p -- "$outdir"/**/*(/N) 2>/dev/null || true
else
7z x -y -- "$archive" -o"$outdir"; ok=0
fi
;;
(*.gz)
_need gzip
if [[ "$outdir" == "." ]]; then
gzip -d -- "$archive"; ok=0
else
gzip -dc -- "$archive" > "$outdir/${root}" ; ok=0
fi
;;
(*.bz2)
_need bzip2
if [[ "$outdir" == "." ]]; then
bzip2 -d -- "$archive"; ok=0
else
bzip2 -dc -- "$archive" > "$outdir/${root}" ; ok=0
fi
;;
(*.xz)
_need xz
if [[ "$outdir" == "." ]]; then
xz -d -- "$archive"; ok=0
else
xz -dc -- "$archive" > "$outdir/${root}" ; ok=0
fi
;;
(*.zst)
_need zstd
if [[ "$outdir" == "." ]]; then
zstd -d -- "$archive"; ok=0
else
zstd -dc -- "$archive" > "$outdir/${root}" ; ok=0
fi
;;
esac
if (( ok )); then
if _have file; then
local kind; kind="$(file -b -- "$archive" 2>/dev/null || true)"
case "$kind" in
(*tar\ archive*) _need tar; tar -xf -- "$archive" -C "$outdir"; ok=0 ;;
(*Zip\ archive*) _need unzip; unzip -q -- "$archive" -d "$outdir"; ok=0 ;;
(*7-zip\ archive*) _need 7z; 7z x -y -- "$archive" -o"$outdir"; ok=0 ;;
(*RAR\ archive*) _need 7z; 7z x -y -- "$archive" -o"$outdir"; ok=0 ;;
(*gzip\ compressed*) _need gzip; gzip -dc -- "$archive" > "$outdir/${root}"; ok=0 ;;
(*bzip2\ compressed*) _need bzip2; bzip2 -dc -- "$archive" > "$outdir/${root}"; ok=0 ;;
(*XZ\ compressed*) _need xz; xz -dc -- "$archive" > "$outdir/${root}"; ok=0 ;;
(*Zstandard\ compressed*) _need zstd; zstd -dc -- "$archive" > "$outdir/${root}"; ok=0 ;;
esac
fi
fi
if (( ok )); then
print -u2 "ext: don't know how to extract: $archive"
return 3
fi
[[ "$outdir" == "." ]] || print "ext: extracted to: ${(q)outdir}"
}