hgext/inotify/linux/_inotify.c
author Matt Mackall <mpm@selenic.com>
Thu, 27 Feb 2014 13:46:32 -0600
changeset 20572 c724cb0141ee
parent 17531 c133b1fb7467
permissions -rw-r--r--
config: add initial implementation of --edit This launches the preferred editor on either: a) the first non-empty user rc file found b) the first user rc file in the search list
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     1
/*
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
 * _inotify.c - Python extension interfacing to the Linux inotify subsystem
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     3
 *
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     4
 * Copyright 2006 Bryan O'Sullivan <bos@serpentine.com>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     5
 *
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     6
 * This library is free software; you can redistribute it and/or
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
 * modify it under the terms of version 2.1 of the GNU Lesser General
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9428
diff changeset
     8
 * Public License or any later version.
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
 */
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
#include <Python.h>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
#include <alloca.h>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
#include <sys/inotify.h>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    14
#include <stdint.h>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    15
#include <sys/ioctl.h>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    16
#include <unistd.h>
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    17
11549
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
    18
#include <util.h>
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
    19
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
    20
/* Variables used in the event string representation */
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
    21
static PyObject *join;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
    22
static PyObject *er_wm;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
    23
static PyObject *er_wmc;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
    24
static PyObject *er_wmn;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
    25
