-
Notifications
You must be signed in to change notification settings - Fork 0
/
XAttr.xs
194 lines (180 loc) · 4 KB
/
XAttr.xs
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
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include <sys/xattr.h>
#include "const-c.inc"
MODULE = MacOSX::File::XAttr PACKAGE = MacOSX::File::XAttr
INCLUDE: const-xs.inc
PROTOTYPES: ENABLE
void
getxattr(path, name, options = 0, position = 0)
char *path;
char *name;
int options;
U32 position;
PREINIT:
ssize_t len;
PPCODE:
len = getxattr(path, name, NULL, 0, position, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_UNDEF;
}
char buffer[len];
len = getxattr(path, name, buffer, len, position, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_UNDEF;
}
mXPUSHp(buffer, len);
void
fgetxattr(file, name, options = 0, position = 0)
PerlIO *file;
char *name;
int options;
U32 position;
PROTOTYPE: *$;$$
PREINIT:
int fd;
ssize_t len;
PPCODE:
fd = PerlIO_fileno(file);
len = fgetxattr(fd, name, NULL, 0, position, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_UNDEF;
}
char buffer[len];
len = fgetxattr(fd, name, buffer, len, position, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_UNDEF;
}
mXPUSHp(buffer, len);
bool
setxattr(path, name, value, options = 0, position = 0)
char *path;
char *name;
SV *value;
int options;
U32 position;
PREINIT:
void *val;
size_t len;
int status;
CODE:
val = SvPV(value, len);
status = setxattr(path, name, val, len, position, options);
if (status < 0) {
SETERRNO(errno, errno);
XSRETURN_NO;
}
else {
XSRETURN_YES;
}
bool
fsetxattr(file, name, value, options = 0, position = 0)
PerlIO *file;
char *name;
SV *value;
int options;
U32 position;
PROTOTYPE: *$$;$$
PREINIT:
int fd = PerlIO_fileno(file);
void *val;
size_t len;
int status;
CODE:
val = SvPV(value, len);
status = fsetxattr(fd, name, val, len, position, options);
if (status < 0) {
SETERRNO(errno, errno);
XSRETURN_NO;
}
else {
XSRETURN_YES;
}
OUTPUT:
RETVAL
void
listxattr(path, options = 0)
char *path;
int options;
PREINIT:
ssize_t len, slen;
char *scan, *end;
PPCODE:
len = listxattr(path, NULL, 0, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_EMPTY;
}
char buffer[len];
len = listxattr(path, buffer, len, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_EMPTY;
}
end = buffer + len;
for (scan = buffer; scan < end; scan += slen + 1) {
slen = strnlen(scan, end - scan);
mXPUSHp(scan, slen);
}
void
flistxattr(file, options = 0)
PerlIO *file;
int options;
PROTOTYPE: *;$
PREINIT:
ssize_t len, slen;
char *scan, *end;
int fd = PerlIO_fileno(file);
PPCODE:
len = flistxattr(fd, NULL, 0, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_EMPTY;
}
char buffer[len];
len = flistxattr(fd, buffer, len, options);
if (len < 0) {
SETERRNO(errno, errno);
XSRETURN_EMPTY;
}
end = buffer + len;
for (scan = buffer; scan < end; scan += slen + 1) {
slen = strnlen(scan, end - scan);
mXPUSHp(scan, slen);
}
int
removexattr(path, name, options = 0)
char *path
char *name
int options
PREINIT:
int status;
CODE:
if (removexattr(path, name, options) < 0) {
SETERRNO(errno, errno);
XSRETURN_NO;
}
else {
XSRETURN_YES;
}
int
fremovexattr(file, name, options = 0)
PerlIO *file
char *name
int options
PREINIT:
int status;
CODE:
if (fremovexattr(PerlIO_fileno(file), name, options) < 0) {
SETERRNO(errno, errno);
XSRETURN_NO;
}
else {
XSRETURN_YES;
}