From 7480e1eb3d808b32773bae015b72283f4a2caefe Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 26 Dec 2021 08:16:26 +0000 Subject: [PATCH] Directory-related commands apparently working now, file commands to be reworked. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15253 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/various/shell/shell_cmd.c | 6 +++--- os/vfs/drivers/overlay/drvoverlay.c | 10 ++++++++-- os/vfs/include/vfspaths.h | 1 + os/vfs/src/vfspaths.c | 31 +++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/os/various/shell/shell_cmd.c b/os/various/shell/shell_cmd.c index 253118380..89ef44ead 100644 --- a/os/various/shell/shell_cmd.c +++ b/os/various/shell/shell_cmd.c @@ -354,13 +354,13 @@ static void cmd_cd(BaseSequentialStream *chp, int argc, char *argv[]) { while (false); } -static void cmd_cwd(BaseSequentialStream *chp, int argc, char *argv[]) { +static void cmd_pwd(BaseSequentialStream *chp, int argc, char *argv[]) { char *buf = NULL; (void)argv; if (argc != 0) { - chprintf(chp, "Usage: cwd" SHELL_NEWLINE_STR); + chprintf(chp, "Usage: pwd" SHELL_NEWLINE_STR); return; } @@ -419,7 +419,7 @@ const ShellCommand shell_local_commands[] = { {"tree", cmd_tree}, {"cat", cmd_cat}, {"cd", cmd_cd}, - {"cwd", cmd_cwd}, + {"pwd", cmd_pwd}, #endif #if SHELL_CMD_TEST_ENABLED == TRUE {"test", cmd_test}, diff --git a/os/vfs/drivers/overlay/drvoverlay.c b/os/vfs/drivers/overlay/drvoverlay.c index 7377c069e..a92bb5733 100644 --- a/os/vfs/drivers/overlay/drvoverlay.c +++ b/os/vfs/drivers/overlay/drvoverlay.c @@ -195,8 +195,14 @@ static msg_t open_absolute_dir(vfs_overlay_driver_c *drvp, msg_t err; do { - /* Initial separator is expected, skipping.*/ - const char *scanpath = path + 1; + const char *scanpath; + + /* Making sure there is a final separator.*/ + err = vfs_path_add_separator(path, VFS_CFG_PATHLEN_MAX + 1); + VFS_BREAK_ON_ERROR(err); + + /* Initial separator is expected, skipping it.*/ + scanpath = path + 1; /* If it is the root.*/ if (*scanpath == '\0') { diff --git a/os/vfs/include/vfspaths.h b/os/vfs/include/vfspaths.h index 11a4c3077..9ccc38d32 100644 --- a/os/vfs/include/vfspaths.h +++ b/os/vfs/include/vfspaths.h @@ -57,6 +57,7 @@ extern "C" { #endif msg_t vfs_path_append(char *dst, const char *src, size_t size); msg_t vfs_path_prepend(char *dst, const char *src, size_t size); + msg_t vfs_path_add_separator(char *dst, size_t size); msg_t vfs_path_normalize(char *dst, const char *src, size_t size); #ifdef __cplusplus } diff --git a/os/vfs/src/vfspaths.c b/os/vfs/src/vfspaths.c index a8b028ea5..8b3bb211d 100644 --- a/os/vfs/src/vfspaths.c +++ b/os/vfs/src/vfspaths.c @@ -129,6 +129,37 @@ msg_t vfs_path_prepend(char *dst, const char *src, size_t size) { return (msg_t)sn; } +/** + * @brief Adds a separator to the end of a path if it is missing. + * + * @param[in] dst The destination path. + * @param[in[ size Destination buffer size. + * @return The operation status. + * @retval VFS_RET_ERANGE If the path size exceeded the buffer size. + */ +msg_t vfs_path_add_separator(char *dst, size_t size) { + size_t dn; + + dn = strnlen(dst, size - 1U); + + if (dn == 0U) { + dst[0] = '/'; + dst[1] = '\0'; + } + else { + if (!vfs_parse_is_separator(dst[dn - 1])) { + if (dn >= size - 1) { + return VFS_RET_ERANGE; + } + + dst[dn] = '/'; + dst[dn + 1] = '\0'; + } + } + + return dn + 1; +} + /** * @brief Normalizes an absolute path. * @note The destination buffer can be the same of the source buffer.