2014-05-20 67 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

相關問題