/**
 * @file       xattr.h
 * @brief      Obtaining xattrs of files or current process
 * @author     Ivan Vorobiov (i.vorobiov@samsung.com)
 * @version    1.0
 * @date       Created Jul 20, 2016
 * @copyright  In Samsung Ukraine R&D Center (SURC) under a contract between
 * @copyright  LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
 * @copyright  "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
 * @copyright  Copyright: (c) Samsung Electronics Co, Ltd 2016. All rights reserved.
**/

#ifndef PA_NWD_LIB_SRC_XATTR_H_
#define PA_NWD_LIB_SRC_XATTR_H_

#include <stddef.h>

#include "pa_api.h"

/**
 * @brief Check has filename the attribute or not, and can return size of attribute
 * @param [in] filename Path to file
 * @param [in] attribute Attribute name
 * @param [out] value_size Size of xattr value buffer, may be NULL
 * @return ::PA_SUCCESS, ::PA_GENERAL_ERROR
 */
PaResult XattrHasAttr(const char *filename, const char *attribute,
                      size_t *value_size);

/**
 * @brief Read xattr value from filename by attribute name
 * @param [in] filename Path to file
 * @param [in] attribute Attribute name
 * @param [out] value Allocated memory with xattr value
 * @param [out] value_size Size of xattr value buffer
 * @warning Returned memory should be freed by caller
 * @return ::PA_SUCCESS, ::PA_MALLOC_ERROR, ::PA_GENERAL_ERROR
 */
PaResult XattrRead(const char *filename, const char *attribute, void **value,
                   size_t *value_size);

/**
 * @brief Write xattr value in filename by attribute name
 * @param [in] filename Path to file
 * @param [in] attribute Attribute name
 * @param [in] value Allocated memory with xattr value
 * @param [in] value_size Size of xattr value buffer
 * @return ::PA_SUCCESS, ::PA_MALLOC_ERROR, ::PA_GENERAL_ERROR
 */
PaResult XattrWrite(const char *filename, const char *attribute,
                    const void *value, size_t value_size);

/**
 * @brief Check has \a fd the  \a attribute or not, and can return size of attribute
 * @param [in] fd File descriptor
 * @param [in] attribute Attribute name
 * @param [out] value_size Size of xattr value buffer, may be NULL
 * @return ::PA_SUCCESS, ::PA_GENERAL_ERROR
 */
PaResult XattrFdHasAttr(int fd, const char *attribute,
                        size_t *value_size);

/**
 * @brief Write xattr value to \a fd by attribute name
 * @param [in] fd File descriptor
 * @param [in] attribute Attribute name
 * @param [in] value Allocated memory with xattr value
 * @param [in] value_size Size of xattr value buffer
 * @return ::PA_SUCCESS, ::PA_MALLOC_ERROR, ::PA_GENERAL_ERROR
 */
PaResult XattrFdWrite(int fd, const char *attribute,
                      const void *value, size_t value_size);

/**
 * @brief Write "user.pa" xattr value to \a fd
 * @param [in] fd File descriptor
 * @param [in] value Allocated memory with xattr value
 * @param [in] value_size Size of xattr value buffer
 * @note Function uses special fcntl that provides FIVE-enabled kernel
 * @return ::PA_SUCCESS, ::PA_MALLOC_ERROR, ::PA_GENERAL_ERROR
 */
PaResult XattrUserPaFcntl(int fd, const void *value, size_t value_size);

#endif // PA_NWD_LIB_SRC_XATTR_H_
