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.
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
)
60 char *dir
, *env
, *envdup
, *tmp
= NULL
;
62 env
= getenv("ZPOOL_IMPORT_PATH");
67 for (dir
= strtok_r(envdup
, ":", &tmp
);
68 dir
!= NULL
&& error
!= 0;
69 dir
= strtok_r(NULL
, ":", &tmp
)) {
70 (void) snprintf(path
, len
, "%s/%s", dir
, name
);
71 error
= access(path
, F_OK
);
75 const char * const *zpool_default_import_path
;
78 zpool_default_import_path
= zpool_default_search_paths(&count
);
80 for (i
= 0; i
< count
&& error
< 0; i
++) {
81 (void) snprintf(path
, len
, "%s/%s",
82 zpool_default_import_path
[i
], name
);
83 error
= access(path
, F_OK
);
87 return (error
? ENOENT
: 0);
91 * Given a shorthand device name look for a match against 'cmp_name'. This
92 * is done by checking all prefix expansions using either the default
93 * 'zpool_default_import_paths' or the ZPOOL_IMPORT_PATH environment
94 * variable. Proper partition suffixes will be appended if this is a
95 * whole disk. When a match is found 0 is returned otherwise ENOENT.
98 zfs_strcmp_shortname(const char *name
, const char *cmp_name
, int wholedisk
)
100 int path_len
, cmp_len
, i
= 0, error
= ENOENT
;
101 char *dir
, *env
, *envdup
= NULL
, *tmp
= NULL
;
102 char path_name
[MAXPATHLEN
];
103 const char * const *zpool_default_import_path
= NULL
;
106 cmp_len
= strlen(cmp_name
);
107 env
= getenv("ZPOOL_IMPORT_PATH");
110 envdup
= strdup(env
);
111 dir
= strtok_r(envdup
, ":", &tmp
);
113 zpool_default_import_path
= zpool_default_search_paths(&count
);
114 dir
= (char *)zpool_default_import_path
[i
];
118 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
120 while (dir
[strlen(dir
)-1] == '/')
121 dir
[strlen(dir
)-1] = '\0';
124 path_len
= snprintf(path_name
, MAXPATHLEN
, "%s/%s", dir
, name
);
126 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
128 if ((path_len
== cmp_len
) && strcmp(path_name
, cmp_name
) == 0) {
134 dir
= strtok_r(NULL
, ":", &tmp
);
135 } else if (++i
< count
) {
136 dir
= (char *)zpool_default_import_path
[i
];
149 * Given either a shorthand or fully qualified path name look for a match
150 * against 'cmp'. The passed name will be expanded as needed for comparison
151 * purposes and redundant slashes stripped to ensure an accurate match.
154 zfs_strcmp_pathname(const char *name
, const char *cmp
, int wholedisk
)
156 int path_len
, cmp_len
;
157 char path_name
[MAXPATHLEN
];
158 char cmp_name
[MAXPATHLEN
];
159 char *dir
, *tmp
= NULL
;
161 /* Strip redundant slashes if they exist due to ZPOOL_IMPORT_PATH */
163 (void) strlcpy(path_name
, cmp
, sizeof (path_name
));
164 for (dir
= strtok_r(path_name
, "/", &tmp
);
166 dir
= strtok_r(NULL
, "/", &tmp
)) {
167 strlcat(cmp_name
, "/", sizeof (cmp_name
));
168 strlcat(cmp_name
, dir
, sizeof (cmp_name
));
172 return (zfs_strcmp_shortname(name
, cmp_name
, wholedisk
));
174 (void) strlcpy(path_name
, name
, MAXPATHLEN
);
175 path_len
= strlen(path_name
);
176 cmp_len
= strlen(cmp_name
);
179 path_len
= zfs_append_partition(path_name
, MAXPATHLEN
);
184 if ((path_len
!= cmp_len
) || strcmp(path_name
, cmp_name
))