2014-05-20 65 views
2

我需要修改我的文件属性。有一个属性NSFileGroupOwnerAccountID哪个苹果说它可以被修改,但是当我修改它时,它没有得到更新。 我也尝试先将权限修改为0777,但仍然无效。修改NSFileManager中文件的NSFileGroupOwnerAccountID属性没有得到更新

代码:

NSFileManager *filemgr; 
    NSDictionary *attribs; 
    NSArray *dirPaths; 
    NSString *docsDir; 
    filemgr =[NSFileManager defaultManager]; 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    docsDir = [dirPaths objectAtIndex:0]; 
    NSArray * filePathsArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: docsDir error:NULL]; 
    NSError *error; 
    for(int i=0;i<[filePathsArray count];i++) 
    { 
     NSString *filePath = [filePathsArray objectAtIndex:i]; 
     NSString *fullPath = [docsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",filePath]]; 
     attribs = [filemgr attributesOfItemAtPath: 
        docsDir error: NULL]; 
     NSLog (@"POSIX Permissions %@", [attribs objectForKey: NSFilePosixPermissions]); 
     NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:attribs]; 
     [attributes setValue:[NSNumber numberWithShort:0777] 
         forKey:NSFilePosixPermissions]; // chmod permissions 777 
     [[NSFileManager defaultManager]setAttributes:attributes ofItemAtPath:docsDir error:&error]; 
     NSLog(@"updated: %@",[filemgr attributesOfItemAtPath: 
        docsDir error: NULL]); 
     NSDictionary *dict2 = [filemgr attributesOfItemAtPath: 
              docsDir error: NULL]; 
     NSMutableDictionary *attributes2 = [NSMutableDictionary dictionaryWithDictionary:dict2]; 
     [attributes2 setValue:[NSNumber numberWithUnsignedLong:12345]forKey:NSFileGroupOwnerAccountID]; 
     [[NSFileManager defaultManager]setAttributes:attributes2 ofItemAtPath:docsDir error:&error]; 
     NSLog(@"updated2: %@",[filemgr attributesOfItemAtPath: 
           docsDir error: NULL]); 
    } 

URL

更新2:

修改仅单个属性:

NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; 
     [attributes setValue:[NSNumber numberWithUnsignedLong:12345]forKey:NSFileGroupOwnerAccountID]; 

    BOOL check= [[NSFileManager defaultManager]setAttributes:attributes ofItemAtPath:docsDir error:&error]; 

仍然提示错误:

Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x8d0b900 {NSFilePath=/Users/a/Library/Application Support/iPhone Simulator/7.1/Applications/81ADKJ1A2-B612-4784-9524-05456F323212/Documents, NSUnderlyingError=0x8d11b80 "The operation couldn’t be completed. Operation not permitted"} 

请建议如何修改属性。

在此先感谢。

回答

1

除非您作为超级用户运行,否则您只能更改您拥有的文件组。此外,您只能将该组更改为您所属的组。

您不应该使用完整的文件的当前属性字典。构建一个只有你想改变的属性的字典。否则,系统可能会认为你正试图“改变”你不属于的属性,并且因为它不能完成你(显然)所要求的所有事情而失败。

您应该检查-setAttributes:ofItemAtPath:error:方法的返回值以查看它是否失败。如果是这样,您需要检查它提供的错误对象以查看原因。


更新:顺便说一下,您总是可以下载到POSIX API以进行这些类型的文件操作。有时,Cocoa并不是低层次操作的最佳API,并且更接近金属可以更清晰。

int result; 
do 
{ 
    result = chown([docsDir fileSystemRepresentation], -1, 12345); 
} 
while (result != 0 && errno == EINTR); 
if (result != 0) 
    /* handle error; examine errno to learn failure reason */; 

(在这种情况下,虽然,由您记录的错误判断,我敢肯定,errnoEPERM(1)。)

+0

请参阅编辑问题。我试图做你的建议,但它仍然给出了同样的错误。我也添加了错误。请帮忙。 – Gypsa

+0

你是否拥有该文件?您的用户帐户是否为12345组的成员? –

+0

是的我已经使用代码创建了文件:NSArray * array = [NSArray arrayWithObjects:@“One”, @“Two”, @“Three”, @“Four”,nil]; NSString * filePath = [docsDir stringByAppendingFormat:@“/ myfile.plist”]; [array writeToFile:filePath atomically:YES]; NSLog(@“文件存储在%@”,filePath);我想将自定义值设置为任何属性,因此我尝试将NSFileGroupOwnerAccountID值修改为我的随机值12345.它与任何组都没有关系。如果我做错了,请提出建议。 – Gypsa

相关问题