]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/amd/amd/info_union.c
MFC r308493, r308619: Update amd from am-utils 6.1.5 to 6.2.
[FreeBSD/stable/10.git] / contrib / amd / amd / info_union.c
1 /*
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.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  *
36  * File: am-utils/amd/info_union.c
37  *
38  */
39
40 /*
41  * Get info from the system namespace
42  *
43  * NOTE: Cannot handle reads back through the automounter.
44  * THIS WILL CAUSE A DEADLOCK!
45  */
46
47 #ifdef HAVE_CONFIG_H
48 # include <config.h>
49 #endif /* HAVE_CONFIG_H */
50 #include <am_defs.h>
51 #include <amd.h>
52
53 #define UNION_PREFIX    "union:"
54 #define UNION_PREFLEN   6
55
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 *));
60
61
62 /*
63  * No way to probe - check the map name begins with "union:"
64  */
65 int
66 union_init(mnt_map *m, char *map, time_t *tp)
67 {
68   *tp = 0;
69   return NSTREQ(map, UNION_PREFIX, UNION_PREFLEN) ? 0 : ENOENT;
70 }
71
72
73 int
74 union_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
75 {
76   char *mapd = xstrdup(map + UNION_PREFLEN);
77   char **v = strsplit(mapd, ':', '\"');
78   char **p;
79   size_t l;
80
81   for (p = v; p[1]; p++) ;
82   l = strlen(*p) + 5;
83   *pval = xmalloc(l);
84   xsnprintf(*pval, l, "fs:=%s", *p);
85   XFREE(mapd);
86   XFREE(v);
87   return 0;
88 }
89
90
91 int
92 union_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *))
93 {
94   static const char fseq[] = "fs:=";
95   char *mapd = xstrdup(map + UNION_PREFLEN);
96   char **v = strsplit(mapd, ':', '\"');
97   char **dir;
98
99   /*
100    * Add fake /defaults entry
101    */
102   (*fn) (m, xstrdup("/defaults"), xstrdup("type:=link;opts:=nounmount;sublink:=${key}"));
103
104   for (dir = v; *dir; dir++) {
105     size_t l;
106     struct dirent *dp;
107
108     DIR *dirp = opendir(*dir);
109     if (!dirp) {
110       plog(XLOG_USER, "Cannot read directory %s: %m", *dir);
111       continue;
112     }
113     l = strlen(*dir) + sizeof(fseq);
114
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')))
121         continue;
122
123       dlog("... gives %s", dp->d_name);
124       val = xmalloc(l);
125       xsnprintf(val, l, "%s%s", fseq, *dir);
126       (*fn) (m, xstrdup(dp->d_name), val);
127     }
128     closedir(dirp);
129   }
130
131   /*
132    * Add wildcard entry
133    */
134   {
135     size_t l = strlen(*(dir-1)) + sizeof(fseq);
136     char *val = xmalloc(l);
137
138     xsnprintf(val, l, "%s%s", fseq, *(dir-1));
139     (*fn) (m, xstrdup("*"), val);
140   }
141   XFREE(mapd);
142   XFREE(v);
143   return 0;
144 }