5
我正在使用NSFileManager setAttributes来更改目录的权限。我的问题是,它似乎不递归。有什么办法可以强制它这么做吗?如何使用NSFileManager递归地更改目录的权限setAttributes
我正在使用NSFileManager setAttributes来更改目录的权限。我的问题是,它似乎不递归。有什么办法可以强制它这么做吗?如何使用NSFileManager递归地更改目录的权限setAttributes
我不认为有一个内置的方法来做到这一点,但它不应该是很难做这样的事情:
NSString *path = @"/The/root/directory";
NSDictionary *attributes; // Assume that this is already setup
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
NSArray *subPaths = [fileManager subpathsAtPath:path];
for (NSString *aPath in subPaths) {
BOOL isDirectory;
[fileManager fileExistsAtPath:aPath isDirectory:&isDirectory];
if (isDirectory) {
// Change the permissions on the directory here
NSError *error = nil;
[fileManager setAttributes:attributes ofItemAtPath:aPath error:error];
if (error) {
// Handle the error
}
}
}
这是未经测试,但应该给你一个起点。
NSString *path = @"/User/user/aPath";
NSFileManager *manager = [[[NSFileManager alloc] init] autorelease];
if ([manager fileExistsAtPath: path]) {
NSDictionary *attrib = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0], NSFileGroupOwnerAccountID,
[NSNumber numberWithInt:0], NSFileOwnerAccountID,
@"root", NSFileGroupOwnerAccountName,
@"root", NSFileOwnerAccountName, nil ];
NSError *error = nil;
[manager setAttributes:attrib ofItemAtPath:path error:&error];
NSDirectoryEnumerator *dirEnum = [manager enumeratorAtPath: path];
NSString *file;
while (file = [dirEnum nextObject]) {
[manager setAttributes:attrib ofItemAtPath:[path stringByAppendingPathComponent:file] error:&error];
}
}