/**
 * @file fs_utils.h
 * @brief Multibuild's FS utility functionality declarations
 * @author Iaroslav Makarchuk (i.makarchuk@samsung.com)
 * @date Created Oct 3, 2016
 * @par In Samsung Ukraine R&D Center (SURC) under a contract between
 * @par LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
 * @par "Samsung Elecrtronics Co", Ltd (Seoul, Republic of Korea)
 * @par Copyright: (c) Samsung Electronics Co, Ltd 2015. All rights reserved.
 *
 * This software is proprietary of Samsung Electronics.
 * No part of this software, either material or conceptual may be copied
 * or distributed, transmitted, transcribed, stored in a retrieval system
 * or translated into any human or computer language in any form by any means,
 * electronic, mechanical, manual or otherwise, or disclosed to third parties
 * without the express written permission of Samsung Electronics.
 *
 * The file was taken from tzsl repo and modified for Multibuild project's
 * needs.
 */

#ifndef MB_FS_UTILS_H_
#define MB_FS_UTILS_H_

#include <stdlib.h>
#include <stddef.h>

#include <fs_po.h>

/**
 * @brief Writes data to the specified file
 *
 * @note This function writes data into the temporary file and once it is done
 * it renames the temporary file to the destination name. In this case if the
 * operation is aborted, the old contents of the destination file will be kept.
 *
 * @param[in]  path     The destination path, specifies the file where the data
 *                      must be written
 * @param[in]  path_len Length of the destination path in bytes
 * @param[in]  data     A pointer to the buffer with data to be written
 * @param[in]  data_len Data length in bytes
 *
 * @retval MB_FS_OK                   Write successful
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult MultiBuildWriteFileAtomic(const char *path, size_t path_len,
                                             const void *data, uint32_t data_len);

/**
 * @brief Reads data from the specifie file
 *
 * @param[in]      path     The path to the file to be read
 * @param[in]      path_len Length of the destination path in bytes
 * @param[out]     data     Data buffer where the read data should be stored
 * @param[in, out] data_len In: size of data buffer; Out: The size of read data
 *                          in bytes
 *
 * @retval MB_FS_OK                   Data eead successfully
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_NOT_FOUND      The specified path doesn't exist
 * @retval MB_FS_ERROR_SHORT_BUFFER   Data buffer is too short to store the data
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult MultiBuildReadFile(const char *path, size_t path_len,
                                      void *data, uint32_t *data_len);

/**
 * @brief Reads data from the specifie file
 *
 * @param[in]      path         The path to the file whose owner to be changed
 * @param[in]      path_len     Path length
 * @param[in]      uid          New owner's user ID
 * @param[in]      gid          New owner's group ID
 */
void MultiBuildChown(const char *path, uint32_t path_len,
                     uint32_t uid, uint32_t gid);

#endif /* MB_FS_UTILS_H_ */