static PyObject *er_wmcn;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
    26
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    27
static PyObject *init(PyObject *self, PyObject *args)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    28
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    29
	PyObject *ret = NULL;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    30
	int fd = -1;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    31
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    32
	if (!PyArg_ParseTuple(args, ":init"))
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    33
		goto bail;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    34
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    35
	Py_BEGIN_ALLOW_THREADS;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    36
	fd = inotify_init();
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    37
	Py_END_ALLOW_THREADS;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    38
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    39
	if (fd == -1) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    40
		PyErr_SetFromErrno(PyExc_OSError);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    41
		goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    42
	}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    43
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    44
	ret = PyInt_FromLong(fd);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    45
	if (ret == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    46
		goto bail;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    47
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    48
	goto done;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    49
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    50
bail:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    51
	if (fd != -1)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    52
		close(fd);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    53
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    54
	Py_CLEAR(ret);
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    55
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    56
done:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    57
	return ret;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    58
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    59
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    60
PyDoc_STRVAR(
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    61
	init_doc,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    62
	"init() -> fd\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    63
	"\n"
17531
c133b1fb7467 en-us: Initialize
timeless@mozdev.org
parents: 13302
diff changeset
    64
	"Initialize an inotify instance.\n"
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    65
	"Return a file descriptor associated with a new inotify event queue.");
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    66
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    67
static PyObject *add_watch(PyObject *self, PyObject *args)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    68
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    69
	PyObject *ret = NULL;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    70
	uint32_t mask;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    71
	int wd = -1;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    72
	char *path;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    73
	int fd;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    74
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    75
	if (!PyArg_ParseTuple(args, "isI:add_watch", &fd, &path, &mask))
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    76
		goto bail;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    77
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    78
	Py_BEGIN_ALLOW_THREADS;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    79
	wd = inotify_add_watch(fd, path, mask);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    80
	Py_END_ALLOW_THREADS;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    81
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    82
	if (wd == -1) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    83
		PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    84
		goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    85
	}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    86
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    87
	ret = PyInt_FromLong(wd);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    88
	if (ret == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    89
		goto bail;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    90
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    91
	goto done;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    92
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    93
bail:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    94
	if (wd != -1)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    95
		inotify_rm_watch(fd, wd);
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
    96
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    97
	Py_CLEAR(ret);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    98
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    99
done:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   100
	return ret;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   101
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   102
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   103
PyDoc_STRVAR(
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   104
	add_watch_doc,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   105
	"add_watch(fd, path, mask) -> wd\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   106
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   107
	"Add a watch to an inotify instance, or modify an existing watch.\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   108
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   109
	"        fd: file descriptor returned by init()\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   110
	"        path: path to watch\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   111
	"        mask: mask of events to watch for\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   112
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   113
	"Return a unique numeric watch descriptor for the inotify instance\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   114
	"mapped by the file descriptor.");
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   115
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   116
static PyObject *remove_watch(PyObject *self, PyObject *args)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   117
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   118
	uint32_t wd;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   119
	int fd;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   120
	int r;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   121
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   122
	if (!PyArg_ParseTuple(args, "iI:remove_watch", &fd, &wd))
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   123
		return NULL;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   124
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   125
	Py_BEGIN_ALLOW_THREADS;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   126
	r = inotify_rm_watch(fd, wd);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   127
	Py_END_ALLOW_THREADS;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   128
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   129
	if (r == -1) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   130
		PyErr_SetFromErrno(PyExc_OSError);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   131
		return NULL;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   132
	}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   133
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   134
	Py_INCREF(Py_None);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   135
	return Py_None;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   136
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   137
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   138
PyDoc_STRVAR(
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   139
	remove_watch_doc,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   140
	"remove_watch(fd, wd)\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   141
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   142
	"        fd: file descriptor returned by init()\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   143
	"        wd: watch descriptor returned by add_watch()\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   144
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   145
	"Remove a watch associated with the watch descriptor wd from the\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   146
	"inotify instance associated with the file descriptor fd.\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   147
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   148
	"Removing a watch causes an IN_IGNORED event to be generated for this\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   149
	"watch descriptor.");
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   150
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   151
#define bit_name(x) {x, #x}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   152
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   153
static struct {
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   154
	int bit;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   155
	const char *name;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   156
	PyObject *pyname;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   157
} bit_names[] = {
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   158
	bit_name(IN_ACCESS),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   159
	bit_name(IN_MODIFY),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   160
	bit_name(IN_ATTRIB),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   161
	bit_name(IN_CLOSE_WRITE),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   162
	bit_name(IN_CLOSE_NOWRITE),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   163
	bit_name(IN_OPEN),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   164
	bit_name(IN_MOVED_FROM),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   165
	bit_name(IN_MOVED_TO),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   166
	bit_name(IN_CREATE),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   167
	bit_name(IN_DELETE),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   168
	bit_name(IN_DELETE_SELF),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   169
	bit_name(IN_MOVE_SELF),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   170
	bit_name(IN_UNMOUNT),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   171
	bit_name(IN_Q_OVERFLOW),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   172
	bit_name(IN_IGNORED),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   173
	bit_name(IN_ONLYDIR),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   174
	bit_name(IN_DONT_FOLLOW),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   175
	bit_name(IN_MASK_ADD),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   176
	bit_name(IN_ISDIR),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   177
	bit_name(IN_ONESHOT),
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   178
	{0}
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   179
};
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   180
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   181
static PyObject *decode_mask(int mask)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   182
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   183
	PyObject *ret = PyList_New(0);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   184
	int i;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   185
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   186
	if (ret == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   187
		goto bail;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   188
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   189
	for (i = 0; bit_names[i].bit; i++) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   190
		if (mask & bit_names[i].bit) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   191
			if (bit_names[i].pyname == NULL) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   192
				bit_names[i].pyname = PyString_FromString(bit_names[i].name);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   193
				if (bit_names[i].pyname == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   194
					goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   195
			}
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   196
			Py_INCREF(bit_names[i].pyname);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   197
			if (PyList_Append(ret, bit_names[i].pyname) == -1)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   198
				goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   199
		}
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   200
	}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   201
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   202
	goto done;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   203
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   204
bail:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   205
	Py_CLEAR(ret);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   206
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   207
done:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   208
	return ret;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   209
}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   210
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   211
static PyObject *pydecode_mask(PyObject *self, PyObject *args)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   212
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   213
	int mask;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   214
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   215
	if (!PyArg_ParseTuple(args, "i:decode_mask", &mask))
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   216
		return NULL;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   217
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   218
	return decode_mask(mask);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   219
}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   220
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   221
PyDoc_STRVAR(
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   222
	decode_mask_doc,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   223
	"decode_mask(mask) -> list_of_strings\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   224
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   225
	"Decode an inotify mask value into a list of strings that give the\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   226
	"name of each bit set in the mask.");
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   227
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   228
static char doc[] = "Low-level inotify interface wrappers.";
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   229
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   230
static void define_const(PyObject *dict, const char *name, uint32_t val)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   231
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   232
	PyObject *pyval = PyInt_FromLong(val);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   233
	PyObject *pyname = PyString_FromString(name);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   234
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   235
	if (!pyname || !pyval)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   236
		goto bail;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   237
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   238
	PyDict_SetItem(dict, pyname, pyval);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   240
