Skip to content

Commit

Permalink
sys/vfs_util: add VFS helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Apr 30, 2022
1 parent 5a6bb44 commit 4321e82
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sys/include/vfs_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2021 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/

/**
* @defgroup sys_vfs_util VFS helper functions
* @ingroup sys_vfs
* @{
*
* @file
* @brief VFS helper functions
*
* @author Benjamin Valentin <[email protected]>
*/

#ifndef VFS_UTIL_H
#define VFS_UTIL_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Writes the content of a buffer to a file
* If the file already exists, it will be overwritten.
*
* @param[in] file Destination file path
* @param[in] buf Source buffer
* @param[in] len Buffer size
*
* @return 0 on success
* @return negative error
*/
int vfs_file_from_buffer(const char *file, const void *buf, size_t len);

/**
* @brief Reads the content of a file to a buffer
*
* @param[in] file Source file path
* @param[out] buf Destination buffer
* @param[in] len Buffer size
*
* @return number of bytes read on success
* @return negative error
*/
int vfs_file_to_buffer(const char* file, void* buf, size_t len);

#ifdef __cplusplus
}
#endif

#endif /* VFS_UTIL_H */
/** @} */
1 change: 1 addition & 0 deletions sys/vfs_util/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
63 changes: 63 additions & 0 deletions sys/vfs_util/vfs_util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2021 Benjamin Valentin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup sys_vfs_util
* @{
* @file
* @brief VFS layer helper functions
* @author Benjamin Valentin <[email protected]>
* @}
*/

#include <fcntl.h>
#include <string.h>

#include "vfs.h"
#include "vfs_util.h"

#define ENABLE_DEBUG 0
#include "debug.h"

int vfs_file_from_buffer(const char *file, const void *buf, size_t len)
{
int res, fd = vfs_open(file, O_CREAT | O_TRUNC | O_WRONLY, 0644);

if (fd < 0) {
DEBUG("can't open %s for writing\n", file);
return fd;
}

res = vfs_write(fd, buf, len);
vfs_close(fd);

if (res) {
return res;
}

return 0;
}

int vfs_file_to_buffer(const char* file, void* buf, size_t len)
{
int res, fd = vfs_open(file, O_RDONLY, 0);

if (fd < 0) {
DEBUG("can't open %s for reading\n", file);
return fd;
}

res = vfs_read(fd, buf, len);
vfs_close(fd);

if (res > 0 && res < (int)len) {
memset((char *)buf + res, 0, len - res);
}

return res;
}

0 comments on commit 4321e82

Please sign in to comment.