0

I'm trying to watch for size change on directory using kqueue, is this possible? The reason for this because I am watching directories and whenever an event triggers, I stat the directory and compare last mod times etc to figure out if contents-modifed, added, removed, or renamed events happend. My goal is to get an even to trigger on directory when contents-modified happens on a file inside the directory, I couldn't accomplish that so we had an idea, we want to detect size change on directory, as if a contents-modified happend on a file within then the size of directory will change. Is this possible?

Thanks

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 2
    The "size" of a directory is just the size of the directory file. That is, the simple mapping of file names to inodes. It is **not** the cumulative size of everything within the directory hierarchy. The size of a directory changes in a subset of the situations where `kqueue` would detect modifications to the directory. You don't gain anything by monitoring the size of a directory. There is no way to monitor the cumulative size of everything within a directory hierarchy using `kqueue`. – Ken Thomases May 02 '15 at 23:01

1 Answers1

1

You don't want/need to stat() the directory. You need to read the list of files in the directory each time kqueue says the directory was modified, and compare it to the list as it was the last time you read it. Only then will you know if a new file has appeared, or if a file has been removed, or if a file has been renamed (you will also need to keep track of the inode numbers for each file in the list to detect renames).

If you want to further monitor for changes to each file then you also need to add events for each file in the directory and update this list of events each time the event for the directory file is signalled.

FYI: This command-line utility does what you want, and can be built to use kqueue: https://github.com/emcrisostomo/fswatch

Greg A. Woods
  • 2,663
  • 29
  • 26
  • Thanks Greg, yep recognizing remove/add/renamed is not a problem. Im trying to catch that contents-modified of files within. I think your second paragraph is the only way :) – Noitidart May 07 '15 at 00:19
  • After so long I verify that I couldn't get contents-modified events on immediate child files of a directory I was watching. I had to setup a watch directly on that file. Sucks :( – Noitidart Jul 14 '16 at 20:49