OK: I'm implementing File Sharing in an iPhone OS app, and of course this means filesystem monitoring. Yay!
Basically, the OS copies and/or deletes from and to a directory I can access when the user manipulates files into my app's section in iTunes. Thus, I need to monitor the directory for changes presumably via an efficient mechanism like a kqueue()
.
How do I implement this so that I know that the files have finished copying? I was thinking along the lines of:
- Monitor with
kqueue()
. - At event, start (or reset existing) timeout.
- When timeout elapses, do work.
but is there a better way of doing it that ensures I'm not stepping over the OS's toes?
This is only a partial answer, but might be somewhat useful.
The kqueue mechanism indeed does work as a means to be notified when an app's Documents directory is modified.
The notification comes as soon as the directory is modified and before copying a file is complete. I have observed this in testing.
Unfortunately, there is no form of notification when the copy is done. At least, none that I have found.
What I hope Apple will realize is there needs to be a notification for "iTunes synch" is done, because this seems to be a general problem.
A loop checking if the new file's modification time is not changing might work, but only most of the time. In some extreme circumstances, iOS can pause to do all kinds of time consuming work of its own in the background.
If there is a way to check if a particular file is open by another process, then this would be enough.
Geoff
来源:https://stackoverflow.com/questions/2972563/what-is-the-optimal-way-to-monitor-changes-in-a-directory-with-a-kqueue