d874274f1cefc0dcd45f09193a727ecd81ea13bd
[util-vserver.git] / lib_internal / matchvserverinfo-init.c
1 // $Id: matchvserverinfo-init.c,v 1.2 2005/03/24 00:09:58 ensc Exp $    --*- c -*--
2
3 // Copyright (C) 2005 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.h"
25
26 #include <pathconfig.h>
27
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <assert.h>
32 #include <sys/param.h>
33
34 static bool
35 exists(char *path, size_t len, char const *name)
36 {
37   if (name) strcpy(path + len, name);
38
39   return access(path, R_OK)!=-1;
40 }
41
42 #define EXISTS(X)               exists(X, sizeof(X)-1, 0)
43 #define DEFAULT_EXISTS(X)       EXISTS(CONFDIR "/.defaults/apps/vunify/" X)
44
45 bool
46 MatchVserverInfo_init(struct MatchVserverInfo *info)
47 {
48   assert(info->name!=0);
49   assert(info->vdir.d==0 && info->appdir.d==0);
50   
51   info->style    = vc_getVserverCfgStyle(info->name);
52   info->vdir.d   = vc_getVserverVdir    (info->name, info->style, true);
53   info->appdir.d = vc_getVserverAppDir  (info->name, info->style, "vunify");
54
55   if (info->vdir.d==0 || info->appdir.d==0) {
56     free(const_cast(char *)(info->vdir.d));
57     free(const_cast(char *)(info->appdir.d));
58
59     return false;
60   }
61
62   info->vdir.l   = strlen(info->vdir.d);
63   info->appdir.l = strlen(info->appdir.d);
64
65   size_t const  l = info->appdir.l;
66   char          tmp[l + MAX(sizeof("/pkgmgmt-ignore"),sizeof("/pkgmgmt-force"))];
67
68   memcpy(tmp, info->appdir.d, l);
69
70   if      (exists(tmp, l, "/pkgmgmt-ignore")) info->use_pkgmgmt = false;
71   else if (exists(tmp, l, "/pkgmgmt-force"))  info->use_pkgmgmt = true;
72   else if (DEFAULT_EXISTS ("pkgmgmt-ignore")) info->use_pkgmgmt = false;
73   else if (DEFAULT_EXISTS ("pkgmgmt-force"))  info->use_pkgmgmt = true;
74
75   return true;
76 }