Merge pull request #2967 from huangboju/master

Simplify the xattr helper method's code with modern Objective-C syntax
This commit is contained in:
DreamPiggy 2020-04-02 11:19:10 +08:00 committed by GitHub
commit 5d45bb9301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 39 deletions

View File

@ -17,15 +17,15 @@
ssize_t nameBuffLen = listxattr([path fileSystemRepresentation], NULL, 0, flags); ssize_t nameBuffLen = listxattr([path fileSystemRepresentation], NULL, 0, flags);
if (nameBuffLen == -1) { if (nameBuffLen == -1) {
if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo: if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:
[NSDictionary dictionaryWithObjectsAndKeys: @{
[NSString stringWithUTF8String:strerror(errno)], @"error", @"error": [NSString stringWithUTF8String:strerror(errno)],
@"listxattr", @"function", @"function": @"listxattr",
path, @":path", @":path": path,
[NSNumber numberWithBool:follow], @":traverseLink", @":traverseLink": @(follow)
nil] }
]; ];
return nil; return nil;
} else if (nameBuffLen == 0) return [NSArray array]; } else if (nameBuffLen == 0) return @[];
// get name list // get name list
NSMutableData *nameBuff = [NSMutableData dataWithLength:nameBuffLen]; NSMutableData *nameBuff = [NSMutableData dataWithLength:nameBuffLen];
@ -36,7 +36,7 @@
char *nextName, *endOfNames = [nameBuff mutableBytes] + nameBuffLen; char *nextName, *endOfNames = [nameBuff mutableBytes] + nameBuffLen;
for(nextName = [nameBuff mutableBytes]; nextName < endOfNames; nextName += 1+strlen(nextName)) for(nextName = [nameBuff mutableBytes]; nextName < endOfNames; nextName += 1+strlen(nextName))
[names addObject:[NSString stringWithUTF8String:nextName]]; [names addObject:[NSString stringWithUTF8String:nextName]];
return [NSArray arrayWithArray:names]; return names.copy;
} }
+ (BOOL)hasExtendedAttribute:(NSString *)name atPath:(NSString *)path traverseLink:(BOOL)follow error:(NSError **)err { + (BOOL)hasExtendedAttribute:(NSString *)name atPath:(NSString *)path traverseLink:(BOOL)follow error:(NSError **)err {
@ -46,12 +46,12 @@
ssize_t nameBuffLen = listxattr([path fileSystemRepresentation], NULL, 0, flags); ssize_t nameBuffLen = listxattr([path fileSystemRepresentation], NULL, 0, flags);
if (nameBuffLen == -1) { if (nameBuffLen == -1) {
if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo: if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:
[NSDictionary dictionaryWithObjectsAndKeys: @{
[NSString stringWithUTF8String:strerror(errno)], @"error", @"error": [NSString stringWithUTF8String:strerror(errno)],
@"listxattr", @"function", @"function": @"listxattr",
path, @":path", @":path": path,
[NSNumber numberWithBool:follow], @":traverseLink", @":traverseLink": @(follow)
nil] }
]; ];
return NO; return NO;
} else if (nameBuffLen == 0) return NO; } else if (nameBuffLen == 0) return NO;
@ -73,13 +73,13 @@
ssize_t attrLen = getxattr([path fileSystemRepresentation], [name UTF8String], NULL, 0, 0, flags); ssize_t attrLen = getxattr([path fileSystemRepresentation], [name UTF8String], NULL, 0, 0, flags);
if (attrLen == -1) { if (attrLen == -1) {
if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo: if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:
[NSDictionary dictionaryWithObjectsAndKeys: @{
[NSString stringWithUTF8String:strerror(errno)], @"error", @"error": [NSString stringWithUTF8String:strerror(errno)],
@"getxattr", @"function", @"function": @"getxattr",
name, @":name", @":name": name,
path, @":path", @":path": path,
[NSNumber numberWithBool:follow], @":traverseLink", @":traverseLink": @(follow)
nil] }
]; ];
return nil; return nil;
} }
@ -95,15 +95,15 @@
if (0 == setxattr([path fileSystemRepresentation], [name UTF8String], [value bytes], [value length], 0, flags)) return YES; if (0 == setxattr([path fileSystemRepresentation], [name UTF8String], [value bytes], [value length], 0, flags)) return YES;
// error // error
if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo: if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:
[NSDictionary dictionaryWithObjectsAndKeys: @{
[NSString stringWithUTF8String:strerror(errno)], @"error", @"error": [NSString stringWithUTF8String:strerror(errno)],
@"setxattr", @"function", @"function": @"setxattr",
name, @":name", @":name": name,
[NSNumber numberWithUnsignedInteger:[value length]], @":value.length", @":value.length": @(value.length),
path, @":path", @":path": path,
[NSNumber numberWithBool:follow], @":traverseLink", @":traverseLink": @(follow),
[NSNumber numberWithBool:overwrite], @":overwrite", @":overwrite": @(overwrite)
nil] }
]; ];
return NO; return NO;
} }
@ -113,13 +113,13 @@
if (0 == removexattr([path fileSystemRepresentation], [name UTF8String], flags)) return YES; if (0 == removexattr([path fileSystemRepresentation], [name UTF8String], flags)) return YES;
// error // error
if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo: if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:
[NSDictionary dictionaryWithObjectsAndKeys: @{
[NSString stringWithUTF8String:strerror(errno)], @"error", @"error": [NSString stringWithUTF8String:strerror(errno)],
@"removexattr", @"function", @"function": @"removexattr",
name, @":name", @":name": name,
path, @":path", @":path": path,
[NSNumber numberWithBool:follow], @":traverseLink", @":traverseLink": @(follow)
nil] }
]; ];
return NO; return NO;
} }