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
|
/* EINA - EFL data type library
* Copyright (C) 2011 Cedric Bail
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <string.h>
#include <math.h>
#ifdef HAVE_XATTR
# include <sys/xattr.h>
#endif
#include "eina_config.h"
#include "eina_private.h"
#include "eina_safety_checks.h"
#include "eina_xattr.h"
#include "eina_convert.h"
/*============================================================================*
* Local *
*============================================================================*/
/**
* @cond LOCAL
*/
typedef struct _Eina_Xattr_Iterator Eina_Xattr_Iterator;
struct _Eina_Xattr_Iterator
{
Eina_Iterator iterator;
ssize_t length;
ssize_t offset;
char xattr[1];
};
#ifdef HAVE_XATTR
static Eina_Bool
_eina_xattr_ls_iterator_next(Eina_Xattr_Iterator *it, void **data)
{
if (it->offset >= it->length)
return EINA_FALSE;
*data = it->xattr + it->offset;
it->offset += strlen(it->xattr + it->offset) + 1;
return EINA_TRUE;
}
static void *
_eina_xattr_ls_iterator_container(Eina_Xattr_Iterator *it __UNUSED__)
{
return NULL;
}
static void
_eina_xattr_ls_iterator_free(Eina_Xattr_Iterator *it)
{
EINA_MAGIC_SET(&it->iterator, 0);
free(it);
}
#endif
/**
* @endcond
*/
/*============================================================================*
* Global *
*============================================================================*/
/*============================================================================*
* API *
*============================================================================*/
EAPI Eina_Iterator *
eina_xattr_ls(const char *file)
{
#ifdef HAVE_XATTR
Eina_Xattr_Iterator *it;
ssize_t length;
EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
length = listxattr(file, NULL, 0);
if (length <= 0) return NULL;
it = calloc(1, sizeof (Eina_Xattr_Iterator) + length - 1);
if (!it) return NULL;
EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
it->length = listxattr(file, it->xattr, length);
if (it->length != length)
{
free(it);
return NULL;
}
it->iterator.version = EINA_ITERATOR_VERSION;
it->iterator.next = FUNC_ITERATOR_NEXT(_eina_xattr_ls_iterator_next);
it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_xattr_ls_iterator_container);
it->iterator.free = FUNC_ITERATOR_FREE(_eina_xattr_ls_iterator_free);
return &it->iterator;
#else
return NULL;
(void)file;
#endif
}
EAPI void *
eina_xattr_get(const char *file, const char *attribute, ssize_t *size)
{
#ifdef HAVE_XATTR
void *ret = NULL;
ssize_t tmp;
EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
EINA_SAFETY_ON_NULL_RETURN_VAL(attribute, NULL);
EINA_SAFETY_ON_TRUE_RETURN_VAL(!size, NULL);
*size = getxattr(file, attribute, NULL, 0);
/* Size should be less than 2MB (already huge in my opinion) */
if (!(*size > 0 && *size < 2 * 1024 * 1024))
goto on_error;
ret = malloc(*size);
if (!ret) return NULL;
tmp = getxattr(file, attribute, ret, *size);
if (tmp != *size)
goto on_error;
return ret;
on_error:
free(ret);
*size = 0;
return NULL;
#else
EINA_SAFETY_ON_TRUE_RETURN_VAL(!size, NULL);
*size = 0;
return NULL;
(void)file;
(void)attribute;
#endif
}
EAPI Eina_Bool
eina_xattr_set(const char *file, const char *attribute, const void *data, ssize_t length, Eina_Xattr_Flags flags)
{
#ifdef HAVE_XATTR
int iflags;
EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
EINA_SAFETY_ON_NULL_RETURN_VAL(attribute, EINA_FALSE);
EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
EINA_SAFETY_ON_TRUE_RETURN_VAL(!(length > 0 && length < 2 * 1024 * 1024), EINA_FALSE);
switch (flags)
{
case EINA_XATTR_INSERT: iflags = 0; break;
case EINA_XATTR_REPLACE: iflags = XATTR_REPLACE; break;
case EINA_XATTR_CREATED: iflags = XATTR_CREATE; break;
default:
return EINA_FALSE;
}
if (setxattr(file, attribute, data, length, iflags))
return EINA_FALSE;
return EINA_TRUE;
#else
return EINA_FALSE;
(void)file;
(void)attribute;
(void)data;
(void)length;
(void)flags;
#endif
}
EAPI Eina_Bool
eina_xattr_string_set(const char *file, const char *attribute, const char *data, Eina_Xattr_Flags flags)
{
EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
return eina_xattr_set(file, attribute, data, strlen(data) + 1, flags);
}
EAPI char *
eina_xattr_string_get(const char *file, const char *attribute)
{
char *tmp;
ssize_t size;
tmp = eina_xattr_get(file, attribute, &size);
if (!tmp) return NULL;
if (tmp[size - 1] != '\0')
{
free(tmp);
return NULL;
}
return tmp;
}
EAPI Eina_Bool
eina_xattr_double_set(const char *file, const char *attribute, double value, Eina_Xattr_Flags flags)
{
char buffer[128];
eina_convert_dtoa(value, buffer);
return eina_xattr_string_set(file, attribute, buffer, flags);
}
EAPI Eina_Bool
eina_xattr_double_get(const char *file, const char *attribute, double *value)
{
char *tmp;
long long int m = 0;
long int e = 0;
EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE);
tmp = eina_xattr_string_get(file, attribute);
if (!tmp) return EINA_FALSE;
if (!eina_convert_atod(tmp, strlen(tmp), &m, &e))
{
free(tmp);
return EINA_FALSE;
}
*value = ldexp((double)m, e);
free(tmp);
return EINA_TRUE;
}
EAPI Eina_Bool
eina_xattr_int_set(const char *file, const char *attribute, int value, Eina_Xattr_Flags flags)
{
char buffer[10];
eina_convert_itoa(value, buffer);
return eina_xattr_string_set(file, attribute, buffer, flags);
}
EAPI Eina_Bool
eina_xattr_int_get(const char *file, const char *attribute, int *value)
{
char *tmp;
char *eos;
Eina_Bool result;
EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE);
tmp = eina_xattr_string_get(file, attribute);
if (!tmp) return EINA_FALSE;
*value = (int) strtol(tmp, &eos, 10);
result = (*eos == '\0');
free(tmp);
return result;
}
|