common/casefold: Add infrastructure to test filename casefold feature
[xfstests-dev.git] / common / casefold
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2019 Collabora, Ltd.  All Rights Reserved.
3 #
4 # Common functions for testing filename casefold feature
5
6 _has_casefold_kernel_support()
7 {
8         case $FSTYP in
9         ext4)
10                 test -f '/sys/fs/ext4/features/casefold'
11                 ;;
12         *)
13                 # defaults to unsupported
14                 false
15                 ;;
16         esac
17 }
18
19 _require_scratch_casefold()
20 {
21         if ! _has_casefold_kernel_support ; then
22                 _notrun "$FSTYP does not support casefold feature"
23         fi
24
25         if ! _scratch_mkfs_casefold &>>seqres.full; then
26                 _notrun "$FSTYP userspace tools do not support casefold"
27         fi
28
29         # Make sure the kernel can mount a filesystem with the encoding
30         # defined by the userspace tools.  This will fail if
31         # the userspace tool used a more recent encoding than the one
32         # supported in kernel space.
33         if ! _try_scratch_mount &>>seqres.full; then
34                 _notrun "kernel can't mount filesystem with the encoding set by userspace"
35         fi
36         _scratch_unmount
37
38         # utilities used by casefold
39         _require_command "$CHATTR_PROG" chattr
40         _require_command "$LSATTR_PROG" lsattr
41 }
42
43 _scratch_mkfs_casefold()
44 {
45         case $FSTYP in
46         ext4)
47                 _scratch_mkfs -O casefold $*
48                 ;;
49         *)
50                 _notrun "Don't know how to mkfs with casefold support on $FSTYP"
51                 ;;
52         esac
53 }
54
55 _scratch_mkfs_casefold_strict()
56 {
57         case $FSTYP in
58         ext4)
59                 _scratch_mkfs -O casefold -E encoding_flags=strict
60                 ;;
61         *)
62                 _notrun "Don't know how to mkfs with casefold-strict support on $FSTYP"
63                 ;;
64         esac
65 }
66
67 # To get the exact disk name, we need some method that does a
68 # getdents() on the parent directory, such that we don't get
69 # normalized/casefolded results.  'Find' works ok.
70 _casefold_check_exact_name()
71 {
72         local basedir=$1
73         local exact_name=$2
74         find ${basedir} | grep -q ${exact_name}
75 }
76
77 _casefold_set_attr()
78 {
79         $CHATTR_PROG +F "${1}"
80 }
81
82 _casefold_unset_attr()
83 {
84         $CHATTR_PROG -F "${1}"
85 }
86
87 _casefold_lsattr_dir()
88 {
89         $LSATTR_PROG -ld "${1}" | _filter_spaces
90 }