t / helper / test-chmtime.con commit Merge branch 'bw/protocol-v2' into HEAD (bbc39d4)
   1/*
   2 * This program can either change modification time of the given
   3 * file(s) or just print it. The program does not change atime or
   4 * ctime (their values are explicitly preserved).
   5 *
   6 * The mtime can be changed to an absolute value:
   7 *
   8 *      test-tool chmtime =<seconds> file...
   9 *
  10 * Relative to the current time as returned by time(3):
  11 *
  12 *      test-tool chmtime =+<seconds> (or =-<seconds>) file...
  13 *
  14 * Or relative to the current mtime of the file:
  15 *
  16 *      test-tool chmtime <seconds> file...
  17 *      test-tool chmtime +<seconds> (or -<seconds>) file...
  18 *
  19 * Examples:
  20 *
  21 * To just print the mtime use --verbose and set the file mtime offset to 0:
  22 *
  23 *      test-tool chmtime -v +0 file
  24 *
  25 * To set the mtime to current time:
  26 *
  27 *      test-tool chmtime =+0 file
  28 *
  29 */
  30#include "test-tool.h"
  31#include "git-compat-util.h"
  32#include <utime.h>
  33
  34static const char usage_str[] = "-v|--verbose (+|=|=+|=-|-)<seconds> <file>...";
  35
  36static int timespec_arg(const char *arg, long int *set_time, int *set_eq)
  37{
  38        char *test;
  39        const char *timespec = arg;
  40        *set_eq = (*timespec == '=') ? 1 : 0;
  41        if (*set_eq) {
  42                timespec++;
  43                if (*timespec == '+') {
  44                        *set_eq = 2; /* relative "in the future" */
  45                        timespec++;
  46                }
  47        }
  48        *set_time = strtol(timespec, &test, 10);
  49        if (*test) {
  50                fprintf(stderr, "Not a base-10 integer: %s\n", arg + 1);
  51                return 0;
  52        }
  53        if ((*set_eq && *set_time < 0) || *set_eq == 2) {
  54                time_t now = time(NULL);
  55                *set_time += now;
  56        }
  57        return 1;
  58}
  59
  60int cmd__chmtime(int argc, const char **argv)
  61{
  62        static int verbose;
  63
  64        int i = 1;
  65        /* no mtime change by default */
  66        int set_eq = 0;
  67        long int set_time = 0;
  68
  69        if (argc < 3)
  70                goto usage;
  71
  72        if (strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
  73                verbose = 1;
  74                ++i;
  75        }
  76        if (timespec_arg(argv[i], &set_time, &set_eq))
  77                ++i;
  78        else
  79                goto usage;
  80
  81        for (; i < argc; i++) {
  82                struct stat sb;
  83                struct utimbuf utb;
  84
  85                if (stat(argv[i], &sb) < 0) {
  86                        fprintf(stderr, "Failed to stat %s: %s\n",
  87                                argv[i], strerror(errno));
  88                        return 1;
  89                }
  90
  91#ifdef GIT_WINDOWS_NATIVE
  92                if (!(sb.st_mode & S_IWUSR) &&
  93                                chmod(argv[i], sb.st_mode | S_IWUSR)) {
  94                        fprintf(stderr, "Could not make user-writable %s: %s",
  95                                argv[i], strerror(errno));
  96                        return 1;
  97                }
  98#endif
  99
 100                utb.actime = sb.st_atime;
 101                utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
 102
 103                if (verbose) {
 104                        uintmax_t mtime = utb.modtime < 0 ? 0: utb.modtime;
 105                        printf("%"PRIuMAX"\t%s\n", mtime, argv[i]);
 106                }
 107
 108                if (utb.modtime != sb.st_mtime && utime(argv[i], &utb) < 0) {
 109                        fprintf(stderr, "Failed to modify time on %s: %s\n",
 110                                argv[i], strerror(errno));
 111                        return 1;
 112                }
 113        }
 114
 115        return 0;
 116
 117usage:
 118        fprintf(stderr, "usage: %s %s\n", argv[0], usage_str);
 119        return 1;
 120}