我尝试写一些数据(该数据的长度为367字节)使用下面的代码在文件的标题:写扩展的文件属性
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);
当我尝试读它时,结果是不同的:
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);
有人能告诉我怎么做这项工作?提前致谢。
那么,对于初学者来说,人为地限制你的getter为255字节。如果你的数据更长,它会被截断 - 你已经mallocing正确的大小缓冲区来保存它,为什么不通过bufferLength传递给'getxattr'? – 2013-05-06 13:59:41
@BenZotto谢谢,我改变了它。还要别的吗? – Levi 2013-05-06 14:05:52
只是为了防止任何人需要它:这是一个Swift包装获取,设置,列出和删除扩展属性:http://stackoverflow.com/questions/38343186/write-extend-file-attributes-swift-example。 – 2016-10-12 08:36:18