138494Sobrien/*
2174294Sobrien * Copyright (c) 1997-2006 Erez Zadok
338494Sobrien * Copyright (c) 1990 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1990 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
1938494Sobrien * 3. All advertising materials mentioning features or use of this software
2042629Sobrien *    must display the following acknowledgment:
2138494Sobrien *      This product includes software developed by the University of
2238494Sobrien *      California, Berkeley and its contributors.
2338494Sobrien * 4. Neither the name of the University nor the names of its contributors
2438494Sobrien *    may be used to endorse or promote products derived from this software
2538494Sobrien *    without specific prior written permission.
2638494Sobrien *
2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3038494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3738494Sobrien * SUCH DAMAGE.
3838494Sobrien *
3938494Sobrien *
40174294Sobrien * File: am-utils/amd/info_union.c
4138494Sobrien *
4238494Sobrien */
4338494Sobrien
4438494Sobrien/*
4538494Sobrien * Get info from the system namespace
4638494Sobrien *
4738494Sobrien * NOTE: Cannot handle reads back through the automounter.
4838494Sobrien * THIS WILL CAUSE A DEADLOCK!
4938494Sobrien */
5038494Sobrien
5138494Sobrien#ifdef HAVE_CONFIG_H
5238494Sobrien# include <config.h>
5338494Sobrien#endif /* HAVE_CONFIG_H */
5438494Sobrien#include <am_defs.h>
5538494Sobrien#include <amd.h>
5638494Sobrien
5738494Sobrien#define	UNION_PREFIX	"union:"
5838494Sobrien#define	UNION_PREFLEN	6
5938494Sobrien
6038494Sobrien/* forward declarations */
6138494Sobrienint union_init(mnt_map *m, char *map, time_t *tp);
6238494Sobrienint union_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
6338494Sobrienint union_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *));
6438494Sobrien
6538494Sobrien
6638494Sobrien/*
6738494Sobrien * No way to probe - check the map name begins with "union:"
6838494Sobrien */
6938494Sobrienint
7038494Sobrienunion_init(mnt_map *m, char *map, time_t *tp)
7138494Sobrien{
7238494Sobrien  *tp = 0;
7338494Sobrien  return NSTREQ(map, UNION_PREFIX, UNION_PREFLEN) ? 0 : ENOENT;
7438494Sobrien}
7538494Sobrien
7638494Sobrien
7738494Sobrienint
7838494Sobrienunion_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
7938494Sobrien{
8038494Sobrien  char *mapd = strdup(map + UNION_PREFLEN);
8138494Sobrien  char **v = strsplit(mapd, ':', '\"');
8238494Sobrien  char **p;
83174294Sobrien  size_t l;
8438494Sobrien
8538494Sobrien  for (p = v; p[1]; p++) ;
86174294Sobrien  l = strlen(*p) + 5;
87174294Sobrien  *pval = xmalloc(l);
88174294Sobrien  xsnprintf(*pval, l, "fs:=%s", *p);
8938494Sobrien  XFREE(mapd);
9038494Sobrien  XFREE(v);
9138494Sobrien  return 0;
9238494Sobrien}
9338494Sobrien
9438494Sobrien
9538494Sobrienint
9638494Sobrienunion_reload(mnt_map *m, char *map, void (*fn) (mnt_map *, char *, char *))
9738494Sobrien{
9838494Sobrien  char *mapd = strdup(map + UNION_PREFLEN);
9938494Sobrien  char **v = strsplit(mapd, ':', '\"');
10038494Sobrien  char **dir;
10138494Sobrien
10238494Sobrien  /*
10338494Sobrien   * Add fake /defaults entry
10438494Sobrien   */
10538494Sobrien  (*fn) (m, strdup("/defaults"), strdup("type:=link;opts:=nounmount;sublink:=${key}"));
10638494Sobrien
10738494Sobrien  for (dir = v; *dir; dir++) {
108174294Sobrien    size_t l;
10938494Sobrien    struct dirent *dp;
11038494Sobrien
11138494Sobrien    DIR *dirp = opendir(*dir);
11238494Sobrien    if (!dirp) {
11338494Sobrien      plog(XLOG_USER, "Cannot read directory %s: %m", *dir);
11438494Sobrien      continue;
11538494Sobrien    }
116174294Sobrien    l = strlen(*dir) + 5;
11738494Sobrien
11838494Sobrien    dlog("Reading directory %s...", *dir);
11938494Sobrien    while ((dp = readdir(dirp))) {
12038494Sobrien      char *val, *dpname = &dp->d_name[0];
12138494Sobrien      if (dpname[0] == '.' &&
12238494Sobrien	  (dpname[1] == '\0' ||
12338494Sobrien	   (dpname[1] == '.' && dpname[2] == '\0')))
12438494Sobrien	continue;
12538494Sobrien
12638494Sobrien      dlog("... gives %s", dp->d_name);
127174294Sobrien      val = xmalloc(l);
128174294Sobrien      xsnprintf(val, l + 5, "fs:=%s", *dir);
12938494Sobrien      (*fn) (m, strdup(dp->d_name), val);
13038494Sobrien    }
13138494Sobrien    closedir(dirp);
13238494Sobrien  }
13338494Sobrien
13438494Sobrien  /*
13538494Sobrien   * Add wildcard entry
13638494Sobrien   */
13738494Sobrien  {
138174294Sobrien    size_t l = strlen(*(dir-1)) + 5;
139174294Sobrien    char *val = xmalloc(l);
14038494Sobrien
141174294Sobrien    xsnprintf(val, l, "fs:=%s", *(dir-1));
14238494Sobrien    (*fn) (m, strdup("*"), val);
14338494Sobrien  }
14438494Sobrien  XFREE(mapd);
14538494Sobrien  XFREE(v);
14638494Sobrien  return 0;
14738494Sobrien}
148