This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
/
tar.sh
287 lines (224 loc) · 5.93 KB
/
tar.sh
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
bashmenot_internal_tar_create () {
local src_dir dst_file
expect_args src_dir dst_file -- "$@"
shift 2
expect_existing "${src_dir}" || return 1
local name format dst_dir
name=$( basename "${dst_file}" ) || return 1
format="${name##*.}"
dst_dir=$( dirname "${dst_file}" ) || return 1
mkdir -p "${dst_dir}" || return 1
case "${format}" in
'tar')
COPYFILE_DISABLE=1 \
tar -c -f "${dst_file}" -C "${src_dir}" "$@" '.' || return 1
;;
'gz')
if which 'pigz' >'/dev/null' 2>&1; then
COPYFILE_DISABLE=1 \
tar -c -C "${src_dir}" "$@" '.' |
pigz -7 >"${dst_file}" || return 1
else
COPYFILE_DISABLE=1 \
tar -c -z -f "${dst_file}" -C "${src_dir}" "$@" '.' || return 1
fi
;;
'bz2')
if which 'pbzip2' >'/dev/null' 2>&1; then
COPYFILE_DISABLE=1 \
tar -c -C "${src_dir}" "$@" '.' |
pbzip2 -7 >"${dst_file}" || return 1
else
COPYFILE_DISABLE=1 \
tar -c -j -f "${dst_file}" -C "${src_dir}" "$@" '.' || return 1
fi
;;
'xz')
if which 'pxz' >'/dev/null' 2>&1; then
COPYFILE_DISABLE=1 \
tar -c -C "${src_dir}" "$@" '.' |
pxz -7 >"${dst_file}" || return 1
else
COPYFILE_DISABLE=1 \
tar -c -J -f "${dst_file}" -C "${src_dir}" "$@" '.' || return 1
fi
;;
*)
log_error "Unexpected archive format: ${name}"
return 1
esac
}
bashmenot_internal_tar_extract () {
local src_file dst_dir
expect_args src_file dst_dir -- "$@"
shift 2
expect_existing "${src_file}" || return 1
local name format
name=$( basename "${src_file}" ) || return 1
format="${name##*.}"
mkdir -p "${dst_dir}" || return 1
case "${format}" in
'tar')
COPYFILE_DISABLE=1 \
tar -xp -f "${src_file}" -C "${dst_dir}" "$@" || return 1
;;
'gz')
if which 'pigz' >'/dev/null' 2>&1; then
COPYFILE_DISABLE=1 \
pigz -d <"${src_file}" |
tar -xp -C "${dst_dir}" "$@" || return 1
else
COPYFILE_DISABLE=1 \
tar -xp -z -f "${src_file}" -C "${dst_dir}" "$@" || return 1
fi
;;
'bz2')
if which 'pbzip2' >'/dev/null' 2>&1; then
COPYFILE_DISABLE=1 \
pbzip2 -d <"${src_file}" |
tar -xp -C "${dst_dir}" "$@" || return 1
else
COPYFILE_DISABLE=1 \
tar -xp -j -f "${src_file}" -C "${dst_dir}" "$@" || return 1
fi
;;
'xz')
if which 'pxz' >'/dev/null' 2>&1; then
COPYFILE_DISABLE=1 \
pxz -d <"${src_file}" |
tar -xp -C "${dst_dir}" "$@" || return 1
else
COPYFILE_DISABLE=1 \
tar -xp -J -f "${src_file}" -C "${dst_dir}" "$@" || return 1
fi
;;
*)
log_error "Unexpected archive format: ${name}"
return 1
esac
}
copy_file () {
local src_file dst_file
expect_args src_file dst_file -- "$@"
expect_existing "${src_file}" || return 1
local dst_dir
dst_dir=$( dirname "${dst_file}" ) || return 1
mkdir -p "${dst_dir}" || return 1
cp "${src_file}" "${dst_file}" 2>&1 | quote || return 1
}
copy_dir_entry_into () {
local src_dir src_file dst_dir
expect_args src_dir src_file dst_dir -- "$@"
shift 3
expect_existing "${src_dir}/${src_file}" || return 1
mkdir -p "${dst_dir}" || return 1
COPYFILE_DISABLE=1 \
tar -c -f - -C "${src_dir}" "$@" "${src_file}" |
tar -xp -f - -C "${dst_dir}" 2>&1 | quote || return 1
}
copy_dir_glob_into () {
local src_dir src_glob dst_dir
expect_args src_dir src_glob dst_dir -- "$@"
shift 3
expect_existing "${src_dir}" || return 1
# TODO: Use read -rd $'\0'.
local glob_file
expand_glob "${src_dir}" "${src_glob}" |
while read -r glob_file; do
copy_dir_entry_into "${src_dir}" "${glob_file}" "${dst_dir}" "$@" || return 1
done || return 1
}
copy_dir_into () {
local src_dir dst_dir
expect_args src_dir dst_dir -- "$@"
shift 2
expect_existing "${src_dir}" || return 1
mkdir -p "${dst_dir}" || return 1
COPYFILE_DISABLE=1 \
tar -c -f - -C "${src_dir}" "$@" '.' |
tar -xp -f - -C "${dst_dir}" 2>&1 | quote || return 1
}
copy_dir_over () {
local src_dir dst_dir
expect_args src_dir dst_dir -- "$@"
shift 2
expect_existing "${src_dir}" || return 1
rm -rf "${dst_dir}" || return 1
copy_dir_into "${src_dir}" "${dst_dir}" "$@" || return 1
}
create_archive () {
local src_dir dst_file
expect_args src_dir dst_file -- "$@"
shift 2
expect_existing "${src_dir}" || return 1
local name stderr
name=$( basename "${dst_file}" ) || return 1
stderr=$( get_tmp_file 'tar.stderr' ) || return 1
log_indent_begin "Creating ${name}..."
if ! bashmenot_internal_tar_create "${src_dir}" "${dst_file}" "$@" 2>"${stderr}"; then
log_indent_end 'error'
quote <"${stderr}"
return 1
fi
local size
size=$( get_size "${dst_file}" ) || return 1
log_indent_end "done, ${size}"
rm -f "${stderr}" || true
}
extract_archive_into () {
local src_file dst_dir
expect_args src_file dst_dir -- "$@"
shift 2
expect_existing "${src_file}" || return 1
local name stderr
name=$( basename "${src_file}" ) || return 1
stderr=$( get_tmp_file 'tar.stderr' ) || return 1
log_indent_begin "Extracting ${name}..."
if ! bashmenot_internal_tar_extract "${src_file}" "${dst_dir}" "$@" 2>"${stderr}"; then
log_indent_end 'error'
quote <"${stderr}"
return 1
fi
local size
size=$( get_size "${dst_dir}" ) || return 1
log_indent_end "done, ${size}"
rm -f "${stderr}" || true
}
extract_archive_over () {
local src_file dst_dir
expect_args src_file dst_dir -- "$@"
shift 2
expect_existing "${src_file}" || return 1
rm -rf "${dst_dir}" || return 1
extract_archive_into "${src_file}" "${dst_dir}" || return 1
}
case $( uname -s ) in
'Linux'|'FreeBSD')
strip_tree () {
local dir
expect_args dir -- "$@"
local file
find "${dir}" "$@" -type f -print0 2>'/dev/null' |
sort0_natural |
while read -rd $'\0' file; do
strip --strip-unneeded "${file}" 2>'/dev/null' | quote || true
done || return 0
}
;;
'Darwin')
strip_tree () {
local dir
expect_args dir -- "$@"
local file
find "${dir}" "$@" -type f -print0 2>'/dev/null' |
sort0_natural |
while read -rd $'\0' file; do
strip -u -r "${file}" 2>'/dev/null' | quote || true
done || return 0
}
;;
*)
strip_tree () {
log_warning 'Cannot strip'
}
esac