Commit | Line | Data |
---|---|---|
00891c37 WD |
1 | John Taylor's patch for implementing --time-limit and --stop-at, reworked |
2 | to be simpler and more efficient by Wayne Davison. | |
3 | ||
ed44ffcf | 4 | Do we need configure support for mktime()? |
00891c37 | 5 | |
9a7eef96 WD |
6 | --- old/io.c |
7 | +++ new/io.c | |
5398d042 | 8 | @@ -57,6 +57,7 @@ extern int remove_sent_files; |
d608ca23 | 9 | extern int preserve_hard_links; |
16a742de | 10 | extern char *filesfrom_host; |
9cd8d7aa | 11 | extern struct stats stats; |
d608ca23 WD |
12 | +extern time_t stop_at_utime; |
13 | extern struct file_list *the_file_list; | |
c70898d5 | 14 | |
d608ca23 | 15 | const char phase_unknown[] = "unknown"; |
0ca6aebe | 16 | @@ -168,16 +169,24 @@ static void check_timeout(void) |
00891c37 WD |
17 | { |
18 | time_t t; | |
c70898d5 | 19 | |
8ab3ce19 WD |
20 | + if ((!io_timeout || ignore_timeout) && !stop_at_utime) |
21 | + return; | |
22 | + | |
00891c37 | 23 | + t = time(NULL); |
c70898d5 | 24 | + |
00891c37 WD |
25 | + if (stop_at_utime && t >= stop_at_utime) { |
26 | + rprintf(FERROR, "run-time limit exceeded\n"); | |
27 | + exit_cleanup(RERR_TIMEOUT); | |
28 | + } | |
c70898d5 | 29 | + |
8ab3ce19 WD |
30 | if (!io_timeout || ignore_timeout) |
31 | return; | |
32 | ||
5398d042 WD |
33 | if (!last_io_in) { |
34 | - last_io_in = time(NULL); | |
35 | + last_io_in = t; | |
00891c37 WD |
36 | return; |
37 | } | |
c70898d5 | 38 | |
7ce4946a WD |
39 | - t = time(NULL); |
40 | - | |
5398d042 | 41 | if (t - last_io_in >= io_timeout) { |
00891c37 | 42 | if (!am_server && !am_daemon) { |
a7219d20 | 43 | rprintf(FERROR, "io timeout after %d seconds -- exiting\n", |
9a7eef96 WD |
44 | --- old/options.c |
45 | +++ new/options.c | |
afcb578c | 46 | @@ -116,6 +116,7 @@ int checksum_seed = 0; |
f6c3b300 | 47 | int inplace = 0; |
79f132a1 | 48 | int delay_updates = 0; |
2c2d83dc | 49 | long block_size = 0; /* "long" because popt can't set an int32. */ |
00891c37 | 50 | +time_t stop_at_utime = 0; |
c70898d5 | 51 | |
c70898d5 | 52 | |
00891c37 | 53 | /** Network address family. **/ |
afcb578c | 54 | @@ -365,6 +366,8 @@ void usage(enum logcode F) |
79f132a1 | 55 | rprintf(F," --password-file=FILE read password from FILE\n"); |
be73a66e | 56 | rprintf(F," --list-only list the files instead of copying them\n"); |
79f132a1 | 57 | rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n"); |
ed44ffcf WD |
58 | + rprintf(F," --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute\n"); |
59 | + rprintf(F," --time-limit=MINS Stop rsync after MINS minutes have elapsed\n"); | |
79f132a1 | 60 | rprintf(F," --write-batch=FILE write a batched update to FILE\n"); |
c2f72cff | 61 | rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n"); |
79f132a1 | 62 | rprintf(F," --read-batch=FILE read a batched update from FILE\n"); |
afcb578c | 63 | @@ -385,7 +388,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP |
6849cd84 | 64 | OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP, |
0ca6aebe | 65 | OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD, |
5398d042 | 66 | OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE, |
4a65fe72 WD |
67 | - OPT_NO_D, |
68 | + OPT_NO_D, OPT_STOP_AT, OPT_TIME_LIMIT, | |
0ca6aebe | 69 | OPT_SERVER, OPT_REFUSED_BASE = 9000}; |
00891c37 WD |
70 | |
71 | static struct poptOption long_options[] = { | |
afcb578c | 72 | @@ -494,6 +497,8 @@ static struct poptOption long_options[] |
c70898d5 | 73 | {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 }, |
0ca6aebe | 74 | {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 }, |
c70898d5 | 75 | {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 }, |
00891c37 WD |
76 | + {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 }, |
77 | + {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 }, | |
489b0a72 | 78 | {"backup", 'b', POPT_ARG_NONE, &make_backups, 0, 0, 0 }, |
c70898d5 | 79 | {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 }, |
489b0a72 | 80 | {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 }, |
afcb578c | 81 | @@ -1060,6 +1065,36 @@ int parse_arguments(int *argc, const cha |
0ca6aebe WD |
82 | usage(FINFO); |
83 | exit_cleanup(0); | |
00891c37 WD |
84 | |
85 | + case OPT_STOP_AT: | |
86 | + arg = poptGetOptArg(pc); | |
ed44ffcf | 87 | + if ((stop_at_utime = parse_time(arg)) == (time_t)-1) { |
00891c37 WD |
88 | + snprintf(err_buf, sizeof err_buf, |
89 | + "invalid --stop-at format: %s\n", | |
90 | + arg); | |
91 | + rprintf(FERROR, "ERROR: %s", err_buf); | |
92 | + return 0; | |
93 | + } | |
00891c37 WD |
94 | + if (stop_at_utime < time(NULL)) { |
95 | + snprintf(err_buf, sizeof err_buf, | |
96 | + "--stop-at time is in the past: %s\n", | |
97 | + arg); | |
98 | + rprintf(FERROR, "ERROR: %s", err_buf); | |
99 | + return 0; | |
100 | + } | |
101 | + break; | |
102 | + | |
103 | + case OPT_TIME_LIMIT: | |
104 | + arg = poptGetOptArg(pc); | |
105 | + if ((stop_at_utime = atol(arg) * 60) <= 0) { | |
106 | + snprintf(err_buf, sizeof err_buf, | |
107 | + "invalid --time-limit value: %s\n", | |
108 | + arg); | |
109 | + rprintf(FERROR, "ERROR: %s", err_buf); | |
110 | + return 0; | |
111 | + } | |
112 | + stop_at_utime += time(NULL); | |
113 | + break; | |
114 | + | |
115 | default: | |
116 | /* A large opt value means that set_refuse_options() | |
27a7053c | 117 | * turned this option off. */ |
afcb578c | 118 | @@ -1610,6 +1645,15 @@ void server_options(char **args,int *arg |
7ce4946a WD |
119 | args[ac++] = arg; |
120 | } | |
c70898d5 | 121 | |
00891c37 WD |
122 | + if (stop_at_utime) { |
123 | + long mins = (stop_at_utime - time(NULL)) / 60; | |
124 | + if (mins <= 0) | |
125 | + mins = 1; | |
126 | + if (asprintf(&arg, "--time-limit=%ld", mins) < 0) | |
7ce4946a WD |
127 | + goto oom; |
128 | + args[ac++] = arg; | |
129 | + } | |
130 | + | |
131 | if (backup_dir) { | |
132 | args[ac++] = "--backup-dir"; | |
133 | args[ac++] = backup_dir; | |
9a7eef96 WD |
134 | --- old/rsync.yo |
135 | +++ new/rsync.yo | |
afcb578c | 136 | @@ -388,6 +388,8 @@ to the detailed description below for a |
79f132a1 | 137 | --password-file=FILE read password from FILE |
be73a66e | 138 | --list-only list the files instead of copying them |
79f132a1 | 139 | --bwlimit=KBPS limit I/O bandwidth; KBytes per second |
ed44ffcf WD |
140 | + --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute |
141 | + --time-limit=MINS Stop rsync after MINS minutes have elapsed | |
a7219d20 | 142 | --write-batch=FILE write a batched update to FILE |
c2f72cff | 143 | --only-write-batch=FILE like --write-batch but w/o updating dest |
79f132a1 | 144 | --read-batch=FILE read a batched update from FILE |
afcb578c | 145 | @@ -1584,6 +1586,19 @@ transfer was too fast, it will wait befo |
c70898d5 WD |
146 | result is an average transfer rate equaling the specified limit. A value |
147 | of zero specifies no limit. | |
7ce4946a | 148 | |
ed44ffcf WD |
149 | +dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what |
150 | +time to stop rsync, in year-month-dayThour:minute numeric format (e.g. | |
ab74148e | 151 | +2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also |
ed44ffcf WD |
152 | +leave off various items and the result will be the next possible time |
153 | +that matches the specified data. For example, "1-30" specifies the next | |
16711fbf WD |
154 | +January 30th (at midnight), "04:00" specifies the next 4am, "1" |
155 | +specifies the next 1st of the month at midnight, and ":59" specifies the | |
ab74148e WD |
156 | +next 59th minute after the hour. If you prefer, you may separate the |
157 | +date numbers using slashes instead of dashes. | |
00891c37 | 158 | + |
ed44ffcf | 159 | +dit(bf(--time-limit=MINS)) This option allows you to specify the maximum |
00891c37 | 160 | +number of minutes rsync will run for. |
7ce4946a | 161 | + |
9be39c35 | 162 | dit(bf(--write-batch=FILE)) Record a file that can later be applied to |
a7219d20 | 163 | another identical destination with bf(--read-batch). See the "BATCH MODE" |
388bf7cc | 164 | section for details, and also the bf(--only-write-batch) option. |
9a7eef96 WD |
165 | --- old/util.c |
166 | +++ new/util.c | |
afcb578c | 167 | @@ -125,6 +125,133 @@ void overflow_exit(char *str) |
ed44ffcf WD |
168 | exit_cleanup(RERR_MALLOC); |
169 | } | |
170 | ||
171 | +/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while | |
172 | + * also allowing abbreviated data. For instance, if the time is omitted, | |
173 | + * it defaults to midnight. If the date is omitted, it defaults to the | |
174 | + * next possible date in the future with the specified time. Even the | |
175 | + * year or year-month can be omitted, again defaulting to the next date | |
176 | + * in the future that matches the specified information. A 2-digit year | |
177 | + * is also OK, as is using '/' instead of '-'. */ | |
178 | +time_t parse_time(const char *arg) | |
179 | +{ | |
180 | + const char *cp; | |
181 | + time_t val, now = time(NULL); | |
182 | + struct tm t, *today = localtime(&now); | |
16711fbf | 183 | + int in_date, n; |
ed44ffcf WD |
184 | + |
185 | + memset(&t, 0, sizeof t); | |
186 | + t.tm_year = t.tm_mon = t.tm_mday = -1; | |
187 | + t.tm_hour = t.tm_min = t.tm_isdst = -1; | |
16711fbf WD |
188 | + cp = arg; |
189 | + if (*cp == 'T' || *cp == 't' || *cp == ':') { | |
190 | + cp++; | |
191 | + in_date = 0; | |
192 | + } else | |
193 | + in_date = 1; | |
194 | + for ( ; ; cp++) { | |
195 | + if (!isdigit(*cp)) | |
ed44ffcf | 196 | + return -1; |
16711fbf WD |
197 | + |
198 | + n = 0; | |
199 | + do { | |
200 | + n = n * 10 + *cp++ - '0'; | |
201 | + } while (isdigit(*cp)); | |
202 | + | |
203 | + if (*cp == ':') | |
204 | + in_date = 0; | |
ed44ffcf | 205 | + if (in_date) { |
16711fbf WD |
206 | + if (t.tm_year != -1) |
207 | + return -1; | |
208 | + t.tm_year = t.tm_mon; | |
209 | + t.tm_mon = t.tm_mday; | |
210 | + t.tm_mday = n; | |
211 | + if (!*cp) | |
212 | + break; | |
213 | + if (*cp == 'T' || *cp == 't') { | |
214 | + if (!cp[1]) | |
ed44ffcf | 215 | + break; |
16711fbf WD |
216 | + in_date = 0; |
217 | + } else if (*cp != '-' && *cp != '/') | |
218 | + return -1; | |
219 | + continue; | |
ed44ffcf WD |
220 | + } |
221 | + if (t.tm_hour != -1) | |
222 | + return -1; | |
223 | + t.tm_hour = t.tm_min; | |
224 | + t.tm_min = n; | |
225 | + if (!*cp) | |
226 | + break; | |
227 | + if (*cp != ':') | |
228 | + return -1; | |
229 | + } | |
230 | + | |
231 | + in_date = 0; | |
232 | + if (t.tm_year < 0) { | |
233 | + t.tm_year = today->tm_year; | |
234 | + in_date = 1; | |
235 | + } else if (t.tm_year < 100) { | |
236 | + while (t.tm_year < today->tm_year) | |
237 | + t.tm_year += 100; | |
238 | + } else | |
239 | + t.tm_year -= 1900; | |
240 | + if (t.tm_mon < 0) { | |
241 | + t.tm_mon = today->tm_mon; | |
242 | + in_date = 2; | |
243 | + } else | |
244 | + t.tm_mon--; | |
245 | + if (t.tm_mday < 0) { | |
ed44ffcf WD |
246 | + t.tm_mday = today->tm_mday; |
247 | + in_date = 3; | |
248 | + } | |
249 | + | |
250 | + n = 0; | |
251 | + if (t.tm_min < 0) { | |
252 | + t.tm_hour = t.tm_min = 0; | |
253 | + } else if (t.tm_hour < 0) { | |
254 | + if (in_date != 3) | |
255 | + return -1; | |
256 | + in_date = 0; | |
257 | + t.tm_hour = today->tm_hour; | |
258 | + n = 60*60; | |
259 | + } | |
260 | + | |
16711fbf WD |
261 | + if (t.tm_hour > 23 || t.tm_min > 59 |
262 | + || t.tm_mon < 0 || t.tm_mon >= 12 | |
263 | + || t.tm_mday < 1 || t.tm_mday > 31 | |
264 | + || (val = mktime(&t)) == (time_t)-1) | |
ed44ffcf WD |
265 | + return -1; |
266 | + | |
267 | + if (val <= now && in_date) { | |
268 | + tweak_date: | |
269 | + switch (in_date) { | |
270 | + case 3: | |
271 | + t.tm_mday++; | |
272 | + break; | |
273 | + case 2: | |
274 | + if (++t.tm_mon == 12) | |
275 | + t.tm_mon = 0; | |
276 | + else | |
277 | + break; | |
278 | + case 1: | |
279 | + t.tm_year++; | |
280 | + break; | |
281 | + } | |
282 | + if ((val = mktime(&t)) == (time_t)-1) { | |
283 | + if (in_date == 3 && t.tm_mday > 28) { | |
284 | + t.tm_mday = 1; | |
285 | + in_date = 2; | |
286 | + goto tweak_date; | |
287 | + } | |
288 | + return -1; | |
289 | + } | |
290 | + } | |
291 | + if (n) { | |
292 | + while (val <= now) | |
293 | + val += n; | |
294 | + } | |
295 | + return val; | |
296 | +} | |
afcb578c | 297 | + |
489b0a72 | 298 | int set_modtime(char *fname, time_t modtime, mode_t mode) |
afcb578c WD |
299 | { |
300 | #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES |