I'm looking at extended file attributes for iOS and Mac files using setxattr command. From what I understand, I can store arbitrary data there, up to 128kb.
How can I write and read extended attributes as if I'm dealing with a dictionary, not dereferencing string pointers?
So far I have this code that attempts to set a single attribute.
NSString* filepath = [MyValueObject filepath];
const char *systemPath = [filepath fileSystemRepresentation];
const char *name = "special_value";
const char *value = "test string";
int result = setxattr(systemPath, name, &value, strlen(value), 0, 0);
If I need to store a small set of values (say 5 key-value pairs), I'm thinking of:
- Creating a NSDictionary with my attributes
- Converting the dictionary to JSON string
- Converting the string to character pointer
- Writing the string to the extended attributes
- To read the attribute back, I would read back the string pointer
- Convert to NSString
- Convert to JSON object
- Create a dictionary back
- Retrieve a value from dictionary
Does this seem like the right approach? Is there's an easier way to store metadata in extended attributes ? Maybe there is a category on NSObject that handles the pointer operations for xattr?
convert the dictionary to a binary plist and write that -- and vice versa :)
Write:
--
Read:
I found a Cocoanetics/DTFoundation that allows reading/writing arbitrary strings to xattr: Together with other posts, I was able to accomplish what I wanted - write/restore a dictionary
Reading: