Skip to content

Commit

Permalink
ssh_filter_btrbk.sh: properly normalise pathnames
Browse files Browse the repository at this point in the history
Previously, pathnames specified via the `--restrict-path`-option had only a
single trailing `/`, if any, stripped.

This commit adds (and utilises) a function which normalises pathnames as
described in its comments.

Signed-off-by: Christoph Anton Mitterer <[email protected]>
  • Loading branch information
calestyo committed Dec 4, 2022
1 parent 0d34d67 commit a0237fe
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ssh_filter_btrbk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ file_match_sane='/[0-9a-zA-Z_@+./-]*' # matches file path (equal to ${file_match
file_match="/[^']*" # btrbk >= 0.32.0 quotes file arguments: match all but single quote
file_arg_match="('${file_match}'|${file_match_sane})" # support btrbk < 0.32.0

print_normalised_pathname()
{
# Normalises a pathname given via the positional parameter #1 as follows:
# * Folds any >=3 leading `/` into 1.
# POSIX specifies that implementations may treat exactly 2 leading `//`
# specially and therefore such are not folded here.
# * Folds any >=2 non-leading `/` into 1.
# * Strips any trailing `/`.

local pathname="$1"

printf '%s' "${pathname}" | sed -E 's%^///+%/%; s%(.)//+%\1/%g; s%/+$%%'
}

log_cmd()
{
local priority="$1"
Expand Down Expand Up @@ -141,7 +155,7 @@ while [ "$#" -ge 1 ]; do
;;

-p|--restrict-path)
restrict_path_list="${restrict_path_list}|${2%/}" # add to list while removing trailing slash
restrict_path_list="${restrict_path_list}|$(print_normalised_pathname "$2")"
shift # past argument
;;

Expand Down

0 comments on commit a0237fe

Please sign in to comment.