Path: blob/main/sys/contrib/openzfs/lib/libzutil/zutil_device_path.c
48378 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.24*/2526#include <errno.h>27#include <stdio.h>28#include <stdlib.h>29#include <string.h>30#include <unistd.h>3132#include <libzutil.h>3334/* Substring from after the last slash, or the string itself if none */35const char *36zfs_basename(const char *path)37{38const char *bn = strrchr(path, '/');39return (bn ? bn + 1 : path);40}4142/* Return index of last slash or -1 if none */43ssize_t44zfs_dirnamelen(const char *path)45{46const char *end = strrchr(path, '/');47return (end ? end - path : -1);48}4950/*51* Given a shorthand device name check if a file by that name exists in any52* of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If53* one is found, store its fully qualified path in the 'path' buffer passed54* by the caller and return 0, otherwise return an error.55*/56int57zfs_resolve_shortname(const char *name, char *path, size_t len)58{59const char *env = getenv("ZPOOL_IMPORT_PATH");60char resolved_path[PATH_MAX];6162if (env) {63for (;;) {64env += strspn(env, ":");65size_t dirlen = strcspn(env, ":");66if (dirlen) {67(void) snprintf(path, len, "%.*s/%s",68(int)dirlen, env, name);69if (access(path, F_OK) == 0)70return (0);7172env += dirlen;73} else74break;75}76} else {77size_t count;78const char *const *zpool_default_import_path =79zpool_default_search_paths(&count);8081for (size_t i = 0; i < count; ++i) {82(void) snprintf(path, len, "%s/%s",83zpool_default_import_path[i], name);84if (access(path, F_OK) == 0)85return (0);86}87}8889/*90* The user can pass a relative path like ./file1 for the vdev. The path91* must contain a directory prefix like './file1' or '../file1'. Simply92* passing 'file1' is not allowed, as it may match a block device name.93*/94if ((strncmp(name, "./", 2) == 0 || strncmp(name, "../", 3) == 0) &&95realpath(name, resolved_path) != NULL) {96if (access(resolved_path, F_OK) == 0) {97if (strlen(resolved_path) + 1 <= len) {98if (strlcpy(path, resolved_path, len) < len)99return (0); /* success */100}101}102}103return (errno = ENOENT);104}105106/*107* Given a shorthand device name look for a match against 'cmp_name'. This108* is done by checking all prefix expansions using either the default109* 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment110* variable. Proper partition suffixes will be appended if this is a111* whole disk. When a match is found 0 is returned otherwise ENOENT.112*/113static int114zfs_strcmp_shortname(const char *name, const char *cmp_name, int wholedisk)115{116int path_len, cmp_len, i = 0, error = ENOENT;117char *dir, *env, *envdup = NULL, *tmp = NULL;118char path_name[MAXPATHLEN];119const char *const *zpool_default_import_path = NULL;120size_t count;121122cmp_len = strlen(cmp_name);123env = getenv("ZPOOL_IMPORT_PATH");124125if (env) {126envdup = strdup(env);127dir = strtok_r(envdup, ":", &tmp);128} else {129zpool_default_import_path = zpool_default_search_paths(&count);130dir = (char *)zpool_default_import_path[i];131}132133while (dir) {134/* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */135if (env) {136while (dir[strlen(dir)-1] == '/')137dir[strlen(dir)-1] = '\0';138}139140path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name);141if (wholedisk)142path_len = zfs_append_partition(path_name, MAXPATHLEN);143144if ((path_len == cmp_len) && strcmp(path_name, cmp_name) == 0) {145error = 0;146break;147}148149if (env) {150dir = strtok_r(NULL, ":", &tmp);151} else if (++i < count) {152dir = (char *)zpool_default_import_path[i];153} else {154dir = NULL;155}156}157158if (env)159free(envdup);160161return (error);162}163164/*165* Given either a shorthand or fully qualified path name look for a match166* against 'cmp'. The passed name will be expanded as needed for comparison167* purposes and redundant slashes stripped to ensure an accurate match.168*/169int170zfs_strcmp_pathname(const char *name, const char *cmp, int wholedisk)171{172int path_len, cmp_len;173char path_name[MAXPATHLEN];174char cmp_name[MAXPATHLEN];175char *dir, *tmp = NULL;176177/* Strip redundant slashes if they exist due to ZPOOL_IMPORT_PATH */178cmp_name[0] = '\0';179(void) strlcpy(path_name, cmp, sizeof (path_name));180for (dir = strtok_r(path_name, "/", &tmp);181dir != NULL;182dir = strtok_r(NULL, "/", &tmp)) {183strlcat(cmp_name, "/", sizeof (cmp_name));184strlcat(cmp_name, dir, sizeof (cmp_name));185}186187if (name[0] != '/')188return (zfs_strcmp_shortname(name, cmp_name, wholedisk));189190(void) strlcpy(path_name, name, MAXPATHLEN);191path_len = strlen(path_name);192cmp_len = strlen(cmp_name);193194if (wholedisk) {195path_len = zfs_append_partition(path_name, MAXPATHLEN);196if (path_len == -1)197return (ENOMEM);198}199200if ((path_len != cmp_len) || strcmp(path_name, cmp_name))201return (ENOENT);202203return (0);204}205206207