1 // $Id: vhashify-init.hc 1967 2005-03-23 02:10:23Z ensc $ --*- c -*--
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 initHashMethod(struct HashDirConfiguration *conf, char const *filename)
97 int fd = open(filename, O_RDONLY);
98 if (fd==-1 && conf->method==0)
99 conf->method = hashFunctionDefault();
102 assert(conf->method!=0);
103 if (conf->method==0) return false;
104 if (global_args->dry_run) return true; // do not create the file
106 fd = Eopen(filename, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, 0644);
107 TEMP_FAILURE_RETRY(write(fd, conf->method->name, strlen(conf->method->name)));
108 TEMP_FAILURE_RETRY(write(fd, "\n", 1));
111 off_t s = Elseek(fd, 0, SEEK_END);
113 Elseek(fd, 0, SEEK_SET);
117 if (s>0 && read(fd, buf, s+1)==s) {
118 while (s>0 && (buf[s-1]=='\0' || buf[s-1]=='\n'))
122 conf->method = hashFunctionFind(buf);
123 if (conf->method==0) {
124 WRITE_MSG(2, "Can not find hash-function '");
130 WRITE_MSG(2, "Can not read configuration file for hash-method\n");
133 if (conf->method!=0 && conf->method->digestsize*8>HASH_MAXBITS) {
134 WRITE_MSG(2, "Wow... what an huge hash-function. I can not handle so much bits; giving up...\n");
139 return conf->method!=0;
143 initHashBlocks(struct HashDirConfiguration *conf, char const *filename)
145 int fd = open(filename, O_RDONLY);
148 char str[sizeof("all,start,middle,end,")] = { [0] = '\0' };
150 if (global_args->dry_run) return true; // do not create the file
152 fd = Eopen(filename, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, 0644);
154 if (conf->blocks== hshALL) strcat(str, "all\n");
155 if (conf->blocks & hshSTART) strcat(str, "start\n");
156 if (conf->blocks & hshMIDDLE) strcat(str, "middle\n");
157 if (conf->blocks & hshEND) strcat(str, "end\n");
159 EwriteAll(fd, str, strlen(str));
162 off_t s = Elseek(fd, 0, SEEK_END);
164 Elseek(fd, 0, SEEK_SET);
166 conf->blocks = hshINVALID;
168 if (s>0 && read(fd, buf, s+1)==s) {
170 char *sep = "\n,\t ";
173 conf->blocks = hshALL;
176 char *ptr = strsep(&tok, sep);
178 if (*ptr=='#') { sep = "\n"; continue; }
180 if (*ptr=='\0') continue;
182 if (strcasecmp(ptr, "all") ==0) conf->blocks = hshALL;
184 if (conf->blocks==hshINVALID) conf->blocks = 0;
186 else if (strcasecmp(ptr, "start") ==0) conf->blocks |= hshSTART;
187 else if (strcasecmp(ptr, "middle")==0) conf->blocks |= hshMIDDLE;
188 else if (strcasecmp(ptr, "end") ==0) conf->blocks |= hshEND;
190 WRITE_MSG(2, "Invalid block descriptor '");
193 conf->blocks = hshINVALID;
200 WRITE_MSG(2, "Can not read configuration file for hash-blocks\n");
204 return conf->blocks!=hshINVALID;
208 initHashBlockSize(struct HashDirConfiguration *conf, char const *filename)
210 if (conf->blocks==hshALL) return true;
212 int fd = open(filename, O_RDONLY);
214 char str[sizeof("0x") + sizeof(size_t)*3+2] = {
217 size_t len = utilvserver_fmt_xuint(str+2, conf->blocksize);
219 if (global_args->dry_run) return true; // do not create the file
221 fd = Eopen(filename, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, 0644);
222 EwriteAll(fd, str, len+2);
225 off_t s = Elseek(fd, 0, SEEK_END);
227 Elseek(fd, 0, SEEK_SET);
229 conf->blocksize = (size_t)(-1);
231 if (s>0 && read(fd, buf, s+1)==s) {
234 while (s>0 && (buf[s-1]=='\0' || buf[s-1]=='\n'))
238 conf->blocksize = strtol(buf, &errptr, 0);
239 if (errptr==buf || (*errptr!='\0' && *errptr!='\n')) {
240 WRITE_MSG(2, "Failed to parse blocksize '");
243 conf->blocksize = (size_t)(-1);
247 WRITE_MSG(2, "Can not read configuration file for hash-blocksize\n");
251 return conf->blocksize!=(size_t)(-1);
255 initHashConf(struct HashDirConfiguration *conf, char const *hashdir)
257 size_t l = strlen(hashdir);
258 char tmp[l + MAX(MAX(sizeof("/method"), sizeof("/blocks")),
259 sizeof("/blocksize"))];
261 memcpy(tmp, hashdir, l);
263 return ((strcpy(tmp+l, "/method"), initHashMethod (conf, tmp)) &&
264 (strcpy(tmp+l, "/blocks"), initHashBlocks (conf, tmp)) &&
265 (strcpy(tmp+l, "/blocksize"), initHashBlockSize(conf, tmp)));
269 searchHashdir(char const *lhs, char const *rhs)
271 size_t l1 = strlen(lhs);
272 size_t l2 = rhs ? strlen(rhs) : 0;
273 char * res = Emalloc(l1 + l2 + 1);
277 if (rhs) strcat(res, rhs);
279 if (stat(res, &st)==-1 || !S_ISDIR(st.st_mode)) {
288 initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
290 assert(args->hash_dir!=0);
293 WRITE_MSG(2, "No exclude list specified\n");
297 if (!initHashConf(&global_info.hash_conf, args->hash_dir)) {
298 WRITE_MSG(2, "failed to initialize hash-configuration\n");
302 global_info.hash_dirs_max_size = initHashList(&global_info.hash_dirs,
304 MatchList_initManually(&global_info.dst_list, 0, strdup(argv[0]), argv[1]);
308 initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
310 char const *hashdir = args->hash_dir;
311 struct MatchVserverInfo vserver = {
316 if (!MatchVserverInfo_init(&vserver)) {
317 WRITE_MSG(2, "Failed to initialize unification for vserver\n");
322 WRITE_MSG(2, "More than one vserver is not supported\n");
326 if (!MatchList_initByVserver(&global_info.dst_list, &vserver)) {
327 WRITE_MSG(2, "unification not configured for this vserver\n");
331 if (hashdir==0) hashdir = searchHashdir(vserver.appdir.d, "/hash");
332 if (hashdir==0) hashdir = searchHashdir(CONFDIR "/.defaults/apps/vunify/hash", 0);
335 WRITE_MSG(2, "no hash-directory configured for this vserver.\n");
339 if (!initHashConf(&global_info.hash_conf, hashdir)) {
340 WRITE_MSG(2, "failed to initialize hash-configuration\n");
344 global_info.hash_dirs_max_size = initHashList(&global_info.hash_dirs, hashdir);
346 free(const_cast(char *)(hashdir));
347 MatchVserverInfo_free(&vserver);