| 1 | #!/bin/bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | set -eu |
| 21 | |
| 22 | : "${SELFTEST:=}" |
| 23 | if [ -n "${SELFTEST}" ]; then |
| 24 | unset SELFTEST |
| 25 | tab=$(printf '\t') |
| 26 | check() { |
| 27 | local desc=$1 input=$2 want=$3 got |
| 28 | got=$(printf '%s\n' "${input}" | "$0") |
| 29 | if [ "${got}" = "${want}" ]; then |
| 30 | echo "ok: ${desc}" |
| 31 | else |
| 32 | echo "FAIL: ${desc}" >&2 |
| 33 | printf ' want: %q\n got: %q\n' "${want}" "${got}" >&2 |
| 34 | return 1 |
| 35 | fi |
| 36 | } |
| 37 | echo >&2 "=== Running tests ===" |
| 38 | check "space in the mount point is escaped" \ |
| 39 | "tag${tab}/tmp/dir with spaces${tab}virtiofs${tab}rw,nofail,comment=cloudconfig${tab}0${tab}0" \ |
| 40 | "tag${tab}/tmp/dir\\040with\\040spaces${tab}virtiofs${tab}rw,nofail,comment=cloudconfig${tab}0${tab}0" |
| 41 | check "already-escaped path is unchanged (idempotent)" \ |
| 42 | "tag${tab}/tmp/dir\\040with\\040spaces${tab}virtiofs${tab}rw,nofail,comment=cloudconfig${tab}0${tab}0" \ |
| 43 | "tag${tab}/tmp/dir\\040with\\040spaces${tab}virtiofs${tab}rw,nofail,comment=cloudconfig${tab}0${tab}0" |
| 44 | check "path without whitespace is unchanged" \ |
| 45 | "tag${tab}/mnt/nospace${tab}virtiofs${tab}rw,nofail,comment=cloudconfig${tab}0${tab}0" \ |
| 46 | "tag${tab}/mnt/nospace${tab}virtiofs${tab}rw,nofail,comment=cloudconfig${tab}0${tab}0" |
| 47 | check "backslash is escaped before the space" \ |
| 48 | "tag${tab}/a b\\c${tab}virtiofs${tab}rw,comment=cloudconfig${tab}0${tab}0" \ |
| 49 | "tag${tab}/a\\040b\\134c${tab}virtiofs${tab}rw,comment=cloudconfig${tab}0${tab}0" |
| 50 | check "entry without comment=cloudconfig is unchanged" \ |
| 51 | "tag${tab}/tmp/dir with spaces${tab}virtiofs${tab}rw${tab}0${tab}0" \ |
| 52 | "tag${tab}/tmp/dir with spaces${tab}virtiofs${tab}rw${tab}0${tab}0" |
| 53 | check "non-virtiofs entry is unchanged" \ |
| 54 | "/dev/sda1${tab}/data dir${tab}ext4${tab}defaults,comment=cloudconfig${tab}0${tab}0" \ |
| 55 | "/dev/sda1${tab}/data dir${tab}ext4${tab}defaults,comment=cloudconfig${tab}0${tab}0" |
| 56 | echo >&2 "=== All tests passed ===" |
| 57 | exit 0 |
| 58 | fi |
| 59 | |
| 60 | awk -F'\t' 'BEGIN { OFS = "\t" } |
| 61 | $3 == "virtiofs" && $4 ~ /comment=cloudconfig/ && $2 ~ /[ \t]/ { |
| 62 | p = $2 |
| 63 | gsub(/\\/, "\\134", p) # backslash first so introduced escapes are not re-escaped |
| 64 | gsub(/ /, "\\040", p) |
| 65 | gsub(/\t/, "\\011", p) |
| 66 | $2 = p |
| 67 | } |
| 68 | { print }' |