5

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?

Ram
  • 1,872
  • 5
  • 31
  • 54
  • What sort of locking? POSIX locking or the higher level one (that I don't know the name of)? – trojanfoe Jul 31 '12 at 10:17
  • @trojanfoe: I am not really sure about the locking.May be it is POSIX. I locked the file using finder. I would like to check the locked files in my application. – Ram Jul 31 '12 at 10:24

3 Answers3

13

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");
}
Alexander
  • 59,041
  • 12
  • 98
  • 151
Ram
  • 1,872
  • 5
  • 31
  • 54
  • There's a [`NSFileImmutable`](https://developer.apple.com/documentation/foundation/nsfileimmutable?language=objc) constant that you can use rather than hard-coding the string, I'll edit that in. – Alexander Aug 16 '20 at 13:53
3

I don't really know the answer to this question as I don't know how OS X implements its locking mechanism.

It might use the POSIX advisory locking as documented in the flock() manpage and if I were you I would write a 10 31-line test program in C to show what fcntl() (manpage) thinks about the advisory lock you have made from within Finder.

Something like (untested):

#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, const char **argv)
{
    for (int i = 1; i < argc; i++)
    {
        const char *filename = argv[i];
        int fd = open(filename, O_RDONLY);
        if (fd >= 0)
        {
            struct flock flock;
            if (fcntl(fd, F_GETLK, &flock) < 0)
            {
                fprintf(stderr, "Failed to get lock info for '%s': %s\n", filename, strerror(errno));
            }
            else
            {
                // Possibly print out other members of flock as well...
                printf("l_type=%d\n", (int)flock.l_type);
            }
            close(fd);
        }
        else
        {
            fprintf(stderr, "Failed to open '%s': %s\n", filename, strerror(errno));
        }
    }
    return 0;
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • The OS X file lock/immutable is a file flag (see my answer) – codingFriend1 Oct 29 '12 at 10:12
  • Regarding trojanfoe's untested code, it has now been tested :) and found to need one more line. Before calling fcntl, you need to tell it what kind of lock you are looking for. Typically, use flock.l_type = F_WRLCK; in which F_WRLCK says to look for either Read or Write (aka Exclusive) lock. – Jerry Krinock Jan 09 '15 at 16:47
3

If necessary, the immutable flag (OS X 'file locked') can also be determined with POSIX C functions. The immutable property is not a lock in unix terms but a file flag. It can be obtained with the stat function:

struct stat buf;
stat("my/file/path", &buf);
if (0 != (buf.st_flags & UF_IMMUTABLE)) {
     //is immutable
}

For reference see: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/stat.2.html

The immutable flag can be set with the chflags function:

chflags("my/file/path", UF_IMMUTABLE);

For reference see: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/chflags.2.html

codingFriend1
  • 6,487
  • 6
  • 45
  • 67