1 /* $NetBSD: info_union.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1997-2014 Erez Zadok 5 * Copyright (c) 1990 Jan-Simon Pendry 6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine 7 * Copyright (c) 1990 The Regents of the University of California. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * Jan-Simon Pendry at Imperial College, London. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * 38 * File: am-utils/amd/info_union.c 39 * 40 */ 41 42 /* 43 * Get info from the system namespace 44 * 45 * NOTE: Cannot handle reads back through the automounter. 46 * THIS WILL CAUSE A DEADLOCK! 47 */ 48 49 #ifdef HAVE_CONFIG_H 50 # include <config.h> 51 #endif /* HAVE_CONFIG_H */ 52 #include <am_defs.h> 53 #include <amd.h> 54 55 #define UNION_PREFIX "union:" 56 #define UNION_PREFLEN 6 57 58 /* forward declarations */ 59 int union_init(mnt_map *m, char *map, time_t *tp); 60 int union_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp); 61 int union_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *)); 62 63 64 /* 65 * No way to probe - check the map name begins with "union:" 66 */ 67 int 68 union_init(mnt_map *m, char *map, time_t *tp) 69 { 70 *tp = 0; 71 return NSTREQ(map, UNION_PREFIX, UNION_PREFLEN) ? 0 : ENOENT; 72 } 73 74 75 int 76 union_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp) 77 { 78 char *mapd = xstrdup(map + UNION_PREFLEN); 79 char **v = strsplit(mapd, ':', '\"'); 80 char **p; 81 size_t l; 82 83 for (p = v; p[1]; p++) ; 84 l = strlen(*p) + 5; 85 *pval = xmalloc(l); 86 xsnprintf(*pval, l, "fs:=%s", *p); 87 XFREE(mapd); 88 XFREE(v); 89 return 0; 90 } 91 92 93 int 94 union_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *)) 95 { 96 static const char fseq[] = "fs:="; 97 char *mapd = xstrdup(map + UNION_PREFLEN); 98 char **v = strsplit(mapd, ':', '\"'); 99 char **dir; 100 101 /* 102 * Add fake /defaults entry 103 */ 104 (*fn) (m, xstrdup("/defaults"), xstrdup("type:=link;opts:=nounmount;sublink:=${key}")); 105 106 for (dir = v; *dir; dir++) { 107 size_t l; 108 struct dirent *dp; 109 110 DIR *dirp = opendir(*dir); 111 if (!dirp) { 112 plog(XLOG_USER, "Cannot read directory %s: %m", *dir); 113 continue; 114 } 115 l = strlen(*dir) + sizeof(fseq); 116 117 dlog("Reading directory %s...", *dir); 118 while ((dp = readdir(dirp))) { 119 char *val, *dpname = &dp->d_name[0]; 120 if (dpname[0] == '.' && 121 (dpname[1] == '\0' || 122 (dpname[1] == '.' && dpname[2] == '\0'))) 123 continue; 124 125 dlog("... gives %s", dp->d_name); 126 val = xmalloc(l); 127 xsnprintf(val, l, "%s%s", fseq, *dir); 128 (*fn) (m, xstrdup(dp->d_name), val); 129 } 130 closedir(dirp); 131 } 132 133 /* 134 * Add wildcard entry 135 */ 136 { 137 size_t l = strlen(*(dir-1)) + sizeof(fseq); 138 char *val = xmalloc(l); 139 140 xsnprintf(val, l, "%s%s", fseq, *(dir-1)); 141 (*fn) (m, xstrdup("*"), val); 142 } 143 XFREE(mapd); 144 XFREE(v); 145 return 0; 146 } 147