t / helper / test-chmtime.con commit t/helper: 'test-chmtime (--get|-g)' to print only the mtime (decf711)
   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-chmtime =<seconds> file...
   9 *
  10 * Relative to the current time as returned by time(3):
  11 *
  12 *      test-chmtime =+<seconds> (or =-<seconds>) file...
  13 *
  14 * Or relative to the current mtime of the file:
  15 *
  16 *      test-chmtime <seconds> file...
  17 *      test-chmtime +<seconds> (or -<seconds>) file...
  18 *
  19 * Examples:
  20 *
  21 * To print the mtime and the file name use --verbose and set
  22 * the file mtime offset to 0:
  23 *
  24 *      test-chmtime -v +0 file
  25 *
  26 * To print only the mtime use --get:
  27 *
  28 *      test-chmtime --get file
  29 *
  30 * To set the mtime to current time:
  31 *
  32 *      test-chmtime =+0 file
  33 *
  34 * To set the file mtime offset to +1 and print the new value:
  35 *
  36 *      test-chmtime --get +1 file
  37 *
  38 */
  39#include "git-compat-util.h"
  40#include <utime.h>
  41
  42static const char usage_str[] =
  43        "(-v|--verbose|-g|--get) (+|=|=+|=-|-)<seconds> <file>...";
  44
  45static int timespec_arg(const char *arg, long int *set_time, int *set_eq)
  46{
  47        char *test;
  48        const char *timespec = arg;
  49        *set_eq = (*timespec == '=') ? 1 : 0;
  50        if (*set_eq) {
  51                timespec++;
  52                if (*timespec == '+') {
  53                        *set_eq = 2; /* relative "in the future" */
  54                        timespec++;
  55                }
  56        }
  57        *set_time = strtol(timespec, &test, 10);
  58        if (*test) {
  59                return 0;
  60        }
  61        if ((*set_eq && *set_time < 0) || *set_eq == 2) {
  62                time_t now = time(NULL);
  63                *set_time += now;
  64        }
  65        return 1;
  66}
  67
  68int cmd_main(int argc, const char **argv)
  69{
  70        static int verbose;
  71        static int get;
  72
  73        int i = 1;
  74        /* no mtime change by default */
  75        int set_eq = 0;
  76        long int set_time = 0;
  77
  78        if (argc < 3)
  79                goto usage;
  80
  81        if (strcmp(argv[i], "--get") == 0 || strcmp(argv[i], "-g") == 0) {
  82                get = 1;
  83                ++i;
  84        } else if (strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
  85                verbose = 1;
  86                ++i;
  87        }
  88
  89        if (i == argc) {
  90                goto usage;
  91        }
  92
  93        if (timespec_arg(argv[i], &set_time, &set_eq)) {
  94                ++i;
  95        } else {
  96                if (get == 0) {
  97                        fprintf(stderr, "Not a base-10 integer: %s\n", argv[i] + 1);
  98                        goto usage;
  99                }
 100        }
 101
 102        if (i == argc)
 103                goto usage;
 104
 105        for (; i < argc; i++) {
 106                struct stat sb;
 107                struct utimbuf utb;
 108                uintmax_t mtime;
 109
 110                if (stat(argv[i], &sb) < 0) {
 111                        fprintf(stderr, "Failed to stat %s: %s\n",
 112                                argv[i], strerror(errno));
 113                        return 1;
 114                }
 115
 116#ifdef GIT_WINDOWS_NATIVE
 117                if (!(sb.st_mode & S_IWUSR) &&
 118                                chmod(argv[i], sb.st_mode | S_IWUSR)) {
 119                        fprintf(stderr, "Could not make user-writable %s: %s",
 120                                argv[i], strerror(errno));
 121                        return 1;
 122                }
 123#endif
 124
 125                utb.actime = sb.st_atime;
 126                utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
 127
 128                mtime = utb.modtime < 0 ? 0: utb.modtime;
 129                if (get) {
 130                        printf("%"PRIuMAX"\n", mtime);
 131                } else if (verbose) {
 132                        printf("%"PRIuMAX"\t%s\n", mtime, argv[i]);
 133                }
 134
 135                if (utb.modtime != sb.st_mtime && utime(argv[i], &utb) < 0) {
 136                        fprintf(stderr, "Failed to modify time on %s: %s\n",
 137                                argv[i], strerror(errno));
 138                        return 1;
 139                }
 140        }
 141
 142        return 0;
 143
 144usage:
 145        fprintf(stderr, "usage: %s %s\n", argv[0], usage_str);
 146        return 1;
 147}