summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCedric BAIL <cedric.bail@free.fr>2011-09-15 15:37:28 +0000
committerCedric BAIL <cedric.bail@free.fr>2011-09-15 15:37:28 +0000
commit0c6ed213ada3cca2d3362c202c0c587b75576fc2 (patch)
treebca00aa284ba1e71babdfc19d4c24796c6605a6b
parent286252b18ae24d6956cba95d4426ad7a0dc1c102 (diff)
eina: add eina_xattr.{c,h}.
SVN revision: 63412
-rw-r--r--legacy/eina/ChangeLog6
-rw-r--r--legacy/eina/src/include/Eina.h1
-rw-r--r--legacy/eina/src/include/Makefile.am3
-rw-r--r--legacy/eina/src/include/eina_file.h48
-rw-r--r--legacy/eina/src/include/eina_xattr.h166
-rw-r--r--legacy/eina/src/lib/Makefile.am3
-rw-r--r--legacy/eina/src/lib/eina_file.c133
-rw-r--r--legacy/eina/src/lib/eina_xattr.c251
8 files changed, 430 insertions, 181 deletions
diff --git a/legacy/eina/ChangeLog b/legacy/eina/ChangeLog
index 1b00657235..157e696ef5 100644
--- a/legacy/eina/ChangeLog
+++ b/legacy/eina/ChangeLog
@@ -136,3 +136,9 @@
136 * Strbuf + Ustrbuf: Added eina_(u)strbuf_manage_new. This lets us 136 * Strbuf + Ustrbuf: Added eina_(u)strbuf_manage_new. This lets us
137 take a string and use it as the string buffer instead of copying 137 take a string and use it as the string buffer instead of copying
138 and allocating. 138 and allocating.
139
1402011-09-15 Cedric Bail
141
142 * Add eina_xattr_ls, eina_xattr_get, eina_xattr_set, eina_xattr_string_set,
143 eina_xattr_string_get, eina_xattr_double_set, eina_xattr_double_get,
144 eina_xattr_int_set, eina_xattr_int_get.
diff --git a/legacy/eina/src/include/Eina.h b/legacy/eina/src/include/Eina.h
index 69e463cbf4..7dcddca636 100644
--- a/legacy/eina/src/include/Eina.h
+++ b/legacy/eina/src/include/Eina.h
@@ -185,6 +185,7 @@ extern "C" {
185#include "eina_prefix.h" 185#include "eina_prefix.h"
186#include "eina_refcount.h" 186#include "eina_refcount.h"
187#include "eina_mmap.h" 187#include "eina_mmap.h"
188#include "eina_xattr.h"
188 189
189#ifdef __cplusplus 190#ifdef __cplusplus
190} 191}
diff --git a/legacy/eina/src/include/Makefile.am b/legacy/eina/src/include/Makefile.am
index 8fc4763d00..5204db51b8 100644
--- a/legacy/eina/src/include/Makefile.am
+++ b/legacy/eina/src/include/Makefile.am
@@ -58,7 +58,8 @@ eina_simple_xml_parser.h \
58eina_lock.h \ 58eina_lock.h \
59eina_prefix.h \ 59eina_prefix.h \
60eina_refcount.h \ 60eina_refcount.h \
61eina_mmap.h 61eina_mmap.h \
62eina_xattr.h
62 63
63# Will be back for developper after 1.1. 64# Will be back for developper after 1.1.
64# eina_object.h 65# eina_object.h
diff --git a/legacy/eina/src/include/eina_file.h b/legacy/eina/src/include/eina_file.h
index c9067c7861..472ff98d49 100644
--- a/legacy/eina/src/include/eina_file.h
+++ b/legacy/eina/src/include/eina_file.h
@@ -1,5 +1,6 @@
1/* EINA - EFL data type library 1/* EINA - EFL data type library
2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga 2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
3 * 2011 Cedric Bail
3 * 4 *
4 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public 6 * modify it under the terms of the GNU Lesser General Public
@@ -112,12 +113,6 @@ typedef enum {
112 EINA_FILE_WHT /**< Whiteout file type (unused on Windows). */ 113 EINA_FILE_WHT /**< Whiteout file type (unused on Windows). */
113} Eina_File_Type; 114} Eina_File_Type;
114 115
115typedef enum {
116 EINA_XATTR_INSERT, /**< This is the default behaviour, it will either create or replace the extended attribute */
117 EINA_XATTR_REPLACE, /**< This will only succeed if the extended attribute previously existed */
118 EINA_XATTR_CREATED /**< This will only succeed if the extended attribute wasn't previously set */
119} Eina_Xattr_Flags;
120
121typedef struct _Eina_File Eina_File; 116typedef struct _Eina_File Eina_File;
122 117
123typedef enum { 118typedef enum {
@@ -280,47 +275,6 @@ EAPI Eina_Iterator *eina_file_stat_ls(const char *dir) EINA_WARN_UNUSED_RESULT E
280EAPI Eina_Iterator *eina_file_direct_ls(const char *dir) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC; 275EAPI Eina_Iterator *eina_file_direct_ls(const char *dir) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
281 276
282/** 277/**
283 * @brief Get an iterator that list all extended attribute of a file.
284 *
285 * @param file The filename to retrieve the extended attribute list from.
286 * @return an iterator.
287 *
288 * The iterator will not allocate any data during the iteration step, so you need to copy them yourself
289 * if you need.
290 *
291 * @since 1.1
292 */
293EAPI Eina_Iterator *eina_xattr_ls(const char *file);
294
295/**
296 * @brief Retrieve an extended attribute from a file.
297 *
298 * @param file The file to retrieve the extended attribute from.
299 * @param atttribute The extended attribute name to retrieve.
300 * @param size The size of the retrieved extended attribute.
301 * @return the allocated data that hold the extended attribute value.
302 *
303 * It will return NULL and *size will be @c 0 if it fails.
304 *
305 * @since 1.1
306 */
307EAPI void *eina_xattr_get(const char *file, const char *attribute, ssize_t *size);
308
309/**
310 * @brief Set an extended attribute on a file.
311 *
312 * @param file The file to set the extended attribute to.
313 * @param attribute The attribute to set.
314 * @param data The data to set.
315 * @param length The length of the data to set.
316 * @param flags Define the set policy
317 * @return EINA_TRUE on success, EINA_FALSE otherwise.
318 *
319 * @since 1.1
320 */
321EAPI Eina_Bool eina_xattr_set(const char *file, const char *attribute, const void *data, ssize_t length, Eina_Xattr_Flags flags);
322
323/**
324 * @brief Get a read-only handler to a file. 278 * @brief Get a read-only handler to a file.
325 * 279 *
326 * @param name Filename to open 280 * @param name Filename to open
diff --git a/legacy/eina/src/include/eina_xattr.h b/legacy/eina/src/include/eina_xattr.h
new file mode 100644
index 0000000000..80586b7bba
--- /dev/null
+++ b/legacy/eina/src/include/eina_xattr.h
@@ -0,0 +1,166 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2011 Cedric Bail
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef EINA_XATTR_H_
20#define EINA_XATTR_H_
21
22#include "eina_types.h"
23
24/**
25 * @addtogroup Eina_Tools_Group Tools
26 *
27 * @{
28 */
29
30/**
31 * @typedef Eina_Xattr_Flags
32 * define extended attribute creation
33 */
34typedef enum {
35 EINA_XATTR_INSERT, /**< This is the default behaviour, it will either create or replace the extended attribute */
36 EINA_XATTR_REPLACE, /**< This will only succeed if the extended attribute previously existed */
37 EINA_XATTR_CREATED /**< This will only succeed if the extended attribute wasn't previously set */
38} Eina_Xattr_Flags;
39
40
41/**
42 * @brief Get an iterator that list all extended attribute of a file.
43 *
44 * @param file The filename to retrieve the extended attribute list from.
45 * @return an iterator.
46 *
47 * The iterator will not allocate any data during the iteration step, so you need to copy them yourself
48 * if you need.
49 *
50 * @since 1.1
51 */
52EAPI Eina_Iterator *eina_xattr_ls(const char *file);
53
54/**
55 * @brief Retrieve an extended attribute from a file.
56 *
57 * @param file The file to retrieve the extended attribute from.
58 * @param atttribute The extended attribute name to retrieve.
59 * @param size The size of the retrieved extended attribute.
60 * @return the allocated data that hold the extended attribute value.
61 *
62 * It will return NULL and *size will be @c 0 if it fails.
63 *
64 * @since 1.1
65 */
66EAPI void *eina_xattr_get(const char *file, const char *attribute, ssize_t *size);
67
68/**
69 * @brief Set an extended attribute on a file.
70 *
71 * @param file The file to set the extended attribute to.
72 * @param attribute The attribute to set.
73 * @param data The data to set.
74 * @param length The length of the data to set.
75 * @param flags Define the set policy.
76 * @return EINA_TRUE on success, EINA_FALSE otherwise.
77 *
78 * @since 1.1
79 */
80EAPI Eina_Bool eina_xattr_set(const char *file, const char *attribute, const void *data, ssize_t length, Eina_Xattr_Flags flags);
81
82/**
83 * @brief Set a string as a extended attribute properties.
84 *
85 * @param file The file to set the string to.
86 * @param attribute The attribute to set.
87 * @param data The NULL terminated string to set.
88 * @param flags Define the set policy.
89 * @return EINA_TRUE on success, EINA_FALSE otherwise.
90 *
91 * @since 1.1
92 */
93EAPI Eina_Bool eina_xattr_string_set(const char *file, const char *attribute, const char *data, Eina_Xattr_Flags flags);
94
95/**
96 * @brief Get a string from an extended attribute properties.
97 *
98 * @param file The file to get the string from.
99 * @param attribute The attribute to get.
100 * @return a valid string on success, NULL otherwise.
101 *
102 * This call check that the string is properly NULL-terminated before returning it.
103 *
104 * @since 1.1
105 */
106EAPI char *eina_xattr_string_get(const char *file, const char *attribute);
107
108/**
109 * @brief Set a double as a extended attribute properties.
110 *
111 * @param file The file to set the double to.
112 * @param attribute The attribute to set.
113 * @param data The NULL terminated double to set.
114 * @param flags Define the set policy.
115 * @return EINA_TRUE on success, EINA_FALSE otherwise.
116 *
117 * @since 1.1
118 */
119EAPI Eina_Bool eina_xattr_double_set(const char *file, const char *attribute, double value, Eina_Xattr_Flags flags);
120
121/**
122 * @brief Get a double from an extended attribute properties.
123 *
124 * @param file The file to get the string from.
125 * @param attribute The attribute to get.
126 * @param value Where to put the extracted value
127 * @return EINA_TRUE on success, EINA_FALSE otherwise.
128 *
129 * This call check that the double is correctly set.
130 *
131 * @since 1.1
132 */
133EAPI Eina_Bool eina_xattr_double_get(const char *file, const char *attribute, double *value);
134
135/**
136 * @brief Set an int as a extended attribute properties.
137 *
138 * @param file The file to set the int to.
139 * @param attribute The attribute to set.
140 * @param data The NULL terminated int to set.
141 * @param flags Define the set policy.
142 * @return EINA_TRUE on success, EINA_FALSE otherwise.
143 *
144 * @since 1.1
145 */
146EAPI Eina_Bool eina_xattr_int_set(const char *file, const char *attribute, int value, Eina_Xattr_Flags flags);
147
148/**
149 * @brief Get a int from an extended attribute properties.
150 *
151 * @param file The file to get the string from.
152 * @param attribute The attribute to get.
153 * @param value Where to put the extracted value
154 * @return EINA_TRUE on success, EINA_FALSE otherwise.
155 *
156 * This call check that the int is correctly set.
157 *
158 * @since 1.1
159 */
160EAPI Eina_Bool eina_xattr_int_get(const char *file, const char *attribute, int *value);
161
162/**
163 * @}
164 */
165
166#endif
diff --git a/legacy/eina/src/lib/Makefile.am b/legacy/eina/src/lib/Makefile.am
index 7793593ee3..0537a619d2 100644
--- a/legacy/eina/src/lib/Makefile.am
+++ b/legacy/eina/src/lib/Makefile.am
@@ -49,7 +49,8 @@ eina_tiler.c \
49eina_unicode.c \ 49eina_unicode.c \
50eina_ustrbuf.c \ 50eina_ustrbuf.c \
51eina_ustringshare.c \ 51eina_ustringshare.c \
52eina_value.c 52eina_value.c \
53eina_xattr.c
53 54
54# Will be back for developper after 1.1 55# Will be back for developper after 1.1
55# eina_object.c 56# eina_object.c
diff --git a/legacy/eina/src/lib/eina_file.c b/legacy/eina/src/lib/eina_file.c
index 2c7a6dcc47..a2fedd1a20 100644
--- a/legacy/eina/src/lib/eina_file.c
+++ b/legacy/eina/src/lib/eina_file.c
@@ -1,6 +1,6 @@
1/* EINA - EFL data type library 1/* EINA - EFL data type library
2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga, Vincent Torri 2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga, Vincent Torri
3 * Copyright (C) 2010 Cedric Bail 3 * Copyright (C) 2010-2011 Cedric Bail
4 * 4 *
5 * This library is free software; you can redistribute it and/or 5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public 6 * modify it under the terms of the GNU Lesser General Public
@@ -47,10 +47,6 @@ void *alloca (size_t);
47#include <sys/mman.h> 47#include <sys/mman.h>
48#include <fcntl.h> 48#include <fcntl.h>
49 49
50#ifdef HAVE_XATTR
51# include <sys/xattr.h>
52#endif
53
54#define PATH_DELIM '/' 50#define PATH_DELIM '/'
55 51
56#ifdef __sun 52#ifdef __sun
@@ -103,7 +99,6 @@ void *alloca (size_t);
103 99
104typedef struct _Eina_File_Iterator Eina_File_Iterator; 100typedef struct _Eina_File_Iterator Eina_File_Iterator;
105typedef struct _Eina_File_Map Eina_File_Map; 101typedef struct _Eina_File_Map Eina_File_Map;
106typedef struct _Eina_Xattr_Iterator Eina_Xattr_Iterator;
107 102
108struct _Eina_File_Iterator 103struct _Eina_File_Iterator
109{ 104{
@@ -115,16 +110,6 @@ struct _Eina_File_Iterator
115 char dir[1]; 110 char dir[1];
116}; 111};
117 112
118struct _Eina_Xattr_Iterator
119{
120 Eina_Iterator iterator;
121
122 ssize_t length;
123 ssize_t offset;
124
125 char xattr[1];
126};
127
128struct _Eina_File 113struct _Eina_File
129{ 114{
130 const char *filename; 115 const char *filename;
@@ -394,33 +379,6 @@ _eina_file_stat_ls_iterator_next(Eina_File_Direct_Iterator *it, void **data)
394 return EINA_TRUE; 379 return EINA_TRUE;
395} 380}
396 381
397#ifdef HAVE_XATTR
398static Eina_Bool
399_eina_xattr_ls_iterator_next(Eina_Xattr_Iterator *it, void **data)
400{
401 if (it->offset >= it->length)
402 return EINA_FALSE;
403
404 *data = it->xattr + it->offset;
405 it->offset += strlen(it->xattr + it->offset) + 1;
406
407 return EINA_TRUE;
408}
409
410static void *
411_eina_xattr_ls_iterator_container(Eina_Xattr_Iterator *it __UNUSED__)
412{
413 return NULL;
414}
415
416static void
417_eina_xattr_ls_iterator_free(Eina_Xattr_Iterator *it)
418{
419 EINA_MAGIC_SET(&it->iterator, 0);
420 free(it);
421}
422#endif
423
424static void 382static void
425_eina_file_real_close(Eina_File *file) 383_eina_file_real_close(Eina_File *file)
426{ 384{
@@ -775,95 +733,6 @@ eina_file_stat_ls(const char *dir)
775 return &it->iterator; 733 return &it->iterator;
776} 734}
777 735
778EAPI Eina_Iterator *
779eina_xattr_ls(const char *file)
780{
781 Eina_Xattr_Iterator *it;
782 ssize_t length;
783
784 EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
785
786#ifdef HAVE_XATTR
787 length = listxattr(file, NULL, 0);
788 if (length <= 0) return NULL;
789
790 it = calloc(1, sizeof (Eina_Xattr_Iterator) + length - 1);
791 if (!it) return NULL;
792
793 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
794
795 it->length = listxattr(file, it->xattr, length);
796 if (it->length != length)
797 {
798 free(it);
799 return NULL;
800 }
801
802 it->iterator.version = EINA_ITERATOR_VERSION;
803 it->iterator.next = FUNC_ITERATOR_NEXT(_eina_xattr_ls_iterator_next);
804 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_xattr_ls_iterator_container);
805 it->iterator.free = FUNC_ITERATOR_FREE(_eina_xattr_ls_iterator_free);
806
807 return &it->iterator;
808#else
809 return NULL;
810#endif
811}
812
813EAPI void *
814eina_xattr_get(const char *file, const char *attribute, ssize_t *size)
815{
816 void *ret = NULL;
817 ssize_t tmp;
818
819 EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
820 EINA_SAFETY_ON_NULL_RETURN_VAL(attribute, NULL);
821 EINA_SAFETY_ON_TRUE_RETURN_VAL(!size, NULL);
822
823 *size = getxattr(file, attribute, NULL, 0);
824 /* Size should be less than 2MB (already huge in my opinion) */
825 if (!(*size > 0 && *size < 2 * 1024 * 1024))
826 goto on_error;
827
828 ret = malloc(*size);
829 if (!ret) return NULL;
830
831 tmp = getxattr(file, attribute, ret, *size);
832 if (tmp != *size)
833 goto on_error;
834
835 return ret;
836
837 on_error:
838 free(ret);
839 *size = 0;
840 return NULL;
841}
842
843EAPI Eina_Bool
844eina_xattr_set(const char *file, const char *attribute, const void *data, ssize_t length, Eina_Xattr_Flags flags)
845{
846 int iflags;
847
848 EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
849 EINA_SAFETY_ON_NULL_RETURN_VAL(attribute, EINA_FALSE);
850 EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
851 EINA_SAFETY_ON_TRUE_RETURN_VAL(!(length > 0 && length < 2 * 1024 * 1024), EINA_FALSE);
852
853 switch (flags)
854 {
855 case EINA_XATTR_INSERT: iflags = 0; break;
856 case EINA_XATTR_REPLACE: iflags = XATTR_REPLACE; break;
857 case EINA_XATTR_CREATED: iflags = XATTR_CREATE; break;
858 default:
859 return EINA_FALSE;
860 }
861
862 if (setxattr(file, attribute, data, length, iflags))
863 return EINA_FALSE;
864 return EINA_TRUE;
865}
866
867EAPI Eina_File * 736EAPI Eina_File *
868eina_file_open(const char *filename, Eina_Bool shared) 737eina_file_open(const char *filename, Eina_Bool shared)
869{ 738{
diff --git a/legacy/eina/src/lib/eina_xattr.c b/legacy/eina/src/lib/eina_xattr.c
new file mode 100644
index 0000000000..f7e0ddff5e
--- /dev/null
+++ b/legacy/eina/src/lib/eina_xattr.c
@@ -0,0 +1,251 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2011 Cedric Bail
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <sys/types.h>
24#include <string.h>
25#include <math.h>
26
27#ifdef HAVE_XATTR
28# include <sys/xattr.h>
29#endif
30
31#include "eina_config.h"
32#include "eina_private.h"
33
34#include "eina_safety_checks.h"
35#include "eina_xattr.h"
36#include "eina_convert.h"
37
38typedef struct _Eina_Xattr_Iterator Eina_Xattr_Iterator;
39
40struct _Eina_Xattr_Iterator
41{
42 Eina_Iterator iterator;
43
44 ssize_t length;
45 ssize_t offset;
46
47 char xattr[1];
48};
49
50#ifdef HAVE_XATTR
51static Eina_Bool
52_eina_xattr_ls_iterator_next(Eina_Xattr_Iterator *it, void **data)
53{
54 if (it->offset >= it->length)
55 return EINA_FALSE;
56
57 *data = it->xattr + it->offset;
58 it->offset += strlen(it->xattr + it->offset) + 1;
59
60 return EINA_TRUE;
61}
62
63static void *
64_eina_xattr_ls_iterator_container(Eina_Xattr_Iterator *it __UNUSED__)
65{
66 return NULL;
67}
68
69static void
70_eina_xattr_ls_iterator_free(Eina_Xattr_Iterator *it)
71{
72 EINA_MAGIC_SET(&it->iterator, 0);
73 free(it);
74}
75#endif
76
77EAPI Eina_Iterator *
78eina_xattr_ls(const char *file)
79{
80 Eina_Xattr_Iterator *it;
81 ssize_t length;
82
83 EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
84
85#ifdef HAVE_XATTR
86 length = listxattr(file, NULL, 0);
87 if (length <= 0) return NULL;
88
89 it = calloc(1, sizeof (Eina_Xattr_Iterator) + length - 1);
90 if (!it) return NULL;
91
92 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
93
94 it->length = listxattr(file, it->xattr, length);
95 if (it->length != length)
96 {
97 free(it);
98 return NULL;
99 }
100
101 it->iterator.version = EINA_ITERATOR_VERSION;
102 it->iterator.next = FUNC_ITERATOR_NEXT(_eina_xattr_ls_iterator_next);
103 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_xattr_ls_iterator_container);
104 it->iterator.free = FUNC_ITERATOR_FREE(_eina_xattr_ls_iterator_free);
105
106 return &it->iterator;
107#else
108 return NULL;
109#endif
110}
111
112EAPI void *
113eina_xattr_get(const char *file, const char *attribute, ssize_t *size)
114{
115 void *ret = NULL;
116 ssize_t tmp;
117
118 EINA_SAFETY_ON_NULL_RETURN_VAL(file, NULL);
119 EINA_SAFETY_ON_NULL_RETURN_VAL(attribute, NULL);
120 EINA_SAFETY_ON_TRUE_RETURN_VAL(!size, NULL);
121
122 *size = getxattr(file, attribute, NULL, 0);
123 /* Size should be less than 2MB (already huge in my opinion) */
124 if (!(*size > 0 && *size < 2 * 1024 * 1024))
125 goto on_error;
126
127 ret = malloc(*size);
128 if (!ret) return NULL;
129
130 tmp = getxattr(file, attribute, ret, *size);
131 if (tmp != *size)
132 goto on_error;
133
134 return ret;
135
136 on_error:
137 free(ret);
138 *size = 0;
139 return NULL;
140}
141
142EAPI Eina_Bool
143eina_xattr_set(const char *file, const char *attribute, const void *data, ssize_t length, Eina_Xattr_Flags flags)
144{
145 int iflags;
146
147 EINA_SAFETY_ON_NULL_RETURN_VAL(file, EINA_FALSE);
148 EINA_SAFETY_ON_NULL_RETURN_VAL(attribute, EINA_FALSE);
149 EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
150 EINA_SAFETY_ON_TRUE_RETURN_VAL(!(length > 0 && length < 2 * 1024 * 1024), EINA_FALSE);
151
152 switch (flags)
153 {
154 case EINA_XATTR_INSERT: iflags = 0; break;
155 case EINA_XATTR_REPLACE: iflags = XATTR_REPLACE; break;
156 case EINA_XATTR_CREATED: iflags = XATTR_CREATE; break;
157 default:
158 return EINA_FALSE;
159 }
160
161 if (setxattr(file, attribute, data, length, iflags))
162 return EINA_FALSE;
163 return EINA_TRUE;
164}
165
166EAPI Eina_Bool
167eina_xattr_string_set(const char *file, const char *attribute, const char *data, Eina_Xattr_Flags flags)
168{
169 EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
170
171 return eina_xattr_set(file, attribute, data, strlen(data) + 1, flags);
172}
173
174EAPI char *
175eina_xattr_string_get(const char *file, const char *attribute)
176{
177 char *tmp;
178 ssize_t size;
179
180 tmp = eina_xattr_get(file, attribute, &size);
181 if (!tmp) return NULL;
182
183 if (tmp[size - 1] != '\0')
184 {
185 free(tmp);
186 return NULL;
187 }
188
189 return tmp;
190}
191
192EAPI Eina_Bool
193eina_xattr_double_set(const char *file, const char *attribute, double value, Eina_Xattr_Flags flags)
194{
195 char buffer[128];
196
197 eina_convert_dtoa(value, buffer);
198 return eina_xattr_string_set(file, attribute, buffer, flags);
199}
200
201EAPI Eina_Bool
202eina_xattr_double_get(const char *file, const char *attribute, double *value)
203{
204 char *tmp;
205 long long int m = 0;
206 long int e = 0;
207
208 EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE);
209
210 tmp = eina_xattr_string_get(file, attribute);
211 if (!tmp) return EINA_FALSE;
212
213 if (!eina_convert_atod(tmp, strlen(tmp), &m, &e))
214 {
215 free(tmp);
216 return EINA_FALSE;
217 }
218
219 *value = ldexp((double)m, e);
220 free(tmp);
221
222 return EINA_TRUE;
223}
224
225EAPI Eina_Bool
226eina_xattr_int_set(const char *file, const char *attribute, int value, Eina_Xattr_Flags flags)
227{
228 char buffer[10];
229
230 eina_convert_itoa(value, buffer);
231 return eina_xattr_string_set(file, attribute, buffer, flags);
232}
233
234EAPI Eina_Bool
235eina_xattr_int_get(const char *file, const char *attribute, int *value)
236{
237 char *tmp;
238 char *eos;
239 Eina_Bool result;
240
241 EINA_SAFETY_ON_NULL_RETURN_VAL(value, EINA_FALSE);
242
243 tmp = eina_xattr_string_get(file, attribute);
244 if (!tmp) return EINA_FALSE;
245
246 *value = (int) strtol(tmp, &eos, 10);
247 result = (*eos == '\0');
248 free(tmp);
249
250 return result;
251}