Skip to content

Commit 12f11b9

Browse files
committed
Improve quoting performance
If features or users have transferred a large number of files or directories for packing into an image, then quoting a very long string takes too much time. For quoting, bash-specific syntax can be used to speed up character-by-character string processing. Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
1 parent 226e74e commit 12f11b9

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

tools/create-rootfs

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,84 @@ DIRS=()
1515
LIB_PATHS=()
1616
BIN_PATHS=()
1717

18+
quote_bash_args()
19+
{
20+
### This is an internal function to avoid the use of ugly namespace.
21+
__quote_bash_args() {
22+
local r='' c='' l="$1"
23+
### backslash/double/single quote mode
24+
local bq='' dq='' sq=''
25+
26+
__quote_bash_args_toggle() {
27+
### toggle $1 value
28+
eval [ -n \"\$$1\" ] && eval "$1=" || eval "$1=\$$2"
29+
}
30+
31+
for ((i=0; i<${#l}; i++)); do
32+
c="${l:i:1}"
33+
34+
case "$c" in
35+
\")
36+
### toggle double quote mode unless
37+
### in backslash or single quote mode
38+
[ -n "$bq$sq" ] || __quote_bash_args_toggle dq c
39+
;;
40+
\')
41+
### toggle single quote mode unless
42+
### in backslash or double quote mode
43+
[ -n "$bq$dq" ] || __quote_bash_args_toggle sq c
44+
;;
45+
\$|\`)
46+
### quote special character unless
47+
### in backslash or single quote mode
48+
[ -n "$bq$sq" ] || bq=\\
49+
;;
50+
\\)
51+
### toggle backslash quote mode unless
52+
### in single quote mode
53+
if [ -z "$sq" ]; then
54+
if [ -z "$bq" ]; then
55+
### enter backslash quote mode
56+
bq=\\
57+
continue
58+
else
59+
### leave backslash quote mode
60+
r="$r\\"
61+
bq=
62+
fi
63+
fi
64+
;;
65+
[!A-Za-z0-9_\ \ ])
66+
### quote non-regular character unless
67+
### in any quote mode
68+
[ -n "$bq$dq$sq" ] || bq=\\
69+
;;
70+
esac
71+
r="$r$bq$c"
72+
### leave backslash quote mode if any
73+
bq=
74+
done
75+
76+
[ -z "$bq$dq$sq" ] ||
77+
{ message "unmatched character ($bq$dq$sq) found"; return 1; }
78+
__quote_bash_args_out="$r"
79+
}
80+
local __quote_bash_args_rc=0
81+
82+
if [ -z "${2##*[\"\'\$\`\\\\]*}" ]; then
83+
local __quote_bash_args_out=''
84+
__quote_bash_args "$2" || __quote_bash_args_rc=1
85+
eval "$1=\"\$__quote_bash_args_out\""
86+
else
87+
eval "$1=\"\$2\""
88+
fi
89+
90+
### Remove internal functions from user namespace.
91+
unset -f __quote_bash_args __quote_bash_args_toggle
92+
93+
return $__quote_bash_args_rc
94+
}
95+
1896
in_runtimedir()
1997
{
2098
local f="${1#$LOCALBUILDDIR}"
@@ -34,7 +112,7 @@ append_progs()
34112
[ -n "$1" ] ||
35113
return 0
36114

37-
quote_shell_args args "$1"
115+
quote_bash_args args "$1"
38116
eval "set -- $args"
39117

40118
for n; do
@@ -113,7 +191,7 @@ append_libs()
113191
[ -n "$1" ] ||
114192
return 0
115193

116-
quote_shell_args args "$1"
194+
quote_bash_args args "$1"
117195
eval "set -- $args"
118196

119197
for n; do
@@ -158,7 +236,7 @@ append_libs()
158236
get_bin_paths
159237
get_lib_paths
160238

161-
quote_shell_args args "${PUT_FEATURE_DIRS-} $PUT_DIRS"
239+
quote_bash_args args "${PUT_FEATURE_DIRS-} $PUT_DIRS"
162240
eval "set -- $args"
163241

164242
for n in \
@@ -184,7 +262,7 @@ append_libs required "${PUT_FEATURE_LIBS-}"
184262
append_libs optional "${PUT_FEATURE_OPTIONAL_LIBS-}"
185263

186264
if [ -n "${PUT_FEATURE_PROGS_WILDCARD-}" ]; then
187-
quote_shell_args args "$PUT_FEATURE_PROGS_WILDCARD"
265+
quote_bash_args args "$PUT_FEATURE_PROGS_WILDCARD"
188266
eval "set -- $args"
189267

190268
while read -d: -r n; do
@@ -209,7 +287,7 @@ if [ -n "${PUT_FEATURE_PROGS_WILDCARD-}" ]; then
209287
fi
210288

211289
if [ -n "${PUT_FEATURE_FILES-}" ] || [ -n "${PUT_FILES-}" ]; then
212-
quote_shell_args args "${PUT_FEATURE_FILES-} ${PUT_FILES-}"
290+
quote_bash_args args "${PUT_FEATURE_FILES-} ${PUT_FILES-}"
213291
eval "set -- $args"
214292

215293
for n in "$@"; do

0 commit comments

Comments
 (0)