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).
6 * The mtime can be changed to an absolute value:
8 * test-tool chmtime =<seconds> file...
10 * Relative to the current time as returned by time(3):
12 * test-tool chmtime =+<seconds> (or =-<seconds>) file...
14 * Or relative to the current mtime of the file:
16 * test-tool chmtime <seconds> file...
17 * test-tool chmtime +<seconds> (or -<seconds>) file...
21 * To print the mtime and the file name use --verbose and set
22 * the file mtime offset to 0:
24 * test-tool chmtime -v +0 file
26 * To print only the mtime use --get:
28 * test-tool chmtime --get file
30 * To set the mtime to current time:
32 * test-tool chmtime =+0 file
34 * To set the file mtime offset to +1 and print the new value:
36 * test-tool chmtime --get +1 file
39 #include "test-tool.h"
40 #include "git-compat-util.h"
43 static const char usage_str
[] =
44 "(-v|--verbose|-g|--get) (+|=|=+|=-|-)<seconds> <file>...";
46 static int timespec_arg(const char *arg
, long int *set_time
, int *set_eq
)
49 const char *timespec
= arg
;
50 *set_eq
= (*timespec
== '=') ? 1 : 0;
53 if (*timespec
== '+') {
54 *set_eq
= 2; /* relative "in the future" */
58 *set_time
= strtol(timespec
, &test
, 10);
62 if ((*set_eq
&& *set_time
< 0) || *set_eq
== 2) {
63 time_t now
= time(NULL
);
69 int cmd__chmtime(int argc
, const char **argv
)
75 /* no mtime change by default */
77 long int set_time
= 0;
82 if (strcmp(argv
[i
], "--get") == 0 || strcmp(argv
[i
], "-g") == 0) {
85 } else if (strcmp(argv
[i
], "--verbose") == 0 || strcmp(argv
[i
], "-v") == 0) {
94 if (timespec_arg(argv
[i
], &set_time
, &set_eq
)) {
98 fprintf(stderr
, "Not a base-10 integer: %s\n", argv
[i
] + 1);
106 for (; i
< argc
; i
++) {
111 if (stat(argv
[i
], &sb
) < 0) {
112 fprintf(stderr
, "Failed to stat %s: %s\n",
113 argv
[i
], strerror(errno
));
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
));
126 utb
.actime
= sb
.st_atime
;
127 utb
.modtime
= set_eq
? set_time
: sb
.st_mtime
+ set_time
;
129 mtime
= utb
.modtime
< 0 ? 0: utb
.modtime
;
131 printf("%"PRIuMAX
"\n", mtime
);
132 } else if (verbose
) {
133 printf("%"PRIuMAX
"\t%s\n", mtime
, argv
[i
]);
136 if (utb
.modtime
!= sb
.st_mtime
&& utime(argv
[i
], &utb
) < 0) {
137 fprintf(stderr
, "Failed to modify time on %s: %s\n",
138 argv
[i
], strerror(errno
));
146 fprintf(stderr
, "usage: %s %s\n", argv
[0], usage_str
);