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 "pathconfig.h"
21 readExcludeListFD(int fd,
22 char ***files, size_t *size,
29 if (fd==-1) return; // todo: message on verbose?
31 len = Elseek(fd, 0, SEEK_END);
32 Elseek(fd, 0, SEEK_SET);
34 *buf = Emalloc(sizeof(*buf) * (len+1));
35 EreadAll(fd, *buf, len);
39 while ((ptr=strchr(ptr, '\n'))) {
45 *files = Emalloc(sizeof(**files) * lines);
50 char *end_ptr = strchr(ptr, '\n');
53 if (end_ptr==0) break;
59 } while (tmp>ptr && *tmp==' ');
61 if (tmp>ptr) (*files)[(*size)++] = ptr;
69 readExcludeList(char const *filename,
70 char ***files, size_t *size,
73 int fd = open(filename, O_RDONLY);
74 if (fd==-1) return; // todo: message on verbose?
76 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 (global_args->verbosity>3) {
138 WRITE_MSG(1, "Initializing exclude-list for ");
142 WRITE_STR(1, vserver);
147 if (vserver && global_args->do_renew) {
148 if (global_args->verbosity>4)
149 WRITE_MSG(1, " Fetching configuration-file list from packagemanagement\n");
150 getConfigfileList(vserver, &fixed_files, &fixed_count, buf+0);
153 // abuse special values (NULL, empty string) to skip the next step
154 if (exclude_file && *exclude_file) {
155 if (global_args->verbosity>4) WRITE_MSG(1, " Reading exclude file\n");
156 readExcludeList(exclude_file,
157 &expr_files, &expr_count,
161 MatchList_init(list, vdir, fixed_count + expr_count);
162 list->buf = Emalloc(sizeof(void *) * 3);
163 list->buf[0] = buf[0];
164 list->buf[1] = buf[1];
168 MatchList_appendFiles(list, 0, fixed_files, fixed_count, false);
169 MatchList_appendFiles(list, fixed_count, expr_files, expr_count, true);
176 initMatchListByVserver(struct MatchList *list, char const *vserver,
177 char const **res_appdir)
183 style = vc_getVserverCfgStyle(vserver);
184 vdir = vc_getVserverVdir( vserver, style, true);
185 appdir = vc_getVserverAppDir(vserver, style, "vunify");
187 if (vdir==0 || appdir==0) {
188 free((char *)appdir);
194 size_t l1 = strlen(appdir);
195 char tmp[l1 + sizeof("/exclude")];
196 char const * excl_list;
198 memcpy(tmp, appdir, l1);
199 memcpy(tmp+l1, "/exclude", 9);
202 if (access(excl_list, R_OK)==-1) excl_list = CONFDIR "/.defaults/apps/vunify/exclude";
203 if (access(excl_list, R_OK)==-1) excl_list = PKGLIBDIR "/defaults/vunify-exclude";
205 // 'vdir' is transferred to matchlist and must not be free'ed here
206 initMatchList(list, vserver, vdir, excl_list);
210 *res_appdir = appdir;
212 free((char *)appdir);
219 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
224 WRITE_MSG(2, "Odd number of (path,excludelist) arguments\n");
229 WRITE_MSG(2, "No reference path(s) given\n");
233 initMatchList(&global_info.dst_list, 0, argv[0], argv[1]);
236 global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
237 global_info.src_lists.l = count;
239 for (i=0; i<count; ++i)
240 initMatchList(global_info.src_lists.v+i, 0,
241 strdup(argv[2 + i*2]), argv[3 + i*2]);
245 selectRefserver(struct dirent const *ent)
247 return strncmp(ent->d_name, "refserver.", 10)==0;
251 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
254 int cur_dir = Eopen(".", O_RDONLY, 0);
255 struct dirent **entries;
259 WRITE_MSG(2, "More than one vserver is not supported\n");
263 if (!initMatchListByVserver(&global_info.dst_list, argv[0], &appdir)) {
264 WRITE_MSG(2, "unification not configured for this vserver\n");
269 count = scandir(".", &entries, selectRefserver, alphasort);
276 WRITE_MSG(2, "no reference vserver configured\n");
280 global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
281 global_info.src_lists.l = count;
282 for (i=0; i<count; ++i) {
283 char const *tmp = entries[i]->d_name;
284 size_t l = strlen(tmp);
285 char vname[sizeof("./") + l];
287 memcpy(vname, "./", 2);
288 memcpy(vname+2, tmp, l+1);
290 if (!initMatchListByVserver(global_info.src_lists.v+i, vname, 0)) {
291 WRITE_MSG(2, "unification for reference vserver not configured\n");
298 free(const_cast(char *)(appdir));