Monitor unlimited files/folders under Mac osx with Qt

881 views Asked by At

I am working on an application targeted to Mac OSX 10.6+ using Qt 4.7.4

I have a folder with as much as 1000 files + and some or many or even all of these files may be renamed or moved or deleted, so I want to report to my application if:

  1. File is renamed (report original and renamed filename)
  2. Folder renamed (report original and renamed folder name)
  3. File/folder is deleted (just report it as deleted)/moved (report the moved location)

PROBLEM: is the underlying system may (its MAY) only allow 256 descriptors to be monitored so at most 256 files! How can I over come this?

Note: used QFileSystemWatcher interface (it has the above stated problem)

ALSO : How to handle in case of version lower than OSX 10.5

Do mention how do i get renamed filename/foldername

1

There are 1 answers

3
Mat On

From the QFileSystemWatcher docs:

On Mac OS X 10.4 and all BSD variants, for example, an open file descriptor is required for each monitored file. Some system limits the number of open file descriptors to 256 by default. This means that addPath() and addPaths() will fail if your process tries to add more than 256 files or directories to the file system monitor. Also note that your process may have other file descriptors open in addition to the ones for files being monitored, and these other open descriptors also count in the total. Mac OS X 10.5 and up use a different backend and do not suffer from this issue.

So you should not need to worry about this at all in your case.

QFileSystemWatcher doesn't provide the information you requested in your edit. It will emit signals when one of the paths it monitors changes, but in case of a rename, you won't get the new name. It's intended more for things like file manager programs that will just update/reload their current view on receipt of such events.

If you need more information than that, you'll need to use OS specific APIs. You can look at the code Qt uses for different platforms in the Qt source. It's in src/core/io/qfilsystemwatcher_*.[h|cpp].

For Mac OS X 10.5 or greater, the underlying API used is the FSEvents API. You can read in the Technology Overview page:

The important point to take away is that the granularity of notifications is at a directory level. It tells you only that something in the directory has changed, but does not tell you what changed.

So that OS-level API doesn't provide what you want either directly.
For older versions of Mac OS X and FreeBSD, Qt uses the kqueue API, with the EVFILT_VNODE event filter. That API doesn't provide the new name of a renamed file either.

In short, either you'll need to code something yourself based on one of those APIs, find a library that does it (with guarantees that meet your needs), or you'll need to redesign your application. "Watching" a directory in a portable manner is at best very tricky, and generally race- and error-prone. If I were you, I wouldn't be too optimistic especially if your design requires that no "event" be missed.