From 229db6418bba26516f247202ad6a2b4adecce9a7 Mon Sep 17 00:00:00 2001 From: Terje Io Date: Sun, 11 Sep 2022 20:37:47 +0200 Subject: [PATCH] Minor options preprocessor change for littlefs, "hardened" VFS code. Core build date not changed. --- changelog.md | 26 +++++++++++++++++++++-- driver_opts.h | 4 ++++ vfs.c | 57 +++++++++++++++++++++++++++++++++++++++++++-------- vfs.h | 1 + 4 files changed, 78 insertions(+), 10 deletions(-) diff --git a/changelog.md b/changelog.md index b9927a9..cbf762d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,28 @@ ## grblHAL changelog -20220905: +20220911: + +Core: + +* Minor options preprocessor change for littlefs, "hardened" VFS code. Core build date not changed. + +Plugins: + +* WebUI: Added all grblHAL settings to ESP400 response, file systems handling improvements++. Still WIP, but getting closer to final version! + +* Networking: some general enhancements \(for WebUI\). + +* SDCard: added VFS wrapper for littlefs. This plugin will be renamed late, likely to _Plugin_storage_. + +Drivers: + +* iMXRT1062, RP2040 and ESP32: Added [littlefs](https://github.com/littlefs-project/littlefs) support for flash based file storage. + +* STM32F7xx: Fixed [typo](https://github.com/grblHAL/core/issues/186#issuecomment-1242943739). + +--- + +20220906: Core: @@ -18,7 +40,7 @@ Plugins: Drivers: -* All: updated for _grbl/limits.h_ name change and HAL version number increase. +* All: updated for grbl/limits.h name change and HAL version number increase. * iMXRT1062, STM32F4xx, STM32F7xx and ESP32: Added RTC support. diff --git a/driver_opts.h b/driver_opts.h index 0206bf8..00abb05 100644 --- a/driver_opts.h +++ b/driver_opts.h @@ -172,6 +172,10 @@ #define SDCARD_ENABLE 0 #endif +#ifndef LITTLEFS_ENABLE +#define LITTLEFS_ENABLE 0 +#endif + #ifndef SPI_ENABLE #if SDCARD_ENABLE || TRINAMIC_SPI_ENABLE #define SPI_ENABLE 1 diff --git a/vfs.c b/vfs.c index 105d9f6..7a29f68 100644 --- a/vfs.c +++ b/vfs.c @@ -143,23 +143,48 @@ char *vfs_fixpath (char *path) return path; } -static vfs_mount_t *get_mount (const char *filename) +static inline vfs_mount_t *path_is_mount_dir (const char *path) +{ + size_t plen = strlen(path); + + if(*path == '/' && plen == 1) + return &root; + + size_t mlen; + vfs_mount_t *mount = root.next; + + if(mount) do { + mlen = strlen(mount->path) - 1; + if(mlen == plen && !strncmp(mount->path, path, mlen)) + break; + } while((mount = mount->next)); + + return mount; +} + +static vfs_mount_t *get_mount (const char *path) { vfs_errno = 0; - if(*filename == '/') { + if(*path != '/') + return cwdmount; - vfs_mount_t *mount = root.next; + vfs_mount_t *mount; + + if((mount = path_is_mount_dir(path)) == NULL) { + + mount = root.next; if(mount) do { - if(!strncmp(mount->path, filename, strlen(mount->path))) + if(!strncmp(mount->path, path, strlen(mount->path))) break; } while((mount = mount->next)); - return mount ? mount : &root; + if(mount == NULL) + mount = &root; + } - } else - return cwdmount; + return mount; } static const char *get_filename (vfs_mount_t *mount, const char *filename) @@ -245,7 +270,7 @@ int vfs_rename (const char *from, const char *to) int vfs_unlink (const char *filename) { - vfs_mount_t *mount = get_mount(filename); + vfs_mount_t *mount = get_mount(filename); // TODO: test for dir? return mount ? mount->vfs->funlink(get_filename(mount, filename)) : -1; } @@ -374,6 +399,9 @@ bool vfs_mount (const char *path, const vfs_t *fs) else if((vfs = (vfs_mount_t *)malloc(sizeof(vfs_mount_t)))) { strcpy(vfs->path, path); + if(vfs->path[strlen(path) - 1] != '/') + strcat(vfs->path, "/"); + vfs->vfs = fs; vfs->next = NULL; @@ -415,6 +443,19 @@ bool vfs_unmount (const char *path) return true; } +vfs_drive_t *vfs_get_drive (const char *path) +{ + static vfs_drive_t drive; + + vfs_mount_t *mount = get_mount(path); + drive.name = mount->vfs->fs_name; + drive.path = (const char *)mount->path; + drive.mode = mount->vfs->mode; + drive.fs = mount->vfs; + + return &drive; +} + vfs_drives_t *vfs_drives_open (void) { vfs_drives_t *handle; diff --git a/vfs.h b/vfs.h index e01d14d..b81cb9a 100644 --- a/vfs.h +++ b/vfs.h @@ -201,5 +201,6 @@ vfs_drive_t *vfs_drives_read (vfs_drives_t *handle); void vfs_drives_close (vfs_drives_t *handle); vfs_free_t *vfs_drive_getfree (vfs_drive_t *drive); int vfs_drive_format (vfs_drive_t *drive); +vfs_drive_t *vfs_get_drive (const char *path); #endif // INCLUDE_VFS_H