Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/helper/test-chmtime.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / helper / test-chmtime.c` 163 lines ยท 3627 bytes Raw Blame History 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 = file... 9 * 10 * Relative to the current time as returned by time(3): 11 * 12 * test-tool chmtime =+ (or =-) file... 13 * 14 * Or relative to the current mtime of the file: 15 * 16 * test-tool chmtime file... 17 * test-tool chmtime + (or -) 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-tool chmtime -v +0 file 25 * 26 * To print only the mtime use --get: 27 * 28 * test-tool chmtime --get file 29 * 30 * To set the mtime to current time: 31 * 32 * test-tool chmtime =+0 file 33 * 34 * To set the file mtime offset to +1 and print the new value: 35 * 36 * test-tool chmtime --get +1 file 37 * 38 */ 39 #include "test-tool.h" 40 #include "git-compat-util.h" 41 #include 42 43 static const char usage_str[] = 44 "(-v|--verbose|-g|--get) (+|=|=+|=-|-) ..."; 45 46 static int timespec_arg(const char *arg, long int *set_time, int *set_eq) 47 { 48 char *test; 49 const char *timespec = arg; 50 *set_eq = (*timespec == '=') ? 1 : 0; 51 if (*set_eq) { 52 timespec++; 53 if (*timespec == '+') { 54 *set_eq = 2; /* relative "in the future" */ 55 timespec++; 56 } 57 } 58 *set_time = strtol(timespec, &test, 10); 59 if (*test) { 60 return 0; 61 } 62 if ((*set_eq && *set_time < 0) || *set_eq == 2) { 63 time_t now = time(NULL); 64 *set_time += now; 65 } 66 return 1; 67 } 68 69 int cmd__chmtime(int argc, const char **argv) 70 { 71 static int verbose; 72 static int get; 73 74 int i = 1; 75 /* no mtime change by default */ 76 int set_eq = 0; 77 long int set_time = 0; 78 79 if (argc < 3) 80 goto usage; 81 82 if (strcmp(argv[i], "--get") == 0 || strcmp(argv[i], "-g") == 0) { 83 get = 1; 84 ++i; 85 } else if (strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) { 86 verbose = 1; 87 ++i; 88 } 89 90 if (i == argc) { 91 goto usage; 92 } 93 94 if (timespec_arg(argv[i], &set_time, &set_eq)) { 95 ++i; 96 } else { 97 if (get == 0 && verbose == 0) { 98 fprintf(stderr, "Not a base-10 integer: %s\n", argv[i] + 1); 99 goto usage; 100 } 101 } 102 103 if (i == argc) 104 goto usage; 105 106 for (; i < argc; i++) { 107 struct stat sb; 108 struct utimbuf utb; 109 uintmax_t mtime; 110 111 if (stat(argv[i], &sb) < 0) { 112 fprintf(stderr, "Failed to stat %s: %s. Skipping\n", 113 argv[i], strerror(errno)); 114 continue; 115 } 116 117 #ifdef GIT_WINDOWS_NATIVE 118 if (!(sb.st_mode & S_IWUSR) && 119 chmod(argv[i], sb.st_mode | S_IWUSR)) { 120 fprintf(stderr, "Could not make user-writable %s: %s", 121 argv[i], strerror(errno)); 122 return 1; 123 } 124 #endif 125 126 utb.actime = sb.st_atime; 127 utb.modtime = set_eq ? set_time : sb.st_mtime + set_time; 128 129 mtime = utb.modtime < 0 ? 0: utb.modtime; 130 if (get) { 131 printf("%"PRIuMAX"\n", mtime); 132 } else if (verbose) { 133 printf("%"PRIuMAX"\t%s\n", mtime, argv[i]); 134 } 135 136 if (utb.modtime != sb.st_mtime && utime(argv[i], &utb) < 0) { 137 #ifdef GIT_WINDOWS_NATIVE 138 if (S_ISDIR(sb.st_mode)) { 139 /* 140 * NEEDSWORK: The Windows version of `utime()` 141 * (aka `mingw_utime()`) does not correctly 142 * handle directory arguments, since it uses 143 * `_wopen()`. Ignore it for now since this 144 * is just a test. 145 */ 146 fprintf(stderr, 147 ("Failed to modify time on directory %s. " 148 "Skipping\n"), argv[i]); 149 continue; 150 } 151 #endif 152 fprintf(stderr, "Failed to modify time on %s: %s\n", 153 argv[i], strerror(errno)); 154 return 1; 155 } 156 } 157 158 return 0; 159 160 usage: 161 fprintf(stderr, "usage: %s %s\n", argv[0], usage_str); 162 return 1; 163 }