summaryrefslogtreecommitdiff
path: root/legacy/eina/src/lib/eina_file.c
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/eina/src/lib/eina_file.c
parent8e9fbe56623a491dc53443b3be537f16ce8c13d7 (diff)
eina: add XAttr support to eina.
SVN revision: 63408
Diffstat (limited to '')
-rw-r--r--legacy/eina/src/lib/eina_file.c127
1 files changed, 127 insertions, 0 deletions
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{