aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libzutil
diff options
context:
space:
mode:
authorнаб <[email protected]>2022-03-15 23:23:53 +0100
committerBrian Behlendorf <[email protected]>2022-03-23 08:55:18 -0700
commit8a2ed86001583280b4d84d1f37919ea8a34ddb79 (patch)
tree9137ccdfbaf8eed0470f95adaa0312e4de120f60 /lib/libzutil
parent018937884f93e0f4ae7f560c352a138ec6b69cb4 (diff)
libzutil: zfs_resolve_shortname: don't strdup() ZPOOL_IMPORT_PATH
Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes #13223
Diffstat (limited to 'lib/libzutil')
-rw-r--r--lib/libzutil/zutil_device_path.c39
1 files changed, 20 insertions, 19 deletions
diff --git a/lib/libzutil/zutil_device_path.c b/lib/libzutil/zutil_device_path.c
index 435c444b2..6a8c8869e 100644
--- a/lib/libzutil/zutil_device_path.c
+++ b/lib/libzutil/zutil_device_path.c
@@ -56,35 +56,36 @@ zfs_dirnamelen(const char *path)
int
zfs_resolve_shortname(const char *name, char *path, size_t len)
{
- int i, error = -1;
- char *dir, *env, *envdup, *tmp = NULL;
-
- env = getenv("ZPOOL_IMPORT_PATH");
- errno = ENOENT;
+ const char *env = getenv("ZPOOL_IMPORT_PATH");
if (env) {
- envdup = strdup(env);
- for (dir = strtok_r(envdup, ":", &tmp);
- dir != NULL && error != 0;
- dir = strtok_r(NULL, ":", &tmp)) {
- (void) snprintf(path, len, "%s/%s", dir, name);
- error = access(path, F_OK);
+ for (;;) {
+ env += strspn(env, ":");
+ size_t dirlen = strcspn(env, ":");
+ if (dirlen) {
+ (void) snprintf(path, len, "%.*s/%s",
+ (int)dirlen, env, name);
+ if (access(path, F_OK) == 0)
+ return (0);
+
+ env += dirlen;
+ } else
+ break;
}
- free(envdup);
} else {
- const char * const *zpool_default_import_path;
size_t count;
+ const char *const *zpool_default_import_path =
+ zpool_default_search_paths(&count);
- zpool_default_import_path = zpool_default_search_paths(&count);
-
- for (i = 0; i < count && error < 0; i++) {
+ for (size_t i = 0; i < count; ++i) {
(void) snprintf(path, len, "%s/%s",
zpool_default_import_path[i], name);
- error = access(path, F_OK);
+ if (access(path, F_OK) == 0)
+ return (0);
}
}
- return (error ? ENOENT : 0);
+ return (errno = ENOENT);
}
/*
@@ -100,7 +101,7 @@ zfs_strcmp_shortname(const char *name, const char *cmp_name, int wholedisk)
int path_len, cmp_len, i = 0, error = ENOENT;
char *dir, *env, *envdup = NULL, *tmp = NULL;
char path_name[MAXPATHLEN];
- const char * const *zpool_default_import_path = NULL;
+ const char *const *zpool_default_import_path = NULL;
size_t count;
cmp_len = strlen(cmp_name);