-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupk
executable file
·221 lines (197 loc) · 4.1 KB
/
upk
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
#!/bin/sh
# Copyright 2015-2021 Yury Gribov
#
# Use of this source code is governed by MIT license that can be
# found in the LICENSE.txt file.
# Unpacks files from archives of various types.
# For details run without arguments.
set -eu
error() {
echo >&2 "$(basename $0): error: $@"
exit 1
}
warn() {
echo >&2 "$(basename $0): warning: $@"
}
check_prog_silent() {
which "$1" > /dev/null 2>&1
}
check_prog() {
for p; do
if ! check_prog_silent "$p"; then
details=
# TODO: rpm
if which apt-file 2>/dev/null; then
details=":
$(apt-file $p)"
elif which yum 2>/dev/null; then
details=":
$(yum whatprovides $p)"
fi
error "your system is missing program '$p'" \
"which is required to unpack this type of file$details"
fi
done
}
error_missing() {
error "your system is missing one of the programs ($(echo "$@" | tr ' ' ', ')) "
"needed to unpack this type of file"
}
isemptydir() {
test -d "$1" && test "$(find "$1" -maxdepth 0 -empty)"
}
strip_ext() {
# Handle things like glibc-2.20.tar.bz2
noext="${1%.*}"
noext="${noext%.tar}"
echo "$noext"
}
# Detect situations where it makes sense to unnest subfolder
# e.g. glibc-2.20/glibc-2.20
maybe_unnest() {
d=$(ls -1 "$1")
if [ $(echo "$d" | wc -l) -eq 1 -a "$d" = "$(basename "$1")" ]; then
d=$(ls -1 "$1")
mv "$d" "$d.$$" # Workaround if some $f matches $d
d="$d.$$"
OIFS="$IFS"
IFS='
'
for f in $(ls -A "$1/$d"); do # Respect hidden files
mv "$1/$d/$f" "$1/$(basename $f)"
done
OIFS="$IFS"
rmdir "$1/$d"
fi
}
print_help_and_exit() {
cat <<EOF
Syntax: $me FILE...
Unpacks archives of various types (based on extension of FILE).
Supported archives:
.a
.apk
.cab
.cpio
.deb
.jar
.rar
.rpm
.tar*
winmail.dat
.zip
.7z
Example:
$ pk my.tar.gz 1.txt 2.txt
$ upk my.tar.gz
$ ls my/
1.txt 2.txt
EOF
exit 1
}
me=$(basename $0)
curdir="$PWD"
case "${1:-h}" in
-h | --help)
print_help_and_exit
;;
esac
for src; do
src=$(readlink -f "$src")
src_no_ext=$(strip_ext "$(basename "$src")")
src_basename=$(basename "$src")
test -f $src || error "file $src does not exist"
# Nest
mkdir -p "$src_no_ext"
isemptydir "$src_no_ext" || error "cowardly refusing to clear original contents of $src_no_ext"
trap "cd '$curdir' && rm -rf '$src_no_ext'" EXIT INT TERM
cd "$src_no_ext"
# Unpack
case "$src_basename" in
*.a | *.ar)
check_prog ar
ar x "$src"
;;
*.cab)
check_prog cabextract
cabextract "$src"
;;
*.cpio)
check_prog cpio
cat "$src" | cpio -id
;;
*.deb | *.ddeb)
if check_prog_silent dpkg; then
dpkg -x "$src" .
elif check_prog_silent ar; then
ar x "$src"
else
error_missing dpkg ar
fi
;;
# *.gz)
# gunzip "$src"
# ;;
*.rar)
if check_prog_silent unrar; then
unrar e "$src"
elif check_prog_silent 7z; then
7z x "$src"
else
error_missing unrar 7z
fi
;;
*.rpm)
check_prog rpm2cpio cpio
rpm2cpio "$src" | cpio -id
;;
*.tar)
check_prog tar
tar xf "$src"
;;
*.tar.gz | *.tgz)
check_prog tar gunzip
tar xzf "$src"
;;
*.tar.bz2 | *.tbz2)
check_prog tar bunzip2
tar xjf "$src"
;;
*.tar.lz | *.tar.lzip | *.tlz)
check_prog tar lzip
tar --lzip xf "$src"
;;
*.tar.xz | *.txz)
check_prog tar unxz
tar xJf "$src"
;;
winmail*.dat*)
check_prog tnef
tnef --file=$src
;;
*.lzma | *.zip | *.jar | *.apk)
if check_prog_silent 7z; then
# 7z supports newer ZIP formats
7z x "$src"
elif check_prog_silent unzip; then
unzip "$src"
else
error_missing 7z unzip
fi
;;
*.lz | *.lzip | *.lzma)
check_prog lzip
lzip -d "$src"
;;
*.7z)
check_prog 7zr
7zr x "$src"
;;
*)
error "unsupported archive: $src"
;;
esac
maybe_unnest "$PWD"
cd "$curdir"
trap '' EXIT INT TERM # Unset
done