Open Bug 992895 Opened 6 years ago Updated Last year

[OS.File] Add a function watching for a file being changed under MacOS

Categories

(Toolkit :: OS.File, defect)

defect
Not set

Tracking

()

People

(Reporter: Yoric, Unassigned)

References

(Blocks 1 open bug)

Details

Splitting bug 958280 in per-platform implementations.
for mac osx i just read about launchd being able to help. just thought i would share the article here so you can pick which ever is best.

see the section "launchd as a filesystem watcher" on this article: http://paul.annesley.cc/2012/09/mac-os-x-launchd-is-cool/
Unfortunately, there doesn't seem to be a libSystem or Core call to use launchd like this. Fortunately, MacOS X has other syscalls to monitor directories: https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/FSEvents_ProgGuide/UsingtheFSEventsFramework/UsingtheFSEventsFramework.html
(In reply to David Rajchenbach-Teller [:Yoric] (away until Febrary - use "needinfo") from comment #2)
> Unfortunately, there doesn't seem to be a libSystem or Core call to use
> launchd like this. Fortunately, MacOS X has other syscalls to monitor
> directories:
> https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/
> FSEvents_ProgGuide/UsingtheFSEventsFramework/UsingtheFSEventsFramework.html

Oh sweetness :)
Me and a friend (I dont know his online handle) were looking into starting up some FS watching via js-ctypes on the side (not high priority, we both have some extensions we want to get out asap). He mentioned kqueue but that looks like a loop. What are your thoughts about kqueue? Is there anything that can just register callbacks instead of a loop watch? It looks like the FSEventsFramework does callbacks? In a rush heading to the airport otherwise I would look into it myself. But kqueue seems easily doable so I was mainly wondering what your thoughts were about that.
Whoops took a quick look at that FSEvents link you posted and looks like it does offer callback support. So that part is answered and it looks like a easy thing to do in ctypes as well. Maybe we can do both ways (good for the community!!), Ill ask if he can take one and ill take the other.
(In reply to David Rajchenbach-Teller [:Yoric] (use "needinfo") from comment #2)
> Unfortunately, there doesn't seem to be a libSystem or Core call to use
> launchd like this. Fortunately, MacOS X has other syscalls to monitor
> directories:
> https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/
> FSEvents_ProgGuide/UsingtheFSEventsFramework/UsingtheFSEventsFramework.html

Hi Yoric this is a basic questoin probably what do you mean by libSystem or Core? I was able to use launchd like this: https://gist.github.com/Noitidart/0dc5aa735c528f796ed6
A note about FSEvents from wikipedia:

> Until Mac OS X 10.7, FSEvents did not "watch" the filesystem, such as Linux's inotify: the API provided no notifications for changes to individual files. An application was able to register to receive changes to a given directory, and had to determine for itself which file or files were changed.[3]

> Mac OS X 10.7 (Lion) added the ability to register for file modification notifications.[4]

just fyi.
So I merged kqueue into jscFileWatcher to suppoert darwin, freebsd, and openbsd

Can install and test from here: https://github.com/Noitidart/jscFileWatcher/tree/9578ff36a75f443f4ced48ddbfb8deb333253ada

I addPath OS.Constants.Path.desktopDir, so as you do file activity on the desktop it logs to console. However I'm not able to make the string be right like "deleted" "created" "renamed", it keeps giving me "contents-modified" but this is probably some super simple flag checking, i had a long day so I'm missing something probably, something in this section: https://github.com/Noitidart/jscFileWatcher/blob/9578ff36a75f443f4ced48ddbfb8deb333253ada/modules/workers/FSWPollWorker.js#L290 and https://github.com/Noitidart/jscFileWatcher/blob/9578ff36a75f443f4ced48ddbfb8deb333253ada/modules/workers/FSWPollWorker.js#L164

* Needs testing on FreeBSD and OpenBSD, I tested on OSX 10.10.1
* As soon as we can get this reading the event right, then I'll make it return promise to mainthread and call the callback, but the merge of kqueue to the Promise API is 99% done, just need to make it return to call mainthread callback, then re-call the poll
* I didn't try to break it, so debugging is needed


How it works:
* The poll actually checks every 500ms. Reason I didn't make the poll just go forever is because if addPath is called, the kevent needs to be updated and called again with the right structure. I felt this was ok as it a similar approach to WINNT addPath solution (not yet implemented)
btw i made it so it force thinks your osx version is 10.6.9 because the way p0lip and i though to do is if osx <10.7 use kqueue else use FSEventFramework (which i havent implemented thus the temp force to 10.6.9)
FSEventFramework method ready (stand alone example, not yet merged into final use api), callback needs to be fleshed out: To test, install from here and do stuff on desktop, i didnt review the flags but creation/deletion and folder access works:  https://github.com/Noitidart/jscFileWatcher/tree/for-osx10.7-FSEventFramework-work

code of interest in that commit is here: https://github.com/Noitidart/jscFileWatcher/blob/for-osx10.7-FSEventFramework-work/modules/workers/FSWatcherWorker.js#L180-240

so the callback is being triggered, i set it to check every 500ms so its just a tiny bit slow. the callback just needs to be fleshed out, like test what flags are there and doing what. then we need to merge to jscFileWatcher API
whoops apparently the lib for FSEvents is different if osx < 10.10 so fixed that here: https://github.com/Noitidart/jscFileWatcher/tree/for-osx10.7-FSEventFramework-work
Landed FSEvents to master for 10.7+ and kq for < 10.7 can install and test from here: https://github.com/Noitidart/jscFileWatcher/tree/99e816ecb49c696eb5409fef932dab04000681f2 (watches desktop)
You need to log in before you can comment on or make changes to this bug.