]> granicus.if.org Git - zfs/blob - lib/libzutil/zutil_device_path.c
OpenZFS restructuring - libzutil
[zfs] / lib / libzutil / zutil_device_path.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <libzutil.h>
33
34 /*
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.
39  */
40 int
41 zfs_resolve_shortname(const char *name, char *path, size_t len)
42 {
43         int i, error = -1;
44         char *dir, *env, *envdup;
45
46         env = getenv("ZPOOL_IMPORT_PATH");
47         errno = ENOENT;
48
49         if (env) {
50                 envdup = strdup(env);
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, ":");
56                 }
57                 free(envdup);
58         } else {
59                 const char * const *zpool_default_import_path;
60                 size_t count;
61
62                 zpool_default_import_path = zpool_default_search_paths(&count);
63
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);
68                 }
69         }
70
71         return (error ? ENOENT : 0);
72 }
73
74 /*
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.
80  */
81 static int
82 zfs_strcmp_shortname(const char *name, const char *cmp_name, int wholedisk)
83 {
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;
88         size_t count;
89
90         zpool_default_import_path = zpool_default_search_paths(&count);
91
92         cmp_len = strlen(cmp_name);
93         env = getenv("ZPOOL_IMPORT_PATH");
94
95         if (env) {
96                 envdup = strdup(env);
97                 dir = strtok(envdup, ":");
98         } else {
99                 dir =  (char *)zpool_default_import_path[i];
100         }
101
102         while (dir) {
103                 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */
104                 if (env) {
105                         while (dir[strlen(dir)-1] == '/')
106                                 dir[strlen(dir)-1] = '\0';
107                 }
108
109                 path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name);
110                 if (wholedisk)
111                         path_len = zfs_append_partition(path_name, MAXPATHLEN);
112
113                 if ((path_len == cmp_len) && strcmp(path_name, cmp_name) == 0) {
114                         error = 0;
115                         break;
116                 }
117
118                 if (env) {
119                         dir = strtok(NULL, ":");
120                 } else if (++i < count) {
121                         dir = (char *)zpool_default_import_path[i];
122                 } else {
123                         dir = NULL;
124                 }
125         }
126
127         if (env)
128                 free(envdup);
129
130         return (error);
131 }
132
133 /*
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.
137  */
138 int
139 zfs_strcmp_pathname(const char *name, const char *cmp, int wholedisk)
140 {
141         int path_len, cmp_len;
142         char path_name[MAXPATHLEN];
143         char cmp_name[MAXPATHLEN];
144         char *dir, *dup;
145
146         /* Strip redundant slashes if one exists due to ZPOOL_IMPORT_PATH */
147         memset(cmp_name, 0, MAXPATHLEN);
148         dup = strdup(cmp);
149         dir = strtok(dup, "/");
150         while (dir) {
151                 strlcat(cmp_name, "/", sizeof (cmp_name));
152                 strlcat(cmp_name, dir, sizeof (cmp_name));
153                 dir = strtok(NULL, "/");
154         }
155         free(dup);
156
157         if (name[0] != '/')
158                 return (zfs_strcmp_shortname(name, cmp_name, wholedisk));
159
160         (void) strlcpy(path_name, name, MAXPATHLEN);
161         path_len = strlen(path_name);
162         cmp_len = strlen(cmp_name);
163
164         if (wholedisk) {
165                 path_len = zfs_append_partition(path_name, MAXPATHLEN);
166                 if (path_len == -1)
167                         return (ENOMEM);
168         }
169
170         if ((path_len != cmp_len) || strcmp(path_name, cmp_name))
171                 return (ENOENT);
172
173         return (0);
174 }