summaryrefslogtreecommitdiff
path: root/Modules/xattr/_xattr.c
blob: 2d900e5637a872f2505534a1915103cab285b5a1 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "Python.h"
#include <sys/xattr.h>
#ifndef XATTR_NOFOLLOW
/* Linux compatibility API */
#define XATTR_XATTR_NOFOLLOW 0x0001
#define XATTR_XATTR_CREATE 0x0002
#define XATTR_XATTR_REPLACE 0x0004
#define XATTR_XATTR_NOSECURITY 0x0008
static ssize_t xattr_getxattr(const char *path, const char *name, void *value, ssize_t size, u_int32_t position, int options) {
    if (position != 0 || !(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return lgetxattr(path, name, value, size);
    } else {
        return getxattr(path, name, value, size);
    }
}

static ssize_t xattr_setxattr(const char *path, const char *name, void *value, ssize_t size, u_int32_t position, int options) {
    int nofollow;
    if (position != 0) {
        return -1;
    }
    nofollow = options & XATTR_XATTR_NOFOLLOW;
    options &= ~XATTR_XATTR_NOFOLLOW;
    if (options == XATTR_XATTR_CREATE) {
        options = XATTR_CREATE;
    } else if (options == XATTR_XATTR_REPLACE) {
        options = XATTR_REPLACE;
    } else if (options != 0) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return lsetxattr(path, name, value, size, options);
    } else {
        return setxattr(path, name, value, size, options);
    }
}

static ssize_t xattr_removexattr(const char *path, const char *name, int options) {
    if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return lremovexattr(path, name, value);
    } else {
        return removexattr(path, name, value);
    }
}


static ssize_t xattr_listxattr(const char *path, const char *namebuf, size_t size, int options) {
    if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return llistxattr(path, namebuf, size);
    } else {
        return listxattr(path, namebuf, size);
    }
}

static ssize_t xattr_fgetxattr(int fd, const char *name, void *value, ssize_t size, u_int32_t position, int options) {
    if (position != 0 || !(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return -1;
    } else {
        return fgetxattr(fd, name, value, size);
    }
}

static ssize_t xattr_fsetxattr(int fd, const char *name, void *value, ssize_t size, u_int32_t position, int options) {
    int nofollow;
    if (position != 0) {
        return -1;
    }
    nofollow = options & XATTR_XATTR_NOFOLLOW;
    options &= ~XATTR_XATTR_NOFOLLOW;
    if (options == XATTR_XATTR_CREATE) {
        options = XATTR_CREATE;
    } else if (options == XATTR_XATTR_REPLACE) {
        options = XATTR_REPLACE;
    } else if (options != 0) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return -1;
    } else {
        return fsetxattr(fd, name, value, size, options);
    }
}

static ssize_t xattr_fremovexattr(int fd, const char *name, int options) {
    if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return -1;
    } else {
        return fremovexattr(fd, name, value);
    }
}


static ssize_t xattr_flistxattr(int fd, const char *namebuf, size_t size, int options) {
    if (!(options == 0 || options == XATTR_XATTR_NOFOLLOW)) {
        return -1;
    }
    if (options & XATTR_XATTR_NOFOLLOW) {
        return -1;
    } else {
        return flistxattr(fd, namebuf, size);
    }
}

#else
#define xattr_getxattr getxattr
#define xattr_fgetxattr fgetxattr
#define xattr_removexattr removexattr
#define xattr_fremovexattr fremovexattr
#define xattr_setxattr setxattr
#define xattr_fsetxattr fsetxattr
#define xattr_listxattr listxattr
#define xattr_flistxattr flistxattr
#endif

static PyObject *xattr_error(void);
static PyObject *xattr_error_with_filename(char *name);

static PyObject *
xattr_error(void)
{
    return PyErr_SetFromErrno(PyExc_IOError);
}

static PyObject *
xattr_error_with_filename(char *name)
{
    return PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
}

