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 initHashList(HashDirCollection *hash_vec, char const *hashdir)
26 int cur_dir = Eopen(".", O_RDONLY|O_DIRECTORY, 0);
29 DIR *d = Eopendir(".");
31 size_t l = strlen(hashdir);
34 while ((ep=readdir(d)) != 0) {
37 if (isDotfile(ep->d_name) ||
38 stat(ep->d_name, &st)==-1 || !S_ISDIR(st.st_mode))
41 if (HashDirInfo_findDevice(hash_vec, st.st_dev)!=0) {
42 WRITE_MSG(2, "Duplicate hash-dir entry '");
43 WRITE_STR(2, ep->d_name);
44 WRITE_MSG(2, "' found\n");
48 char *full_path = Emalloc(l + strlen(ep->d_name) + 3);
49 char *ptr = full_path + l;
51 memcpy(full_path, hashdir, l);
52 while (ptr>full_path && ptr[-1]=='/') --ptr;
54 strcpy(ptr, ep->d_name);
55 strcat(ptr, "/"); // append a trailing '/'
58 struct HashDirInfo tmp = {
60 .path = { full_path, strlen(full_path) },
63 res = MAX(res, tmp.path.l);
65 memcpy(Vector_pushback(hash_vec), &tmp, sizeof tmp);
68 if (Vector_count(hash_vec)==0) {
69 WRITE_MSG(2, "Could not find a place for the hashified files at '");
70 WRITE_STR(2, hashdir);
72 exit(wrapper_exit_code);
83 searchHashdir(char const *lhs, char const *rhs)
85 size_t l1 = strlen(lhs);
86 size_t l2 = rhs ? strlen(rhs) : 0;
87 char * res = Emalloc(l1 + l2);
91 if (rhs) strcat(res, rhs);
93 if (stat(res, &st)==-1 || !S_ISDIR(st.st_mode)) {
102 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
104 assert(args->hash_dir!=0);
107 WRITE_MSG(2, "No exclude list specified\n");
111 global_info.hash_dirs_max_size = initHashList(&global_info.hash_dirs,
113 MatchList_initManually(&global_info.dst_list, 0, strdup(argv[0]), argv[1]);
117 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
120 char const *hashdir = args->hash_dir;
121 struct MatchVserverInfo const dst_vserver = { argv[0], true };
124 WRITE_MSG(2, "More than one vserver is not supported\n");
128 if (!MatchList_initByVserver(&global_info.dst_list, &dst_vserver, &appdir)) {
129 WRITE_MSG(2, "unification not configured for this vserver\n");
133 if (hashdir==0) hashdir = searchHashdir(appdir, "/hash");
134 if (hashdir==0) hashdir = searchHashdir(CONFDIR "/.defaults/apps/vunify/hash", 0);
137 WRITE_MSG(2, "no hash-directory configured for this vserver.\n");
141 global_info.hash_dirs_max_size = initHashList(&global_info.hash_dirs, hashdir);
143 free(const_cast(char *)(hashdir));
144 free(const_cast(char *)(appdir));