-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb-pull
executable file
·384 lines (310 loc) · 8.42 KB
/
adb-pull
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
shopt -s extglob
declare -r SCRIPT_PATH="$0";
declare -r SCRIPT="${SCRIPT_PATH##*/}";
declare -r TAR_EXT="-${SCRIPT}-tmp.tar";
declare SRC;
declare BASE;
declare DST;
declare SRC_ORIG;
# declare BASE_ORIG;
declare DST_ORIG;
declare BASE_TAR;
declare SRC_TAR;
declare DST_TAR;
declare DO_TAR;
declare PARENT_DIR;
# Print help and exit.
_help() {
local path="$0";
local script="${path##*/}";
cat << EOH >&2
${script}
DESCRIPTION:
Run adb pull and verify the integrity of the pulled files.
USAGE:
${script} "<files/directories>..."
EOH
local statusCode="${1}";
if ! [ "${statusCode}" ]; then
statusCode=0;
fi;
exit "${statusCode}";
};
_init() {
# Check if user requested help.
_getopts "$@";
# Declare the name of the target file to be pulled.
SRC="${1}";
[ -z "${SRC}" ] && SRC="/sdcard/";
# Give the pulled/local file the basename of the file on the Android device.
BASE="$(basename "${SRC}")";
_setSrc;
DST="${BASE}";
SRC_ORIG="${SRC}";
# BASE_ORIG="${BASE}";
DST_ORIG="${DST}";
BASE_TAR="${BASE}${TAR_EXT}";
SRC_TAR="${SRC}${TAR_EXT}";
DST_TAR="${DST}${TAR_EXT}";
# If the target file is a directory,
# prepare to tar it in order to read an md5sum later.
local isDir;
isDir="$(adb shell "if [ -d \"${SRC}\" ]; then printf 1; fi")";
if [ "${isDir}" = 1 ] && [ "${BASE}" != "sdcard" ] ; then
DO_TAR=1;
PARENT_DIR="$(dirname "${SRC}")";
fi;
};
# Analyze user arguments.
_getopts() {
# Declare bool options.
#local -a Bools=(
# ""
#);
# Iterate over user arguments.
local -i i;
local flag;
for ((i=1; i<=$#; i++)); do
eval flag="\$$i";
case "${flag}" in
*(-)[hH] | *(-)[hH][eE][lL][pP] )
_help 0;
;;
esac;
done;
# Set bool options.
#="${Bools[0]}"
};
# If the name of the requested file does not begin with '/',
# guess the path of the target file.
_setSrc() {
[ "$(printf '%c' "${SRC}")" = "/" ] && return 0;
# `adb-pull sdcard` > `adb-pull /sdcard/`
if [ "${SRC}" = "sdcard" ]; then
SRC="/sdcard/";
# `adb-pull Downloads` > `adb-pull /sdcard/Downloads`
elif [ "${SRC}" = "${BASE}" ]; then
SRC="/sdcard/${SRC}";
# `adb-pull sdcard/Downloads` > `adb-pull /sdcard/Downloads`
else
SRC="/${SRC}";
fi;
};
# Run main function.
_main() {
# Print in green the name of the target file in order to serve as a title.
# (This makes the output easier to read when pulling multiple files.)
_c green echo "${SRC}";
# Wait for Android device to connect.
_connect;
# If the target file does not exist on the Android device, quit.
if ! _srcExists; then
_c red "echo \"${SRC}\" does not exist.";
return 1;
fi;
# Tar the target file if it is a directory
# in order to check its md5sum later.
if [ -n "${DO_TAR}" ]; then
_tarSrc;
if (($? != 0)); then
_retry "Failed to create tar archive of \"${SRC}\". Retrying...";
return $?;
fi;
SRC="${SRC_TAR}";
DST="${DST_TAR}";
fi;
# Run `adb pull`.
_pull;
if (($? != 0)); then
_retry "Failed to pull \"${SRC}\". Retrying...";
return $?;
fi;
# Compare the md5sum of the pulled file with that of the original one.
# If they do not match, remove the pulled file and rerun this script.
_verifyChecksums;
if (($? != 0)); then
_retry "${SRC} transferred incorrectly." " Re-pulling \"${SRC}\"...";
return $?;
fi;
# If the remote file was tarred earlier,
# remove the temporary tar file on the Android device
# and untar the pulled one.
if [ -n "${DO_TAR}" ]; then
_untarSrcDst &&
SRC="${SRC_ORIG}" &&
DST="${DST_ORIG}";
fi;
echo "Successfully pulled \"${SRC}\".";
# Make the final line of this script's output
# the name of the new/pulled file.
echo "${DST}";
};
# Wait for Android device to connect.
_connect() {
local connected;
connected="$(adb devices | head -2 | tail -1)";
[ "${connected}" ] &&
return 0;
echo "Waiting for Android device to connect...";
adb wait-for-device &&
echo " Android device connected.";
};
# Verify whether the remote file exists.
_srcExists() {
local exists;
exists="$(adb shell "if [ -e \"${SRC}\" ]; then printf 0; fi")";
[ "${exists}" = 0 ] &&
return 0;
return 1;
};
# Create a tar archive of the file to pull [if it is a directory].
_tarSrc() {
echo "Creating tar archive of '${BASE}'...";
echo " adb shell 'f=\"${PARENT_DIR}/${BASE_TAR}\"; if [ -e \"\${f}\" ]; then rm \"\${f}\"; else exit 0; fi;'";
adb shell "f=\"${PARENT_DIR}/${BASE_TAR}\"; if [ -e \"\${f}\" ]; then rm \"\${f}\"; else exit 0; fi;" || return $?;
echo " adb shell 'cd \"${PARENT_DIR}\" && tar -cf \"${BASE_TAR}\" \"${BASE}\"'";
adb shell "cd \"${PARENT_DIR}\" && tar -cf \"${BASE_TAR}\" \"${BASE}\"" || return $?;
local valid;
echo "Checking tar integrity of '${BASE_TAR}'...";
valid="$(adb shell "v=\"$(tar -xOf \"${SRC_TAR}\" &>/dev/null)\"; printf \$?;")";
if [ "${valid}" != 0 ]; then
_tarSrc;
fi;
};
# Run `adb pull` to copy the file from the Android device.
_pull() {
echo "Pulling '${SRC}'...";
_bak "${DST}";
echo " adb pull -p -a \"${SRC}\" \"${DST}\"";
adb pull -p -a "${SRC}" "${DST}";
};
# Back up a file if it exists.
_bak() {
local file="${1}";
if [ ! -e "${file}" ] || [ -e "${file}.bak" ]; then
return 0;
fi;
echo " " mv "${file}" "${file}.bak";
mv "${file}" "${file}.bak";
};
# Rerun script.
_retry() {
if (($# == 0)); then
_retry "Failed to pull \"${SRC}\". Retrying...";
return $?;
fi;
_c red;
echo "$@";
_u;
[ -e "${DST}" ] &&
rm -r "${DST}";
"$0" "${SRC}";
};
# Verify whether the md5sum of the pulled file matches that of the original.
_verifyChecksums() {
[ -e "${DST}" ] ||
return 1;
echo "Verifying MD5 checksums...";
local md5Src;
md5Src="$(adb shell "md5sum \"${SRC}\"")";
echo " ${md5Src}";
md5Src="${md5Src% *}";
local md5Dst;
md5Dst="$(md5sum "${DST}")";
echo " ${md5Dst}";
md5Dst="${md5Dst% *}";
[ "${md5Src}" = "${md5Dst}" ] || return 1;
return 0;
};
# Extract the pulled tar archive.
_untarSrcDst() {
_bak "${BASE}";
echo "Removing remote temporary tar archive '${SRC_TAR}...";
echo " adb shell rm \"${SRC_TAR}\"";
adb shell "rm \"${SRC_TAR}\"";
echo "Extracting local temporary tar archive '${DST_TAR}...";
echo " tar -xf \"${DST_TAR}\"";
tar -xf "${DST_TAR}" &&
rm "${DST_TAR}";
};
# Set console color.
# Optionally, run a command afterwards, and once the command returns,
# reset the console color to normal.
_c() {
local -A Codes=(
["black"]="0"
["red"]="1"
["green"]="2"
["yellow"]="3"
["blue"]="4"
["magenta"]="5"
["cyan"]="6"
["white"]="7"
["bk"]="0"
["r"]="1"
["g"]="2"
["y"]="3"
["bu"]="4"
["m"]="5"
["c"]="6"
["w"]="7"
);
local k c;
for k in "${!Codes[@]}"; do
[ "${k}" = "${1}" ] &&
c="${Codes[$k]}";
done;
[ -n "${c}" ] &&
tput setaf "${c}";
[ -z "${*:2}" ] &&
return;
eval "${@:2}";
tput sgr0;
};
# Set the console color to bold.
# Optionally, pass any arguments to function '_c'.
_C() {
tput bold;
[ -n "$*" ] &&
_c "$@";
};
# Reset console color to normal.
_u() {
tput sgr0;
};
_ifErr() {
local msg="${1}";
[ $? -ne 0 ] && _err "${msg}" $?;
};
# Exit with status 1 after echoing an error message.
_err() {
local err="$*";
[ -z "${err}" ] &&
err="An error occurred. Cannot continue.";
_c red;
echo -e "${err}";
_u;
exit 1;
};
# Remove an extension from a filename.
_rmExt() {
local file="${1}";
local ext="${2}";
[ "${file:$((${#file}-1)):1}" = "." ] &&
file="$(_rmExt "${file}" "")";
if [[ "${file%\.${ext}*}" != "${file}" ]]; then
echo "${file%\.${ext}*}";
else
echo "${file%${ext}*}";
fi;
};
_init "$@";
# Start the main function.
_main;
# Loop through this script for any other files the user requested.
if (($# > 1)); then
echo;
$0 "${@:2}";
fi;
exit $?;