3 // Copyright (C) 2005 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"
19 #include "lib_internal/util-dotfile.h"
21 #include <sys/param.h>
24 freeHashList(HashDirCollection *hash_vec)
26 for (struct HashDirInfo *itm = Vector_begin(hash_vec);
27 itm!=Vector_end(hash_vec);
29 free(const_cast(char *)(itm->path.d));
32 Vector_free(hash_vec);
36 initHashList(HashDirCollection *hash_vec, char const *hashdir)
38 int cur_dir = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
41 DIR *d = Eopendir(".");
43 size_t l = strlen(hashdir);
46 while ((ep=readdir(d)) != 0) {
49 if (isDotfile(ep->d_name) ||
50 stat(ep->d_name, &st)==-1 || !S_ISDIR(st.st_mode))
53 if (HashDirInfo_findDevice(hash_vec, st.st_dev)!=0) {
54 WRITE_MSG(2, "Duplicate hash-dir entry '");
55 WRITE_STR(2, ep->d_name);
56 WRITE_MSG(2, "' found\n");
60 char *full_path = Emalloc(l + strlen(ep->d_name) + 3);
61 char *ptr = full_path + l;
63 memcpy(full_path, hashdir, l);
64 while (ptr>full_path && ptr[-1]=='/') --ptr;
66 strcpy(ptr, ep->d_name);
67 strcat(ptr, "/"); // append a trailing '/'
70 struct HashDirInfo tmp = {
72 .path = { full_path, strlen(full_path) },
75 res = MAX(res, tmp.path.l);
77 memcpy(Vector_pushback(hash_vec), &tmp, sizeof tmp);
80 if (Vector_count(hash_vec)==0) {
81 WRITE_MSG(2, "Could not find a place for the hashified files at '");
82 WRITE_STR(2, hashdir);
84 exit(wrapper_exit_code);
95 searchHashdir(char const *lhs, char const *rhs)
97 size_t l1 = strlen(lhs);
98 size_t l2 = rhs ? strlen(rhs) : 0;
99 char * res = Emalloc(l1 + l2 + 1);
103 if (rhs) strcat(res, rhs);
105 if (stat(res, &st)==-1 || !S_ISDIR(st.st_mode)) {
114 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
116 assert(args->hash_dir!=0);
119 WRITE_MSG(2, "No exclude list specified\n");
123 global_info.hash_dirs_max_size = initHashList(&global_info.hash_dirs,
125 MatchList_initManually(&global_info.dst_list, 0, strdup(argv[0]), argv[1]);
129 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
131 char const *hashdir = args->hash_dir;
132 struct MatchVserverInfo vserver = {
137 if (!MatchVserverInfo_init(&vserver)) {
138 WRITE_MSG(2, "Failed to initialize unification for vserver\n");
143 WRITE_MSG(2, "More than one vserver is not supported\n");
147 if (!MatchList_initByVserver(&global_info.dst_list, &vserver)) {
148 WRITE_MSG(2, "unification not configured for this vserver\n");
152 if (hashdir==0) hashdir = searchHashdir(vserver.appdir.d, "/hash");
153 if (hashdir==0) hashdir = searchHashdir(CONFDIR "/.defaults/apps/vunify/hash", 0);
156 WRITE_MSG(2, "no hash-directory configured for this vserver.\n");
160 global_info.hash_dirs_max_size = initHashList(&global_info.hash_dirs, hashdir);
162 free(const_cast(char *)(hashdir));
163 MatchVserverInfo_free(&vserver);