summaryrefslogtreecommitdiff
path: root/common/casefold
blob: 8fbdb52a71b19e4ef583dc1051eb52811b109aab (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2019 Collabora, Ltd.  All Rights Reserved.
#
# Common functions for testing filename casefold feature

_has_casefold_kernel_support()
{
	case $FSTYP in
	ext4)
		test -f '/sys/fs/ext4/features/casefold'
		;;
	f2fs)
		test -f '/sys/fs/f2fs/features/casefold'
		;;
	bcachefs)
		true
		;;
	*)
		# defaults to unsupported
		false
		;;
	esac
}

_require_scratch_casefold()
{
	if ! _has_casefold_kernel_support ; then
		_notrun "$FSTYP does not support casefold feature"
	fi

	if ! _scratch_mkfs_casefold &>> $seqres.full ; then
		_notrun "$FSTYP userspace tools do not support casefold"
	fi

	# Make sure the kernel can mount a filesystem with the encoding
	# defined by the userspace tools.  This will fail if
	# the userspace tool used a more recent encoding than the one
	# supported in kernel space.
	if ! _try_scratch_mount &>> $seqres.full ; then
		_notrun "kernel can't mount filesystem with the encoding set by userspace"
	fi
	_scratch_unmount

	# utilities used by casefold
	_require_command "$CHATTR_PROG" chattr
	_require_command "$LSATTR_PROG" lsattr
}

_scratch_mkfs_casefold()
{
	case $FSTYP in
	ext4)
		_scratch_mkfs -O casefold $*
		;;
	f2fs)
		_scratch_mkfs -C utf8 $*
		;;
	bcachefs)
		_scratch_mkfs $*
		;;
	*)
		_notrun "Don't know how to mkfs with casefold support on $FSTYP"
		;;
	esac
}

_scratch_mkfs_casefold_strict()
{
	case $FSTYP in
	ext4)
		_scratch_mkfs -O casefold -E encoding_flags=strict
		;;
	f2fs)
		_scratch_mkfs -C utf8:strict
		;;
	bcachefs)
		_scratch_mkfs $*
		;;
	*)
		_notrun "Don't know how to mkfs with casefold-strict support on $FSTYP"
		;;
	esac
}

# To get the exact disk name, we need some method that does a
# getdents() on the parent directory, such that we don't get
# normalized/casefolded results.  'Find' works ok.
_casefold_check_exact_name()
{
	local basedir=$1
	local exact_name=$2
	find ${basedir} | grep -q ${exact_name}
}

_casefold_set_attr()
{
	$CHATTR_PROG +F "${1}"
}

_casefold_unset_attr()
{
	$CHATTR_PROG -F "${1}"
}

_casefold_lsattr_dir()
{
	if $LSATTR_PROG -ld "${1}" | grep -q Casefold ; then
		echo "${1} Casefold"
	else
		echo "${1}"
	fi
}