You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.5 KiB
82 lines
2.5 KiB
#!/bin/sh |
|
|
|
# This spell reads the enchanted attributes from the specified file. |
|
# If a field name is provided as the second argument, it reads the attribute with that name. |
|
# If the file or attribute does not exist, it raises an error. |
|
|
|
# Check that the correct number of arguments was provided |
|
if [ $# -lt 1 ] || [ $# -gt 2 ]; then |
|
echo "Error: This spell requires one or two arguments: a file path and an optional attribute name." |
|
exit 1 |
|
fi |
|
|
|
# Set the file path and attribute name variables |
|
file=$1 |
|
attribute=$2 |
|
|
|
# Check that the file exists |
|
if [ ! -f "$file" ] && [ ! -d "$file" ]; then |
|
echo "Error: The file does not exist." |
|
exit 1 |
|
fi |
|
|
|
# Define a function to read the attribute value using the available commands |
|
read_attribute_value() { |
|
# Try to read the attribute using the 'attr' command |
|
attribute_value=$(attr -g "$1" "$2" 2> /dev/null | cut -d ":" -f 2 | xargs) |
|
|
|
# If the 'attr' command is not available, try using the 'xattr' command |
|
if [ -z "$attribute_value" ]; then |
|
attribute_value=$(xattr -p "$1" "$2" 2> /dev/null | cut -d ":" -f 2 | xargs) |
|
fi |
|
|
|
# If the 'xattr' command is not available, try using the 'getfattr' command |
|
if [ -z "$attribute_value" ]; then |
|
attribute_value=$(getfattr -n "$1" --only-values "$2" 2> /dev/null) |
|
fi |
|
|
|
# Return the attribute value |
|
echo "$attribute_value" |
|
} |
|
|
|
# If an attribute name was not provided, print all attributes in YAML format |
|
if [ -z "$attribute" ]; then |
|
attributes_found=0 |
|
|
|
# Define a function to process each attribute |
|
process_attribute() { |
|
# Read the attribute value using the read_attribute_value function |
|
value=$(read_attribute_value "$1" "$file") |
|
|
|
# Check if the attribute value was set |
|
if [ -n "$value" ]; then |
|
# Print the attribute and value in YAML format |
|
printf "%s: %s\n" "$1" "$value" |
|
attributes_found=$((attributes_found + 1)) |
|
fi |
|
} |
|
|
|
# Call the process_attribute function for each attribute |
|
for attribute in $(getfattr -m ".*" -e hex "$file" | cut -d ":" -f 1 | cut -d "." -f 2 | tr -d ' '); do |
|
process_attribute "$attribute" |
|
done |
|
|
|
# Check if no extended attributes were found |
|
if [ "$attributes_found" -eq 0 ]; then |
|
echo "No enchanted attributes found." |
|
fi |
|
|
|
# If an attribute name was provided, print the attribute value |
|
else |
|
# Read the attribute value using the read_attribute_value function |
|
value=$(read_attribute_value "$attribute" "$file") |
|
|
|
# Check if the attribute value was set |
|
if [ -z "$value" ]; then |
|
echo "Error: The attribute does not exist." |
|
exit 1 |
|
fi |
|
|
|
# Print the attribute value |
|
echo "$value" |
|
fi
|
|
|