-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sys/vfs_util: add VFS helper functions
- Loading branch information
Showing
3 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
/** @} */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |