4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
35 * Given a shorthand device name check if a file by that name exists in any
36 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
37 * one is found, store its fully qualified path in the 'path' buffer passed
38 * by the caller and return 0, otherwise return an error.
41 zfs_resolve_shortname(const char *name, char *path, size_t len)
44 char *dir, *env, *envdup;
46 env = getenv("ZPOOL_IMPORT_PATH");
51 dir = strtok(envdup, ":");
52 while (dir && error) {
53 (void) snprintf(path, len, "%s/%s", dir, name);
54 error = access(path, F_OK);
55 dir = strtok(NULL, ":");
59 const char * const *zpool_default_import_path;
62 zpool_default_import_path = zpool_default_search_paths(&count);
64 for (i = 0; i < count && error < 0; i++) {
65 (void) snprintf(path, len, "%s/%s",
66 zpool_default_import_path[i], name);
67 error = access(path, F_OK);
71 return (error ? ENOENT : 0);
75 * Given a shorthand device name look for a match against 'cmp_name'. This
76 * is done by checking all prefix expansions using either the default
77 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
78 * variable. Proper partition suffixes will be appended if this is a
79 * whole disk. When a match is found 0 is returned otherwise ENOENT.
82 zfs_strcmp_shortname(const char *name, const char *cmp_name, int wholedisk)
84 int path_len, cmp_len, i = 0, error = ENOENT;
85 char *dir, *env, *envdup = NULL;
86 char path_name[MAXPATHLEN];
87 const char * const *zpool_default_import_path;
90 zpool_default_import_path = zpool_default_search_paths(&count);
92 cmp_len = strlen(cmp_name);
93 env = getenv("ZPOOL_IMPORT_PATH");
97 dir = strtok(envdup, ":");
99 dir = (char *)zpool_default_import_path[i];
103 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
105 while (dir[strlen(dir)-1] == '/')
106 dir[strlen(dir)-1] = '\0';
109 path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name);
111 path_len = zfs_append_partition(path_name, MAXPATHLEN);
113 if ((path_len == cmp_len) && strcmp(path_name, cmp_name) == 0) {
119 dir = strtok(NULL, ":");
120 } else if (++i < count) {
121 dir = (char *)zpool_default_import_path[i];
134 * Given either a shorthand or fully qualified path name look for a match
135 * against 'cmp'. The passed name will be expanded as needed for comparison
136 * purposes and redundant slashes stripped to ensure an accurate match.
139 zfs_strcmp_pathname(const char *name, const char *cmp, int wholedisk)
141 int path_len, cmp_len;
142 char path_name[MAXPATHLEN];
143 char cmp_name[MAXPATHLEN];
146 /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
147 memset(cmp_name, 0, MAXPATHLEN);
149 dir = strtok(dup, "/");
151 strlcat(cmp_name, "/", sizeof (cmp_name));
152 strlcat(cmp_name, dir, sizeof (cmp_name));
153 dir = strtok(NULL, "/");
158 return (zfs_strcmp_shortname(name, cmp_name, wholedisk));
160 (void) strlcpy(path_name, name, MAXPATHLEN);
161 path_len = strlen(path_name);
162 cmp_len = strlen(cmp_name);
165 path_len = zfs_append_partition(path_name, MAXPATHLEN);
170 if ((path_len != cmp_len) || strcmp(path_name, cmp_name))