From b2dc1ce8abf8fa85da9ebe1fb5bdc3ce4a14508d Mon Sep 17 00:00:00 2001 From: Cedric BAIL Date: Fri, 16 Sep 2011 14:18:08 +0000 Subject: [PATCH] eio: use eina xattr helper and add more eio xattr function. SVN revision: 63441 --- legacy/eio/src/lib/Eio.h | 51 ++++- legacy/eio/src/lib/eio_private.h | 36 ++- legacy/eio/src/lib/eio_xattr.c | 381 +++++++++++++++++++++++++++---- 3 files changed, 422 insertions(+), 46 deletions(-) diff --git a/legacy/eio/src/lib/Eio.h b/legacy/eio/src/lib/Eio.h index 762706e8f3..9fffec6724 100644 --- a/legacy/eio/src/lib/Eio.h +++ b/legacy/eio/src/lib/Eio.h @@ -117,7 +117,10 @@ typedef void (*Eio_Main_Direct_Cb)(void *data, Eio_File *handler, const Eina_Fil typedef void (*Eio_Stat_Cb)(void *data, Eio_File *handler, const struct stat *stat); typedef void (*Eio_Progress_Cb)(void *data, Eio_File *handler, const Eio_Progress *info); -typedef void (*Eio_Done_Length_Cb)(void *data, Eio_File *handler, const char *xattr_data, unsigned int xattr_size); +typedef void (*Eio_Done_Data_Cb)(void *data, Eio_File *handler, const char *xattr_data, unsigned int xattr_size); +typedef void (*Eio_Done_String_Cb)(void *data, Eio_File *handler, const char *xattr_string); +typedef void (*Eio_Done_Double_Cb)(void *data, Eio_File *handler, double xattr_double); +typedef void (*Eio_Done_Int_Cb)(void *data, Eio_File *handler, int xattr_int); typedef void (*Eio_Done_Cb)(void *data, Eio_File *handler); typedef void (*Eio_Error_Cb)(void *data, Eio_File *handler, int error); @@ -238,6 +241,35 @@ EAPI Eio_File *eio_file_xattr(const char *path, Eio_Error_Cb error_cb, const void *data); +EAPI Eio_File *eio_file_xattr_set(const char *path, + const char *attribute, + const char *xattr_data, + unsigned int xattr_size, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data); +EAPI Eio_File *eio_file_xattr_int_set(const char *path, + const char *attribute, + int xattr_int, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data); +EAPI Eio_File *eio_file_xattr_double_set(const char *path, + const char *attribute, + double xattr_double, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data); +EAPI Eio_File *eio_file_xattr_string_set(const char *path, + const char *attribute, + const char *xattr_string, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data); EAPI Eio_File *eio_file_xattr_set(const char *path, const char *attribute, const char *xattr_data, @@ -249,9 +281,24 @@ EAPI Eio_File *eio_file_xattr_set(const char *path, EAPI Eio_File *eio_file_xattr_get(const char *path, const char *attribute, - Eio_Done_Length_Cb done_cb, + Eio_Done_Data_Cb done_cb, Eio_Error_Cb error_cb, const void *data); +EAPI Eio_File *eio_file_xattr_int_get(const char *path, + const char *attribute, + Eio_Done_Int_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data); +EAPI Eio_File *eio_file_xattr_double_get(const char *path, + const char *attribute, + Eio_Done_Double_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data); +EAPI Eio_File *eio_file_xattr_string_get(const char *path, + const char *attribute, + Eio_Done_String_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data); EAPI void *eio_file_container_get(Eio_File *ls); diff --git a/legacy/eio/src/lib/eio_private.h b/legacy/eio/src/lib/eio_private.h index 26d10fb757..a501801b65 100644 --- a/legacy/eio/src/lib/eio_private.h +++ b/legacy/eio/src/lib/eio_private.h @@ -159,18 +159,46 @@ struct _Eio_File_Stat const char *path; }; +typedef enum { + EIO_XATTR_DATA, + EIO_XATTR_STRING, + EIO_XATTR_DOUBLE, + EIO_XATTR_INT +} Eio_File_Xattr_Op; + struct _Eio_File_Xattr { Eio_File common; - Eio_Done_Length_Cb done_cb; - const char *path; const char *attribute; Eina_Xattr_Flags flags; - char *xattr_data; - ssize_t xattr_size; + Eio_File_Xattr_Op op; + + union { + struct { + Eio_Done_Data_Cb done_cb; + + char *xattr_data; + ssize_t xattr_size; + } xdata; + struct { + Eio_Done_String_Cb done_cb; + + char *xattr_string; + } xstring; + struct { + Eio_Done_Double_Cb done_cb; + + double xattr_double; + } xdouble; + struct { + Eio_Done_Int_Cb done_cb; + + int xattr_int; + } xint; + } todo; }; struct _Eio_File_Progress diff --git a/legacy/eio/src/lib/eio_xattr.c b/legacy/eio/src/lib/eio_xattr.c index 665322996f..38d235fc02 100644 --- a/legacy/eio/src/lib/eio_xattr.c +++ b/legacy/eio/src/lib/eio_xattr.c @@ -75,12 +75,35 @@ static void _eio_file_xattr_get(void *data, Ecore_Thread *thread) { Eio_File_Xattr *async = data; + Eina_Bool failure = EINA_FALSE; + const char *file; + const char *attribute; - async->xattr_size = 0; - async->xattr_data = NULL; + file = async->path; + attribute = async->attribute; - async->xattr_data = eina_xattr_get(async->path, async->attribute, &async->xattr_size); - if (!async->xattr_data) + switch (async->op) + { + case EIO_XATTR_DATA: + async->todo.xdata.xattr_size = 0; + async->todo.xdata.xattr_data = NULL; + + async->todo.xdata.xattr_data = eina_xattr_get(file, attribute, &async->todo.xdata.xattr_size); + if (!async->todo.xdata.xattr_data) failure = EINA_TRUE; + break; + case EIO_XATTR_STRING: + async->todo.xstring.xattr_string = eina_xattr_string_get(file, attribute); + if (!async->todo.xstring.xattr_string) failure = EINA_TRUE; + break; + case EIO_XATTR_DOUBLE: + failure = !eina_xattr_double_get(file, attribute, &async->todo.xdouble.xattr_double); + break; + case EIO_XATTR_INT: + failure = !eina_xattr_int_get(file, attribute, &async->todo.xint.xattr_int); + break; + } + + if (failure) ecore_thread_cancel(thread); } @@ -89,7 +112,8 @@ _eio_file_xattr_free(Eio_File_Xattr *async) { eina_stringshare_del(async->path); eina_stringshare_del(async->attribute); - free(async->xattr_data); + if (async->op == EIO_XATTR_DATA) free(async->todo.xdata.xattr_data); + if (async->op == EIO_XATTR_STRING) free(async->todo.xstring.xattr_string); free(async); } @@ -98,8 +122,25 @@ _eio_file_xattr_get_done(void *data, Ecore_Thread *thread __UNUSED__) { Eio_File_Xattr *async = data; - if (async->done_cb) - async->done_cb((void *) async->common.data, &async->common, async->xattr_data, async->xattr_size); + switch (async->op) + { + case EIO_XATTR_DATA: + if (async->todo.xdata.done_cb) + async->todo.xdata.done_cb((void *) async->common.data, &async->common, async->todo.xdata.xattr_data, async->todo.xdata.xattr_size); + break; + case EIO_XATTR_STRING: + if (async->todo.xstring.done_cb) + async->todo.xstring.done_cb((void *) async->common.data, &async->common, async->todo.xstring.xattr_string); + break; + case EIO_XATTR_DOUBLE: + if (async->todo.xdouble.done_cb) + async->todo.xdouble.done_cb((void *) async->common.data, &async->common, async->todo.xdouble.xattr_double); + break; + case EIO_XATTR_INT: + if (async->todo.xint.done_cb) + async->todo.xint.done_cb((void *) async->common.data, &async->common, async->todo.xint.xattr_int); + break; + } _eio_file_xattr_free(async); } @@ -117,10 +158,32 @@ static void _eio_file_xattr_set(void *data, Ecore_Thread *thread) { Eio_File_Xattr *async = data; + const char *file; + const char *attribute; + Eina_Xattr_Flags flags; + Eina_Bool failure = EINA_FALSE; - if (eina_xattr_set(async->path, async->attribute, async->xattr_data, async->xattr_size, async->flags)) - eio_file_thread_error(&async->common, thread); - async->xattr_data = NULL; + file = async->path; + attribute = async->attribute; + flags = async->flags; + + switch (async->op) + { + case EIO_XATTR_DATA: + failure = !eina_xattr_set(file, attribute, async->todo.xdata.xattr_data, async->todo.xdata.xattr_size, flags); + break; + case EIO_XATTR_STRING: + failure = !eina_xattr_string_set(file, attribute, async->todo.xstring.xattr_string, flags); + break; + case EIO_XATTR_DOUBLE: + failure = !eina_xattr_double_set(file, attribute, async->todo.xdouble.xattr_double, flags); + break; + case EIO_XATTR_INT: + failure = !eina_xattr_int_set(file, attribute, async->todo.xint.xattr_int, flags); + break; + } + + if (failure) eio_file_thread_error(&async->common, thread); } static void @@ -143,6 +206,53 @@ _eio_file_xattr_set_error(void *data, Ecore_Thread *thread __UNUSED__) _eio_file_xattr_free(async); } +static Eio_File * +_eio_file_xattr_setup_get(Eio_File_Xattr *async, + const char *path, + const char *attribute, + Eio_Error_Cb error_cb, + const void *data) +{ + async->path = eina_stringshare_add(path); + async->attribute = eina_stringshare_add(attribute); + + if (!eio_file_set(&async->common, + NULL, + error_cb, + data, + _eio_file_xattr_get, + _eio_file_xattr_get_done, + _eio_file_xattr_get_error)) + return NULL; + + return &async->common; +} + +static Eio_File * +_eio_file_xattr_setup_set(Eio_File_Xattr *async, + const char *path, + const char *attribute, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data) +{ + async->path = eina_stringshare_add(path); + async->attribute = eina_stringshare_add(attribute); + async->flags = flags; + + if (!eio_file_set(&async->common, + done_cb, + error_cb, + data, + _eio_file_xattr_set, + _eio_file_xattr_set_done, + _eio_file_xattr_set_error)) + return NULL; + + return &async->common; +} + /** * @endcond */ @@ -217,11 +327,10 @@ eio_file_xattr(const char *path, * * eio_file_xattr_get call getxattr from another thread. This prevent lock in your apps. */ - EAPI Eio_File * eio_file_xattr_get(const char *path, const char *attribute, - Eio_Done_Length_Cb done_cb, + Eio_Done_Data_Cb done_cb, Eio_Error_Cb error_cb, const void *data) { @@ -233,24 +342,110 @@ eio_file_xattr_get(const char *path, async = malloc(sizeof (Eio_File_Xattr)); if (!async) return NULL; - async->path = eina_stringshare_add(path); - async->attribute = eina_stringshare_add(attribute); - async->done_cb = done_cb; + async->op = EIO_XATTR_DATA; + async->todo.xdata.done_cb = done_cb; - if (!eio_file_set(&async->common, - NULL, - error_cb, - data, - _eio_file_xattr_get, - _eio_file_xattr_get_done, - _eio_file_xattr_get_error)) - return NULL; - - return &async->common; + return _eio_file_xattr_setup_get(async, path, attribute, error_cb, data); } /** - * @brief Define the extented attribute on a file/directory. + * @brief Retrieve a string extended attribute of a file/directory. + * @param path The path to retrieve the extended attribute from. + * @param attribute The name of the attribute to retrieve. + * @param done_cb Callback called from the main loop when getxattr succeeded. + * @param error_cb Callback called from the main loop when getxattr failed or has been canceled. + * @param data Private data given to callback. + * @return A reference to the IO operation. + * + * eio_file_xattr_string_get call eina_xattr_string_get from another thread. This prevent lock in your apps. + */ +EAPI Eio_File * +eio_file_xattr_string_get(const char *path, + const char *attribute, + Eio_Done_String_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data) +{ + Eio_File_Xattr *async; + + if (!path || !attribute || !done_cb || !error_cb) + return NULL; + + async = malloc(sizeof (Eio_File_Xattr)); + if (!async) return NULL; + + async->op = EIO_XATTR_STRING; + async->todo.xstring.done_cb = done_cb; + + return _eio_file_xattr_setup_get(async, path, attribute, error_cb, data); +} + +/** + * @brief Retrieve a extended attribute of a file/directory. + * @param path The path to retrieve the extended attribute from. + * @param attribute The name of the attribute to retrieve. + * @param done_cb Callback called from the main loop when getxattr succeeded. + * @param error_cb Callback called from the main loop when getxattr failed or has been canceled. + * @param data Private data given to callback. + * @return A reference to the IO operation. + * + * eio_file_xattr_double_get call eina_xattr_double_get from another thread. This prevent lock in your apps. + */ +EAPI Eio_File * +eio_file_xattr_double_get(const char *path, + const char *attribute, + Eio_Done_Double_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data) +{ + Eio_File_Xattr *async; + + if (!path || !attribute || !done_cb || !error_cb) + return NULL; + + async = malloc(sizeof (Eio_File_Xattr)); + if (!async) return NULL; + + async->op = EIO_XATTR_DOUBLE; + async->todo.xdouble.done_cb = done_cb; + + return _eio_file_xattr_setup_get(async, path, attribute, error_cb, data); +} + +/** + * @brief Retrieve a extended attribute of a file/directory. + * @param path The path to retrieve the extended attribute from. + * @param attribute The name of the attribute to retrieve. + * @param done_cb Callback called from the main loop when getxattr succeeded. + * @param error_cb Callback called from the main loop when getxattr failed or has been canceled. + * @param data Private data given to callback. + * @return A reference to the IO operation. + * + * eio_file_xattr_int_get call eina_xattr_int_get from another thread. This prevent lock in your apps. + */ +EAPI Eio_File * +eio_file_xattr_int_get(const char *path, + const char *attribute, + Eio_Done_Int_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data) +{ + Eio_File_Xattr *async; + + if (!path || !attribute || !done_cb || !error_cb) + return NULL; + + async = malloc(sizeof (Eio_File_Xattr)); + if (!async) return NULL; + + async->op = EIO_XATTR_INT; + async->todo.xint.done_cb = done_cb; + + return _eio_file_xattr_setup_get(async, path, attribute, error_cb, data); +} + +/** + * @brief Define an extented attribute on a file/directory. * @param path The path to set the attribute on. * @param attribute The name of the attribute to define. * @param xattr_data The data to link the attribute with. @@ -281,23 +476,129 @@ eio_file_xattr_set(const char *path, async = malloc(sizeof (Eio_File_Xattr) + xattr_size); if (!async) return NULL; - async->path = eina_stringshare_add(path); - async->attribute = eina_stringshare_add(attribute); - async->xattr_size = xattr_size; - async->xattr_data = (char*) (async + 1); - memcpy(async->xattr_data, xattr_data, xattr_size); - async->flags = flags; + async->op = EIO_XATTR_DATA; + async->todo.xdata.xattr_size = xattr_size; + async->todo.xdata.xattr_data = (char*) (async + 1); + memcpy(async->todo.xdata.xattr_data, xattr_data, xattr_size); - if (!eio_file_set(&async->common, - done_cb, - error_cb, - data, - _eio_file_xattr_set, - _eio_file_xattr_set_done, - _eio_file_xattr_set_error)) + return _eio_file_xattr_setup_set(async, path, attribute, flags, done_cb, error_cb, data); +} + +/** + * @brief Define a string extented attribute on a file/directory. + * @param path The path to set the attribute on. + * @param attribute The name of the attribute to define. + * @param xattr_string The string to link the attribute with. + * @param done_cb The callback called from the main loop when setxattr succeeded. + * @param error_cb The callback called from the main loop when setxattr failed. + * @param data Private data given to callback. + * @return A reference to the IO operation. + * + * eio_file_xattr_string_set call eina_xattr_string_set from another thread. This prevent lock in your apps. If + * the writing succeeded, the done_cb will be called even if a cancel was requested, but came to late. + */ +EAPI Eio_File * +eio_file_xattr_string_set(const char *path, + const char *attribute, + const char *xattr_string, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data) +{ + Eio_File_Xattr *async; + int length; + + if (!path || !attribute || !done_cb || !xattr_string || !error_cb) return NULL; - return &async->common; + async = malloc(sizeof (Eio_File_Xattr)); + if (!async) return NULL; + + length = strlen(xattr_string) + 1; + + async->op = EIO_XATTR_STRING; + async->todo.xstring.xattr_string = malloc(length); + if (!async->todo.xstring.xattr_string) + { + free(async); + return NULL; + } + memcpy(async->todo.xstring.xattr_string, xattr_string, length); + + return _eio_file_xattr_setup_set(async, path, attribute, flags, done_cb, error_cb, data); +} + +/** + * @brief Define an extented attribute on a file/directory. + * @param path The path to set the attribute on. + * @param attribute The name of the attribute to define. + * @param xattr_double The value to link the attribute with. + * @param done_cb The callback called from the main loop when setxattr succeeded. + * @param error_cb The callback called from the main loop when setxattr failed. + * @param data Private data given to callback. + * @return A reference to the IO operation. + * + * eio_file_xattr_double_set call eina_xattr_double_set from another thread. This prevent lock in your apps. If + * the writing succeeded, the done_cb will be called even if a cancel was requested, but came to late. + */ +EAPI Eio_File * +eio_file_xattr_double_set(const char *path, + const char *attribute, + double xattr_double, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data) +{ + Eio_File_Xattr *async; + + if (!path || !attribute || !done_cb || !error_cb) + return NULL; + + async = malloc(sizeof (Eio_File_Xattr)); + if (!async) return NULL; + + async->op = EIO_XATTR_DOUBLE; + async->todo.xdouble.xattr_double = xattr_double; + + return _eio_file_xattr_setup_set(async, path, attribute, flags, done_cb, error_cb, data); +} + +/** + * @brief Define an extented attribute on a file/directory. + * @param path The path to set the attribute on. + * @param attribute The name of the attribute to define. + * @param xattr_int The value to link the attribute with. + * @param done_cb The callback called from the main loop when setxattr succeeded. + * @param error_cb The callback called from the main loop when setxattr failed. + * @param data Private data given to callback. + * @return A reference to the IO operation. + * + * eio_file_xattr_int_set call eina_xattr_int_set from another thread. This prevent lock in your apps. If + * the writing succeeded, the done_cb will be called even if a cancel was requested, but came to late. + */ +EAPI Eio_File * +eio_file_xattr_int_set(const char *path, + const char *attribute, + int xattr_int, + Eina_Xattr_Flags flags, + Eio_Done_Cb done_cb, + Eio_Error_Cb error_cb, + const void *data) +{ + Eio_File_Xattr *async; + + if (!path || !attribute || !done_cb || !error_cb) + return NULL; + + async = malloc(sizeof (Eio_File_Xattr)); + if (!async) return NULL; + + async->op = EIO_XATTR_INT; + async->todo.xint.xattr_int = xattr_int; + + return _eio_file_xattr_setup_set(async, path, attribute, flags, done_cb, error_cb, data); } /**