2 * Copyright (c) 1997-2014 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/info_union.c
41 * Get info from the system namespace
43 * NOTE: Cannot handle reads back through the automounter.
44 * THIS WILL CAUSE A DEADLOCK!
49 #endif /* HAVE_CONFIG_H */
53 #define UNION_PREFIX "union:"
54 #define UNION_PREFLEN 6
56 /* forward declarations */
57 int union_init(mnt_map *m, char *map, time_t *tp);
58 int union_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
59 int union_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *));
63 * No way to probe - check the map name begins with "union:"
66 union_init(mnt_map *m, char *map, time_t *tp)
69 return NSTREQ(map, UNION_PREFIX, UNION_PREFLEN) ? 0 : ENOENT;
74 union_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
76 char *mapd = xstrdup(map + UNION_PREFLEN);
77 char **v = strsplit(mapd, ':', '\"');
81 for (p = v; p[1]; p++) ;
84 xsnprintf(*pval, l, "fs:=%s", *p);
92 union_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *))
94 static const char fseq[] = "fs:=";
95 char *mapd = xstrdup(map + UNION_PREFLEN);
96 char **v = strsplit(mapd, ':', '\"');
100 * Add fake /defaults entry
102 (*fn) (m, xstrdup("/defaults"), xstrdup("type:=link;opts:=nounmount;sublink:=${key}"));
104 for (dir = v; *dir; dir++) {
108 DIR *dirp = opendir(*dir);
110 plog(XLOG_USER, "Cannot read directory %s: %m", *dir);
113 l = strlen(*dir) + sizeof(fseq);
115 dlog("Reading directory %s...", *dir);
116 while ((dp = readdir(dirp))) {
117 char *val, *dpname = &dp->d_name[0];
118 if (dpname[0] == '.' &&
119 (dpname[1] == '\0' ||
120 (dpname[1] == '.' && dpname[2] == '\0')))
123 dlog("... gives %s", dp->d_name);
125 xsnprintf(val, l, "%s%s", fseq, *dir);
126 (*fn) (m, xstrdup(dp->d_name), val);
135 size_t l = strlen(*(dir-1)) + sizeof(fseq);
136 char *val = xmalloc(l);
138 xsnprintf(val, l, "%s%s", fseq, *(dir-1));
139 (*fn) (m, xstrdup("*"), val);