X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fvhashify.c;h=005efd829fe3e80e77936bf881ead3780cb0b389;hb=95e2774070e989fe9cf9f48dae5fa054e55e2a3e;hp=2950f97d7434aacda50dffa069f7d26195e172d7;hpb=2822ba293eb308225c50d346930c47bf98d9927b;p=util-vserver.git diff --git a/src/vhashify.c b/src/vhashify.c index 2950f97..005efd8 100644 --- a/src/vhashify.c +++ b/src/vhashify.c @@ -1,4 +1,4 @@ -// $Id: vhashify.c,v 1.6 2005/03/24 12:46:59 ensc Exp $ --*- c -*-- +// $Id: vhashify.c 2569 2007-07-22 17:24:29Z dhozac $ --*- c -*-- // Copyright (C) 2005 Enrico Scholz // @@ -20,6 +20,8 @@ # include #endif +#define UTIL_VSERVER_UNIFY_MTIME_OPTIONAL + #include "vhashify.h" #include "util.h" @@ -70,6 +72,7 @@ #define CMD_SLEDGE 0x1002 #define CMD_MANUALLY 0x1003 #define CMD_REFRESH 0x1004 +#define CMD_NOMTIME 0x1005 struct option const CMDLINE_OPTIONS[] = { @@ -80,6 +83,7 @@ CMDLINE_OPTIONS[] = { { "sledgehammer", no_argument, 0, CMD_SLEDGE }, { "manually", no_argument, 0, CMD_MANUALLY }, { "refresh", no_argument, 0, CMD_REFRESH }, + { "ignore-mtime", no_argument, 0, CMD_NOMTIME }, { "dry-run", no_argument, 0, 'n' }, { "verbose", no_argument, 0, 'v' }, { 0,0,0,0 } @@ -269,6 +273,7 @@ convertDigest(HashPath d_path) return true; } +#ifndef ENSC_TESTSUITE static bool addStatHash(hashFunctionContext *h_ctx, struct stat const * const st) { @@ -288,12 +293,23 @@ addStatHash(hashFunctionContext *h_ctx, struct stat const * const st) SET_ATTR(gid), SET_ATTR(rdev), SET_ATTR(size), - SET_ATTR(mtime) + .mtime = (global_args->ignore_mtime ? 0 : st->st_mtime), }; +#undef SET_ATTR +#undef DECL_ATTR + + return hashFunctionContextUpdate(h_ctx, (void *)&tmp, sizeof tmp)!=-1; } - +#else +static bool +addStatHash(hashFunctionContext UNUSED *h_ctx, struct stat const UNUSED * const st) +{ + return true; +} +#endif + static bool calculateHashFromFD(int fd, HashPath d_path, struct stat const * const st) { @@ -377,69 +393,6 @@ calculateHash(PathInfo const *filename, HashPath d_path, struct stat const * con return res; } -static enum { mkdirFAIL, mkdirSUCCESS, mkdirSKIP } -mkdirSingle(char const *path, char *end_ptr, int good_err) -{ - *end_ptr = '\0'; - if (mkdir(path, 0700)!=-1 || errno==EEXIST) { - *end_ptr = '/'; - return mkdirSUCCESS; - } - else if (errno==good_err) { - *end_ptr = '/'; - return mkdirSKIP; - } - else { - int old_errno = errno; - WRITE_MSG(2, "mkdir('"); - WRITE_STR(2, path); - errno = old_errno; - perror("')"); - return mkdirFAIL; - } -} - -static char * -rstrchr(char *str, char c) -{ - while (*str!=c) --str; - return str; -} - -static bool -mkdirRecursive(char const *path) -{ - if (path[0]!='/') return false; // only absolute paths - - char buf[strlen(path)+1]; - char * ptr = buf + sizeof(buf) - 2; - - strcpy(buf, path); - - while (ptr>buf && (ptr = rstrchr(ptr, '/'))!=0) { - switch (mkdirSingle(buf, ptr, ENOENT)) { - case mkdirSUCCESS : break; - case mkdirSKIP : --ptr; continue; - case mkdirFAIL : return false; - } - - break; // implied by mkdirSUCCESS - } - - assert(ptr!=0); - ++ptr; - - while ((ptr=strchr(ptr, '/'))!=0) { - switch (mkdirSingle(buf, ptr, 0)) { - case mkdirSKIP : - case mkdirFAIL : return false; - case mkdirSUCCESS : ++ptr; continue; - } - } - - return true; -} - static bool resolveCollisions(char *result, PathInfo const *root, HashPath d_path, struct stat *st, struct stat *hash_st) @@ -481,18 +434,16 @@ resolveCollisions(char *result, PathInfo const *root, HashPath d_path, if (!global_args->dry_run) { *ptr = '\0'; - if (!mkdirRecursive(result)) + if (!mkdirRecursive(result)) { + PERROR_Q("mkdir", result); return false; + } *ptr = '-'; int fd = open(result, O_NOFOLLOW|O_EXCL|O_CREAT|O_WRONLY, 0200); if (fd==-1) { - int old_errno = errno; - WRITE_MSG(2, "open('"); - WRITE_STR(2, buf); - errno = old_errno; - perror("')"); + PERROR_Q("open", buf); return false; } @@ -676,6 +627,7 @@ int main(int argc, char *argv[]) .insecure = 0, .dry_run = false, .do_refresh = false, + .ignore_mtime = false, }; Vector_init(&global_info.hash_dirs, sizeof(struct HashDirInfo)); @@ -694,12 +646,13 @@ int main(int argc, char *argv[]) case CMD_INSECURE : args.insecure = 1; break; case CMD_SLEDGE : args.insecure = 2; break; case CMD_REFRESH : args.do_refresh = true; break; + case CMD_NOMTIME : args.ignore_mtime = true; break; case 'n' : args.dry_run = true; break; case 'v' : ++args.verbosity; break; default : WRITE_MSG(2, "Try '"); WRITE_STR(2, argv[0]); - WRITE_MSG(2, " --help\" for more information.\n"); + WRITE_MSG(2, " --help' for more information.\n"); return EXIT_FAILURE; break; } @@ -740,4 +693,6 @@ int main(int argc, char *argv[]) freeHashList(&global_info.hash_dirs); hashFunctionContextFree(&global_info.hash_context); #endif + + return EXIT_SUCCESS; }