Frage

I'm working on a file monitoring daemon written in PHP, using the inotify PECL extension. I've nearly got it finished, as in it tells me when an inotify event happens.

The return value of inotify_read($fd) is an array that looks like this:

Array
(
    [0] => Array
        (
            [wd] => 2
            [mask] => 1073741840
            [cookie] => 0
            [name] => collaphoto
        )

    [1] => Array
        (
            [wd] => 2
            [mask] => 1073741856
            [cookie] => 0
            [name] => filewatcher
        )

    [2] => Array
        (
            [wd] => 2
            [mask] => 1073741840
            [cookie] => 0
            [name] => filewatcher
        )

)

If I understand it correctly, each sub array is an individual event, with information about that event. wd is the descriptor of the inotify instance, mask is the integer value of the flags that triggered the event, eg IN_ATTRIB or IN_ACCESS, cookie is a unique ID to connect this event to another event in the queue, and name is the directory or file that was changed. The name is only given if a directory is being watched by inotify.

My question is how do I figure out what Bit Mask triggered the event based on the mask value given? I'm still fairly inexperienced with dealing with Bit Masks, so go easy on me.

Full List of inotify Flags

War es hilfreich?

Lösung

It's really not the most elegant solution I could think of, because we're talking about an event that could occur quite often and be difficult to come up with a decent strategy for dealing with constant model changes, observing filesystem changes that is. Here is my sort of brute force method for dealing with such a thing.

So we have a master list of constants (which underneath all have a value of course), and we have numerical values coming back from the filesystem that need to match up with one of the constants.

What do? Well, stringify the numeric values to create an associative array where the numeric values are the keys and the named constants are the values. This array is just a place holder of sorts and not meant to be modified, so make try to keep it as constant as possible. You can't declare an array as constant, but there are some hacks out there to do so. I'd also recommend making it static so that only one copy exists, and see if you can figure out a way to maximize seek times in the structure. That may be based on how much your filesystem changes and how many lookups you need to perform on the array, but you should be safe so long as PHP does a good job with its hash tables and keeps associative arrays at O(1) time.

NOTE: Just in case you run into memory issues with your keys being the way they are: The scoop on PHP array sizes

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top