File: linotify.c

package info (click to toggle)
lua-inotify 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 88 kB
  • sloc: ansic: 239; makefile: 21
file content (310 lines) | stat: -rw-r--r-- 7,661 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (c) 2009-2017 Robert Hoelz <rob@hoelz.ro>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <lua.h>
#include <lauxlib.h>

#include <sys/inotify.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#define MT_NAME "INOTIFY_HANDLE"
#define READ_BUFFER_SIZE 1024
#define INVALID_FD (-1)

struct inotify_context {
    char buffer[READ_BUFFER_SIZE];
    int offset;
    int bytes_remaining;
};

void push_inotify_handle(lua_State *L, int fd)
{
    int *udata = (int *) lua_newuserdata(L, sizeof(int));
    *udata = fd;
    luaL_getmetatable(L, MT_NAME);
    lua_setmetatable(L, -2);
}

int get_inotify_handle(lua_State *L, int index)
{
    return *((int *) luaL_checkudata(L, index, MT_NAME));
}

static int handle_error(lua_State *L)
{
    lua_pushnil(L);
    lua_pushstring(L, strerror(errno));
    lua_pushinteger(L, errno);
    return 3;
}

static int init(lua_State *L)
{
    int fd;
    int flags = 0;

    if(lua_type(L, 1) == LUA_TTABLE) {
        lua_getfield(L, 1, "blocking");

        if(lua_type(L, -1) != LUA_TNIL && !lua_toboolean(L, -1)) {
            flags |= IN_NONBLOCK;
        }
        lua_pop(L, 1);
    }

    if((fd = inotify_init1(flags)) == -1) {
        return handle_error(L);
    } else {
        push_inotify_handle(L, fd);
        return 1;
    }
}

static int handle_fileno(lua_State *L)
{
    lua_pushinteger(L, get_inotify_handle(L, 1));
    return 1;
}

static void
push_inotify_event(lua_State *L, struct inotify_event *ev)
{
    lua_createtable(L, 0, 4);

    lua_pushinteger(L, ev->wd);
    lua_setfield(L, -2, "wd");

    lua_pushinteger(L, ev->mask);
    lua_setfield(L, -2, "mask");

    lua_pushinteger(L, ev->cookie);
    lua_setfield(L, -2, "cookie");

    if(ev->len) {
        lua_pushstring(L, ev->name);
        lua_setfield(L, -2, "name");
    }
}

static int handle_read(lua_State *L)
{
    int fd;
    int i = 0;
    int n = 1;
    ssize_t bytes;
    struct inotify_event *iev;
    char buffer[1024];

    fd = get_inotify_handle(L, 1);
    if((bytes = read(fd, buffer, 1024)) < 0) {
        if(errno == EAGAIN || errno == EWOULDBLOCK) {
            lua_newtable(L);
            return 1;
        }
        return handle_error(L);
    }
    lua_newtable(L);

    while(bytes >= sizeof(struct inotify_event)) {
        iev = (struct inotify_event *) (buffer + i);

        push_inotify_event(L, iev);
        lua_rawseti(L, -2, n++);

        i += (sizeof(struct inotify_event) + iev->len);
        bytes -= (sizeof(struct inotify_event) + iev->len);
    }

    return 1;
}

static int
handle_events_iterator(lua_State *L)
{
    struct inotify_context *context;
    struct inotify_event *event;
    int fd;

    fd      = get_inotify_handle(L, 1);
    context = lua_touserdata(L, lua_upvalueindex(1));

    if(context->bytes_remaining < sizeof(struct inotify_event)) {
        context->offset = 0;

        if((context->bytes_remaining = read(fd, context->buffer, READ_BUFFER_SIZE)) < 0) {
            if(errno == EAGAIN || errno == EWOULDBLOCK) {
                lua_pushnil(L);
                return 1;
            }
            return luaL_error(L, "read error: %s\n", strerror(errno));
        }
    }
    event = (struct inotify_event *) (context->buffer + context->offset);

    context->bytes_remaining -= (sizeof(struct inotify_event) + event->len);
    context->offset          += (sizeof(struct inotify_event) + event->len);

    push_inotify_event(L, event);

    return 1;
}

