summaryrefslogtreecommitdiff
path: root/firmware/common/pathfuncs.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/common/pathfuncs.c')
-rw-r--r--firmware/common/pathfuncs.c66
1 files changed, 57 insertions, 9 deletions
diff --git a/firmware/common/pathfuncs.c b/firmware/common/pathfuncs.c
index db3abe6940..b5e5ecb0cf 100644
--- a/firmware/common/pathfuncs.c
+++ b/firmware/common/pathfuncs.c
@@ -24,10 +24,14 @@
24#include "pathfuncs.h" 24#include "pathfuncs.h"
25#include "string-extra.h" 25#include "string-extra.h"
26#include <stdio.h> 26#include <stdio.h>
27#include "file_internal.h"
28#include "debug.h"
27 29
28#ifdef HAVE_MULTIVOLUME 30#ifdef HAVE_MULTIVOLUME
29#include "storage.h" 31#include "storage.h"
30 32
33static char vol_dec_strings[NUM_VOLUMES][ALIGN_UP(VOL_MAX_LEN+2, 4)] = {{0}};
34
31enum storage_name_dec_indexes 35enum storage_name_dec_indexes
32{ 36{
33#if (CONFIG_STORAGE & STORAGE_ATA) 37#if (CONFIG_STORAGE & STORAGE_ATA)
@@ -106,6 +110,54 @@ static const unsigned char storage_dec_indexes[STORAGE_NUM_TYPES+1] =
106#endif 110#endif
107}; 111};
108 112
113/* builds a list of drive/volume specifiers <volstr#> */
114void init_volume_names(void)
115{
116 DEBUGF("%s: ", __func__);
117 FOR_EACH_VOLUME(-1, volume)
118 {
119 const char *voldec = "";
120 char *buffer = vol_dec_strings[volume];
121
122 int type = storage_driver_type(volume_drive(volume));
123 if (type < 0 || type > STORAGE_NUM_TYPES)
124 type = STORAGE_NUM_TYPES;
125 voldec = storage_dec_names[storage_dec_indexes[type]];
126 snprintf(buffer, VOL_MAX_LEN + 1, "%c%s%d%c",
127 VOL_START_TOK, voldec, volume, VOL_END_TOK);
128 DEBUGF("vol<%d> = %s ", volume, buffer);
129 }
130 DEBUGF("\n");
131}
132
133#include <stdio.h>
134
135int path_get_volume_id(const char *name)
136{
137 int v = -1;
138
139 if (!name || *name != VOL_START_TOK)
140 goto bail;
141
142 do {
143 switch (*name)
144 {
145 case '0' ... '9': /* digit; parse volume number */
146 v = (v * 10 + *name - '0') % VOL_NUM_MAX;
147 break;
148 case '\0':
149 case PATH_SEPCH: /* no closing bracket; no volume */
150 v = -1;
151 goto bail;
152 default: /* something else; reset volume */
153 v = 0;
154 }
155 } while (*++name != VOL_END_TOK); /* found end token? */
156
157bail:
158 return v;
159}
160
109/* Returns on which volume this is and sets *nameptr to the portion of the 161/* Returns on which volume this is and sets *nameptr to the portion of the
110 * path after the volume specifier, which could be the null if the path is 162 * path after the volume specifier, which could be the null if the path is
111 * just a volume root. If *nameptr > name, then a volume specifier was 163 * just a volume root. If *nameptr > name, then a volume specifier was
@@ -203,7 +255,8 @@ int path_strip_last_volume(const char *name, const char **nameptr, bool greedy)
203} 255}
204 256
205/* Returns the volume specifier decorated with the storage type name. 257/* Returns the volume specifier decorated with the storage type name.
206 * Assumes the supplied buffer size is at least {VOL_MAX_LEN}+1. 258 * Assumes the supplied buffer size is at least {VOL_MAX_LEN}+1,
259 * vol_dec_strings has been initialized by init_volume_names().
207 */ 260 */
208int get_volume_name(int volume, char *buffer) 261int get_volume_name(int volume, char *buffer)
209{ 262{
@@ -218,17 +271,12 @@ int get_volume_name(int volume, char *buffer)
218 271
219 volume %= VOL_NUM_MAX; /* as path parser would have it */ 272 volume %= VOL_NUM_MAX; /* as path parser would have it */
220 273
221 int type = storage_driver_type(volume_drive(volume)); 274 return strlcpy(buffer, vol_dec_strings[volume], VOL_MAX_LEN + 1);
222 if (type < 0 || type > STORAGE_NUM_TYPES)
223 type = STORAGE_NUM_TYPES;
224
225 const char *voldec = storage_dec_names[storage_dec_indexes[type]];
226 return snprintf(buffer, VOL_MAX_LEN + 1, "%c%s%d%c",
227 VOL_START_TOK, voldec, volume, VOL_END_TOK);
228} 275}
229 276
230/* Returns volume name formatted with the root. Assumes buffer size is at 277/* Returns volume name formatted with the root. Assumes buffer size is at
231 * least {VOL_MAX_LEN}+2 */ 278 * least {VOL_MAX_LEN}+2, vol_dec_strings has been initialized by init_volume_names().
279 */
232int make_volume_root(int volume, char *buffer) 280int make_volume_root(int volume, char *buffer)
233{ 281{
234 char *t = buffer; 282 char *t = buffer;