blob: b1b1e8f4df3f3115ee573768363a8933b9aa0261 (
plain) (
blame)
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
|
# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "Configuration file sharing" project.
# For details, see https://github.com/egor-tensin/config-links.
# Distributed under the MIT License.
# Making sure paths point to files/directories
_traverse_path_usage() {
local prefix="${FUNCNAME[0]}"
[ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}"
local msg
for msg; do
echo "$prefix: $msg"
done
echo "usage: $prefix [-h|--help] [-0|--null|-z|--zero] [-e|--exist] [-f|--file] [-d|--directory] [--] [PATH]..."
}
traverse_path() {
local -a paths=()
local must_exist=
local type_flag=
local type_name=
local fmt='%s\n'
while [ "$#" -gt 0 ]; do
local key="$1"
shift
case "$key" in
-h|--help)
_traverse_path_usage
return 0
;;
-0|--null|-z|--zero)
fmt='%s\0'
;;
--)
break
;;
-e|--exist)
must_exist=1
;;
-d|--directory)
type_flag=-d
type_name="directory"
;;
-f|--file)
type_flag=-f
type_name="regular file"
;;
-*)
_traverse_path_usage "unrecognized parameter: $key" >&2
return 1
;;
*)
paths+=("$key")
;;
esac
done
paths+=("$@")
[ "${#paths[@]}" -eq 0 ] && return 0
if is_cygwin; then
local i
for i in "${!paths[@]}"; do
paths[$i]="$( cygpath -- "${paths[$i]}" )"
done
fi
local -a abs_paths=()
local path
while IFS= read -d '' -r path; do
if [ -n "$must_exist" ] && [ ! -e "$path" ]; then
dump "must exist: $path" >&2
return 1
fi
if [ -e "$path" ] && [ -n "$type_flag" ] && ! test "$type_flag" "$path"; then
dump "must be a $type_name: $path" >&2
return 1
fi
abs_paths+=("$path")
done < <( readlink -z --canonicalize-missing -- ${paths[@]+"${paths[@]}"} )
printf -- "$fmt" ${abs_paths[@]+"${abs_paths[@]}"}
}
|