summaryrefslogtreecommitdiff
path: root/legacy
diff options
context:
space:
mode:
authorCedric BAIL <cedric.bail@free.fr>2011-09-15 12:39:40 +0000
committerCedric BAIL <cedric.bail@free.fr>2011-09-15 12:39:40 +0000
commit680170b3f9aaec340ba38587ea1ba5919e61555e (patch)
tree6843bc7f08a228b16d27d48e29cf061dd992c9e4 /legacy
parent8e9fbe56623a491dc53443b3be537f16ce8c13d7 (diff)
eina: add XAttr support to eina.
SVN revision: 63408
Diffstat (limited to 'legacy')
-rw-r--r--legacy/eina/configure.ac24
-rw-r--r--legacy/eina/src/include/eina_file.h47
-rw-r--r--legacy/eina/src/lib/eina_file.c127
3 files changed, 198 insertions, 0 deletions
diff --git a/legacy/eina/configure.ac b/legacy/eina/configure.ac
index abc583afdf..7c2a2d5de4 100644
--- a/legacy/eina/configure.ac
+++ b/legacy/eina/configure.ac
@@ -621,6 +621,29 @@ fi
621 621
622AC_SUBST([rt_libs]) 622AC_SUBST([rt_libs])
623 623
624### Check for extended attribute
625
626AC_COMPILE_IFELSE(
627 [AC_LANG_PROGRAM(
628 [[
629#include <stdlib.h>
630#include <sys/types.h>
631#include <sys/xattr.h>
632 ]],
633 [[
634size_t tmp = listxattr("/", NULL, 0);
635tmp = getxattr("/", "user.ethumb.md5", NULL, 0);
636setxattr("/", "user.ethumb.md5", NULL, 0, 0);
637 ]])],
638 [
639 AC_DEFINE(HAVE_XATTR, 1, [Define to 1 if you have 'listxattr', 'setxattr' and 'getxattr'])
640 have_xattr="yes"
641 ],
642 [have_xattr="no"])
643
644AC_MSG_CHECKING([for Xattr])
645AC_MSG_RESULT([${have_xattr}])
646
624### Modules 647### Modules
625 648
626if test "x${have_default_mempool}" = "xyes" ; then 649if test "x${have_default_mempool}" = "xyes" ; then
@@ -736,6 +759,7 @@ fi
736echo " Amalgamation.........: ${do_amalgamation}" 759echo " Amalgamation.........: ${do_amalgamation}"
737echo " Iconv support........: ${have_iconv}" 760echo " Iconv support........: ${have_iconv}"
738echo " File dirfd...........: ${have_dirfd}" 761echo " File dirfd...........: ${have_dirfd}"
762echo " File xattr...........: ${have_xattr}"
739echo 763echo
740echo " Documentation........: ${build_doc}" 764echo " Documentation........: ${build_doc}"
741echo " Tests................: ${enable_tests}" 765echo " Tests................: ${enable_tests}"
diff --git a/legacy/eina/src/include/eina_file.h b/legacy/eina/src/include/eina_file.h
index ca97440c85..c9067c7861 100644
--- a/legacy/eina/src/include/eina_file.h
+++ b/legacy/eina/src/include/eina_file.h
@@ -112,6 +112,12 @@ typedef enum {
112 EINA_FILE_WHT /**< Whiteout file type (unused on Windows). */ 112 EINA_FILE_WHT /**< Whiteout file type (unused on Windows). */
113} Eina_File_Type; 113} Eina_File_Type;
114 114
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
115typedef struct _Eina_File Eina_File; 121typedef struct _Eina_File Eina_File;
116 122
117typedef enum { 123typedef enum {
@@ -274,6 +280,47 @@ EAPI Eina_Iterator *eina_file_stat_ls(const char *dir) EINA_WARN_UNUSED_RESULT E
274EAPI Eina_Iterator *eina_file_direct_ls(const char *dir) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC; 280EAPI Eina_Iterator *eina_file_direct_ls(const char *dir) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
275 281
276/** 282/**
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/**
277 * @brief Get a read-only handler to a file. 324 * @brief Get a read-only handler to a file.
278 * 325 *
279 * @param name Filename to open 326 * @param name Filename to open
diff --git a/legacy/eina/src/lib/eina_file.c b/legacy/eina/src/lib/eina_file.c
index 211912b5a9..fd76dd1c3e 100644
--- a/legacy/eina/src/lib/eina_file.c
+++ b/legacy/eina/src/lib/eina_file.c
@@ -47,6 +47,10 @@ 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
50#define PATH_DELIM '/' 54#define PATH_DELIM '/'
51 55
52#ifdef __sun 56#ifdef __sun
@@ -99,6 +103,7 @@ void *alloca (size_t);
99 103
100typedef struct _Eina_File_Iterator Eina_File_Iterator; 104typedef struct _Eina_File_Iterator Eina_File_Iterator;
101typedef struct _Eina_File_Map Eina_File_Map; 105typedef struct _Eina_File_Map Eina_File_Map;
106typedef struct _Eina_Xattr_Iterator Eina_Xattr_Iterator;
102 107
103struct _Eina_File_Iterator 108struct _Eina_File_Iterator
104{ 109{
@@ -110,6 +115,16 @@ struct _Eina_File_Iterator
110 char dir[1]; 115 char dir[1];
111}; 116};
112 117
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
113struct _Eina_File 128struct _Eina_File
114{ 129{
115 const char *filename; 130 const char *filename;
@@ -379,6 +394,33 @@ _eina_file_stat_ls_iterator_next(Eina_File_Direct_Iterator *it, void **data)
379 return EINA_TRUE; 394 return EINA_TRUE;
380} 395}
381 396
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
382static void 424static void
383_eina_file_real_close(Eina_File *file) 425_eina_file_real_close(Eina_File *file)
384{ 426{
@@ -736,6 +778,91 @@ eina_file_stat_ls(const char *dir)
736 return &it->iterator; 778 return &it->iterator;
737} 779}
738 780
781EAPI Eina_Iterator *
782eina_xattr_ls(const char *file)
783{
784 Eina_Xattr_Iterator *it;
785 ssize_t length;
786
787#ifdef HAVE_XATTR
788 if (!file)
789 return NULL;
790
791 length = listxattr(file, NULL, 0);
792 if (length <= 0) return NULL;
793
794 it = calloc(1, sizeof (Eina_Xattr_Iterator) + length - 1);
795 if (!it) return NULL;
796
797 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
798
799 it->length = listxattr(file, it->xattr, length);
800 if (it->length != length)
801 {
802 free(it);
803 return NULL;
804 }
805
806 it->iterator.version = EINA_ITERATOR_VERSION;
807 it->iterator.next = FUNC_ITERATOR_NEXT(_eina_xattr_ls_iterator_next);
808 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_xattr_ls_iterator_container);
809 it->iterator.free = FUNC_ITERATOR_FREE(_eina_xattr_ls_iterator_free);
810
811 return &it->iterator;
812#else
813 return NULL;
814#endif
815}
816
817EAPI void *
818eina_xattr_get(const char *file, const char *attribute, ssize_t *size)
819{
820 void *ret = NULL;
821 ssize_t tmp;
822
823 if (!size || !file || !attribute) return NULL;
824 *size = getxattr(file, attribute, NULL, 0);
825 /* Size should be less than 2MB (already huge in my opinion) */
826 if (!(*size > 0 && *size < 2 * 1024 * 1024))
827 goto on_error;
828
829 ret = malloc(*size);
830 if (!ret) return NULL;
831
832 tmp = getxattr(file, attribute, ret, *size);
833 if (tmp != *size)
834 goto on_error;
835
836 return ret;
837
838 on_error:
839 free(ret);
840 *size = 0;
841 return NULL;
842}
843
844EAPI Eina_Bool
845eina_xattr_set(const char *file, const char *attribute, const void *data, ssize_t length, Eina_Xattr_Flags flags)
846{
847 int iflags;
848
849 if (!file || !attribute || !data || length <= 0 || length > 2 * 1024 * 1024)
850 return EINA_FALSE;
851
852 switch (flags)
853 {
854 case EINA_XATTR_INSERT: iflags = 0; break;
855 case EINA_XATTR_REPLACE: iflags = XATTR_REPLACE; break;
856 case EINA_XATTR_CREATED: iflags = XATTR_CREATE; break;
857 default:
858 return EINA_FALSE;
859 }
860
861 if (setxattr(file, attribute, data, length, iflags))
862 return EINA_FALSE;
863 return EINA_TRUE;
864}
865
739EAPI Eina_File * 866EAPI Eina_File *
740eina_file_open(const char *filename, Eina_Bool shared) 867eina_file_open(const char *filename, Eina_Bool shared)
741{ 868{