How to prevent writing to file in macOS FileProviderExtension

I'm implementing FileProviderExtension on macOS (NSFileProviderReplicatedExtension) and one requirement I have to support is the support for locked files.

My requirement is that writing to locked files should be forbidden. Is there a way to support that in FileProviderExtension?

My approach was to report capabilities in FileProviderItem like:

var capabilities: NSFileProviderItemCapabilities {
    if locked {
        return [.allowsReading, .allowsReparenting, .allowsDeleting]
    }
    return [.allowsReading, .allowsWriting, .allowsRenaming, .allowsReparenting, .allowsDeleting]
}

and indeed rename is forbidden (in Finder you cannot enter rename-state) but writing to file is allowed.

Is there a way to prevent writing to file in macOS FileProviderExtension?

Replies

I did have some advancements by utilizing fileSystemFlags in NSFileProviderItem:

var fileSystemFlags: NSFileProviderFileSystemFlags {
    let ret = NSFileProviderFileSystemFlags(rawValue: private_fileSystemFlags)
    if locked {
        return ret.subtracting(.userWritable)
    } else {
        return ret
    }
}

and indeed when opening ie a PNG file in Preview.app and trying to modify it, I get alert that file is locked. However, when user clicks on Unlock, modifyItem is invoked with fileSystemFlags changed, and that's fine because I can handle that callback and remove the userWriteable flag:

if changedFields.contains(.fileSystemFlags) {
    var flags = item.fileSystemFlags!
    if node.nodeLocked() {
        flags.subtract(.userWritable)
    }
    node.fileSystemFlags = Int32(flags.rawValue)
}

however, Preview.app will again offer to Unlock the file and if user clicks on Unlock then, system will just write to the file modifying the fileSystemFlag without invocation of modifyItem, as if it happens under the hood without the FileProviderExtension's control or chance to do anything about that.