Search code examples
objective-cmacoscocoansfilemanager

How to check whether a file is locked in Cocoa?


Is there any API to check whether a file is a locked? I am not able to find any API in the NSFileManager class.Let me know if there is any API to check the lock of the file.

I found the following link related to file lock

http://lists.apple.com/archives/cocoa-dev/2006/Nov/msg01399.html

I can invoke – isWritableFileAtPath: on file. Is there any other way to find whether a file is locked?


Solution

  • Following Code worked for me.

    NSError * error;
    NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
    BOOL isLocked = [[attributes objectForKey:NSFileImmutable] boolValue];
                
    if (isLocked) {
        NSLog(@"File is locked");
    }