Debian Codegraph @

vendor/ocaml-inotify/src/inotify_stubs.c

raw
1 /*
2 * Copyright (C) 2006-2008 Vincent Hanquez <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; version 2.1 only. with the special
7 * exception on linking described in file LICENSE.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * Inotify Ocaml binding - C glue
15 */
16
17 #include <errno.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <limits.h>
21 #include <caml/mlvalues.h>
22 #include <caml/memory.h>
23 #include <caml/alloc.h>
24 #include <caml/custom.h>
25 #include <caml/fail.h>
26 #include <caml/signals.h>
27 #include <caml/callback.h>
28 #include <caml/unixsupport.h>
29
30 #ifdef __linux__
31
32 #include <unistd.h>
33 #include <features.h>
34 #include <sys/inotify.h>
35
36 static int inotify_flag_table[] = {
37 IN_ACCESS, IN_ATTRIB, IN_CLOSE_WRITE, IN_CLOSE_NOWRITE,
38 IN_CREATE, IN_DELETE, IN_DELETE_SELF, IN_MODIFY,
39 IN_MOVE_SELF, IN_MOVED_FROM, IN_MOVED_TO, IN_OPEN,
40 IN_DONT_FOLLOW, IN_MASK_ADD, IN_ONESHOT, IN_ONLYDIR,
41 IN_MOVE, IN_CLOSE, IN_ALL_EVENTS, 0
42 };
43
44 static int inotify_return_table[] = {
45 IN_ACCESS, IN_ATTRIB, IN_CLOSE_WRITE, IN_CLOSE_NOWRITE,
46 IN_CREATE, IN_DELETE, IN_DELETE_SELF, IN_MODIFY,
47 IN_MOVE_SELF, IN_MOVED_FROM, IN_MOVED_TO, IN_OPEN,
48 IN_IGNORED, IN_ISDIR, IN_Q_OVERFLOW, IN_UNMOUNT, 0
49 };
50
51 value caml_inotify_is_supported (value unit) {
52 CAMLparam1(unit);
53
54 CAMLreturn(Val_int(1));
55 }
56
57 value caml_inotify_init(value unit) {
58 CAMLparam1(unit);
59
60 int fd = inotify_init1(IN_CLOEXEC);
61 if (fd == -1) uerror("inotify_init", Nothing);
62
63 CAMLreturn(Val_int(fd));
64 }
65
66 value caml_inotify_add_watch(value fd, value path, value selector_flags) {
67 CAMLparam3(fd, path, selector_flags);
68
69 int selector = caml_convert_flag_list(selector_flags, inotify_flag_table);
70
71 int watch = inotify_add_watch(Int_val(fd), String_val(path), selector);
72 if (watch == -1) uerror("inotify_add_watch", path);
73
74 CAMLreturn(Val_int(watch));
75 }
76
77 value caml_inotify_rm_watch(value fd, value watch) {
78 CAMLparam2(fd, watch);
79
80 int ret = inotify_rm_watch(Int_val(fd), Int_val(watch));
81 if (ret == -1) uerror("inotify_rm_watch", Nothing);
82
83 CAMLreturn(Val_unit);
84 }
85
86 value caml_inotify_struct_size(void) {
87 CAMLparam0();
88 CAMLreturn(Val_int(sizeof(struct inotify_event)));
89 }
90
91 value caml_inotify_name_max(void) {
92 CAMLparam0();
93 CAMLreturn(Val_int(NAME_MAX));
94 }
95
96 value caml_inotify_convert(value buf) {
97 CAMLparam1(buf);
98 CAMLlocal3(event, list, next);
99
100 list = next = Val_emptylist;
101
102 struct inotify_event ievent;
103 memcpy(&ievent, String_val(buf), sizeof(struct inotify_event));
104
105 int flag;
106 for (flag = 0; inotify_return_table[flag]; flag++) {
107 if (!(ievent.mask & inotify_return_table[flag]))
108 continue;
109
110 next = caml_alloc_small(2, Tag_cons);
111 Field(next, 0) = Val_int(flag);
112 Field(next, 1) = list;
113 list = next;
114 }
115
116 event = caml_alloc_tuple(4);
117 Store_field(event, 0, Val_int(ievent.wd));
118 Store_field(event, 1, list);
119 Store_field(event, 2, caml_copy_int32(ievent.cookie));
120 Store_field(event, 3, Val_int(ievent.len));
121
122 CAMLreturn(event);
123 }
124
125 #else
126
127 value caml_inotify_is_supported (value unit) {
128 CAMLparam1(unit);
129
130 CAMLreturn(Val_int(0));
131 }
132
133 value caml_inotify_init(value unit) {
134 CAMLparam1(unit);
135
136 unix_error(ENOTSUP, "inotify_init", Nothing);
137 }
138
139 value caml_inotify_add_watch(value fd, value path, value selector_flags) {
140 CAMLparam3(fd, path, selector_flags);
141
142 unix_error(ENOTSUP, "inotify_add_watch", Nothing);
143 }
144
145 value caml_inotify_rm_watch(value fd, value watch) {
146 CAMLparam2(fd, watch);
147
148 unix_error(ENOTSUP, "inotify_rm_watch", Nothing);
149 }
150
151 value caml_inotify_struct_size(value unit) {
152 CAMLparam1(unit);
153 unix_error(ENOTSUP, "inotify_struct_size", Nothing);
154 }
155
156 value caml_inotify_name_max(value unit) {
157 CAMLparam1(unit);
158 unix_error(ENOTSUP, "inotify_name_max", Nothing);
159 }
160
161 value caml_inotify_convert(value buf) {
162 CAMLparam1(buf);
163
164 unix_error(ENOTSUP, "inotify_convert", Nothing);
165 }
166
167 #endif