#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2025 Red Hat Inc.  All Rights Reserved.
#
# FS QA Test No. 772
#
# Test file_getattr() and file_setattr() syscalls on regular files
#
. ./common/preamble
_begin_fstest auto

. ./common/filter

# Modify as appropriate.
_require_scratch
_require_test_program "file_attr"
_require_symlinks
_require_mknod
_require_file_attr

_scratch_mkfs >>$seqres.full 2>&1
_scratch_mount

file_attr () {
	$here/src/file_attr $*
}

projectdir=$SCRATCH_MNT/prj

# Create normal files and special files
mkdir $projectdir
touch $projectdir/foo

echo "Error codes"
# wrong AT_ flags
file_attr --get --invalid-at $projectdir ./foo
file_attr --set --invalid-at $projectdir ./foo
# wrong fsxattr size (too big, too small)
file_attr --get --too-big-arg $projectdir ./foo
file_attr --get --too-small-arg $projectdir ./foo
file_attr --set --too-big-arg $projectdir ./foo
file_attr --set --too-small-arg $projectdir ./foo
# out of fsx_xflags mask
file_attr --set --new-fsx-flag $projectdir ./foo

echo "Initial attributes state"
file_attr --get $projectdir | _filter_scratch | _filter_vfs_file_attributes ~d
file_attr --get $projectdir ./foo | _filter_vfs_file_attributes ~d

echo "Set FS_XFLAG_NODUMP (d)"
file_attr --set --set-nodump $projectdir
file_attr --set --set-nodump $projectdir ./foo

echo "Read attributes"
file_attr --get $projectdir | _filter_scratch | _filter_vfs_file_attributes ~d
file_attr --get $projectdir ./foo | _filter_vfs_file_attributes ~d

cd $SCRATCH_MNT
touch ./foo2
echo "Initial state of foo2"
file_attr --get --at-cwd ./foo2 | _filter_vfs_file_attributes ~d
echo "Set attribute relative to AT_FDCWD"
file_attr --set --at-cwd --set-nodump ./foo2
file_attr --get --at-cwd ./foo2 | _filter_vfs_file_attributes ~d

echo "Set attribute on AT_FDCWD"
mkdir ./bar
file_attr --get --at-cwd ./bar | _filter_vfs_file_attributes ~d
cd ./bar
file_attr --set --at-cwd --set-nodump ""
file_attr --get --at-cwd . | _filter_vfs_file_attributes ~d

# success, all done
status=0
exit