PyDoc_STRVAR(pydoc_getxattr,
    "getxattr(path, name, size=0, position=0, options=0) -> str\n"
    "\n"
    "..."
);
static PyObject*
py_getxattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "path", "name", "size", "position", "options", NULL }; */
    char *path;
    char *name;
    PyObject *buffer;
    int options = 0;
    size_t size = 0;
    u_int32_t position = 0;
    ssize_t res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "etet|IIi:getxattr", /* keywords, */
            Py_FileSystemDefaultEncoding, &path,
            Py_FileSystemDefaultEncoding, &name,
            &size,
            &position,
            &options)) {
        return NULL;
    }
    if (size == 0) {
        Py_BEGIN_ALLOW_THREADS
        res = xattr_getxattr((const char *)path, (const char *)name, NULL, 0, position, options);
        Py_END_ALLOW_THREADS
        if (res == -1) {    
            return xattr_error_with_filename(path);
        }
        size = res;
    }
    buffer = PyString_FromStringAndSize((char *)NULL, size);
    if (buffer == NULL) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_getxattr((const char *)path, (const char *)name, (void *)PyString_AS_STRING(buffer), size, position, options);
    Py_END_ALLOW_THREADS
    if (res == -1) {
        Py_DECREF(buffer);
        return xattr_error_with_filename(path);
    }
    if (res != size) {
        _PyString_Resize(&buffer, (int)res);
    }
    return buffer;
}

PyDoc_STRVAR(pydoc_fgetxattr,
    "fgetxattr(fd, name, size=0, position=0, options=0) -> str\n"
    "\n"
    "..."
);
static PyObject*
py_fgetxattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "fd", "name", "size", "position", "options", NULL }; */
    int fd;
    char *name;
    PyObject *buffer;
    int options = 0;
    size_t size = 0;
    u_int32_t position = 0;
    ssize_t res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "iet|IIi:fgetxattr", /* keywords, */
            &fd,
            Py_FileSystemDefaultEncoding, &name,
            &size,
            &position,
            &options)) {
        return NULL;
    }
    if (size == 0) {
        Py_BEGIN_ALLOW_THREADS
        res = xattr_fgetxattr(fd, (const char *)name, NULL, 0, position, options);
        Py_END_ALLOW_THREADS
        if (res == -1) {    
            return xattr_error();
        }
        size = res;
    }
    buffer = PyString_FromStringAndSize((char *)NULL, size);
    if (buffer == NULL) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_fgetxattr(fd, (const char *)name, (void *)PyString_AS_STRING(buffer), size, position, options);
    Py_END_ALLOW_THREADS
    if (res == -1) {
        Py_DECREF(buffer);
        return xattr_error();
    }
    if (res != size) {
        _PyString_Resize(&buffer, (int)res);
    }
    return buffer;
}

PyDoc_STRVAR(pydoc_setxattr,
    "setxattr(path, name, value, position=0, options=0) -> None\n"
    "\n"
    "..."
);
static PyObject*
py_setxattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "path", "name", "value", "position", "options", NULL }; */
    char *path;
    char *name;
    int options = 0;
    char *value;
    int size;
    u_int32_t position = 0;
    int res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "etets#|Ii:setxattr", /* keywords, */
            Py_FileSystemDefaultEncoding, &path,
            Py_FileSystemDefaultEncoding, &name,
            &value, &size,
            &position,
            &options)) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_setxattr((const char *)path, (const char *)name, (void *)value, size, position, options);
    Py_END_ALLOW_THREADS
    if (res) {
        return xattr_error_with_filename(path);
    }
    Py_INCREF(Py_None);
    return Py_None;
}

PyDoc_STRVAR(pydoc_fsetxattr,
    "fsetxattr(fd, name, value, position=0, options=0) -> None\n"
    "\n"
    "..."
);
static PyObject*
py_fsetxattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "fd", "name", "value", "position", "options", NULL }; */
    int fd;
    char *name;
    int options = 0;
    char *value;
    int size;
    u_int32_t position = 0;
    int res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "iets#|Ii:fsetxattr", /* keywords, */
            &fd,
            Py_FileSystemDefaultEncoding, &name,
            &value, &size,
            &position,
            &options)) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_fsetxattr(fd, (const char *)name, (void *)value, size, position, options);
    Py_END_ALLOW_THREADS
    if (res) {
        return xattr_error();
    }
    Py_INCREF(Py_None);
    return Py_None;
}

