-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,61 @@ | |||
// | |||
// DTExtendedFileAttributes.h | |||
// DTFoundation | |||
// | |||
// Created by Oliver Drobnik on 3/6/12. | |||
// Copyright (c) 2012 Cocoanetics. All rights reserved. | |||
// | |||
|
|||
/** | |||
This class providse read/write access to extended file attributes of a file or folder. It wraps the standard xattr Posix functions to do that. | |||
Because the file system does not keep track of the data types saved in extended attributes this API so far reads and writes strings. | |||
*/ | |||
@interface DTExtendedFileAttributes : NSObject | |||
|
|||
|
|||
/** | |||
@name Creating an Extended File Attribute Manager | |||
*/ | |||
|
|||
|
|||
/** | |||
Creates an Extended File Attribute Manager. | |||
@param path The file path | |||
*/ | |||
- (id)initWithPath:(NSString *)path; | |||
|
|||
|
|||
/** | |||
@name: Reading/Writing extended attributes | |||
*/ | |||
|
|||
/** | |||
Removes an extended file attribute from the receiver. | |||
@returns `YES` if successful. | |||
*/ | |||
- (BOOL)removeAttribute:(NSString *)attribute; | |||
|
|||
|
|||
/** | |||
Sets the value of an extended file attribute for the receiver. | |||
If the value is `nil` then this is the same as calling <removeAttribute:>. | |||
@param attribute The name of the attribute. | |||
@returns `YES` if successful. | |||
*/ | |||
- (BOOL)setValue:(NSString *)value forAttribute:(NSString *)attribute; | |||
|
|||
|
|||
/** | |||
Gets the value of an extended file attribute from the receiver. | |||
@param attribute The name of the attribute. | |||
@returns The string for the value or `nil` if the value is not set. | |||
*/ | |||
- (NSString *)valueForAttribute:(NSString *)attribute; | |||
|
|||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters