90058c67e0a2c7e89fa98eeaedafe664c043715f
[util-vserver.git] / lib_internal / matchlist-initrefserverlist.c
1 // $Id: matchlist-initrefserverlist.c,v 1.4 2005/03/23 02:05:23 ensc Exp $    --*- 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
30 #define ENSC_WRAPPERS_FCNTL     1
31 #define ENSC_WRAPPERS_UNISTD    1
32 #define ENSC_WRAPPERS_STDLIB    1
33 #include <wrappers.h>
34
35 static int
36 selectRefserver(struct dirent const *ent)
37 {
38   return strncmp(ent->d_name, "refserver.", 10)==0;
39 }
40
41 void
42 MatchList_initRefserverList(struct MatchList **lst, size_t *cnt,
43                             char const *dir)
44 {
45   int                   cur_dir = Eopen(".", O_RDONLY, 0);
46   struct dirent         **entries;
47   int                   count,i;
48   
49   Echdir(dir);
50   count = scandir(".", &entries, selectRefserver, alphasort);
51   if (count==-1) {
52     perror("scandir()");
53     exit(1);
54   }
55
56   if (count==0) {
57     WRITE_MSG(2, "no reference vserver configured\n");
58     exit(1);
59   }
60
61   *lst = Emalloc(sizeof(struct MatchList) * count);
62   *cnt = count;
63   for (i=0; i<count; ++i) {
64     char const                  *tmp   = entries[i]->d_name;
65     size_t                      l      = strlen(tmp);
66     char                        vname[sizeof("./") + l];
67     struct MatchVserverInfo     vserver = {
68       .name        = vname,
69       .use_pkgmgmt = true
70     };
71
72     memcpy(vname,   "./", 2);
73     memcpy(vname+2, tmp,  l+1);
74     
75     if (!MatchVserverInfo_init(&vserver)) {
76       WRITE_MSG(2, "failed to initialize unification of reference vserver\n");
77       exit(1);
78     }
79
80     if (!MatchList_initByVserver((*lst)+i, &vserver)) {
81       WRITE_MSG(2, "unification for reference vserver not configured\n");
82       exit(1);
83     }
84
85     free(entries[i]);
86     MatchVserverInfo_free(&vserver);
87   }
88   free(entries);
89
90   Efchdir(cur_dir);
91   Eclose(cur_dir);
92 }