bail:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   241
	Py_XDECREF(pyname);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   242
	Py_XDECREF(pyval);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   243
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   244
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   245
static void define_consts(PyObject *dict)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   246
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   247
	define_const(dict, "IN_ACCESS", IN_ACCESS);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   248
	define_const(dict, "IN_MODIFY", IN_MODIFY);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   249
	define_const(dict, "IN_ATTRIB", IN_ATTRIB);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   250
	define_const(dict, "IN_CLOSE_WRITE", IN_CLOSE_WRITE);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   251
	define_const(dict, "IN_CLOSE_NOWRITE", IN_CLOSE_NOWRITE);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   252
	define_const(dict, "IN_OPEN", IN_OPEN);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   253
	define_const(dict, "IN_MOVED_FROM", IN_MOVED_FROM);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   254
	define_const(dict, "IN_MOVED_TO", IN_MOVED_TO);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   255
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   256
	define_const(dict, "IN_CLOSE", IN_CLOSE);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   257
	define_const(dict, "IN_MOVE", IN_MOVE);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   258
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   259
	define_const(dict, "IN_CREATE", IN_CREATE);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   260
	define_const(dict, "IN_DELETE", IN_DELETE);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   261
	define_const(dict, "IN_DELETE_SELF", IN_DELETE_SELF);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   262
	define_const(dict, "IN_MOVE_SELF", IN_MOVE_SELF);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   263
	define_const(dict, "IN_UNMOUNT", IN_UNMOUNT);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   264
	define_const(dict, "IN_Q_OVERFLOW", IN_Q_OVERFLOW);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   265
	define_const(dict, "IN_IGNORED", IN_IGNORED);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   266
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   267
	define_const(dict, "IN_ONLYDIR", IN_ONLYDIR);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   268
	define_const(dict, "IN_DONT_FOLLOW", IN_DONT_FOLLOW);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   269
	define_const(dict, "IN_MASK_ADD", IN_MASK_ADD);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   270
	define_const(dict, "IN_ISDIR", IN_ISDIR);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   271
	define_const(dict, "IN_ONESHOT", IN_ONESHOT);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   272
	define_const(dict, "IN_ALL_EVENTS", IN_ALL_EVENTS);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   273
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   274
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   275
struct event {
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   276
	PyObject_HEAD
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   277
	PyObject *wd;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   278
	PyObject *mask;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   279
	PyObject *cookie;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   280
	PyObject *name;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   281
};
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   282
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   283
static PyObject *event_wd(PyObject *self, void *x)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   284
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   285
	struct event *evt = (struct event *)self;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   286
	Py_INCREF(evt->wd);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   287
	return evt->wd;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   288
}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   289
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   290
static PyObject *event_mask(PyObject *self, void *x)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   291
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   292
	struct event *evt = (struct event *)self;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   293
	Py_INCREF(evt->mask);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   294
	return evt->mask;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   295
}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   296
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   297
static PyObject *event_cookie(PyObject *self, void *x)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   298
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   299
	struct event *evt = (struct event *)self;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   300
	Py_INCREF(evt->cookie);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   301
	return evt->cookie;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   302
}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   303
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   304
static PyObject *event_name(PyObject *self, void *x)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   305
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   306
	struct event *evt = (struct event *)self;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   307
	Py_INCREF(evt->name);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   308
	return evt->name;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   309
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   310
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   311
static struct PyGetSetDef event_getsets[] = {
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   312
	{"wd", event_wd, NULL,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   313
	 "watch descriptor"},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   314
	{"mask", event_mask, NULL,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   315
	 "event mask"},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   316
	{"cookie", event_cookie, NULL,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   317
	 "rename cookie, if rename-related event"},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   318
	{"name", event_name, NULL,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   319
	 "file name"},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   320
	{NULL}
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   321
};
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   322
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   323
PyDoc_STRVAR(
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   324
	event_doc,
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   325
	"event: Structure describing an inotify event.");
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   326
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   327
static PyObject *event_new(PyTypeObject *t, PyObject *a, PyObject *k)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   328
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   329
	return (*t->tp_alloc)(t, 0);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   330
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   331
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   332
static void event_dealloc(struct event *evt)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   333
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   334
	Py_XDECREF(evt->wd);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   335
	Py_XDECREF(evt->mask);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   336
	Py_XDECREF(evt->cookie);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   337
	Py_XDECREF(evt->name);
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   338
11547
36a6aeb679da inotify: make proper use of Python API to get object size.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   339
	Py_TYPE(evt)->tp_free(evt);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   340
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   341
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   342
static PyObject *event_repr(struct event *evt)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   343
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   344
	int cookie = evt->cookie == Py_None ? -1 : PyInt_AsLong(evt->cookie);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   345
	PyObject *ret = NULL, *pymasks = NULL, *pymask = NULL;
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   346
	PyObject *tuple = NULL, *formatstr = NULL;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   347
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   348
	pymasks = decode_mask(PyInt_AsLong(evt->mask));
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   349
	if (pymasks == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   350
		goto bail;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   351
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   352
	pymask = _PyString_Join(join, pymasks);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   353
	if (pymask == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   354
		goto bail;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   355
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   356
	if (evt->name != Py_None) {
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   357
		if (cookie == -1) {
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   358
			formatstr = er_wmn;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   359
			tuple = PyTuple_Pack(3, evt->wd, pymask, evt->name);
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   360
		}
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   361
		else {
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   362
			formatstr = er_wmcn;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   363
			tuple = PyTuple_Pack(4, evt->wd, pymask,
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   364
					     evt->cookie, evt->name);
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   365
		}
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   366
	} else {
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   367
		if (cookie == -1) {
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   368
			formatstr = er_wm;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   369
			tuple = PyTuple_Pack(2, evt->wd, pymask);
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   370
		}
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   371
		else {
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   372
			formatstr = er_wmc;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   373
			tuple = PyTuple_Pack(3, evt->wd, pymask, evt->cookie);
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   374
		}
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   375
	}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   376
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   377
	if (tuple == NULL)
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   378
		goto bail;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   379
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   380
	ret = PyNumber_Remainder(formatstr, tuple);
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   381
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   382
	if (ret == NULL)
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   383
		goto bail;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   384
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   385
	goto done;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   386
bail:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   387
	Py_CLEAR(ret);
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   388
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   389
done:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   390
	Py_XDECREF(pymask);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   391
	Py_XDECREF(pymasks);
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   392
	Py_XDECREF(tuple);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   393
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   394
	return ret;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   395
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   396
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   397
static PyTypeObject event_type = {
11549
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   398
	PyVarObject_HEAD_INIT(NULL, 0)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   399
	"_inotify.event",             /*tp_name*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   400
	sizeof(struct event), /*tp_basicsize*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   401
	0,                         /*tp_itemsize*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   402
	(destructor)event_dealloc, /*tp_dealloc*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   403
	0,                         /*tp_print*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   404
	0,                         /*tp_getattr*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   405
	0,                         /*tp_setattr*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   406
	0,                         /*tp_compare*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   407
	(reprfunc)event_repr,      /*tp_repr*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   408
	0,                         /*tp_as_number*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   409
	0,                         /*tp_as_sequence*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   410
	0,                         /*tp_as_mapping*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   411
	0,                         /*tp_hash */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   412
	0,                         /*tp_call*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   413
	0,                         /*tp_str*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   414
	0,                         /*tp_getattro*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   415
	0,                         /*tp_setattro*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   416
	0,                         /*tp_as_buffer*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   417
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   418
	event_doc,           /* tp_doc */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   419
	0,                         /* tp_traverse */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   420
	0,                         /* tp_clear */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   421
	0,                         /* tp_richcompare */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   422
	0,                         /* tp_weaklistoffset */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   423
	0,                         /* tp_iter */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   424
	0,                         /* tp_iternext */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   425
	0,                         /* tp_methods */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   426
	0,                         /* tp_members */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   427
	event_getsets,      /* tp_getset */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   428
	0,                         /* tp_base */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   429
	0,                         /* tp_dict */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   430
	0,                         /* tp_descr_get */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   431
	0,                         /* tp_descr_set */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   432
	0,                         /* tp_dictoffset */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   433
	0,                         /* tp_init */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   434
	0,                         /* tp_alloc */
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   435
	event_new,          /* tp_new */
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   436
};
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   437
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   438
PyObject *read_events(PyObject *self, PyObject *args)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   439
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   440
	PyObject *ctor_args = NULL;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   441
	PyObject *pybufsize = NULL;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   442
	PyObject *ret = NULL;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   443
	int bufsize = 65536;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   444
	char *buf = NULL;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   445
	int nread, pos;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   446
	int fd;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   447
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   448
	if (!PyArg_ParseTuple(args, "i|O:read", &fd, &pybufsize))
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   449
		goto bail;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   450
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   451
	if (pybufsize && pybufsize != Py_None)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   452
		bufsize = PyInt_AsLong(pybufsize);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   453
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   454
	ret = PyList_New(0);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   455
	if (ret == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   456
		goto bail;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   457
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   458
	if (bufsize <= 0) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   459
		int r;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   460
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   461
		Py_BEGIN_ALLOW_THREADS;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   462
		r = ioctl(fd, FIONREAD, &bufsize);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   463
		Py_END_ALLOW_THREADS;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   464
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   465
		if (r == -1) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   466
			PyErr_SetFromErrno(PyExc_OSError);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   467
			goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   468
		}
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   469
		if (bufsize == 0)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   470
			goto done;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   471
	}
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   472
	else {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   473
		static long name_max;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   474
		static long name_fd = -1;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   475
		long min;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   476
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   477
		if (name_fd != fd) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   478
			name_fd = fd;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   479
			Py_BEGIN_ALLOW_THREADS;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   480
			name_max = fpathconf(fd, _PC_NAME_MAX);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   481
			Py_END_ALLOW_THREADS;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   482
		}
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   483
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   484
		min = sizeof(struct inotify_event) + name_max + 1;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   485
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   486
		if (bufsize < min) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   487
			PyErr_Format(PyExc_ValueError,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   488
				     "bufsize must be at least %d", (int)min);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   489
			goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   490
		}
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   491
	}
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   492
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   493
	buf = alloca(bufsize);
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   494
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   495
	Py_BEGIN_ALLOW_THREADS;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   496
	nread = read(fd, buf, bufsize);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   497
	Py_END_ALLOW_THREADS;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   498
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   499
	if (nread == -1) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   500
		PyErr_SetFromErrno(PyExc_OSError);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   501
		goto bail;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   502
	}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   503
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   504
	ctor_args = PyTuple_New(0);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   505
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   506
	if (ctor_args == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   507
		goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   508
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   509
	pos = 0;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   510
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   511
	while (pos < nread) {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   512
		struct inotify_event *in = (struct inotify_event *)(buf + pos);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   513
		struct event *evt;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   514
		PyObject *obj;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   515
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   516
		obj = PyObject_CallObject((PyObject *)&event_type, ctor_args);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   517
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   518
		if (obj == NULL)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   519
			goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   520
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   521
		evt = (struct event *)obj;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   522
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   523
		evt->wd = PyInt_FromLong(in->wd);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   524
		evt->mask = PyInt_FromLong(in->mask);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   525
		if (in->mask & IN_MOVE)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   526
			evt->cookie = PyInt_FromLong(in->cookie);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   527
		else {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   528
			Py_INCREF(Py_None);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   529
			evt->cookie = Py_None;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   530
		}
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   531
		if (in->len)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   532
			evt->name = PyString_FromString(in->name);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   533
		else {
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   534
			Py_INCREF(Py_None);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   535
			evt->name = Py_None;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   536
		}
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   537
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   538
		if (!evt->wd || !evt->mask || !evt->cookie || !evt->name)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   539
			goto mybail;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   540
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   541
		if (PyList_Append(ret, obj) == -1)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   542
			goto mybail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   543
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   544
		pos += sizeof(struct inotify_event) + in->len;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   545
		continue;
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   546
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   547
	mybail:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   548
		Py_CLEAR(evt->wd);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   549
		Py_CLEAR(evt->mask);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   550
		Py_CLEAR(evt->cookie);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   551
		Py_CLEAR(evt->name);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   552
		Py_DECREF(obj);
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   553
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   554
		goto bail;
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   555
	}
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   556
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   557
	goto done;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   558
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   559
bail:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   560
	Py_CLEAR(ret);
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   561
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   562
done:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   563
	Py_XDECREF(ctor_args);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   564
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   565
	return ret;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   566
}
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   567
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   568
static int init_globals(void)
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   569
{
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   570
	join = PyString_FromString("|");
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   571
	er_wm = PyString_FromString("event(wd=%d, mask=%s)");
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   572
	er_wmn = PyString_FromString("event(wd=%d, mask=%s, name=%s)");
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   573
	er_wmc = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x)");
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   574
	er_wmcn = PyString_FromString("event(wd=%d, mask=%s, cookie=0x%x, name=%s)");
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   575
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   576
	return join && er_wm && er_wmn && er_wmc && er_wmcn;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   577
}
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   578
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   579
PyDoc_STRVAR(
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   580
	read_doc,
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   581
	"read(fd, bufsize[=65536]) -> list_of_events\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   582
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   583
	"\nRead inotify events from a file descriptor.\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   584
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   585
	"        fd: file descriptor returned by init()\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   586
	"        bufsize: size of buffer to read into, in bytes\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   587
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   588
	"Return a list of event objects.\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   589
	"\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   590
	"If bufsize is > 0, block until events are available to be read.\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   591
	"Otherwise, immediately return all events that can be read without\n"
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   592
	"blocking.");
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   593
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   594
static PyMethodDef methods[] = {
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   595
	{"init", init, METH_VARARGS, init_doc},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   596
	{"add_watch", add_watch, METH_VARARGS, add_watch_doc},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   597
	{"remove_watch", remove_watch, METH_VARARGS, remove_watch_doc},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   598
	{"read", read_events, METH_VARARGS, read_doc},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   599
	{"decode_mask", pydecode_mask, METH_VARARGS, decode_mask_doc},
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   600
	{NULL},
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   601
};
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   602
11549
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   603
#ifdef IS_PY3K
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   604
static struct PyModuleDef _inotify_module = {
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   605
	PyModuleDef_HEAD_INIT,
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   606
	"_inotify",
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   607
	doc,
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   608
	-1,
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   609
	methods
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   610
};
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   611
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   612
PyMODINIT_FUNC PyInit__inotify(void)
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   613
{
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   614
	PyObject *mod, *dict;
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   615
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   616
	mod = PyModule_Create(&_inotify_module);
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   617
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   618
	if (mod == NULL)
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   619
		return NULL;
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   620
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   621
	if (!init_globals())
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   622
		return;
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   623
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   624
	dict = PyModule_GetDict(mod);
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   625
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   626
	if (dict)
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   627
		define_consts(dict);
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   628
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   629
	return mod;
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   630
}
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   631
#else
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   632
void init_inotify(void)
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   633
{
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   634
	PyObject *mod, *dict;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   635
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   636
	if (PyType_Ready(&event_type) == -1)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   637
		return;
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   638
11548
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   639
	if (!init_globals())
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   640
		return;
dd2f356e1f6f inotify: Better implementation of the event string representation.
Renato Cunha <renatoc@gmail.com>
parents: 11547
diff changeset
   641
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   642
	mod = Py_InitModule3("_inotify", methods, doc);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   643
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   644
	dict = PyModule_GetDict(mod);
6334
7016f7fb8fe3 tab/space cleanup
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6239
diff changeset
   645
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   646
	if (dict)
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   647
		define_consts(dict);
6239
39cfcef4f463 Add inotify extension
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   648
}
11549
935c83ce9172 inotify: Port of the C module to py3k.
Renato Cunha <renatoc@gmail.com>
parents: 11548
diff changeset
   649
#endif