[GIO] How to remove an extended file attribute?

I can use GIO functions for creating and changing custom attributes in the “xattr” namespace, but I failed to find any GIO function for removing such an attribute.

If I try to remove the attribute by unsetting it, this does not work:

g_file_set_attribute(filedata, "xattr::customstring", G_FILE_ATTRIBUTE_TYPE_INVALID, NULL, G_FILE_QUERY_INFO_NONE, NULL, &error);

I get the error message: “Invalid attribute type (string expected)”. If I set the attribute type to string, but the pointer still is NULL, then the program crashes.

The only solution that I was able to find is the use of removexattr function:

removexattr(filenamewithpath, "user.customstring");

But this means that my code becomes mixed: GIO functions are used along with glibc functions. Though, I cannot explain it exactly, I don’t like writing mixed code, where functions of different origin and abstraction level are applied to one and the same data set.

There is no direct g_file_remove_attribute() method.

What you can do is: query the GFile for its GFileInfo, and then call g_file_info_remove_attribute() to remove the attributes you want to drop. Then you can call g_file_set_attributes_from_info() to replace the attributes.

I’d recommend you open an issue against GLib and discuss the possibility of adding a direct API to GFile as well.

Thank you very much for the quick reply!

But what about using only glibc functions like set/getxattr and removexattr? Would it be an appropriated solution? The only target platform is Linux, if it matters.

BTW, the solution with g_file_info_remove_attribute/g_file_set_attributes_from_info probably would not work. Though the attribute is removed from the GFileInfo structure, but then, when we write the structure back to the disk, the removed attribute seems to be kept untouched there.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.