How to get notified if a file is opened - swift

So if I have a directory /dir which can contain any number of files N and these files can be in any sub-directory in /dir. How can I watch /dir such that I get notified when a file in dir or any of its sub-directories is opened? I don't want to watch all files and check if a lock is acquired for that file.
I have looked at FSEvents, but I am pretty sure that I cannot do it with that.
It is for the macOS operating system
So can anyone point me in the right direction or know a solution

One method would be to write a Kernel Extension and use Apple's Kernel Authorisation (KAuth) framework.
You'd then subscribe to the File Operation scope and the KAUTH_FILEOP_OPEN action.
This would also require a user-land application to communicate with the kernel extension, to receive the notifications of file operations, so this method would depend upon your requirements for watching the files as to whether or not this is just overkill.

You could use kqueue, and based on the events that you would like to monitor you could get "notified", for example in your case you could use the EVFILT_VNODE filter:
EVFILT_VNODE Takes a file descriptor as the identifier and the
events to watch for in fflags, and returns when one
or more of the requested events occurs on the
descriptor.
To get a list of all events you could monitor check the man: https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2 (The implementation is pretty similar in all the BSD's including macOS), check this answer to see some differences: https://stackoverflow.com/a/49521218/1135424

Related

Is it possible to use the alert Finder provides when moving a file to a directory where a file with that name exists in my own app?

I'm building a mac-app for people to organise files. One of the things I allow people to do is to move or copy files to specific directories from within my app. I use FileManagers 'moveItem(at:to:)' or 'copyItem(atPath:toPath:)' to do so, which also allows me to catch errors. Based on these errors, I can of course create custom alerts for specific edge cases.
However, since Finder already provides alerts for such occasions, I was wondering if I could prompt Finders default alerts instead of having to re-create them.
I'm especially interested in the alert shown when multiple files already exist at the location the user tries to move/copy files to and Finder allows to 'Keep both', 'Stop' or 'Replace' these files. Because this alert also includes a 'Apply to all'-checkbox unusual place for a custom NSAlert.
Thanks!
You can use apple script to move file.
tell application "Finder"
move POSIX file "/Users/xyz/fileName" to POSIX file "/Users/xyz/test"
end tell
This will display finder alert.

With Chrome Apps soon to be removed, is there another way to edit a local file?

The Chrome Apps API has the very useful FileSystem API which allows a user to select a file for an app to edit (read and write changes to). However, with the entire Apps API soon to be removed, what other ways exists to edit a file on the local file system?
This is not an opinion-based question, I am asking for all conceivable alternatives.
Per https://developers.chrome.com/apps/migration:
Q: My app uses the chrome.fileSystem API to read and write user-specified files and / or directories. Can this be done on the open web?
A: In general, no. The open web can read single files that the user opens, but cannot retain access to those files, write to those files, or have any access to directories.
If it is critical for your app to read and write directories (e.g. it is a text editor with a folder view), you will need to either have a native helper app and extension combo, or create a native app.

Listening to the HTML5 file system in a Chrome Application

I am working on a Google Chrome App which reads from and writes to the sandboxed local file system.
I am accessing the file system by invoking window.webkitRequestFileSystem || window.requestFileSystem
This is a large application, and I have some code components creating and deleting files (call them the producers), and other code components displaying the files (the consumers).
For clean separation of code, I don't want the producers and consumers to know about one another. I would like the consumers to simply watch the file system, and react appropriately when files are created or modified.
Sadly, it appears that the framework provides no way to add a listener to the local file system.
Am I correct in saying that?
It looks like this is in the works and may land within the next few months. See relevant issue tracker

iPhone/iPad: Directory Change Notification

I have an application with UIFileSharingEnabled. If the device is tethered, a user can use iTunes (or other programs) to drop new files or delete existing files. I would like to detect the changes to my application's file system on the device.
Is there a 'directory change' (or similar) notification? Notification
Programming Topics does not appear to have a comprehensive list
of notifications.
I believe Galea's answer (below) would probably work, but GCD is only available in iOS 4.0 and later. Unfortunately, I'm targeting iOS 3.2.
You can use Grand Central Dispatch, in particular dispatch_source_create can be a good start. (by the way GCD is built on top of kqueue, at least for what concerns the event part)
Yes, it's a UNIX system! You can use the kqueue() feature to monitor directory changes as they happen.
Here's an example of how to use it: http://blog.julipedia.org/2004/10/example-of-kqueue.html. Or if you prefer, there's a nice Objective-C wrapper class call UKKQueue: http://www.zathras.de/angelweb/sourcecode.htm
I believe he event will be fired when the new files are created, not when the write operation is finished. So you will not be able to read the files immediately - but displaying the file in a list should be fine.
If you need to read the file (maybe to display a preview of a jpeg for example), you could just wait a few seconds after it's been copied using NSTimer.

Does updating your application delete files?

I wrote a game that I plan on updating soon. The game generates a scoreboard when the application starts, if there is no scoreboard file present.
If people update to my latest version, will the scoreboard file (that's generated by the code itself, not a file that comes preloaded in the app) be deleted?
If so, is there any way to avoid this without any coding previously required?
The updated version of your app will simply replace the existing version's bundle - any files you've written to your app's document area will remain intact.
As such, you simply need to check for the presence of the file within your app's document area as per usual and write a "new" version if none exists.
If the file is within your applications bundle, it will be deleted. Files saved with Core Data and NSUserDefaults will not. I've never personally written a file to the disk, so I don't know where the default write point is. You'll have to find this out yourself.
Happy coding,
Zane