I wrote the following code in VS Code and ran it to set file attribute. It seemed to have run successfully, but when I checked the value, the text was not correct. Is Unicode string supported for file extended attributes? If so, how can I fix the code below?
#include <stdio.h>
#include <sys/xattr.h>
int main()
{
printf("ねこ\n");
ssize_t res = setxattr("/mnt/cat/test.txt", "user.dog"
, "ねこ", 2, 0); /*also tested 4 and 8*/
printf("Result = %lu\n", (unsigned long)res);
return 0;
}
Programme output
ねこ
Result = 0
Reading attribute
$ getfattr test.txt -d
# file: test.txt
user.dog=0s44E=
Obviously
ねこ
can't be stored in 2 bytes. The characters are U+306D and U+3053, encoded in UTF-8 asE3 81 AD E3 81 93
so length must be set to 6. If you did that you'll see thatgetfattr test.txt -d
outputsThat's because
-d
doesn't what format the data is in and just dumps it as binary. The0s
prefix means that the data is in base64 as stated from the manpage:Just plug
44Gt44GT
into any base64 decoder or runecho 44Gt44GT | base64 --decode
and you'll see the correct string printed out. To see the string directly fromgetfattr
you need to specify the format with-e text