90abcee79030d7b9f548f8084ad0cecc2b604cd1
[util-vserver.git] / lib / createskeleton-full.hc
1 // $Id: createskeleton-full.hc,v 1.4 2005/02/01 00:55:51 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 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #define CONCAT_TWO_ARGS(BUF, LHS,RHS)                   \
25   size_t                BUF ## l1 = strlen(LHS);        \
26   size_t                BUF ## l2 = strlen(RHS);        \
27   char                  BUF[BUF##l1 + BUF##l2 + 2];     \
28                                                         \
29   memcpy(BUF,      LHS, BUF##l1 + 1);                   \
30   if (BUF##l2 > 0) {                                    \
31     BUF[ BUF##l1 ] = '/';                               \
32     memcpy(BUF+ BUF##l1 + 1, RHS, BUF##l2 + 1);         \
33   }
34
35 static inline int
36 mkdir2(char const *lhs, char const *rhs, int mode)
37 {
38   CONCAT_TWO_ARGS(buf, lhs, rhs);
39   return mkdir(buf, mode);
40 }
41
42 static inline int
43 setIAttr2(char const *lhs, char const *rhs, int flags)
44 {
45   CONCAT_TWO_ARGS(buf, lhs, rhs);
46
47   return vc_set_iattr(buf, 0, flags, VC_IMMUTABLE_ALL);
48 }
49
50 static inline int
51 symlink2(char const *old_lhs, char const *old_rhs,
52          char const *new_lhs, char const *new_rhs)
53 {
54   CONCAT_TWO_ARGS(old_buf, old_lhs, old_rhs);
55
56   {
57     CONCAT_TWO_ARGS(new_buf, new_lhs, new_rhs);
58     return symlink(old_buf, new_buf);
59   }
60 }
61
62 #undef CONCAT_TWO_ARGS
63
64 static inline int
65 vc_createSkeleton_full(char const *id, char const *name, int flags)
66 {
67   if (mkdir(id, 0755)==-1) return -1;
68
69   if (mkdir2(id, "apps", 0755)==-1 ||
70       ((flags&vcSKEL_INTERFACES) && mkdir2(id, "interfaces", 755)==-1) ||
71       ((flags&vcSKEL_PKGMGMT) && (
72         mkdir2(id, "apps/pkgmgmt", 0755)==-1)))
73     return -1;
74
75   for (;;) {
76     char const  *basedir = CONFDIR "/.defaults/run";
77
78     if (!utilvserver_isDirectory(basedir, true)) basedir = DEFAULT_PKGSTATEDIR;
79     if (!utilvserver_isDirectory(basedir, true)) break;
80
81     if (symlink2(basedir, name, id, "run")==-1)
82       return -1;
83
84     break;
85   }
86   
87   while (flags&vcSKEL_PKGMGMT) {
88     char const  *basedir = CONFDIR "/.defaults/apps/pkgmgmt/base";
89
90     if (!utilvserver_isDirectory(basedir, true)) basedir = DEFAULT_VSERVERPKGDIR;
91     if (!utilvserver_isDirectory(basedir, true)) break;
92
93     if (mkdir2(basedir, name, 0755)==-1 ||
94         symlink2(basedir, name, id, "apps/pkgmgmt/base")==-1)
95       return -1;
96
97     break;
98   }
99
100   while (flags&vcSKEL_FILESYSTEM) {
101     char const  *basedir = CONFDIR "/.defaults/vdirbase";
102
103     if (!utilvserver_isDirectory(basedir, true)) basedir = DEFAULT_VSERVERDIR;
104     if (!utilvserver_isDirectory(basedir, true)) break;
105
106     if (mkdir2(basedir, name, 0755)==-1 ||
107         setIAttr2(basedir, name, 0)==-1 ||
108         symlink2(basedir, name, id, "vdir")==-1)
109       return -1;
110
111     break;
112   }
113
114   return 0;
115 }