#define __LIBCOMMON_FILE_H__
#include <stdarg.h>
+#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
*/
int syscommon_parse_cmdline_scanf(const char *format, ...);
+/**
+ * @brief Copy file
+ *
+ * @param[in] src Absolute path for a source file
+ * @param[in] dst Absolute path for a destination
+ * @param[in] overwrite If true, overwrite destination file if exist.
+ * If false, return -EEXIST if there is destination file.
+ *
+ * @return zero on success, otherwise negative error value
+ */
+int syscommon_file_copy(const char *src, const char *dst, bool overwrite);
+
#ifdef __cplusplus
}
#endif
#define SHARED_H_BUF_MAX 255
+static void close_fd(int *fd)
+{
+ if (*fd != -1)
+ close(*fd);
+}
+
+#define _cleanup_close_ __attribute__((__cleanup__(close_fd)))
+
int sys_read_buf(char *file, char *buf, int len)
{
int fd, r;
return ret;
}
+
+int syscommon_file_copy(const char *src, const char *dst, bool overwrite)
+{
+ _cleanup_close_ int rfd = -1;
+ _cleanup_close_ int wfd = -1;
+
+ char buf[1024] = { 0 ,};
+ ssize_t nread;
+ int r;
+
+ if (!src || !dst)
+ return -EINVAL;
+
+ if (!overwrite) {
+ r = access(dst, F_OK);
+ if (r == 0)
+ return -EALREADY;
+ else if (errno != ENOENT)
+ return -errno;
+ }
+
+ wfd = open(dst, O_CREAT | O_WRONLY | O_TRUNC, 0644);
+ if (wfd < 0)
+ return -errno;
+
+ rfd = open(src, O_RDONLY);
+ if (rfd < 0)
+ return -errno;
+
+ while ((nread = read(rfd, buf, 1024)) > 0)
+ if (write(wfd, buf, nread) != nread)
+ return -errno;
+
+ if (nread < 0)
+ return -errno;
+
+ return 0;
+}