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);
82 getConfigfileList(char const *vserver,
83 char ***files, size_t *size,
86 char tmpname[] = "/tmp/vunify.XXXXXX";
88 int fd = Emkstemp(tmpname);
95 char const **ptr = (char const **)(args)+0;
99 if (fd!=1) Eclose(fd);
103 *ptr++ = "get-conffiles";
106 Eexecv(args[0], args);
111 if (TEMP_FAILURE_RETRY(wait4(pid, &status, 0,0))==-1) {
116 if (!WIFEXITED(status) || WEXITSTATUS(status)!=0) {
117 WRITE_MSG(2, "failed to determine configfiles\n");
121 readExcludeListFD(fd, files, size, buf);
127 initMatchList(struct MatchList *list, char const *vserver,
128 char const *vdir, char const *exclude_file)
130 char *buf[2] = { 0,0 };
132 char **fixed_files = 0;
133 size_t fixed_count = 0;
135 char **expr_files = 0;
136 size_t expr_count = 0;
138 if (global_args->verbosity>3) {
139 WRITE_MSG(1, "Initializing exclude-list for ");
143 WRITE_STR(1, vserver);
148 if (vserver && global_args->do_renew) {
149 if (global_args->verbosity>4)
150 WRITE_MSG(1, " Fetching configuration-file list from packagemanagement\n");
151 getConfigfileList(vserver, &fixed_files, &fixed_count, buf+0);
154 // abuse special values (NULL, empty string) to skip the next step
155 if (exclude_file && *exclude_file) {
156 if (global_args->verbosity>4) WRITE_MSG(1, " Reading exclude file\n");
157 readExcludeList(exclude_file,
158 &expr_files, &expr_count,
162 MatchList_init(list, vdir, fixed_count + expr_count);
163 list->buf = Emalloc(sizeof(void *) * 3);
164 list->buf[0] = buf[0];
165 list->buf[1] = buf[1];
169 MatchList_appendFiles(list, 0, fixed_files, fixed_count, false);
170 MatchList_appendFiles(list, fixed_count, expr_files, expr_count, true);
177 initMatchListByVserver(struct MatchList *list, char const *vserver,
178 char const **res_appdir)
184 style = vc_getVserverCfgStyle(vserver);
185 vdir = vc_getVserverVdir( vserver, style, true);
186 appdir = vc_getVserverAppDir(vserver, style, "vunify");
188 if (vdir==0 || appdir==0) {
189 free((char *)appdir);
195 size_t l1 = strlen(appdir);
196 char tmp[l1 + sizeof("/exclude")];
197 char const * excl_list;
199 memcpy(tmp, appdir, l1);
200 memcpy(tmp+l1, "/exclude", 9);
203 if (access(excl_list, R_OK)==-1) excl_list = CONFDIR "/.defaults/apps/vunify/exclude";
204 if (access(excl_list, R_OK)==-1) excl_list = PKGLIBDIR "/defaults/vunify-exclude";
206 // 'vdir' is transferred to matchlist and must not be free'ed here
207 initMatchList(list, vserver, vdir, excl_list);
211 *res_appdir = appdir;
213 free((char *)appdir);
220 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
225 WRITE_MSG(2, "Odd number of (path,excludelist) arguments\n");
230 WRITE_MSG(2, "No reference path(s) given\n");
234 initMatchList(&global_info.dst_list, 0, argv[0], argv[1]);
237 global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
238 global_info.src_lists.l = count;
240 for (i=0; i<count; ++i)
241 initMatchList(global_info.src_lists.v+i, 0,
242 strdup(argv[2 + i*2]), argv[3 + i*2]);
246 selectRefserver(struct dirent const *ent)
248 return strncmp(ent->d_name, "refserver.", 10)==0;
252 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
255 int cur_dir = Eopen(".", O_RDONLY, 0);
256 struct dirent **entries;
260 WRITE_MSG(2, "More than one vserver is not supported\n");
264 if (!initMatchListByVserver(&global_info.dst_list, argv[0], &appdir)) {
265 WRITE_MSG(2, "unification not configured for this vserver\n");
270 count = scandir(".", &entries, selectRefserver, alphasort);
277 WRITE_MSG(2, "no reference vserver configured\n");
281 global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
282 global_info.src_lists.l = count;
283 for (i=0; i<count; ++i) {
284 char const *tmp = entries[i]->d_name;
285 size_t l = strlen(tmp);
286 char vname[sizeof("./") + l];
288 memcpy(vname, "./", 2);
289 memcpy(vname+2, tmp, l+1);
291 if (!initMatchListByVserver(global_info.src_lists.v+i, vname, 0)) {
292 WRITE_MSG(2, "unification for reference vserver not configured\n");
299 free(const_cast(char *)(appdir));