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