3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
18 #include "wrappers-io.h"
19 #include "pathconfig.h"
22 readExcludeListFD(int fd,
23 char ***files, size_t *size,
30 if (fd==-1) return; // todo: message on verbose?
32 len = Elseek(fd, 0, SEEK_END);
33 Elseek(fd, 0, SEEK_SET);
35 *buf = Emalloc(sizeof(*buf) * (len+1));
36 EreadAll(fd, *buf, len);
40 while ((ptr=strchr(ptr, '\n'))) {
46 *files = Emalloc(sizeof(**files) * lines);
51 char *end_ptr = strchr(ptr, '\n');
54 if (end_ptr==0) break;
60 } while (tmp>ptr && *tmp==' ');
62 if (tmp>ptr) (*files)[(*size)++] = ptr;
70 readExcludeList(char const *filename,
71 char ***files, size_t *size,
74 int fd = open(filename, O_RDONLY);
75 if (fd==-1) return; // todo: message on verbose?
77 readExcludeListFD(fd, files, size, buf);
81 getConfigfileList(char const *vserver,
82 char ***files, size_t *size,
85 char tmpname[] = "/tmp/vunify.XXXXXX";
87 int fd = Emkstemp(tmpname);
94 char const **ptr = (char const **)(args)+0;
98 if (fd!=1) Eclose(fd);
102 *ptr++ = "get-conffiles";
105 Eexecv(args[0], args);
110 if (TEMP_FAILURE_RETRY(wait4(pid, &status, 0,0))==-1) {
115 if (!WIFEXITED(status) || WEXITSTATUS(status)!=0) {
116 WRITE_MSG(2, "failed to determine configfiles\n");
120 readExcludeListFD(fd, files, size, buf);
126 initMatchList(struct MatchList *list, char const *vserver,
127 char const *vdir, char const *exclude_file)
129 char *buf[2] = { 0,0 };
131 char **fixed_files = 0;
132 size_t fixed_count = 0;
134 char **expr_files = 0;
135 size_t expr_count = 0;
137 if (vserver && global_args->do_renew) {
138 getConfigfileList(vserver, &fixed_files, &fixed_count, buf+0);
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,
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];
155 MatchList_appendFiles(list, 0, fixed_files, fixed_count, false);
156 MatchList_appendFiles(list, fixed_count, expr_files, expr_count, true);
163 initMatchListByVserver(struct MatchList *list, char const *vserver,
164 char const **res_appdir)
170 style = vc_getVserverCfgStyle(vserver);
171 vdir = vc_getVserverVdir( vserver, style, true);
172 appdir = vc_getVserverAppDir(vserver, style, "vunify");
174 if (vdir==0 || appdir==0) {
175 free((char *)appdir);
181 size_t l1 = strlen(appdir);
182 char tmp[l1 + sizeof("/exclude")];
183 char const * excl_list;
185 memcpy(tmp, appdir, l1);
186 memcpy(tmp+l1, "/exclude", 9);
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";
192 // 'vdir' is transferred to matchlist and must not be free'ed here
193 initMatchList(list, vserver, vdir, excl_list);
197 *res_appdir = appdir;
199 free((char *)appdir);
206 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
211 WRITE_MSG(2, "Odd number of (path,excludelist) arguments\n");
216 WRITE_MSG(2, "No reference path(s) given\n");
220 initMatchList(&global_info.dst_list, 0, argv[0], argv[1]);
223 global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
224 global_info.src_lists.l = count;
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]);
231 selectRefserver(struct dirent const *ent)
233 return strncmp(ent->d_name, "refserver.", 10)==0;
237 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
240 int cur_dir = Eopen(".", O_RDONLY, 0);
241 struct dirent **entries;
245 WRITE_MSG(2, "More than one vserver is not supported\n");
249 if (!initMatchListByVserver(&global_info.dst_list, argv[0], &appdir)) {
250 WRITE_MSG(2, "unification not configured for this vserver\n");
255 count = scandir(".", &entries, selectRefserver, alphasort);
262 WRITE_MSG(2, "no reference vserver configured\n");
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];
273 memcpy(vname, "./", 2);
274 memcpy(vname+2, tmp, l+1);
276 if (!initMatchListByVserver(global_info.src_lists.v+i, vname, 0)) {
277 WRITE_MSG(2, "unification for reference vserver not configured\n");
284 free(const_cast(char *)(appdir));