forked from iustin/pyxattr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xattr.c
1156 lines (1003 loc) · 32.9 KB
/
xattr.c
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
xattr - a python module for manipulating filesystem extended attributes
Copyright (C) 2002, 2003, 2006, 2008, 2012, 2013 Iustin Pop <iusty@k1024.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <attr/xattr.h>
#include <stdio.h>
/* Compatibility with python 2.4 regarding python size type (PEP 353) */
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#define BYTES_CHAR "y"
#else
#define BYTES_CHAR "s"
#define PyBytes_Check PyString_Check
#define PyBytes_AS_STRING PyString_AS_STRING
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
#define PyBytes_FromString PyString_FromString
#endif
#define ITEM_DOC \
":param item: a string representing a file-name, or a file-like\n" \
" object, or a file descriptor; this represents the file on \n" \
" which to act\n"
#define NOFOLLOW_DOC \
":param nofollow: if true and if\n" \
" the file name given is a symbolic link, the\n" \
" function will operate on the symbolic link itself instead\n" \
" of its target; defaults to false\n" \
":type nofollow: boolean, optional\n" \
#define NS_DOC \
":param namespace: if given, the attribute must not contain the\n" \
" namespace, but instead it will be taken from this parameter\n" \
":type namespace: bytes\n"
#define NAME_GET_DOC \
":param string name: the attribute whose value to retrieve;\n" \
" usually in the form of ``system.posix_acl`` or ``user.mime_type``\n"
#define NAME_SET_DOC \
":param string name: the attribute whose value to set;\n" \
" usually in the form of ``system.posix_acl`` or ``user.mime_type``\n"
#define NAME_REMOVE_DOC \
":param string name: the attribute to remove;\n" \
" usually in the form of ``system.posix_acl`` or \n" \
" ``user.mime_type``\n"
#define VALUE_DOC \
":param string value: possibly with embedded NULLs; note that there\n" \
" are restrictions regarding the size of the value, for\n" \
" example, for ext2/ext3, maximum size is the block size\n" \
#define FLAGS_DOC \
":param flags: if 0 or omitted the attribute will be\n" \
" created or replaced; if :const:`XATTR_CREATE`, the attribute\n" \
" will be created, giving an error if it already exists;\n" \
" if :const:`XATTR_REPLACE`, the attribute will be replaced,\n" \
" giving an error if it doesn't exist;\n" \
":type flags: integer\n"
#define NS_CHANGED_DOC \
".. versionchanged:: 0.5.1\n" \
" The namespace argument, if passed, cannot be None anymore; to\n" \
" explicitly specify an empty namespace, pass an empty\n" \
" string (byte string under Python 3)."
/* the estimated (startup) attribute buffer size in
multi-operations */
#define ESTIMATE_ATTR_SIZE 256
typedef enum {T_FD, T_PATH, T_LINK} target_e;
typedef struct {
target_e type;
union {
const char *name;
int fd;
};
PyObject *tmp;
} target_t;
/* Cleans up a tgt structure */
static void free_tgt(target_t *tgt) {
if (tgt->tmp != NULL) {
Py_DECREF(tgt->tmp);
}
}
/* Used for cpychecker: */
/* The checker automatically defines this preprocessor name when creating
the custom attribute: */
#if defined(WITH_CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION_ATTRIBUTE)
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION \
__attribute__((cpychecker_negative_result_sets_exception))
#else
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
#endif
static int convert_obj(PyObject *myobj, target_t *tgt, int nofollow)
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
static int merge_ns(const char *ns, const char *name,
const char **result, char **buf)
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
/** Converts from a string, file or int argument to what we need.
*
* Returns -1 on failure, 0 on success.
*/
static int convert_obj(PyObject *myobj, target_t *tgt, int nofollow) {
int fd;
tgt->tmp = NULL;
if(PyBytes_Check(myobj)) {
tgt->type = nofollow ? T_LINK : T_PATH;
tgt->name = PyBytes_AS_STRING(myobj);
} else if(PyUnicode_Check(myobj)) {
tgt->type = nofollow ? T_LINK : T_PATH;
tgt->tmp = \
PyUnicode_AsEncodedString(myobj,
Py_FileSystemDefaultEncoding, "strict");
if(tgt->tmp == NULL)
return -1;
tgt->name = PyBytes_AS_STRING(tgt->tmp);
} else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
tgt->type = T_FD;
tgt->fd = fd;
} else {
PyErr_SetString(PyExc_TypeError, "argument must be string or int");
return -1;
}
return 0;
}
/* Combine a namespace string and an attribute name into a
fully-qualified name */
static int merge_ns(const char *ns, const char *name,
const char **result, char **buf) {
if(ns != NULL && *ns != '\0') {
int cnt;
size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
if((*buf = PyMem_Malloc(new_size)) == NULL) {
PyErr_NoMemory();
return -1;
}
cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
if(cnt > new_size || cnt < 0) {
PyErr_SetString(PyExc_ValueError,
"can't format the attribute name");
PyMem_Free(*buf);
return -1;
}
*result = *buf;
} else {
*buf = NULL;
*result = name;
}
return 0;
}
static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
if(tgt->type == T_FD)
return flistxattr(tgt->fd, list, size);
else if (tgt->type == T_LINK)
return llistxattr(tgt->name, list, size);
else
return listxattr(tgt->name, list, size);
}
static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
size_t size) {
if(tgt->type == T_FD)
return fgetxattr(tgt->fd, name, value, size);
else if (tgt->type == T_LINK)
return lgetxattr(tgt->name, name, value, size);
else
return getxattr(tgt->name, name, value, size);
}
static int _set_obj(target_t *tgt, const char *name,
const void *value, size_t size, int flags) {
if(tgt->type == T_FD)
return fsetxattr(tgt->fd, name, value, size, flags);
else if (tgt->type == T_LINK)
return lsetxattr(tgt->name, name, value, size, flags);
else
return setxattr(tgt->name, name, value, size, flags);
}
static int _remove_obj(target_t *tgt, const char *name) {
if(tgt->type == T_FD)
return fremovexattr(tgt->fd, name);
else if (tgt->type == T_LINK)
return lremovexattr(tgt->name, name);
else
return removexattr(tgt->name, name);
}
/*
Checks if an attribute name matches an optional namespace.
If the namespace is NULL or an empty string, it will return the
name itself. If the namespace is non-NULL and the name matches, it
will return a pointer to the offset in the name after the namespace
and the separator. If however the name doesn't match the namespace,
it will return NULL.
*/
const char *matches_ns(const char *ns, const char *name) {
size_t ns_size;
if (ns == NULL || *ns == '\0')
return name;
ns_size = strlen(ns);
if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
name[ns_size] == '.')
return name + ns_size + 1;
return NULL;
}
/* Wrapper for getxattr */
static char __pygetxattr_doc__[] =
"getxattr(item, attribute[, nofollow=False])\n"
"Get the value of a given extended attribute (deprecated).\n"
"\n"
ITEM_DOC
NAME_GET_DOC
NOFOLLOW_DOC
"\n"
".. deprecated:: 0.4\n"
" this function has been deprecated\n"
" by the :func:`get` function.\n"
;
static PyObject *
pygetxattr(PyObject *self, PyObject *args)
{
PyObject *myarg;
target_t tgt;
int nofollow = 0;
char *attrname = NULL;
char *buf;
ssize_t nalloc, nret;
PyObject *res;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0) {
res = NULL;
goto freearg;
}
/* Find out the needed size of the buffer */
if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freetgt;
}
/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
goto freetgt;
}
/* Now retrieve the attribute value */
if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freebuf;
}
/* Create the string which will hold the result */
res = PyBytes_FromStringAndSize(buf, nret);
freebuf:
/* Free the buffer, now it is no longer needed */
PyMem_Free(buf);
freetgt:
free_tgt(&tgt);
freearg:
PyMem_Free(attrname);
/* Return the result */
return res;
}
/* Wrapper for getxattr */
static char __get_doc__[] =
"get(item, name[, nofollow=False, namespace=None])\n"
"Get the value of a given extended attribute.\n"
"\n"
"Example:\n"
" >>> xattr.get('/path/to/file', 'user.comment')\n"
" 'test'\n"
" >>> xattr.get('/path/to/file', 'comment', namespace=xattr.NS_USER)\n"
" 'test'\n"
"\n"
ITEM_DOC
NAME_GET_DOC
NOFOLLOW_DOC
NS_DOC
":return: the value of the extended attribute (can contain NULLs)\n"
":rtype: string\n"
":raises EnvironmentError: caused by any system errors\n"
"\n"
".. versionadded:: 0.4\n"
NS_CHANGED_DOC
;
static PyObject *
xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
{
PyObject *myarg;
target_t tgt;
int nofollow = 0;
char *attrname = NULL, *namebuf;
const char *fullname;
char *buf;
const char *ns = NULL;
ssize_t nalloc, nret;
PyObject *res;
static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
/* Parse the arguments */
if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|i" BYTES_CHAR, kwlist,
&myarg, NULL, &attrname, &nofollow, &ns))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0) {
res = NULL;
goto freearg;
}
if(merge_ns(ns, attrname, &fullname, &namebuf) < 0) {
res = NULL;
goto freearg;
}
/* Find out the needed size of the buffer */
if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freetgt;
}
/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
goto freenamebuf;
}
/* Now retrieve the attribute value */
if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freebuf;
}
/* Create the string which will hold the result */
res = PyBytes_FromStringAndSize(buf, nret);
/* Free the buffers, they are no longer needed */
freebuf:
PyMem_Free(buf);
freenamebuf:
PyMem_Free(namebuf);
freetgt:
free_tgt(&tgt);
freearg:
PyMem_Free(attrname);
/* Return the result */
return res;
}
/* Wrapper for getxattr */
static char __get_all_doc__[] =
"get_all(item[, nofollow=False, namespace=None])\n"
"Get all the extended attributes of an item.\n"
"\n"
"This function performs a bulk-get of all extended attribute names\n"
"and the corresponding value.\n"
"Example:\n"
"\n"
" >>> xattr.get_all('/path/to/file')\n"
" [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
" ('system.posix_acl_access', '\\x02\\x00...')]\n"
" >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
" [('mime-type', 'plain/text'), ('comment', 'test')]\n"
"\n"
ITEM_DOC
":keyword namespace: an optional namespace for filtering the\n"
" attributes; for example, querying all user attributes can be\n"
" accomplished by passing namespace=:const:`NS_USER`\n"
":type namespace: string\n"
NOFOLLOW_DOC
":return: list of tuples (name, value); note that if a namespace\n"
" argument was passed, it (and the separator) will be stripped from\n"
" the names returned\n"
":rtype: list\n"
":raises EnvironmentError: caused by any system errors\n"
"\n"
".. note:: Since reading the whole attribute list is not an atomic\n"
" operation, it might be possible that attributes are added\n"
" or removed between the initial query and the actual reading\n"
" of the attributes; the returned list will contain only the\n"
" attributes that were present at the initial listing of the\n"
" attribute names and that were still present when the read\n"
" attempt for the value is made.\n"
".. versionadded:: 0.4\n"
NS_CHANGED_DOC
;
static PyObject *
get_all(PyObject *self, PyObject *args, PyObject *keywds)
{
PyObject *myarg, *res;
int nofollow=0;
const char *ns = NULL;
char *buf_list, *buf_val;
const char *s;
ssize_t nalloc, nlist, nval;
PyObject *mylist;
target_t tgt;
static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
/* Parse the arguments */
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|i" BYTES_CHAR, kwlist,
&myarg, &nofollow, &ns))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0)
return NULL;
/* Compute first the list of attributes */
/* Find out the needed size of the buffer for the attribute list */
nalloc = _list_obj(&tgt, NULL, 0);
if(nalloc == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freetgt;
}
/* Try to allocate the memory, using Python's allocator */
if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
goto freetgt;
}
/* Now retrieve the list of attributes */
nlist = _list_obj(&tgt, buf_list, nalloc);
if(nlist == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto free_buf_list;
}
/* Create the list which will hold the result */
mylist = PyList_New(0);
if(mylist == NULL) {
res = NULL;
goto free_buf_list;
}
nalloc = ESTIMATE_ATTR_SIZE;
if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
Py_DECREF(mylist);
res = PyErr_NoMemory();
goto free_buf_list;
}
/* Create and insert the attributes as strings in the list */
for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
PyObject *my_tuple;
int missing;
const char *name;
if((name=matches_ns(ns, s))==NULL)
continue;
/* Now retrieve the attribute value */
missing = 0;
while(1) {
nval = _get_obj(&tgt, s, buf_val, nalloc);
if(nval == -1) {
if(errno == ERANGE) {
nval = _get_obj(&tgt, s, NULL, 0);
if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
res = PyErr_NoMemory();
Py_DECREF(mylist);
goto free_buf_list;
}
nalloc = nval;
continue;
} else if(errno == ENODATA || errno == ENOATTR) {
/* this attribute has gone away since we queried
the attribute list */
missing = 1;
break;
}
/* else we're dealing with a different error, which we
don't know how to handle nicely, so we abort */
Py_DECREF(mylist);
res = PyErr_SetFromErrno(PyExc_IOError);
goto freebufval;
}
break;
}
if(missing)
continue;
#ifdef IS_PY3K
my_tuple = Py_BuildValue("yy#", name, buf_val, nval);
#else
my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
#endif
if (my_tuple == NULL) {
Py_DECREF(mylist);
res = NULL;
goto freebufval;
}
PyList_Append(mylist, my_tuple);
Py_DECREF(my_tuple);
}
/* Successful exit */
res = mylist;
freebufval:
PyMem_Free(buf_val);
free_buf_list:
PyMem_Free(buf_list);
freetgt:
free_tgt(&tgt);
/* Return the result */
return res;
}
static char __pysetxattr_doc__[] =
"setxattr(item, name, value[, flags=0, nofollow=False])\n"
"Set the value of a given extended attribute (deprecated).\n"
"\n"
"Be careful in case you want to set attributes on symbolic\n"
"links, you have to use all the 5 parameters; use 0 for the \n"
"flags value if you want the default behaviour (create or "
"replace)\n"
"\n"
ITEM_DOC
NAME_SET_DOC
VALUE_DOC
FLAGS_DOC
NOFOLLOW_DOC
"\n"
".. deprecated:: 0.4\n"
" this function has been deprecated\n"
" by the :func:`set` function.\n"
;
/* Wrapper for setxattr */
static PyObject *
pysetxattr(PyObject *self, PyObject *args)
{
PyObject *myarg, *res;
int nofollow = 0;
char *attrname = NULL;
char *buf = NULL;
int bufsize;
int nret;
int flags = 0;
target_t tgt;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "Oetet#|ii", &myarg, NULL, &attrname,
NULL, &buf, &bufsize, &flags, &nofollow))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0) {
res = NULL;
goto freearg;
}
/* Set the attribute's value */
nret = _set_obj(&tgt, attrname, buf, bufsize, flags);
free_tgt(&tgt);
if(nret == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freearg;
}
Py_INCREF(Py_None);
res = Py_None;
freearg:
PyMem_Free(attrname);
PyMem_Free(buf);
/* Return the result */
return res;
}
static char __set_doc__[] =
"set(item, name, value[, flags=0, namespace=None])\n"
"Set the value of a given extended attribute.\n"
"\n"
"Example:\n"
"\n"
" >>> xattr.set('/path/to/file', 'user.comment', 'test')\n"
" >>> xattr.set('/path/to/file', 'comment', 'test',"
" namespace=xattr.NS_USER)\n"
"\n"
ITEM_DOC
NAME_SET_DOC
VALUE_DOC
FLAGS_DOC
NOFOLLOW_DOC
NS_DOC
":returns: None\n"
":raises EnvironmentError: caused by any system errors\n"
"\n"
".. versionadded:: 0.4\n"
NS_CHANGED_DOC
;
/* Wrapper for setxattr */
static PyObject *
xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
{
PyObject *myarg, *res;
int nofollow = 0;
char *attrname = NULL;
char *buf = NULL;
int bufsize;
int nret;
int flags = 0;
target_t tgt;
const char *ns = NULL;
char *newname;
const char *full_name;
static char *kwlist[] = {"item", "name", "value", "flags",
"nofollow", "namespace", NULL};
/* Parse the arguments */
if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oetet#|ii" BYTES_CHAR,
kwlist, &myarg, NULL, &attrname, NULL,
&buf, &bufsize, &flags, &nofollow, &ns))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0) {
res = NULL;
goto freearg;
}
if(merge_ns(ns, attrname, &full_name, &newname) < 0) {
res = NULL;
goto freearg;
}
/* Set the attribute's value */
nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
PyMem_Free(newname);
free_tgt(&tgt);
if(nret == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freearg;
}
Py_INCREF(Py_None);
res = Py_None;
freearg:
PyMem_Free(attrname);
PyMem_Free(buf);
/* Return the result */
return res;
}
static char __pyremovexattr_doc__[] =
"removexattr(item, name[, nofollow])\n"
"Remove an attribute from a file (deprecated).\n"
"\n"
ITEM_DOC
NAME_REMOVE_DOC
NOFOLLOW_DOC
"\n"
".. deprecated:: 0.4\n"
" this function has been deprecated by the :func:`remove` function.\n"
;
/* Wrapper for removexattr */
static PyObject *
pyremovexattr(PyObject *self, PyObject *args)
{
PyObject *myarg, *res;
int nofollow = 0;
char *attrname = NULL;
int nret;
target_t tgt;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0) {
res = NULL;
goto freearg;
}
/* Remove the attribute */
nret = _remove_obj(&tgt, attrname);
free_tgt(&tgt);
if(nret == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freearg;
}
Py_INCREF(Py_None);
res = Py_None;
freearg:
PyMem_Free(attrname);
/* Return the result */
return res;
}
static char __remove_doc__[] =
"remove(item, name[, nofollow=False, namespace=None])\n"
"Remove an attribute from a file.\n"
"\n"
"Example:\n"
"\n"
" >>> xattr.remove('/path/to/file', 'user.comment')\n"
"\n"
ITEM_DOC
NAME_REMOVE_DOC
NOFOLLOW_DOC
NS_DOC
":returns: None\n"
":raises EnvironmentError: caused by any system errors\n"
"\n"
".. versionadded:: 0.4\n"
NS_CHANGED_DOC
;
/* Wrapper for removexattr */
static PyObject *
xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
{
PyObject *myarg, *res;
int nofollow = 0;
char *attrname = NULL, *name_buf;
const char *ns = NULL;
const char *full_name;
int nret;
target_t tgt;
static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
/* Parse the arguments */
if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|i" BYTES_CHAR, kwlist,
&myarg, NULL, &attrname, &nofollow, &ns))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0) {
res = NULL;
goto freearg;
}
if(merge_ns(ns, attrname, &full_name, &name_buf) < 0) {
res = NULL;
goto freearg;
}
/* Remove the attribute */
nret = _remove_obj(&tgt, full_name);
PyMem_Free(name_buf);
free_tgt(&tgt);
if(nret == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freearg;
}
Py_INCREF(Py_None);
res = Py_None;
freearg:
PyMem_Free(attrname);
/* Return the result */
return res;
}
static char __pylistxattr_doc__[] =
"listxattr(item[, nofollow=False])\n"
"Return the list of attribute names for a file (deprecated).\n"
"\n"
ITEM_DOC
NOFOLLOW_DOC
"\n"
".. deprecated:: 0.4\n"
" this function has been deprecated by the :func:`list` function.\n"
;
/* Wrapper for listxattr */
static PyObject *
pylistxattr(PyObject *self, PyObject *args)
{
char *buf;
int nofollow=0;
ssize_t nalloc, nret;
PyObject *myarg;
PyObject *mylist;
Py_ssize_t nattrs;
char *s;
target_t tgt;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0)
return NULL;
/* Find out the needed size of the buffer */
if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
mylist = PyErr_SetFromErrno(PyExc_IOError);
goto freetgt;
}
/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
mylist = PyErr_NoMemory();
goto freetgt;
}
/* Now retrieve the list of attributes */
if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
mylist = PyErr_SetFromErrno(PyExc_IOError);
goto freebuf;
}
/* Compute the number of attributes in the list */
for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
nattrs++;
}
/* Create the list which will hold the result */
mylist = PyList_New(nattrs);
if(mylist == NULL)
goto freebuf;
/* Create and insert the attributes as strings in the list */
for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
PyObject *item = PyBytes_FromString(s);
if(item == NULL) {
Py_DECREF(mylist);
mylist = NULL;
goto freebuf;
}
PyList_SET_ITEM(mylist, nattrs, item);
nattrs++;
}
freebuf:
/* Free the buffer, now it is no longer needed */
PyMem_Free(buf);
freetgt:
free_tgt(&tgt);
/* Return the result */
return mylist;
}
static char __list_doc__[] =
"list(item[, nofollow=False, namespace=None])\n"
"Return the list of attribute names for a file.\n"
"\n"
"Example:\n"
"\n"
" >>> xattr.list('/path/to/file')\n"
" ['user.test', 'user.comment', 'system.posix_acl_access']\n"
" >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
" ['test', 'comment']\n"
"\n"
ITEM_DOC
NOFOLLOW_DOC
NS_DOC
":returns: the list of attributes; note that if a namespace \n"
" argument was passed, it (and the separator) will be stripped\n"
" from the names\n"
" returned\n"
":rtype: list\n"
":raises EnvironmentError: caused by any system errors\n"
"\n"
".. versionadded:: 0.4\n"
NS_CHANGED_DOC
;
/* Wrapper for listxattr */
static PyObject *
xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
{
char *buf;
int nofollow = 0;
ssize_t nalloc, nret;
PyObject *myarg;
PyObject *res;
const char *ns = NULL;
Py_ssize_t nattrs;
char *s;
target_t tgt;
static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
/* Parse the arguments */
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|i" BYTES_CHAR, kwlist,
&myarg, &nofollow, &ns))
return NULL;
if(convert_obj(myarg, &tgt, nofollow) < 0) {
res = NULL;
goto freearg;
}
/* Find out the needed size of the buffer */
if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freetgt;
}
/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
goto freetgt;
}
/* Now retrieve the list of attributes */
if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto freebuf;
}
/* Compute the number of attributes in the list */
for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
if(matches_ns(ns, s) != NULL)
nattrs++;
}
/* Create the list which will hold the result */
res = PyList_New(nattrs);
if(res == NULL)
goto freebuf;
/* Create and insert the attributes as strings in the list */
for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
const char *name = matches_ns(ns, s);
if(name!=NULL) {
PyObject *item = PyBytes_FromString(name);
if(item == NULL) {
Py_DECREF(res);
res = NULL;
goto freebuf;
}
PyList_SET_ITEM(res, nattrs, item);
nattrs++;