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.
23 #define UTIL_VSERVER_UNIFY_MTIME_OPTIONAL
28 #include "lib/internal.h"
29 #include "lib_internal/matchlist.h"
30 #include "lib_internal/unify.h"
31 #include "ensc_vector/vector.h"
33 #include "lib_internal/crypto-wrapper.h"
49 #define ENSC_WRAPPERS_STDLIB 1
50 #define ENSC_WRAPPERS_UNISTD 1
51 #define ENSC_WRAPPERS_FCNTL 1
52 #define ENSC_WRAPPERS_DIRENT 1
53 #define ENSC_WRAPPERS_IO 1
57 #define HASH_BLOCKSIZE 0x10000000u
58 #define HASH_MINSIZE 0x10
59 #define HASH_MAXBITS 256 // we have to take care about
60 // max filename-length...
63 # error HASH_MINSIZE must be not '0'
67 #define CMD_HELP 0x8000
68 #define CMD_VERSION 0x8001
70 #define CMD_DESTINATION 0x1000
71 #define CMD_INSECURE 0x1001
72 #define CMD_SLEDGE 0x1002
73 #define CMD_MANUALLY 0x1003
74 #define CMD_REFRESH 0x1004
75 #define CMD_NOMTIME 0x1005
79 { "help", no_argument, 0, CMD_HELP },
80 { "version", no_argument, 0, CMD_VERSION },
81 { "destination", required_argument, 0, CMD_DESTINATION },
82 { "insecure", no_argument, 0, CMD_INSECURE },
83 { "sledgehammer", no_argument, 0, CMD_SLEDGE },
84 { "manually", no_argument, 0, CMD_MANUALLY },
85 { "refresh", no_argument, 0, CMD_REFRESH },
86 { "ignore-mtime", no_argument, 0, CMD_NOMTIME },
87 { "dry-run", no_argument, 0, 'n' },
88 { "verbose", no_argument, 0, 'v' },
92 // hash digest grouped by 2 digits + hash-collision counter + 2* '/' + NULL
93 typedef char HashPath[HASH_MAXBITS/4 + (HASH_MAXBITS/4/2) +
94 sizeof(unsigned int)*2 + 3];
96 struct HashDirConfiguration
98 ensc_hash_method const *method;
99 enum { hshALL=0, hshSTART = 1, hshMIDDLE=2,
100 hshEND = 4, hshINVALID = -1 } blocks;
107 struct MatchList dst_list;
108 struct HashDirConfiguration hash_conf;
109 HashDirCollection hash_dirs;
110 size_t hash_dirs_max_size;
112 ensc_hash_context hash_context;
115 int wrapper_exit_code = 1;
116 struct Arguments const *global_args;
117 static struct SkipReason skip_reason;
119 struct WalkdownInfo global_info = {
120 .hash_conf = { .method = 0,
122 .blocksize = 0x10000 }
125 #include "vhashify-init.hc"
127 int Global_getVerbosity() {
128 return global_args->verbosity;
131 int Global_doRenew() {
135 int Global_isVserverRunning() {
137 return global_args->insecure<2;
141 showHelp(char const *cmd)
143 WRITE_MSG(1, "Usage:\n ");
146 " [-nv] [--refresh] <vserver>\n or\n ");
149 " --manually [-nv] [--] <hashdir> <path> <excludelist>\n\n"
150 " --manually ... hashify generic paths; excludelists must be generated\n"
152 " --refresh ... hashify already hashified files also\n"
153 " -n ... do not modify anything; just show what there will be\n"
154 " done (in combination with '-v')\n"
155 " -v ... verbose mode\n"
156 "Please report bugs to " PACKAGE_BUGREPORT "\n");
165 "vhashify " VERSION " -- hashifies vservers and/or directories\n"
166 "This program is part of " PACKAGE_STRING "\n\n"
167 "Copyright (C) 2005 Enrico Scholz\n"
168 VERSION_COPYRIGHT_DISCLAIMER);
173 HashDirInfo_compareDevice(void const *lhs_v, void const *rhs_v)
175 struct HashDirInfo const * const lhs = lhs_v;
176 dev_t const * const rhs = rhs_v;
178 assert(lhs!=0 && rhs!=0);
179 return lhs->device - *rhs;
183 HashDirInfo_findDevice(HashDirCollection const *coll, dev_t dev)
185 struct HashDirInfo const *res;
187 res = Vector_searchSelfOrg_const(coll, &dev,
188 HashDirInfo_compareDevice, vecSHIFT_ONCE);
190 if (res!=0) return &res->path;
194 #include "vserver-visitdir.hc"
197 checkFstat(PathInfo const * const basename,
198 struct stat * const st)
200 assert(basename->d[0] != '/');
202 // local file does not exist... strange
204 skip_reason.r = rsFSTAT;
205 if (lstat(basename->d, st)==-1) return false;
207 // this is a directory and succeeds everytime
208 if (S_ISDIR(st->st_mode))
212 skip_reason.r = rsSYMLINK;
213 if (S_ISLNK(st->st_mode)) return false;
215 // ignore special files
216 skip_reason.r = rsSPECIAL;
217 if (!S_ISREG(st->st_mode) &&
218 !S_ISDIR(st->st_mode)) return false;
220 // ignore small files
221 skip_reason.r = rsTOOSMALL;
222 if (st->st_size < HASH_MINSIZE) return false;
224 switch (Unify_isIUnlinkable(basename->d)) {
225 case unifyUNSUPPORTED : skip_reason.r = rsUNSUPPORTED; return false;
227 // do an implicit refresh on busy files when there are no active links
228 if (st->st_nlink>1 && !global_args->do_refresh) {
230 skip_reason.r = rsUNIFIED;
240 static sigjmp_buf bus_error_restore;
241 static volatile sig_atomic_t bus_error;
244 handlerSIGBUS(int UNUSED num)
247 siglongjmp(bus_error_restore, 1);
251 convertDigest(HashPath d_path)
253 static char const HEX_DIGIT[] = "0123456789abcdef";
254 ensc_hash_context * const h_ctx = &global_info.hash_context;
255 size_t d_size = ensc_crypto_hashctx_get_digestsize(h_ctx);
257 unsigned char digest[d_size];
260 if (ensc_crypto_hashctx_get_digest(h_ctx, digest, NULL, d_size)==-1)
264 out+1<sizeof(HashPath)-(sizeof(unsigned int)*2 + 2) && in<d_size;
266 if ((in+254)%(in<=2 ? 1 : 256) == 0 && in>0)
268 d_path[out++] = HEX_DIGIT[digest[in] >> 4];
269 d_path[out++] = HEX_DIGIT[digest[in] & 0x0f];
271 d_path[out++] = '\0';
276 #ifndef ENSC_TESTSUITE
278 addStatHash(ensc_hash_context *h_ctx, struct stat const * const st)
280 #define DECL_ATTR(X) __typeof__(st->st_##X) X
281 #define SET_ATTR(X) .X = st->st_##X
283 struct __attribute__((__packed__)) {
296 .mtime = (global_args->ignore_mtime ? 0 : st->st_mtime),
303 return ensc_crypto_hashctx_update(h_ctx, (void *)&tmp, sizeof tmp)!=-1;
307 addStatHash(ensc_hash_context UNUSED *h_ctx, struct stat const UNUSED * const st)
314 calculateHashFromFD(int fd, HashPath d_path, struct stat const * const st)
316 ensc_hash_context * const h_ctx = &global_info.hash_context;
317 void const * volatile buf = 0;
318 loff_t volatile buf_size = 0;
319 bool volatile res = false;
322 if (ensc_crypto_hashctx_reset(h_ctx)==-1 ||
323 !addStatHash(h_ctx, st))
327 if (sigsetjmp(bus_error_restore,1)==0) {
329 off_t size = st->st_size;
331 while (offset < size) {
332 buf_size = size-offset;
333 if (buf_size>HASH_BLOCKSIZE) buf_size = HASH_BLOCKSIZE;
335 if ((buf=mmap(0, buf_size, PROT_READ, MAP_SHARED, fd, offset))==MAP_FAILED) {
336 perror("mmap(<hash>)");
341 madvise(const_cast(void *)(buf), buf_size, MADV_SEQUENTIAL); // ignore error...
343 if (ensc_crypto_hashctx_update(h_ctx, buf, buf_size)==-1) goto out;
345 munmap(const_cast(void *)(buf), buf_size);
349 res = convertDigest(d_path);
353 if (buf!=0) munmap(const_cast(void *)(buf), buf_size);
358 calculateHash(PathInfo const *filename, HashPath d_path, struct stat const * const st)
360 int fd = open(filename->d, O_NOFOLLOW|O_NONBLOCK|O_RDONLY|O_NOCTTY);
366 int old_errno = errno;
367 WRITE_MSG(2, "Failed to open '");
368 WRITE_STR(2, filename->d);
374 if (fstat(fd, &fst)==-1 ||
375 fst.st_dev!=st->st_dev || fst.st_ino!=st->st_ino) {
376 WRITE_MSG(2, "An unexpected event occured while stating '");
377 WRITE_STR(2, filename->d);
378 WRITE_MSG(2, "'.\n");
382 if (!calculateHashFromFD(fd, d_path, st)) {
383 WRITE_MSG(2, "Failed to calculate hash for '");
384 WRITE_STR(2, filename->d);
385 WRITE_MSG(2, "'.\n");
392 if (fd!=-1) close(fd);
397 resolveCollisions(char *result, PathInfo const *root, HashPath d_path,
398 struct stat *st, struct stat *hash_st)
400 strcpy(result, root->d); // 'root' ends on '/' already (see initHashList())
401 strcat(result, d_path);
403 char *ptr = result + strlen(result);
404 unsigned int idx = 0;
405 char buf[sizeof(int)*2 + 1];
409 ptr[sizeof(int)*2+1] = '\0';
412 len = utilvserver_fmt_xuint(buf, idx);
413 memset(ptr+1, '0', sizeof(int)*2 - len);
414 memcpy(ptr+1 + sizeof(int)*2 - len, buf, len);
416 if (lstat(result, hash_st)==-1) {
417 if (global_args->dry_run && errno!=ENOENT) {
418 int old_errno = errno;
419 WRITE_MSG(2, "lstat('");
426 else if (Unify_isUnified(st, hash_st)) {
427 skip_reason.r = rsUNIFIED;
430 else if (!Unify_isUnifyable(st, hash_st))
431 continue; // continue with next number*****
433 break; // ok, we finish here
435 if (!global_args->dry_run) {
437 if (!mkdirRecursive(result)) {
438 PERROR_Q("mkdir", result);
443 int fd = open(result, O_NOFOLLOW|O_EXCL|O_CREAT|O_WRONLY, 0200);
446 PERROR_Q("open", buf);
453 // HACK: avoid an additional lstat on the resulting hash-file
454 hash_st->st_size = 0;
462 checkDirEntry(PathInfo const *path, PathInfo const *basename,
464 struct stat *st, struct stat *hash_st,
467 //printf("checkDirEntry(%s, %s, %u)\n", path->d, d_path, is_dir);
469 struct WalkdownInfo const * const info = &global_info;
471 // Check if it is in the exclude/include list of the destination vserver and
472 // abort when it is not matching an allowed entry
473 skip_reason.r = rsEXCL;
474 if (MatchList_compare(&info->dst_list, path->d)!=stINCLUDE) return 0;
476 if (checkFstat(basename, st)) {
477 PathInfo const *hash_root_path;
480 *is_dir = S_ISDIR(st->st_mode);
483 !((skip_reason.r = rsWRONGDEV,
484 (hash_root_path = HashDirInfo_findDevice(&info->hash_dirs, st->st_dev))!=0) &&
485 (skip_reason.r = rsGENERAL,
486 calculateHash(basename, d_path, st)) &&
487 resolveCollisions(result_buf, hash_root_path, d_path, st, hash_st)))
500 switch (skip_reason.r) {
501 case rsDOTFILE : WRITE_MSG(1, "dotfile"); break;
502 case rsEXCL : WRITE_MSG(1, "excluded"); break;
503 case rsTOOSMALL : WRITE_MSG(1, "too small"); break;
504 case rsUNSUPPORTED : WRITE_MSG(1, "operation not supported"); break;
505 case rsFSTAT : WRITE_MSG(1, "fstat error"); break;
506 case rsSYMLINK : WRITE_MSG(1, "symlink"); break;
507 case rsUNIFIED : WRITE_MSG(1, "already unified"); break;
508 case rsSPECIAL : WRITE_MSG(1, "non regular file"); break;
509 case rsWRONGDEV : WRITE_MSG(1, "no matching device"); break;
510 case rsGENERAL : WRITE_MSG(1, "general error"); break;
511 default : assert(false); abort();
517 doit(char const *src, char const *dst,
518 struct stat const *src_st, struct stat const *dst_st,
519 PathInfo const *path)
521 if (global_args->dry_run || Global_getVerbosity()>=2) {
522 WRITE_MSG(1, "unifying '");
523 Vwrite(1, path->d, path->l);
526 if (Global_getVerbosity()>=4) {
527 WRITE_MSG(1, " (to '");
535 // abort here in dry-run mode
536 if (global_args->dry_run) return true;
538 if (dst_st->st_size==0) {
539 // file was not unified yet
541 if (Global_isVserverRunning()) {
543 if (Unify_copy (src, src_st, dst) &&
544 // the mixed 'dst' and 'src_st' params are intentionally...
545 Unify_unify(dst, src_st, src, false))
548 else if (Unify_unify(src, src_st, dst, true))
551 (void)unlink(dst); // cleanup in error-case
553 // there exists already a reference-file
554 else if (Unify_unify(dst, dst_st, src, false))
561 visitDirEntry(struct dirent const *ent)
564 char const * dirname = ent->d_name;
565 PathInfo path = global_info.state;
566 PathInfo tmp_path = {
570 char path_buf[ENSC_PI_APPSZ(path, tmp_path)];
571 char const *match = 0;
574 PathInfo_append(&path, &tmp_path, path_buf);
576 bool is_dotfile = isDotfile(dirname);
578 struct stat src_stat = { .st_mode=0 };
579 struct stat hash_stat;
580 char tmpbuf[global_info.hash_dirs_max_size +
581 sizeof(HashPath) + 2];
583 skip_reason.r = rsDOTFILE;
586 (match=checkDirEntry(&path, &tmp_path,
587 &is_dir, &src_stat, &hash_stat,
590 bool is_link = !is_dotfile && S_ISLNK(src_stat.st_mode);
592 if (Global_getVerbosity()>=1 &&
593 (Global_getVerbosity()>=3 || skip_reason.r!=rsUNIFIED) &&
594 ((!is_dotfile && !is_link) ||
595 (Global_getVerbosity()>=6 && is_dotfile) ||
596 (Global_getVerbosity()>=6 && is_link)) ) {
597 WRITE_MSG(1, " skipping '");
598 Vwrite(1, path.d, path.l);
600 if (Global_getVerbosity()>=2) printSkipReason();
608 res = visitDir(dirname, &src_stat);
610 else if (doit(dirname, match, &src_stat, &hash_stat, &path))
621 int main(int argc, char *argv[])
623 struct Arguments args = {
630 .ignore_mtime = false,
633 Vector_init(&global_info.hash_dirs, sizeof(struct HashDirInfo));
637 int c = getopt_long(argc, argv, "+nv",
642 case CMD_HELP : showHelp(argv[0]);
643 case CMD_VERSION : showVersion();
644 case CMD_DESTINATION : args.hash_dir = optarg; break;
645 case CMD_MANUALLY : args.mode = mdMANUALLY; break;
646 case CMD_INSECURE : args.insecure = 1; break;
647 case CMD_SLEDGE : args.insecure = 2; break;
648 case CMD_REFRESH : args.do_refresh = true; break;
649 case CMD_NOMTIME : args.ignore_mtime = true; break;
650 case 'n' : args.dry_run = true; break;
651 case 'v' : ++args.verbosity; break;
653 WRITE_MSG(2, "Try '");
654 WRITE_STR(2, argv[0]);
655 WRITE_MSG(2, " --help' for more information.\n");
662 WRITE_MSG(2, "No directory/vserver given\n");
666 if (args.hash_dir==0 && args.mode==mdMANUALLY) {
667 WRITE_MSG(2, "'--manually' requires '--destination'\n");
673 case mdMANUALLY : initModeManually(&args, argc-optind, argv+optind); break;
674 case mdVSERVER : initModeVserver (&args, argc-optind, argv+optind); break;
675 default : assert(false); return EXIT_FAILURE;
678 if (ensc_crypto_hashctx_init(&global_info.hash_context,
679 global_info.hash_conf.method)==-1) {
680 WRITE_MSG(2, "Failed to initialize hash-context\n");
684 if (Global_getVerbosity()>=1)
685 WRITE_MSG(1, "Starting to traverse directories...\n");
687 signal(SIGBUS, handlerSIGBUS);
689 Echdir(global_info.dst_list.root.d);
693 MatchList_destroy(&global_info.dst_list);
694 freeHashList(&global_info.hash_dirs);
695 ensc_crypto_hashctx_free(&global_info.hash_context);