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, *tmp = NULL; 45 46 env = getenv("ZPOOL_IMPORT_PATH"); 47 errno = ENOENT; 48 49 if (env) { 50 envdup = strdup(env); 51 for (dir = strtok_r(envdup, ":", &tmp); 52 dir != NULL && error != 0; 53 dir = strtok_r(NULL, ":", &tmp)) { 54 (void) snprintf(path, len, "%s/%s", dir, name); 55 error = access(path, F_OK); 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, *tmp = NULL; 86 char path_name[MAXPATHLEN]; 87 const char * const *zpool_default_import_path = NULL; 88 size_t count; 89 90 cmp_len = strlen(cmp_name); 91 env = getenv("ZPOOL_IMPORT_PATH"); 92 93 if (env) { 94 envdup = strdup(env); 95 dir = strtok_r(envdup, ":", &tmp); 96 } else { 97 zpool_default_import_path = zpool_default_search_paths(&count); 98 dir = (char *)zpool_default_import_path[i]; 99 } 100 101 while (dir) { 102 /* Trim trailing directory slashes from ZPOOL_IMPORT_PATH */ 103 if (env) { 104 while (dir[strlen(dir)-1] == '/') 105 dir[strlen(dir)-1] = '\0'; 106 } 107 108 path_len = snprintf(path_name, MAXPATHLEN, "%s/%s", dir, name); 109 if (wholedisk) 110 path_len = zfs_append_partition(path_name, MAXPATHLEN); 111 112 if ((path_len == cmp_len) && strcmp(path_name, cmp_name) == 0) { 113 error = 0; 114 break; 115 } 116 117 if (env) { 118 dir = strtok_r(NULL, ":", &tmp); 119 } else if (++i < count) { 120 dir = (char *)zpool_default_import_path[i]; 121 } else { 122 dir = NULL; 123 } 124 } 125 126 if (env) 127 free(envdup); 128 129 return (error); 130 } 131 132 /* 133 * Given either a shorthand or fully qualified path name look for a match 134 * against 'cmp'. The passed name will be expanded as needed for comparison 135 * purposes and redundant slashes stripped to ensure an accurate match. 136 */ 137 int 138 zfs_strcmp_pathname(const char *name, const char *cmp, int wholedisk) 139 { 140 int path_len, cmp_len; 141 char path_name[MAXPATHLEN]; 142 char cmp_name[MAXPATHLEN]; 143 char *dir, *tmp = NULL; 144 145 /* Strip redundant slashes if they exist due to ZPOOL_IMPORT_PATH */ 146 cmp_name[0] = '\0'; 147 (void) strlcpy(path_name, cmp, sizeof (path_name)); 148 for (dir = strtok_r(path_name, "/", &tmp); 149 dir != NULL; 150 dir = strtok_r(NULL, "/", &tmp)) { 151 strlcat(cmp_name, "/", sizeof (cmp_name)); 152 strlcat(cmp_name, dir, sizeof (cmp_name)); 153 } 154 155 if (name[0] != '/') 156 return (zfs_strcmp_shortname(name, cmp_name, wholedisk)); 157 158 (void) strlcpy(path_name, name, MAXPATHLEN); 159 path_len = strlen(path_name); 160 cmp_len = strlen(cmp_name); 161 162 if (wholedisk) { 163 path_len = zfs_append_partition(path_name, MAXPATHLEN); 164 if (path_len == -1) 165 return (ENOMEM); 166 } 167 168 if ((path_len != cmp_len) || strcmp(path_name, cmp_name)) 169 return (ENOENT); 170 171 return (0); 172 } 173