Debian Codegraph @

fstab-helpers

raw
1 #!/hint/bash
2 # SPDX-License-Identifier: GPL-2.0-only
3
4 # generated from util-linux source: libmount/src/utils.c
5 declare -A pseudofs_types=([anon_inodefs]=1
6 [apparmorfs]=1
7 [autofs]=1
8 [bdev]=1
9 [binder]=1
10 [binfmt_misc]=1
11 [bpf]=1
12 [cgroup]=1
13 [cgroup2]=1
14 [configfs]=1
15 [cpuset]=1
16 [debugfs]=1
17 [devfs]=1
18 [devpts]=1
19 [devtmpfs]=1
20 [dlmfs]=1
21 [dmabuf]=1
22 [drm]=1
23 [efivarfs]=1
24 [fuse]=1
25 [fuse.archivemount]=1
26 [fuse.avfsd]=1
27 [fuse.dumpfs]=1
28 [fuse.encfs]=1
29 [fuse.gvfs-fuse-daemon]=1
30 [fuse.gvfsd-fuse]=1
31 [fuse.lxcfs]=1
32 [fuse.portal]=1
33 [fuse.rofiles-fuse]=1
34 [fuse.vmware-vmblock]=1
35 [fuse.xwmfs]=1
36 [fusectl]=1
37 [hugetlbfs]=1
38 [ipathfs]=1
39 [mqueue]=1
40 [nfsd]=1
41 [none]=1
42 [nsfs]=1
43 [overlay]=1
44 [pipefs]=1
45 [proc]=1
46 [pstore]=1
47 [ramfs]=1
48 [resctrl]=1
49 [rootfs]=1
50 [rpc_pipefs]=1
51 [securityfs]=1
52 [selinuxfs]=1
53 [smackfs]=1
54 [sockfs]=1
55 [spufs]=1
56 [sysfs]=1
57 [tmpfs]=1
58 [tracefs]=1
59 [vboxsf]=1
60 [virtiofs]=1)
61
62 # generated from: pkgfile -vbr '/fsck\..+' | awk -F. '{ print $NF }' | sort
63 declare -A fsck_types=([btrfs]=0 # btrfs doesn't need a regular fsck utility
64 [cramfs]=1
65 [erofs]=1
66 [exfat]=1
67 [ext2]=1
68 [ext3]=1
69 [ext4]=1
70 [f2fs]=1
71 [fat]=1
72 [jfs]=1
73 [minix]=1
74 [msdos]=1
75 [reiserfs]=1
76 [vfat]=1
77 [xfs]=1)
78
79 # shellcheck disable=SC2059 # $1 and $2 can contain the printf modifiers
80 out() { printf "$1 $2\n" "${@:3}"; }
81 error() { out "==> ERROR:" "$@"; } >&2
82 die() { error "$@"; exit 1; }
83
84 try_cast() (
85 _=$(( $1#$2 ))
86 ) 2>/dev/null
87
88 valid_number_of_base() {
89 local base=$1 len=${#2} i=
90
91 for (( i = 0; i < len; i++ )); do
92 try_cast "$base" "${2:i:1}" || return 1
93 done
94
95 return 0
96 }
97
98 mangle() {
99 local i=0 chr="" out=""
100 local {a..f}= {A..F}=
101
102 for (( i = 0; i < ${#1}; i++ )); do
103 chr=${1:i:1}
104 case $chr in
105 [[:space:]\\])
106 printf -v chr '%03o' "'$chr"
107 out+=\\
108 ;;
109 esac
110 out+=$chr
111 done
112
113 printf '%s' "$out"
114 }
115
116 unmangle() {
117 local i=0 chr="" out="" len=$(( ${#1} - 4 ))
118 local {a..f}= {A..F}=
119
120 for (( i = 0; i < len; i++ )); do
121 chr=${1:i:1}
122 case $chr in
123 \\)
124 if valid_number_of_base 8 "${1:i+1:3}" ||
125 valid_number_of_base 16 "${1:i+1:3}"; then
126 printf -v chr '%b' "${1:i:4}"
127 (( i += 3 ))
128 fi
129 ;;
130 esac
131 out+=$chr
132 done
133
134 printf '%s' "$out${1:i}"
135 }
136
137 optstring_match_option() {
138 local candidate pat patterns
139
140 IFS=, read -ra patterns <<<"$1"
141 for pat in "${patterns[@]}"; do
142 if [[ $pat = *=* ]]; then
143 # "key=val" will only ever match "key=val"
144 candidate=$2
145 else
146 # "key" will match "key", but also "key=anyval"
147 candidate=${2%%=*}
148 fi
149
150 [[ $pat = "$candidate" ]] && return 0
151 done
152
153 return 1
154 }
155
156 optstring_remove_option() {
157 local o options_ remove=$2 IFS=,
158
159 read -ra options_ <<<"${!1}"
160
161 for o in "${!options_[@]}"; do
162 optstring_match_option "$remove" "${options_[o]}" && unset 'options_[o]'
163 done
164
165 declare -g "$1=${options_[*]}"
166 }
167
168 optstring_normalize() {
169 local o options_ norm IFS=,
170
171 read -ra options_ <<<"${!1}"
172
173 # remove empty fields
174 for o in "${options_[@]}"; do
175 [[ $o ]] && norm+=("$o")
176 done
177
178 # avoid empty strings, reset to "defaults"
179 declare -g "$1=${norm[*]:-defaults}"
180 }
181
182 optstring_append_option() {
183 if ! optstring_has_option "$1" "$2"; then
184 declare -g "$1=${!1},$2"
185 fi
186
187 optstring_normalize "$1"
188 }
189
190 optstring_prepend_option() {
191 if ! optstring_has_option "$1" "$2"; then
192 declare -g "$1=$2,${!1}"
193 fi
194
195 optstring_normalize "$1"
196 }
197
198 optstring_get_option() {
199 local _opts o
200
201 IFS=, read -ra _opts <<<"${!1}"
202 for o in "${_opts[@]}"; do
203 if optstring_match_option "$2" "$o"; then
204 declare -g "$o"
205 return 0
206 fi
207 done
208
209 return 1
210 }
211
212 optstring_has_option() {
213 local "${2%%=*}"
214
215 optstring_get_option "$1" "$2"
216 }
217
218 dm_name_for_devnode() {
219 read -r dm_name <"/sys/class/block/${1#/dev/}/dm/name"
220 if [[ $dm_name ]]; then
221 printf '/dev/mapper/%s' "$dm_name"
222 else
223 # don't leave the caller hanging, just print the original name
224 # along with the failure.
225 error 'Failed to resolve device mapper name for: %s' "$1"
226 fi
227 }
228
229 fstype_is_pseudofs() {
230 (( pseudofs_types["$1"] ))
231 }
232
233 fstype_has_fsck() {
234 (( fsck_types["$1"] ))
235 }