PyDoc_STRVAR(pydoc_removexattr,
    "removexattr(path, name, options=0) -> None\n"
    "\n"
    "..."
);
static PyObject*
py_removexattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "path", "name", "options", NULL }; */
    char *path;
    char *name;
    int options = 0;
    int res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "etet|i:removexattr", /* keywords, */
            Py_FileSystemDefaultEncoding, &path,
            Py_FileSystemDefaultEncoding, &name,
            &options)) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_removexattr((const char *)path, (const char *)name, options);
    Py_END_ALLOW_THREADS
    if (res) {
        return xattr_error_with_filename(path);
    }
    Py_INCREF(Py_None);
    return Py_None;
}

PyDoc_STRVAR(pydoc_fremovexattr,
    "fremovexattr(fd, name, options=0) -> None\n"
    "\n"
    "..."
);
static PyObject*
py_fremovexattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "fd", "name", "options", NULL }; */
    int fd;
    char *name;
    int options = 0;
    int res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "iet|i:fremovexattr", /* keywords, */
            &fd,
            Py_FileSystemDefaultEncoding, &name,
            &options)) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_fremovexattr(fd, (const char *)name, options);
    Py_END_ALLOW_THREADS
    if (res) {
        return xattr_error();
    }
    Py_INCREF(Py_None);
    return Py_None;
}

PyDoc_STRVAR(pydoc_listxattr,
    "listxattr(path, options=0) -> str\n"
    "\n"
    "..."
);
static PyObject*
py_listxattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "path", "options", NULL }; */
    PyObject *buffer;
    char *path;
    int options = 0;
    ssize_t res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "et|i:listxattr", /* keywords, */
            Py_FileSystemDefaultEncoding, &path,
            &options)) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_listxattr((const char *)path, NULL, 0, options);
    Py_END_ALLOW_THREADS
    if (res == -1) {    
        return xattr_error_with_filename(path);
    }
    buffer = PyString_FromStringAndSize((char *)NULL, (int)res);
    if (buffer == NULL) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_listxattr((const char *)path, (void *)PyString_AS_STRING(buffer), (size_t)PyString_GET_SIZE(buffer), options);
    Py_END_ALLOW_THREADS
    if (res == -1) {
        Py_DECREF(buffer);
        return xattr_error_with_filename(path);
    }
    if (res != (ssize_t)PyString_GET_SIZE(buffer)) {
        _PyString_Resize(&buffer, (int)res);
    }
    return buffer;
}

PyDoc_STRVAR(pydoc_flistxattr,
    "flistxattr(fd, options=0) -> str\n"
    "\n"
    "..."
);
static PyObject*
py_flistxattr(PyObject* self __attribute__((__unused__)), PyObject *args) /* , PyObject *kwds) */
{
    /* static char *keywords[] = { "fd", "options", NULL }; */
    PyObject *buffer;
    int fd;
    int options = 0;
    ssize_t res;
    if (!PyArg_ParseTuple(args, /* AndKeywords(args, kwds, */
            "i|i:flistxattr", /* keywords, */
            &fd,
            &options)) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_flistxattr(fd, NULL, 0, options);
    Py_END_ALLOW_THREADS
    if (res == -1) {    
        return xattr_error();
    }
    buffer = PyString_FromStringAndSize((char *)NULL, (int)res);
    if (buffer == NULL) {
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    res = xattr_flistxattr(fd, (void *)PyString_AS_STRING(buffer), (size_t)PyString_GET_SIZE(buffer), options);
    Py_END_ALLOW_THREADS
    if (res == -1) {
        Py_DECREF(buffer);
        return xattr_error();
    }
    if (res != (ssize_t)PyString_GET_SIZE(buffer)) {
        _PyString_Resize(&buffer, (int)res);
    }
    return buffer;
}


#define DEFN(n) \
    {  \
        #n, \
        (PyCFunction)py_ ##n, \
        METH_VARARGS /* | METH_KEYWORDS */, \
        pydoc_ ##n \
    }
static PyMethodDef xattr_methods[] = {
    DEFN(getxattr),
    DEFN(fgetxattr),
    DEFN(setxattr),
    DEFN(fsetxattr),
    DEFN(removexattr),
    DEFN(fremovexattr),
    DEFN(listxattr),
    DEFN(flistxattr),
    {}
};
#undef DEFN

void init_xattr(void);

void
init_xattr(void)
{
    PyObject *m;
    m = Py_InitModule4("_xattr", xattr_methods, NULL, NULL, PYTHON_API_VERSION);
}