5

ファイルがロックされているかどうかを確認するためのAPIはありますか?クラスにAPIがNSFileManager見つかりません。ファイルのロックをチェックするAPIがあるかどうか教えてください。

ファイルロックに関連する次のリンクを見つけました

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

呼び出すことができます– isWritableFileAtPath:ファイル上。ファイルがロックされているかどうかを確認する他の方法はありますか?

4

3 に答える 3

12

次のコードは私のために働いた。

NSError * error;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
BOOL isLocked = [[attributes objectForKey:NSFileImmutable] boolValue];
            
if (isLocked) {
    NSLog(@"File is locked");
}
于 2012-07-31T12:17:11.043 に答える
3

OS Xがロックメカニズムをどのように実装しているかわからないので、この質問に対する答えは本当にわかりません。

マンページに記載されているPOSIXアドバイザリロックを使用する場合があります。もし私があなたなら、Finder内から作成したアドバイザリロックについて(マンページ)がどう思うかを示すために、Cで1031行のテストプログラムを作成します。flock() fcntl()

(未テスト)のようなもの:

#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;
}
于 2012-07-31T10:39:52.840 に答える
3

必要に応じて、POSIX C関数を使用して不変フラグ(OS X'ファイルロック')を判別することもできます。不変のプロパティは、UNIX用語ではロックではなく、ファイルフラグです。stat次の関数で取得できます。

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

参考までに、 https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/stat.2.htmlを参照してください。

chflags不変フラグは、次の関数で設定できます。

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

参考までに、 https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/chflags.2.htmlを参照してください。

于 2012-10-29T10:10:45.990 に答える