git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15408 27425a3e-05d8-49a3-a47f-9c15f0e5edd8

This commit is contained in:
Giovanni Di Sirio 2022-01-26 13:36:25 +00:00
parent d824edcfa7
commit e08f28da20
6 changed files with 47 additions and 110 deletions

View File

@ -59,6 +59,7 @@ extern "C" {
size_t path_prepend(char *dst, const char *src, size_t size);
size_t path_add_separator(char *dst, size_t size);
size_t path_add_extension(char *dst, const char *ext, size_t size);
size_t path_copy_element(const char **pathp, char *dst, size_t size);
size_t path_get_element(const char **pathp, char *dst, size_t size);
size_t path_normalize(char *dst, const char *src, size_t size);
#ifdef __cplusplus
@ -69,6 +70,17 @@ extern "C" {
/* Module inline functions. */
/*===========================================================================*/
/**
* @brief Checks for a valid path separator.
*
* @param[in] c The character to be checked.
* @return The operation result.
*/
static inline bool path_is_separator(char c) {
return (bool)(c == '/');
}
#endif /* vfspaths.h */
/** @} */

View File

@ -218,7 +218,7 @@ size_t path_add_extension(char *dst, const char *ext, size_t size) {
* @retval 0 Null element.
* @retval size Buffer overflow.
*/
size_t path_get_element(const char **pathp, char *dst, size_t size) {
size_t path_copy_element(const char **pathp, char *dst, size_t size) {
size_t n;
const char *p;
@ -248,6 +248,30 @@ size_t path_get_element(const char **pathp, char *dst, size_t size) {
}
}
/**
* @brief Fetches the next path element.
* @note Does not consume the next separator, if any.
* @note It can return an empty element, it has to be detected outside.
*
* @param[in, out] pathp Pointer to the path under parsing.
* @param[out] dst Buffer for the extracted path element
* @param[in] size Destination buffer size.
* @return The size of the fetched path element, it does
* not fetch beyond @p size.
* @retval 0 Null element.
* @retval size Buffer overflow.
*/
size_t path_get_element(const char **pathp, char *dst, size_t size) {
size_t n;
n = path_copy_element(pathp, dst, size);
if (n < size) {
dst[n] = '\0';
}
return n;
}
/**
* @brief Normalizes an absolute path.
* @note The destination buffer can be the same of the source buffer.

View File

@ -101,18 +101,22 @@ static msg_t match_driver(vfs_overlay_driver_c *odp,
const char **pathp,
vfs_driver_c **vdpp) {
char fname[VFS_CFG_NAMELEN_MAX + 1];
size_t n;
msg_t err;
do {
unsigned i;
err = vfs_parse_get_fname(pathp, fname, VFS_CFG_PATHLEN_MAX);
CH_BREAK_ON_ERROR(err);
n = path_get_element(pathp, fname, VFS_CFG_NAMELEN_MAX + 1);
if (n >= VFS_CFG_NAMELEN_MAX + 1) {
err = CH_RET_ENAMETOOLONG;
break;
}
/* Searching among registered drivers.*/
i = 0U;
while (i < odp->next_driver) {
if (strncmp(fname, odp->names[i], VFS_CFG_NAMELEN_MAX) == 0) {
if (memcmp(fname, odp->names[i], n) == 0) {
*vdpp = odp->drivers[i];
return CH_RET_SUCCESS;
}
@ -149,7 +153,7 @@ static msg_t build_absolute_path(vfs_overlay_driver_c *drvp,
*buf = '\0';
/* Relative paths handling.*/
if (!vfs_parse_is_separator(*path)) {
if (!path_is_separator(*path)) {
if (path_append(buf,
get_current_directory(drvp),
VFS_CFG_PATHLEN_MAX + 1) == (size_t)0) {

View File

@ -204,7 +204,7 @@ static msg_t drv_open_file(void *instance,
err = vfs_parse_match_separator(&path);
CH_BREAK_ON_ERROR(err);
err = vfs_parse_get_fname(&path, fname, VFS_CFG_PATHLEN_MAX);
err = path_get_element(&path, fname, VFS_CFG_NAMELEN_MAX + 1);
CH_BREAK_ON_ERROR(err);
err = vfs_parse_match_end(&path);

View File

@ -57,8 +57,6 @@ extern "C" {
#endif
msg_t vfs_parse_match_separator(const char **pathp);
msg_t vfs_parse_match_end(const char **pathp);
msg_t vfs_parse_copy_fname(const char **pathp, char *fname, size_t n);
msg_t vfs_parse_get_fname(const char **pathp, char *fname, size_t n);
#ifdef __cplusplus
}
#endif
@ -67,43 +65,6 @@ extern "C" {
/* Module inline functions. */
/*===========================================================================*/
/**
* @brief Checks for a valid path separator.
*
* @param[in] c The character to be checked.
* @return The operation result.
*/
CC_FORCE_INLINE
static inline bool vfs_parse_is_separator(char c) {
return (bool)(c == '/');
}
/**
* @brief Checks for a valid path terminator.
*
* @param[in] c The character to be checked.
* @return The operation result.
*/
CC_FORCE_INLINE
static inline bool vfs_parse_is_terminator(char c) {
return (bool)(c == '\0');
}
/**
* @brief Checks for a path element character.
*
* @param[in] c The character to be checked.
* @return The operation result.
*/
CC_FORCE_INLINE
static inline bool vfs_parse_is_filechar(char c) {
/* Restrictive Posix set.*/
return (bool)(isalnum(c) || (c == '_') || (c == '-') || (c == '.'));
}
#endif /* VFSPARSER_H */
/** @} */

View File

@ -60,7 +60,7 @@ msg_t vfs_parse_match_separator(const char **pathp) {
msg_t err;
const char *p = *pathp;
if (!vfs_parse_is_separator(*p++)) {
if (!path_is_separator(*p++)) {
err = CH_RET_ENOENT;
}
else {
@ -89,68 +89,4 @@ msg_t vfs_parse_match_end(const char **pathp) {
return err;
}
/**
* @brief Fetches the next path element.
* @note Does not consume the next separator, if any.
* @note Does not add a final zero to the fetched fname.
* @note It can return an empty element, it has to be detected outside.
*
* @param[in, out] pathp Pointer to the path under parsing.
* @param[out] fname Extracted file name.
* @param[in] n Maximum size in @fname.
*/
msg_t vfs_parse_copy_fname(const char **pathp, char *fname, size_t n) {
size_t size;
const char *p;
p = *pathp;
size = 0U;
while (true) {
char c = *p;
/* Path elements must be terminated by a separator or an end-of-string.*/
if (vfs_parse_is_separator(c) || vfs_parse_is_terminator(c)) {
/* Advancing the path pointer past the file name in the path and
closing the file name string.*/
*pathp = p;
return (msg_t)size;
}
/* Valid characters for path names.*/
if (!vfs_parse_is_filechar(c)) {
return CH_RET_EINVAL;
}
/* Exceeding the maximum length.*/
if (size > n) {
return CH_RET_ENAMETOOLONG;
}
*fname++ = c;
p++;
size++;
}
}
/**
* @brief Fetches the next path element.
* @note Does not consume the next separator, if any.
* @note It can return an empty element, it has to be detected outside.
*
* @param[in, out] pathp Pointer to the path under parsing.
* @param[out] fname Extracted file name.
* @param[in] n Maximum size in @fname.
*/
msg_t vfs_parse_get_fname(const char **pathp, char *fname, size_t n) {
msg_t ret;
ret = vfs_parse_copy_fname(pathp, fname, n);
CH_RETURN_ON_ERROR(ret);
fname[ret] = '\0';
return ret;
}
/** @} */