写入扩展文件属性 [英] Write extended file attributes

查看:76
本文介绍了写入扩展文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在文件头中写入一些数据(数据长度为 367 字节):

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.

推荐答案

问题在于您对 setxattr 的调用.不能使用 sizeof 调用.你想要:

The problem is with your call to setxattr. The sizeof call can't be used. You want:

int result = setxattr(path, attrName, myDataBytes, [myData length], 0, 0);

sizeof(myDataBytes) 的调用将返回指针的大小,而不是数据的长度.

The call to sizeof(myDataBytes) will return the size of the pointer, not the length of the data.

这篇关于写入扩展文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