I am trying to write some data (the length of the data is 367 bytes) in the header of a file using the following code:
const char *attrName = [kAttributeForKey UTF8String];
const char *path = [filePath fileSystemRepresentation];
const uint8_t *myDataBytes = (const uint8_t*)[myData bytes];
int result = setxattr(path, attrName, myDataBytes, sizeof(myDataBytes), 0, 0);
When I try to read it, the result is different:
const char *attrName = [kAttributeForKey UTF8String];
const char *path = [filePath fileSystemRepresentation];
int bufferLength = getxattr(path, attrName, NULL, 0, 0, 0);
char *buffer = malloc(bufferLength);
getxattr(path, attrName, buffer, bufferLength, 0, 0);
NSData *myData = [[NSData alloc] initWithBytes:buffer length:bufferLength];
free(buffer);
Could someone tell me how can I make this work? Thanks in advance.