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 https://opensource.org/licenses/CDDL-1.0.
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.
34 /* Substring from after the last slash, or the string itself if none */
36 zfs_basename(const char *path
)
38 const char *bn
= strrchr(path
, '/');
39 return (bn
? bn
+ 1 : path
);
42 /* Return index of last slash or -1 if none */
44 zfs_dirnamelen(const char *path
)
46 const char *end
= strrchr(path
, '/');
47 return (end
? end
- path
: -1);
51 * Given a shorthand device name check if a file by that name exists in any
52 * of the 'zpool_default_import_path' or ZPOOL_IMPORT_PATH directories. If
53 * one is found, store its fully qualified path in the 'path' buffer passed
54 * by the caller and return 0, otherwise return an error.
57 zfs_resolve_shortname(const char *name
, char *path
, size_t len
)
59 const char *env
= getenv("ZPOOL_IMPORT_PATH");
63 env
+= strspn(env
, ":");
64 size_t dirlen
= strcspn(env
, ":");
66 (void) snprintf(path
, len
, "%.*s/%s",
67 (int)dirlen
, env
, name
);
68 if (access(path
, F_OK
) == 0)
77 const char *const *zpool_default_import_path
=
78 zpool_default_search_paths(&count
);
80 for (size_t i
= 0; i
< count
; ++i
) {
81 (void) snprintf(path
, len
, "%s/%s",
82 zpool_default_import_path
[i
], name
);
83 if (access(path
, F_OK
) == 0)
88 return (errno
= ENOENT
);
92 * Given a shorthand device name look for a match against 'cmp_name'. This
93 * is done by checking all prefix expansions using either the default
94 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
95 * variable. Proper partition suffixes will be appended if this is a
96 * whole disk. When a match is found 0 is returned otherwise ENOENT.
99 zfs_strcmp_shortname(const char *name
, const char *cmp_name
, int wholedisk
)
101 int path_len
, cmp_len
, i
= 0, error
= ENOENT
;
102 char *dir
, *env
, *envdup
= NULL
, *tmp
= NULL
;
103 char path_name
[MAXPATHLEN
];
104 const char *const *zpool_default_import_path
= NULL
;
107 cmp_len
= strlen(cmp_name
);
108 env
= getenv("ZPOOL_IMPORT_PATH");
111 envdup
= strdup(env
);
112 dir
= strtok_r(envdup
, ":", &tmp
);
114 zpool_default_import_path
= zpool_default_search_paths(&count
);
115 dir
= (char *)zpool_default_import_path
[i
];
119 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
121 while (dir
[strlen(dir
)-1] == '/')
122 dir
[strlen(dir
)-1] = '\0';
125 path_len
= snprintf(path_name
, MAXPATHLEN
, "%s/%s", dir
, name
);
127 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
129 if ((path_len
== cmp_len
) && strcmp(path_name
, cmp_name
) == 0) {
135 dir
= strtok_r(NULL
, ":", &tmp
);
136 } else if (++i
< count
) {
137 dir
= (char *)zpool_default_import_path
[i
];
150 * Given either a shorthand or fully qualified path name look for a match
151 * against 'cmp'. The passed name will be expanded as needed for comparison
152 * purposes and redundant slashes stripped to ensure an accurate match.
155 zfs_strcmp_pathname(const char *name
, const char *cmp
, int wholedisk
)
157 int path_len
, cmp_len
;
158 char path_name
[MAXPATHLEN
];
159 char cmp_name
[MAXPATHLEN
];
160 char *dir
, *tmp
= NULL
;
162 /* Strip redundant slashes if they exist due to ZPOOL_IMPORT_PATH */
164 (void) strlcpy(path_name
, cmp
, sizeof (path_name
));
165 for (dir
= strtok_r(path_name
, "/", &tmp
);
167 dir
= strtok_r(NULL
, "/", &tmp
)) {
168 strlcat(cmp_name
, "/", sizeof (cmp_name
));
169 strlcat(cmp_name
, dir
, sizeof (cmp_name
));
173 return (zfs_strcmp_shortname(name
, cmp_name
, wholedisk
));
175 (void) strlcpy(path_name
, name
, MAXPATHLEN
);
176 path_len
= strlen(path_name
);
177 cmp_len
= strlen(cmp_name
);
180 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
185 if ((path_len
!= cmp_len
) || strcmp(path_name
, cmp_name
))