compareUnify(): check mtime too
[util-vserver.git] / util-vserver / src / vunify-init.ic
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 #include "wrappers-io.h"
19 #include "pathconfig.h"
20
21 static void
22 readExcludeListFD(int fd,
23                   char ***files,  size_t *size,
24                   char **buf)
25 {
26   off_t         len;
27   size_t        lines = 0;
28   char          *ptr;
29   
30   if (fd==-1) return; // todo: message on verbose?
31
32   len = Elseek(fd, 0, SEEK_END);
33   Elseek(fd, 0, SEEK_SET);
34
35   *buf = Emalloc(sizeof(*buf) * (len+1));
36   EreadAll(fd, *buf, len);
37   (*buf)[len] = '\0';
38
39   ptr = *buf;
40   while ((ptr=strchr(ptr, '\n'))) {
41     ++lines;
42     ++ptr;
43   }
44
45   ++lines;
46   *files = Emalloc(sizeof(**files) * lines);
47
48   *size = 0;
49   ptr   = *buf;
50   while (*ptr) {
51     char        *end_ptr = strchr(ptr, '\n');
52
53     assert(*size<lines);
54     if (end_ptr==0) break;
55
56     if (*ptr!='#') {
57       char      *tmp = end_ptr;
58       do {
59         *tmp-- = '\0';
60       } while (tmp>ptr && *tmp==' ');
61       
62       if (tmp>ptr) (*files)[(*size)++] = ptr;
63     }
64
65     ptr = end_ptr+1;
66   }
67 }
68
69 static void
70 readExcludeList(char const *filename,
71                 char ***files,  size_t *size,
72                 char **buf)
73 {
74   int           fd = open(filename, O_RDONLY);
75   if (fd==-1) return; // todo: message on verbose?
76
77   readExcludeListFD(fd, files, size, buf);
78 }
79
80 static void
81 getConfigfileList(char const *vserver,
82                   char ***files, size_t *size,
83                   char **buf)
84 {
85   char          tmpname[] = "/tmp/vunify.XXXXXX";
86   pid_t         pid;
87   int           fd = Emkstemp(tmpname);
88
89   Eunlink(tmpname);
90   pid = Efork();
91
92   if (pid==0) {
93     char        *args[10];
94     char const  **ptr = (char const **)(args)+0;
95     
96     Edup2(fd, 1);
97     //Eclose(0);
98     if (fd!=1) Eclose(fd);
99
100     *ptr++  = VPKG_PROG;
101     *ptr++  = vserver;
102     *ptr++  = "get-conffiles";
103     *ptr    = 0;
104
105     Eexecv(args[0], args);
106   }
107   else {
108     int         status;
109     
110     if (TEMP_FAILURE_RETRY(wait4(pid, &status, 0,0))==-1) {
111       perror("wait4()");
112       exit(1);
113     }
114
115     if (!WIFEXITED(status) || WEXITSTATUS(status)!=0) {
116       WRITE_MSG(2, "failed to determine configfiles\n");
117       exit(1);
118     }
119
120     readExcludeListFD(fd, files, size, buf);
121     Eclose(fd);
122   }
123 }
124
125 static void
126 initMatchList(struct MatchList *list, char const *vserver,
127               char const *vdir, char const *exclude_file)
128 {
129   char                  *buf[2] = { 0,0 };
130   
131   char                  **fixed_files = 0;
132   size_t                fixed_count   = 0;
133
134   char                  **expr_files   = 0;
135   size_t                expr_count     = 0;
136   
137   if (vserver && global_args->do_renew) {
138     getConfigfileList(vserver, &fixed_files, &fixed_count, buf+0);
139   }
140
141   // abuse special values (NULL, empty string) to skip the next step
142   if (exclude_file && *exclude_file) {
143     readExcludeList(exclude_file,
144                     &expr_files,  &expr_count,
145                     buf+1);
146   }
147
148   MatchList_init(list, vdir, fixed_count + expr_count);
149   list->buf       = Emalloc(sizeof(void *) * 3);
150   list->buf[0]    = buf[0];
151   list->buf[1]    = buf[1];
152   list->buf[2]    = vdir;
153   list->buf_count = 3;
154
155   MatchList_appendFiles(list, 0,           fixed_files, fixed_count, false);
156   MatchList_appendFiles(list, fixed_count, expr_files,  expr_count,  true);
157
158   free(expr_files);
159   free(fixed_files);
160 }
161
162 static bool
163 initMatchListByVserver(struct MatchList *list, char const *vserver,
164               char const **res_appdir)
165 {
166   vcCfgStyle    style;
167   char const    *vdir;
168   char const    *appdir;
169
170   style  = vc_getVserverCfgStyle(vserver);
171   vdir   = vc_getVserverVdir(  vserver, style, true);
172   appdir = vc_getVserverAppDir(vserver, style, "vunify");
173
174   if (vdir==0 || appdir==0) {
175     free((char *)appdir);
176     free((char *)vdir);
177     return false;
178   }
179
180   {
181     size_t              l1 = strlen(appdir);
182     char                tmp[l1 + sizeof("/exclude")];
183     char const *        excl_list;
184
185     memcpy(tmp,    appdir, l1);
186     memcpy(tmp+l1, "/exclude", 9);
187
188     excl_list = tmp;
189     if (access(excl_list, R_OK)==-1) excl_list = CONFDIR   "/.defaults/apps/vunify/exclude";
190     if (access(excl_list, R_OK)==-1) excl_list = PKGLIBDIR "/defaults/vunify-exclude";
191
192       // 'vdir' is transferred to matchlist and must not be free'ed here
193     initMatchList(list, vserver, vdir, excl_list);
194   }
195
196   if (res_appdir!=0)
197     *res_appdir = appdir;
198   else
199     free((char *)appdir);
200   
201   return true;
202 }
203
204
205 static void
206 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
207 {
208   int           i, count=argc/2;
209   
210   if (argc%2) {
211     WRITE_MSG(2, "Odd number of (path,excludelist) arguments\n");
212     exit(1);
213   }
214
215   if (count<2) {
216     WRITE_MSG(2, "No reference path(s) given\n");
217     exit(1);
218   }
219
220   initMatchList(&global_info.dst_list, 0, argv[0], argv[1]);
221
222   --count;
223   global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
224   global_info.src_lists.l = count;
225
226   for (i=0; i<count; ++i)
227     initMatchList(global_info.src_lists.v+i, 0, strdup(argv[2 + i*2]), argv[3 + i*2]);
228 }
229
230 static int
231 selectRefserver(struct dirent const *ent)
232 {
233   return strncmp(ent->d_name, "refserver.", 10)==0;
234 }
235
236 static void
237 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
238 {
239   char const    *appdir;
240   int           cur_dir = Eopen(".", O_RDONLY, 0);
241   struct dirent **entries;
242   int           count, i;
243   
244   if (argc!=1) {
245     WRITE_MSG(2, "More than one vserver is not supported\n");
246     exit(1);
247   }
248
249   if (!initMatchListByVserver(&global_info.dst_list, argv[0], &appdir)) {
250     WRITE_MSG(2, "unification not configured for this vserver\n");
251     exit(1);
252   }
253
254   Echdir(appdir);
255   count = scandir(".", &entries, selectRefserver, alphasort);
256   if (count==-1) {
257     perror("scandir()");
258     exit(1);
259   }
260
261   if (count==0) {
262     WRITE_MSG(2, "no reference vserver configured\n");
263     exit(1);
264   }
265
266   global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
267   global_info.src_lists.l = count;
268   for (i=0; i<count; ++i) {
269     char const          *tmp   = entries[i]->d_name;
270     size_t              l      = strlen(tmp);
271     char                vname[sizeof("./") + l];
272
273     memcpy(vname,   "./", 2);
274     memcpy(vname+2, tmp,  l+1);
275     
276     if (!initMatchListByVserver(global_info.src_lists.v+i, vname, 0)) {
277       WRITE_MSG(2, "unification for reference vserver not configured\n");
278       exit(1);
279     }
280
281     free(entries[i]);
282   }
283   free(entries);
284   free(const_cast(char *)(appdir));
285
286   Efchdir(cur_dir);
287   Eclose(cur_dir);
288 }