From c0ac043c6d7b917733df1e0c4f9a74fa54991901 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Tue, 23 Jul 2024 17:30:19 -0400 Subject: get_volume_name generate volume names once then reuse Change-Id: I36c62bfb28af9770b551a1193fbb66eb6fbac76a --- firmware/common/file_internal.c | 3 +++ firmware/common/pathfuncs.c | 36 +++++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'firmware/common') diff --git a/firmware/common/file_internal.c b/firmware/common/file_internal.c index e4554670af..28d370e15f 100644 --- a/firmware/common/file_internal.c +++ b/firmware/common/file_internal.c @@ -761,4 +761,7 @@ void filesystem_init(void) mrsw_init(&file_internal_mrsw); dc_init(); fileobj_mgr_init(); +#ifdef HAVE_MULTIVOLUME + init_volume_names(); +#endif } diff --git a/firmware/common/pathfuncs.c b/firmware/common/pathfuncs.c index db3abe6940..d4f4415526 100644 --- a/firmware/common/pathfuncs.c +++ b/firmware/common/pathfuncs.c @@ -24,10 +24,14 @@ #include "pathfuncs.h" #include "string-extra.h" #include +#include "file_internal.h" +#include "debug.h" #ifdef HAVE_MULTIVOLUME #include "storage.h" +static char vol_dec_strings[NUM_VOLUMES][ALIGN_UP(VOL_MAX_LEN+2, 4)] = {{0}}; + enum storage_name_dec_indexes { #if (CONFIG_STORAGE & STORAGE_ATA) @@ -106,6 +110,24 @@ static const unsigned char storage_dec_indexes[STORAGE_NUM_TYPES+1] = #endif }; +/* builds a list of drive/volume specifiers */ +void init_volume_names(void) +{ + FOR_EACH_VOLUME(-1, volume) + { + const char *voldec = ""; + char *buffer = vol_dec_strings[volume]; + + int type = storage_driver_type(volume_drive(volume)); + if (type < 0 || type > STORAGE_NUM_TYPES) + type = STORAGE_NUM_TYPES; + voldec = storage_dec_names[storage_dec_indexes[type]]; + snprintf(buffer, VOL_MAX_LEN + 1, "%c%s%d%c", + VOL_START_TOK, voldec, volume, VOL_END_TOK); + DEBUGF("%s: vol: %d %s", __func__, volume, buffer); + } +} + /* Returns on which volume this is and sets *nameptr to the portion of the * path after the volume specifier, which could be the null if the path is * just a volume root. If *nameptr > name, then a volume specifier was @@ -203,7 +225,8 @@ int path_strip_last_volume(const char *name, const char **nameptr, bool greedy) } /* Returns the volume specifier decorated with the storage type name. - * Assumes the supplied buffer size is at least {VOL_MAX_LEN}+1. + * Assumes the supplied buffer size is at least {VOL_MAX_LEN}+1, + * vol_dec_strings has been initialized by init_volume_names(). */ int get_volume_name(int volume, char *buffer) { @@ -218,17 +241,12 @@ int get_volume_name(int volume, char *buffer) volume %= VOL_NUM_MAX; /* as path parser would have it */ - int type = storage_driver_type(volume_drive(volume)); - if (type < 0 || type > STORAGE_NUM_TYPES) - type = STORAGE_NUM_TYPES; - - const char *voldec = storage_dec_names[storage_dec_indexes[type]]; - return snprintf(buffer, VOL_MAX_LEN + 1, "%c%s%d%c", - VOL_START_TOK, voldec, volume, VOL_END_TOK); + return strlcpy(buffer, vol_dec_strings[volume], VOL_MAX_LEN + 1); } /* Returns volume name formatted with the root. Assumes buffer size is at - * least {VOL_MAX_LEN}+2 */ + * least {VOL_MAX_LEN}+2, vol_dec_strings has been initialized by init_volume_names(). + */ int make_volume_root(int volume, char *buffer) { char *t = buffer; -- cgit v1.2.3