static int
handle_events(lua_State *L)
{
    struct inotify_context *context;

    context = lua_newuserdata(L, sizeof(struct inotify_context));

    memset(context, 0, sizeof(struct inotify_context));

    lua_pushcclosure(L, handle_events_iterator, 1);
    lua_pushvalue(L, 1);

    return 2;
}

static int handle_close(lua_State *L)
{
    int *fd = (int *) luaL_checkudata(L, 1, MT_NAME);
    if( *fd != INVALID_FD ){
        close(*fd);
        *fd = INVALID_FD;
    }
    return 0;
}

static int handle_add_watch(lua_State *L)
{
    int fd;
    int wd;
    int top;
    int i;
    const char *path;
    uint32_t mask = 0;

    fd = get_inotify_handle(L, 1);
    path = luaL_checkstring(L, 2);
    top = lua_gettop(L);
    for(i = 3; i <= top; i++) {
        mask |= luaL_checkinteger(L, i);
    }

    if((wd = inotify_add_watch(fd, path, mask)) == -1) {
        return handle_error(L);
    } else {
        lua_pushinteger(L, wd);
        return 1;
    }
}

static int handle_rm_watch(lua_State *L)
{
    int fd;
    int wd;

    fd = get_inotify_handle(L, 1);
    wd = luaL_checkinteger(L, 2);

    if(inotify_rm_watch(fd, wd) == -1) {
        return handle_error(L);
    }
    lua_pushboolean(L, 1);
    return 1;
}

static int handle__gc(lua_State *L)
{
    return handle_close(L);
}

static luaL_Reg inotify_funcs[] = {
    {"init", init},
    {NULL, NULL}
};

static luaL_Reg handle_funcs[] = {
    {"read", handle_read},
    {"close", handle_close},
    {"addwatch", handle_add_watch},
    {"rmwatch", handle_rm_watch},
    {"fileno", handle_fileno},
    {"getfd", handle_fileno},
    {"events", handle_events},
    {NULL, NULL}
};

#define register_constant(s)\
    lua_pushinteger(L, s);\
    lua_setfield(L, -2, #s);

int luaopen_inotify(lua_State *L)
{
    luaL_newmetatable(L, MT_NAME);
    lua_createtable(L, 0, sizeof(handle_funcs) / sizeof(luaL_Reg) - 1);
#if LUA_VERSION_NUM > 501
    luaL_setfuncs(L, handle_funcs, 0);
#else
    luaL_register(L, NULL, handle_funcs);
#endif
    lua_setfield(L, -2, "__index");
    lua_pushcfunction(L, handle__gc);
    lua_setfield(L, -2, "__gc");
    lua_pushliteral(L, "inotify_handle");
    lua_setfield(L, -2, "__type");
    lua_pop(L, 1);

    lua_newtable(L);
#if LUA_VERSION_NUM > 501
    luaL_setfuncs(L, inotify_funcs,0);
#else
    luaL_register(L, NULL, inotify_funcs);
#endif

    register_constant(IN_ACCESS);
    register_constant(IN_ATTRIB);
    register_constant(IN_CLOSE_WRITE);
    register_constant(IN_CLOSE_NOWRITE);
    register_constant(IN_CREATE);
    register_constant(IN_DELETE);
    register_constant(IN_DELETE_SELF);
    register_constant(IN_MODIFY);
    register_constant(IN_MOVE_SELF);
    register_constant(IN_MOVED_FROM);
    register_constant(IN_MOVED_TO);
    register_constant(IN_OPEN);
    register_constant(IN_ALL_EVENTS);
    register_constant(IN_MOVE);
    register_constant(IN_CLOSE);
    register_constant(IN_DONT_FOLLOW);
    register_constant(IN_MASK_ADD);
    register_constant(IN_ONESHOT);
    register_constant(IN_ONLYDIR);
    register_constant(IN_IGNORED);
    register_constant(IN_ISDIR);
    register_constant(IN_Q_OVERFLOW);
    register_constant(IN_UNMOUNT);

    return 1;
}