本文介绍了iOS如何在文件的xattr中存储NSDictionary或JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用setxattr命令查看iOS和Mac文件的扩展文件属性.据我了解,我可以在其中存储任意数据,最高可达128kb.

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.

如何像写字典一样写和读扩展属性,而不是取消引用字符串指针?

到目前为止,我有这段代码试图设置单个属性.

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);

如果我需要存储一小部分值(例如5个键值对),我会考虑:

If I need to store a small set of values (say 5 key-value pairs), I'm thinking of:

  1. 使用我的属性创建NSDictionary
  2. 将字典转换为JSON字符串
  3. 将字符串转换为字符指针
  4. 将字符串写入扩展属性
  5. 要读回属性,我要读回字符串指针
  6. 转换为NSString
  7. 转换为JSON对象
  8. 重新创建字典
  9. 从字典中检索值

这似乎是正确的方法吗? 是否有更简单的方法来将元数据存储在扩展属性中?也许NSObject上存在一个类别,用于处理xattr的指针操作?

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?

推荐答案

我发现了一个Cocoanetics/DTFoundation,它允许阅读/向xattr写任意字符串:与其他文章一起,我能够完成我想要的工作-编写/恢复字典

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

#import "Note+ExtendedAttribute.h"
#include <sys/xattr.h>


@implementation MyFile (ExtendedAttribute)

-(NSString*)dictionaryKey
{
    return @"mydictionary";
}

-(BOOL)writeExtendedAttributeDictionary:(NSDictionary*)dictionary
{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:0
                                                         error:&error];
    if (! jsonData) {
        return NO;
    }

    NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];



    const char *filepath = [[self filepath] fileSystemRepresentation];

    const char *key = [[self dictionaryKey] UTF8String];
    const char *value = [jsonString UTF8String];

    int result = setxattr(filepath, key, value, strlen(value), 0, 0);

    if(result != 0)
    {
        return NO;
    }

    return YES;
}

阅读:

-(NSMutableDictionary*)readExtendedAttributeDictionary
{
    const char *attrName = [[self dictionaryKey] UTF8String];
    const char *filePath = [[self filepath] fileSystemRepresentation];

    // get size of needed buffer
    int bufferLength = getxattr(filePath, attrName, NULL, 0, 0, 0);

    if(bufferLength<=0)
    {
        return nil;
    }

    // make a buffer of sufficient length
    char *buffer = malloc(bufferLength);

    // now actually get the attribute string
    getxattr(filePath, attrName, buffer, bufferLength, 0, 0);

    // convert to NSString
    NSString *retString = [[NSString alloc] initWithBytes:buffer length:bufferLength encoding:NSUTF8StringEncoding];

    // release buffer
    free(buffer);

     NSData *data = [retString dataUsingEncoding:NSUTF8StringEncoding];
    if(data == nil || data.length == 0)
    {
        return nil;
    }

    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

    if([json isKindOfClass:[NSDictionary class]])
    {
        return [NSMutableDictionary dictionaryWithDictionary:json];
    }

    if(error)
    {
        return nil;
    }


    return json;
}

这篇关于iOS如何在文件的xattr中存储NSDictionary或JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 19:14