I'm trying to implement a FUSE file system including the ability to set/get extended file attributes. In this case, the attribute that I am interested in is one to create a boolean condition to mark a file as encrypted. Let's say that I have a file called test.txt. From a terminal window, I can easily do the following:
setfattr -n user.encrypted -v 1 test.txt
getfattr -n user.encrypted test.txt
and get the output:
# file: test.txt
user.encrypted="1"
So, I know that my system is correctly set to work with extended attributes.
But I'm stuck on setting and getting these attributes programatically from within FUSE. For example, I have the following function:
/* File open operation */
int bb_open(const char *path, struct fuse_file_info *fi)
{
int retstat = 0;
int fd;
int isEncrypted;
char user[] = "user";
char encAttr[] = "encrypted";
fd = open(path, fi->flags);
if (fd < 0)
retstat = bb_error("bb_open open");
log_msg("\nAbout to get encryption attribute\n");
/* get the encryption attribute */
isEncrypted = fgetxattr(fd, user, encAttr, 1);
log_msg("\nisEncrypted: %d\n", isEncrypted);
return retstat;
}
When I run this, even after setting this attribute from the command line, fgetxattr always fails (i.e. it returns a value of -1). The output in my log file is:
isEncrypted = -1
Clearly I'm calling this function incorrectly, but I don't know how to correct this. Can anyone help? Thanks.