gentoo: use /var/run for new /run compatibility
[util-vserver.git] / lib_internal / matchlist-initrefserverlist.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "matchlist.h"
24 #include "util-io.h"
25
26 #include <dirent.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30
31 #define ENSC_WRAPPERS_FCNTL     1
32 #define ENSC_WRAPPERS_UNISTD    1
33 #define ENSC_WRAPPERS_STDLIB    1
34 #include <wrappers.h>
35
36 static int
37 selectRefserver(struct dirent const *ent)
38 {
39   return strncmp(ent->d_name, "refserver.", 10)==0;
40 }
41
42 void
43 MatchList_initRefserverList(struct MatchList **lst, size_t *cnt,
44                             char const *dir)
45 {
46   int                   cur_dir = Eopen(".", O_RDONLY, 0);
47   struct dirent         **entries;
48   int                   count,i;
49
50   Echdir(dir);
51   count = scandir(".", &entries, selectRefserver, alphasort);
52   if (count==-1) {
53     perror("scandir()");
54     exit(1);
55   }
56
57   if (count==0) {
58     WRITE_MSG(2, "no reference vserver configured\n");
59     exit(1);
60   }
61
62   *lst = Emalloc(sizeof(struct MatchList) * count);
63   *cnt = count;
64   for (i=0; i<count; ++i) {
65     char const                  *tmp   = entries[i]->d_name;
66     size_t                      l      = strlen(tmp);
67     char                        vname[sizeof("./") + l];
68     struct MatchVserverInfo     vserver = {
69       .name        = vname,
70       .use_pkgmgmt = true
71     };
72
73     memcpy(vname,   "./", 2);
74     memcpy(vname+2, tmp,  l+1);
75
76     if (!MatchVserverInfo_init(&vserver)) {
77       WRITE_MSG(2, "failed to initialize unification of reference vserver\n");
78       exit(1);
79     }
80
81     if (!MatchList_initByVserver((*lst)+i, &vserver)) {
82       WRITE_MSG(2, "unification for reference vserver not configured\n");
83       exit(1);
84     }
85
86     free(entries[i]);
87     MatchVserverInfo_free(&vserver);
88   }
89   free(entries);
90
91   Efchdir(cur_dir);
92   Eclose(cur_dir);
93 }