Skip to content

Commit

Permalink
Change conversion routines to use kqueue watch flags as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
wulf7 committed Jan 29, 2015
1 parent a242780 commit 3f71d2e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 37 deletions.
30 changes: 14 additions & 16 deletions conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,22 @@
* Convert the inotify watch mask to the kqueue event filter flags.
*
* @param[in] flags An inotify watch mask.
* @param[in] is_directory 1 for directories, 0 for files.
* @param[in] is_subwatch 1 for subwatches, 0 for user watches.
* @param[in] wf A kqueue watch internal flags.
* @return Converted kqueue event filter flags.
**/
uint32_t
inotify_to_kqueue (uint32_t flags, int is_directory, int is_subwatch)
inotify_to_kqueue (uint32_t flags, watch_flags_t wf)
{
uint32_t result = 0;

if (flags & IN_ATTRIB)
result |= NOTE_ATTRIB;
if (flags & IN_MODIFY && is_directory == 0)
if (flags & IN_MODIFY && !(wf & WF_ISDIR))
result |= NOTE_WRITE;
if (is_subwatch == 0) {
if (is_directory)
if (!(wf & WF_ISSUBWATCH)) {
if (wf & WF_ISDIR)
result |= NOTE_WRITE;
if (flags & IN_ATTRIB && is_directory == 0)
if (flags & IN_ATTRIB && !(wf & WF_ISDIR))
result |= NOTE_LINK;
if (flags & IN_MOVE_SELF)
result |= NOTE_RENAME;
Expand All @@ -60,36 +59,35 @@ inotify_to_kqueue (uint32_t flags, int is_directory, int is_subwatch)
* Convert the kqueue event filter flags to the inotify watch mask.
*
* @param[in] flags A kqueue filter flags.
* @param[in] is_directory 1 for directories, 0 for files.
* @param[in] is_subwatch 1 for subwatches, 0 for user watches.
* @param[in] wf A kqueue watch internal flags.
* @return Converted inotify watch mask.
**/
uint32_t
kqueue_to_inotify (uint32_t flags, int is_directory, int is_subwatch)
kqueue_to_inotify (uint32_t flags, watch_flags_t wf)
{
uint32_t result = 0;

if (flags & NOTE_ATTRIB)
result |= IN_ATTRIB;

if (flags & NOTE_LINK && is_directory == 0 && is_subwatch == 0)
if (flags & NOTE_LINK && !(wf & WF_ISDIR) && !(wf & WF_ISSUBWATCH))
result |= IN_ATTRIB;

if (flags & NOTE_WRITE && is_directory == 0)
if (flags & NOTE_WRITE && !(wf & WF_ISDIR))
result |= IN_MODIFY;

if (flags & NOTE_DELETE && is_subwatch == 0)
if (flags & NOTE_DELETE && !(wf & WF_ISSUBWATCH))
result |= IN_DELETE_SELF;

if (flags & NOTE_RENAME && is_subwatch == 0)
if (flags & NOTE_RENAME && !(wf & WF_ISSUBWATCH))
result |= IN_MOVE_SELF;

if (flags & NOTE_REVOKE && is_subwatch == 0)
if (flags & NOTE_REVOKE && !(wf & WF_ISSUBWATCH))
result |= IN_UNMOUNT;

/* IN_ISDIR flag for subwatches is set in the enqueue_event routine */
if ((result & (IN_ATTRIB | IN_OPEN | IN_ACCESS | IN_CLOSE))
&& is_directory && is_subwatch == 0) {
&& wf & WF_ISDIR && !(wf & WF_ISSUBWATCH)) {
result |= IN_ISDIR;
}

Expand Down
5 changes: 3 additions & 2 deletions conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
#define __CONVERSIONS_H__

#include <stdint.h>
#include "watch.h"

uint32_t inotify_to_kqueue (uint32_t flags, int is_directory, int is_subwatch);
uint32_t kqueue_to_inotify (uint32_t flags, int is_directory, int is_subwatch);
uint32_t inotify_to_kqueue (uint32_t flags, watch_flags_t wf);
uint32_t kqueue_to_inotify (uint32_t flags, watch_flags_t wf);

#endif /* __CONVERSIONS_H__ */
11 changes: 5 additions & 6 deletions inotify-watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ iwatch_add_subwatch (i_watch *iw, dep_item *di)
}

/* Don`t open a watches with empty kqueue filter flags */
if (!S_ISUNK (di->type)
&& inotify_to_kqueue (iw->flags, S_ISDIR (di->type), 1) == 0) {
watch_flags_t wf = (S_ISDIR (di->type) ? WF_ISDIR : 0) | WF_ISSUBWATCH;
if (!S_ISUNK (di->type) && inotify_to_kqueue (iw->flags, wf) == 0) {
return NULL;
}

Expand Down Expand Up @@ -267,7 +267,8 @@ iwatch_add_subwatch (i_watch *iw, dep_item *di)
}

/* Skip watches with empty kqueue filter flags */
if (inotify_to_kqueue (iw->flags, S_ISDIR (di->type), 1) == 0) {
wf = (S_ISDIR (di->type) ? WF_ISDIR : 0) | WF_ISSUBWATCH;
if (inotify_to_kqueue (iw->flags, wf) == 0) {
close (fd);
return NULL;
}
Expand Down Expand Up @@ -339,9 +340,7 @@ iwatch_update_flags (i_watch *iw, uint32_t flags)
watch *w, *tmpw;
/* update kwatches or close those we dont need to watch */
RB_FOREACH_SAFE (w, watch_tree, &iw->watches, tmpw) {
uint32_t fflags = inotify_to_kqueue (flags,
w->flags & WF_ISDIR,
w->flags & WF_ISSUBWATCH);
uint32_t fflags = inotify_to_kqueue (flags, w->flags);
if (fflags == 0) {
RB_REMOVE (watch_tree, &iw->watches, w);
watch_free (w);
Expand Down
4 changes: 1 addition & 3 deletions watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ watch_init (i_watch *iw, watch_type_t watch_type, int fd, struct stat *st)
* differs from readdir`s one at mount points. */
w->inode = st->st_ino;

uint32_t fflags = inotify_to_kqueue (iw->flags,
w->flags & WF_ISDIR,
w->flags & WF_ISSUBWATCH);
uint32_t fflags = inotify_to_kqueue (iw->flags, w->flags);
if (watch_register_event (w, fflags) == -1) {
free (w);
return NULL;
Expand Down
5 changes: 3 additions & 2 deletions watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@

#include <stdint.h> /* uint32_t */
#include <dirent.h> /* ino_t */
#include "compat.h" /* RB tree macroses */

#include <sys/types.h>
#include <sys/stat.h> /* stat */

typedef struct watch watch;
typedef RB_HEAD(watch_tree, watch) watch_tree;
typedef uint32_t watch_flags_t;

#include "compat.h" /* RB tree macroses */
#include "inotify-watch.h"

#define WF_ISSUBWATCH (1<<0) /* a type of watch */
Expand All @@ -46,7 +47,7 @@ typedef enum watch_type {

struct watch {
i_watch *iw; /* A pointer to parent inotify watch */
uint32_t flags; /* A watch flags. Not in inotify/kqueue format */
watch_flags_t flags; /* A watch flags. Not in inotify/kqueue format */
size_t refcount; /* number of dependency list items corresponding
* to that watch */
int fd; /* file descriptor of a watched entry */
Expand Down
10 changes: 2 additions & 8 deletions worker-thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,13 @@ produce_notifications (worker *wrk, struct kevent *event)
produce_directory_diff (iw, event);
}

enqueue_event (iw,
kqueue_to_inotify (flags,
w->flags & WF_ISDIR,
w->flags & WF_ISSUBWATCH),
NULL);
enqueue_event (iw, kqueue_to_inotify (flags, w->flags), NULL);

if (flags & (NOTE_DELETE | NOTE_REVOKE)) {
iw->is_closed = 1;
}
} else {
uint32_t i_flags = kqueue_to_inotify (flags,
w->flags & WF_ISDIR,
w->flags & WF_ISSUBWATCH);
uint32_t i_flags = kqueue_to_inotify (flags, w->flags);
dep_node *iter = NULL;
SLIST_FOREACH (iter, &iw->deps->head, next) {
dep_item *di = iter->item;
Expand Down

0 comments on commit 3f71d2e

Please sign in to comment.