Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3324d28de2
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 101 lines (75 sloc) 1.849 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
//
// DTExtendedFileAttributes.m
// DTFoundation
//
// Created by Oliver Drobnik on 3/6/12.
// Copyright (c) 2012 Cocoanetics. All rights reserved.
//

#import "DTExtendedFileAttributes.h"

#import <sys/xattr.h>

@implementation DTExtendedFileAttributes
{
NSString *_path;
}

- (id)initWithPath:(NSString *)path
{
self = [super init];
if (self)
{
if (![path length])
{
return nil;
}

_path = path;
}

return self;
}

- (BOOL)removeAttribute:(NSString *)attribute
{
const char *attrName = [attribute UTF8String];
const char *filePath = [_path fileSystemRepresentation];

int result = removexattr(filePath, attrName, 0);

return (result==0);
}

- (BOOL)setValue:(NSString *)value forAttribute:(NSString *)attribute
{
if (![attribute length])
{
return NO;
}

if (!value)
{
// remove it instead
return [self removeAttribute:attribute];
}

const char *attrName = [attribute UTF8String];
const char *filePath = [_path fileSystemRepresentation];

const char *val = [value UTF8String];

int result = setxattr(filePath, attrName, val, strlen(val), 0, 0);

return (result==0);
}

- (NSString *)valueForAttribute:(NSString *)attribute
{
if (![attribute length])
{
return nil;
}

const char *attrName = [attribute UTF8String];
const char *filePath = [_path 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, 255, 0, 0);

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

// release buffer
free(buffer);

return retString;
}

@end
Something went wrong with that request. Please try again.