Skip to content

Commit ecc5942

Browse files
committed
genfstab: deal with proper escaping/unescaping of cols
the source, target, and comments should always be mangled on writing, and unmangled on reading.
1 parent 8fc4708 commit ecc5942

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

genfstab

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ shopt -s extglob
44

55
. ./common
66

7+
# we need this because %q isn't the opposite of %b
8+
mangle() {
9+
local i= c= out=
10+
11+
for (( i = 0; i < ${#1}; i++ )); do
12+
c=${1:i:1}
13+
case $c in
14+
[[:space:]])
15+
printf -v c '%x' "'$c"
16+
out+=\\x$c
17+
;;
18+
*)
19+
out+=$c
20+
;;
21+
esac
22+
done
23+
24+
printf '%s' "$out"
25+
}
26+
727
write_source() {
828
local tag= spec= label= uuid= comment=()
929

@@ -31,7 +51,7 @@ write_source() {
3151
if [[ $spec ]]; then
3252
printf '%-20s' "$tag=$spec"
3353
else
34-
printf '%-20s' "$source"
54+
printf '%-20s' "$(mangle "$source")"
3555
fi
3656
}
3757

@@ -84,6 +104,10 @@ findmnt -Recruno SOURCE,TARGET,FSTYPE,OPTIONS "$root" |
84104
# default 5th and 6th columns
85105
dump=0 pass=2
86106

107+
# unescape source and target
108+
printf -v target '%b' "$target"
109+
printf -v source '%b' "$source"
110+
87111
# this is root
88112
target=${target#$root}
89113
if [[ $target = ?(/) ]]; then
@@ -98,13 +122,13 @@ findmnt -Recruno SOURCE,TARGET,FSTYPE,OPTIONS "$root" |
98122

99123
if [[ $source =~ ^(/.+)\[(.+)\]$ ]]; then
100124
# it's a bind mount
101-
source=$(findmnt -funcrevo TARGET "${BASH_REMATCH[1]}")${BASH_REMATCH[2]}
125+
source=$(findmnt -funcevo TARGET "${BASH_REMATCH[1]}")${BASH_REMATCH[2]}
102126
fstype=bind
103127
fi
104128

105129
# write one line
106130
write_source "$source"
107-
printf '\t%-10s' "$target" "$fstype" "$opts"
131+
printf '\t%-10s' "$(mangle "$target")" "$fstype" "$opts"
108132
printf '\t%s %s' "$dump" "$pass"
109133
printf '\n\n'
110134
done

0 commit comments

Comments
 (0)