Class: Sinotify::PrimNotifier

Inherits:
Object
  • Object
show all
Defined in:
ext/src/sinotify.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject



67
68
69
70
71
72
73
74
75
76
# File 'ext/src/sinotify.c', line 67

static VALUE rb_inotify_new(VALUE klass) {
	int *fd;
	VALUE retval;
	fd = malloc(sizeof(int));
	*fd = inotify_init();
	if(*fd < 0) rb_sys_fail("inotify_init()");
	retval = Data_Wrap_Struct(klass, NULL, free, fd);
	rb_obj_call_init(retval, 0, NULL);
	return retval;
}

Instance Method Details

#add_watch(filename, mask) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'ext/src/sinotify.c', line 78

static VALUE rb_inotify_add_watch(VALUE self, VALUE filename, VALUE mask) {
	int *fd, wd;
	Data_Get_Struct(self, int, fd);
	wd = inotify_add_watch(*fd, RSTRING_PTR(filename), NUM2INT(mask));
	if(wd < 0) {
	  rb_sys_fail(RSTRING_PTR(filename));
	}
	return INT2NUM(wd);
}

#closeObject



125
126
127
128
129
130
131
132
# File 'ext/src/sinotify.c', line 125

static VALUE rb_inotify_close(VALUE self) {
	int *fd;
	Data_Get_Struct(self, int, fd);
	if(close(*fd) != 0) {
		rb_sys_fail("closing inotify");
	}
	return Qnil;
}

#each_eventObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/src/sinotify.c', line 97

static VALUE rb_inotify_each_event(VALUE self) {
	int *fd, r;
	struct inotify_event *event, *pevent;
	char buffer[16384];
	size_t buffer_n, event_size;

	Data_Get_Struct(self, int, fd);
	while(1) {
		r = event_check(*fd);
		if(r == 0) {
			continue;
		}
		if((r = read(*fd, buffer, 16384)) < 0) {
			rb_sys_fail("reading event");
		}
		buffer_n = 0;
		while (buffer_n < r) {
			pevent = (struct inotify_event *)&buffer[buffer_n];
			event_size = sizeof(struct inotify_event) + pevent->len;
			event = malloc(event_size);
			memmove(event, pevent, event_size);
			buffer_n += event_size;
			rb_yield(rb_inotify_event_new(event));
		}
	}
	return Qnil;
}

#rm_watch(wdnum) ⇒ Object



88
89
90
91
92
93
94
95
# File 'ext/src/sinotify.c', line 88

static VALUE rb_inotify_rm_watch(VALUE self, VALUE wdnum) {
	int *fd;
	Data_Get_Struct(self, int, fd);
	if(inotify_rm_watch(*fd, NUM2INT(wdnum)) < 0) {
		rb_sys_fail("removing watch");
	}
	return Qtrue;
}