summaryrefslogtreecommitdiff
path: root/_summainmodule.c
blob: d008fef94adc434af5b3d264db6891272eac13e9 (plain)
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
/*
 * _summainmodule.c -- Python extensions for Summain
 *
 * Copyright (C) 2008, 2009  Lars Wirzenius <liw@liw.fi>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <Python.h>


#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

#ifdef __FreeBSD__
    #define NO_NANOSECONDS 1
    #include <sys/extattr.h>
#else
    #define NO_NANOSECONDS 0
    #include <sys/xattr.h>
#endif

/*
 * Since we can't set nanosecond mtime and atimes on some platforms, also
 * don't retrieve that level of precision from lstat(), so comparisons
 * work.
 */
static unsigned long long
remove_precision(unsigned long long nanoseconds)
{
#if NO_NANOSECONDS
    return nanoseconds - (nanoseconds % 1000);
#else
    return nanoseconds;
#endif
}


static PyObject *
lstat_wrapper(PyObject *self, PyObject *args)
{
    int ret;
    const char *filename;
    struct stat st = {0};

    if (!PyArg_ParseTuple(args, "s", &filename))
        return NULL;

    ret = lstat(filename, &st);
    if (ret == -1)
        ret = errno;

    return Py_BuildValue("iKKKKKKKLLLLKLKLK",
                         ret,
                         (unsigned long long) st.st_dev,
                         (unsigned long long) st.st_ino,
                         (unsigned long long) st.st_mode,
                         (unsigned long long) st.st_nlink,
                         (unsigned long long) st.st_uid,
                         (unsigned long long) st.st_gid,
                         (unsigned long long) st.st_rdev,
                         (long long) st.st_size,
                         (long long) st.st_blksize,
                         (long long) st.st_blocks,
                         (long long) st.st_atim.tv_sec,
                         remove_precision(st.st_atim.tv_nsec),
                         (long long) st.st_mtim.tv_sec,
                         remove_precision(st.st_mtim.tv_nsec),
                         (long long) st.st_ctim.tv_sec,
                         remove_precision(st.st_ctim.tv_nsec));
}


static PyObject *
llistxattr_wrapper(PyObject *self, PyObject *args)
{
    const char *filename;
    size_t bufsize;
    PyObject *o;
    char* buf;
    ssize_t n;

    if (!PyArg_ParseTuple(args, "s", &filename))
        return NULL;

#ifdef __FreeBSD__
    bufsize = extattr_list_link(filename, EXTATTR_NAMESPACE_USER, NULL, 0);
    buf = malloc(bufsize);
    n = extattr_list_link(filename, EXTATTR_NAMESPACE_USER, buf, bufsize);
    if (n >= 0) {
         /* Convert from length-prefixed BSD style to '\0'-suffixed
            Linux style. */
         size_t i = 0;
         while (i < n) {
             unsigned char length = (unsigned char) buf[i];
	     memmove(buf + i, buf + i + 1, length);
	     buf[i + length] = '\0';
	     i += length + 1;
	 }
	 o = Py_BuildValue("s#", buf, (int) n);
    } else {
         o = Py_BuildValue("i", errno);
    }
    free(buf);
#else
    bufsize = 0;
    o = NULL;
    do {
        bufsize += 1024;
        buf = malloc(bufsize);
        n = llistxattr(filename, buf, bufsize);

        if (n >= 0)
            o = Py_BuildValue("s#", buf, (int) n);
        else if (n == -1 && errno != ERANGE)
            o = Py_BuildValue("i", errno);
        free(buf);
    } while (o == NULL);
#endif
    return o;
}


static PyObject *
lgetxattr_wrapper(PyObject *self, PyObject *args)
{
    const char *filename;
    const char *attrname;
    size_t bufsize;
    PyObject *o;

    if (!PyArg_ParseTuple(args, "ss", &filename, &attrname))
        return NULL;

    bufsize = 0;
    o = NULL;
    do {
        bufsize += 1024;
        char *buf = malloc(bufsize);
#ifdef __FreeBSD__
	int n = extattr_get_link(filename, EXTATTR_NAMESPACE_USER, attrname, buf, bufsize);
#else
        ssize_t n = lgetxattr(filename, attrname, buf, bufsize);
#endif
        if (n >= 0)
            o = Py_BuildValue("s#", buf, (int) n);
        else if (n == -1 && errno != ERANGE)
            o = Py_BuildValue("i", errno);
        free(buf);
    } while (o == NULL);

    return o;
}


static PyObject *
lsetxattr_wrapper(PyObject *self, PyObject *args)
{
    const char *filename;
    const char *name;
    const char *value;
    int size;
    int ret;

    if (!PyArg_ParseTuple(args, "sss#", &filename, &name, &value, &size))
        return NULL;

#ifdef __FreeBSD__
    ret = extattr_set_link(filename, EXTATTR_NAMESPACE_USER, name, value, size);
#else
    ret = lsetxattr(filename, name, value, size, 0);
#endif
    if (ret == -1)
        ret = errno;
    return Py_BuildValue("i", ret);
}


static PyMethodDef methods[] = {
    {"lstat", lstat_wrapper, METH_VARARGS,
     "lstat(2) wrapper; arg is filename, returns tuple."},
    {"llistxattr", llistxattr_wrapper, METH_VARARGS,
     "llistxattr(2) wrapper; arg is filename, returns tuple."},
    {"lgetxattr", lgetxattr_wrapper, METH_VARARGS,
     "lgetxattr(2) wrapper; arg is filename, returns tuple."},
    {"lsetxattr", lsetxattr_wrapper, METH_VARARGS,
     "lsetxattr(2) wrapper; arg is filename, returns errno."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};


PyMODINIT_FUNC
init_summain(void)
{
    (void) Py_InitModule("_summain", methods);
}