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 | |
a7219d20 | 6 | --- orig/io.c 2005-02-03 02:04:20 |
9cd8d7aa | 7 | +++ io.c 2004-07-17 15:28:36 |
0b2fb126 WD |
8 | @@ -51,6 +51,7 @@ extern int eol_nulls; |
9 | extern int csum_length; | |
9cd8d7aa WD |
10 | extern int checksum_seed; |
11 | extern int protocol_version; | |
00891c37 | 12 | +extern time_t stop_at_utime; |
9cd8d7aa WD |
13 | extern char *remote_filesfrom_file; |
14 | extern struct stats stats; | |
c70898d5 | 15 | |
0b2fb126 | 16 | @@ -137,17 +138,22 @@ static void check_timeout(void) |
00891c37 WD |
17 | { |
18 | time_t t; | |
c70898d5 | 19 | |
00891c37 WD |
20 | - if (!io_timeout) |
21 | + if (!io_timeout && !stop_at_utime) | |
22 | return; | |
23 | ||
24 | + t = time(NULL); | |
c70898d5 | 25 | + |
00891c37 WD |
26 | + if (stop_at_utime && t >= stop_at_utime) { |
27 | + rprintf(FERROR, "run-time limit exceeded\n"); | |
28 | + exit_cleanup(RERR_TIMEOUT); | |
29 | + } | |
c70898d5 | 30 | + |
00891c37 WD |
31 | if (!last_io) { |
32 | - last_io = time(NULL); | |
33 | + last_io = t; | |
34 | return; | |
35 | } | |
c70898d5 | 36 | |
7ce4946a WD |
37 | - t = time(NULL); |
38 | - | |
9cd8d7aa WD |
39 | - if (t - last_io >= io_timeout) { |
40 | + if (io_timeout && t - last_io >= io_timeout) { | |
00891c37 | 41 | if (!am_server && !am_daemon) { |
a7219d20 | 42 | rprintf(FERROR, "io timeout after %d seconds -- exiting\n", |
9cd8d7aa | 43 | (int)(t-last_io)); |
159b101f | 44 | --- orig/options.c 2005-02-15 19:27:05 |
79f132a1 | 45 | +++ options.c 2005-01-28 19:35:23 |
159b101f | 46 | @@ -102,6 +102,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. **/ |
159b101f | 54 | @@ -331,6 +332,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 WD |
60 | rprintf(F," --write-batch=FILE write a batched update to FILE\n"); |
61 | rprintf(F," --read-batch=FILE read a batched update from FILE\n"); | |
159b101f WD |
62 | #ifdef INET6 |
63 | @@ -348,6 +351,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP | |
ac23c334 | 64 | OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, |
00891c37 | 65 | OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, |
58a9031f | 66 | OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT, OPT_MAX_SIZE, |
125d7fca | 67 | + OPT_STOP_AT, OPT_TIME_LIMIT, |
00891c37 WD |
68 | OPT_REFUSED_BASE = 9000}; |
69 | ||
70 | static struct poptOption long_options[] = { | |
159b101f | 71 | @@ -432,6 +436,8 @@ static struct poptOption long_options[] |
c70898d5 | 72 | {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 }, |
159b101f | 73 | {"itemize-changes", 'i', POPT_ARG_NONE, &itemize_changes, 0, 0, 0 }, |
c70898d5 | 74 | {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 }, |
00891c37 WD |
75 | + {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 }, |
76 | + {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 }, | |
c70898d5 WD |
77 | {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 }, |
78 | {"hard-links", 'H', POPT_ARG_NONE, &preserve_hard_links, 0, 0, 0 }, | |
5388f859 | 79 | {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 }, |
159b101f | 80 | @@ -837,6 +843,36 @@ int parse_arguments(int *argc, const cha |
4c1f2ca5 WD |
81 | basis_dir[basis_dir_cnt++] = (char *)arg; |
82 | break; | |
00891c37 WD |
83 | |
84 | + case OPT_STOP_AT: | |
85 | + arg = poptGetOptArg(pc); | |
ed44ffcf | 86 | + if ((stop_at_utime = parse_time(arg)) == (time_t)-1) { |
00891c37 WD |
87 | + snprintf(err_buf, sizeof err_buf, |
88 | + "invalid --stop-at format: %s\n", | |
89 | + arg); | |
90 | + rprintf(FERROR, "ERROR: %s", err_buf); | |
91 | + return 0; | |
92 | + } | |
00891c37 WD |
93 | + if (stop_at_utime < time(NULL)) { |
94 | + snprintf(err_buf, sizeof err_buf, | |
95 | + "--stop-at time is in the past: %s\n", | |
96 | + arg); | |
97 | + rprintf(FERROR, "ERROR: %s", err_buf); | |
98 | + return 0; | |
99 | + } | |
100 | + break; | |
101 | + | |
102 | + case OPT_TIME_LIMIT: | |
103 | + arg = poptGetOptArg(pc); | |
104 | + if ((stop_at_utime = atol(arg) * 60) <= 0) { | |
105 | + snprintf(err_buf, sizeof err_buf, | |
106 | + "invalid --time-limit value: %s\n", | |
107 | + arg); | |
108 | + rprintf(FERROR, "ERROR: %s", err_buf); | |
109 | + return 0; | |
110 | + } | |
111 | + stop_at_utime += time(NULL); | |
112 | + break; | |
113 | + | |
114 | default: | |
115 | /* A large opt value means that set_refuse_options() | |
27a7053c | 116 | * turned this option off. */ |
159b101f | 117 | @@ -1283,6 +1319,15 @@ void server_options(char **args,int *arg |
7ce4946a WD |
118 | args[ac++] = arg; |
119 | } | |
c70898d5 | 120 | |
00891c37 WD |
121 | + if (stop_at_utime) { |
122 | + long mins = (stop_at_utime - time(NULL)) / 60; | |
123 | + if (mins <= 0) | |
124 | + mins = 1; | |
125 | + if (asprintf(&arg, "--time-limit=%ld", mins) < 0) | |
7ce4946a WD |
126 | + goto oom; |
127 | + args[ac++] = arg; | |
128 | + } | |
129 | + | |
130 | if (backup_dir) { | |
131 | args[ac++] = "--backup-dir"; | |
132 | args[ac++] = backup_dir; | |
159b101f | 133 | --- orig/rsync.yo 2005-02-15 19:27:05 |
a7219d20 | 134 | +++ rsync.yo 2005-02-01 10:46:35 |
159b101f | 135 | @@ -378,6 +378,8 @@ to the detailed description below for a |
79f132a1 | 136 | --password-file=FILE read password from FILE |
be73a66e | 137 | --list-only list the files instead of copying them |
79f132a1 | 138 | --bwlimit=KBPS limit I/O bandwidth; KBytes per second |
ed44ffcf WD |
139 | + --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute |
140 | + --time-limit=MINS Stop rsync after MINS minutes have elapsed | |
a7219d20 | 141 | --write-batch=FILE write a batched update to FILE |
79f132a1 WD |
142 | --read-batch=FILE read a batched update from FILE |
143 | --checksum-seed=NUM set block/file checksum seed (advanced) | |
159b101f | 144 | @@ -1182,6 +1184,19 @@ transfer was too fast, it will wait befo |
c70898d5 WD |
145 | result is an average transfer rate equaling the specified limit. A value |
146 | of zero specifies no limit. | |
7ce4946a | 147 | |
ed44ffcf WD |
148 | +dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what |
149 | +time to stop rsync, in year-month-dayThour:minute numeric format (e.g. | |
ab74148e | 150 | +2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also |
ed44ffcf WD |
151 | +leave off various items and the result will be the next possible time |
152 | +that matches the specified data. For example, "1-30" specifies the next | |
16711fbf WD |
153 | +January 30th (at midnight), "04:00" specifies the next 4am, "1" |
154 | +specifies the next 1st of the month at midnight, and ":59" specifies the | |
ab74148e WD |
155 | +next 59th minute after the hour. If you prefer, you may separate the |
156 | +date numbers using slashes instead of dashes. | |
00891c37 | 157 | + |
ed44ffcf | 158 | +dit(bf(--time-limit=MINS)) This option allows you to specify the maximum |
00891c37 | 159 | +number of minutes rsync will run for. |
7ce4946a | 160 | + |
9be39c35 | 161 | dit(bf(--write-batch=FILE)) Record a file that can later be applied to |
a7219d20 | 162 | another identical destination with bf(--read-batch). See the "BATCH MODE" |
9be39c35 | 163 | section for details. |
159b101f | 164 | --- orig/util.c 2005-02-14 02:45:11 |
13bed3dd | 165 | +++ util.c 2004-07-03 20:23:22 |
f635ed27 | 166 | @@ -126,6 +126,132 @@ void overflow(char *str) |
ed44ffcf WD |
167 | exit_cleanup(RERR_MALLOC); |
168 | } | |
169 | ||
170 | +/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while | |
171 | + * also allowing abbreviated data. For instance, if the time is omitted, | |
172 | + * it defaults to midnight. If the date is omitted, it defaults to the | |
173 | + * next possible date in the future with the specified time. Even the | |
174 | + * year or year-month can be omitted, again defaulting to the next date | |
175 | + * in the future that matches the specified information. A 2-digit year | |
176 | + * is also OK, as is using '/' instead of '-'. */ | |
177 | +time_t parse_time(const char *arg) | |
178 | +{ | |
179 | + const char *cp; | |
180 | + time_t val, now = time(NULL); | |
181 | + struct tm t, *today = localtime(&now); | |
16711fbf | 182 | + int in_date, n; |
ed44ffcf WD |
183 | + |
184 | + memset(&t, 0, sizeof t); | |
185 | + t.tm_year = t.tm_mon = t.tm_mday = -1; | |
186 | + t.tm_hour = t.tm_min = t.tm_isdst = -1; | |
16711fbf WD |
187 | + cp = arg; |
188 | + if (*cp == 'T' || *cp == 't' || *cp == ':') { | |
189 | + cp++; | |
190 | + in_date = 0; | |
191 | + } else | |
192 | + in_date = 1; | |
193 | + for ( ; ; cp++) { | |
194 | + if (!isdigit(*cp)) | |
ed44ffcf | 195 | + return -1; |
16711fbf WD |
196 | + |
197 | + n = 0; | |
198 | + do { | |
199 | + n = n * 10 + *cp++ - '0'; | |
200 | + } while (isdigit(*cp)); | |
201 | + | |
202 | + if (*cp == ':') | |
203 | + in_date = 0; | |
ed44ffcf | 204 | + if (in_date) { |
16711fbf WD |
205 | + if (t.tm_year != -1) |
206 | + return -1; | |
207 | + t.tm_year = t.tm_mon; | |
208 | + t.tm_mon = t.tm_mday; | |
209 | + t.tm_mday = n; | |
210 | + if (!*cp) | |
211 | + break; | |
212 | + if (*cp == 'T' || *cp == 't') { | |
213 | + if (!cp[1]) | |
ed44ffcf | 214 | + break; |
16711fbf WD |
215 | + in_date = 0; |
216 | + } else if (*cp != '-' && *cp != '/') | |
217 | + return -1; | |
218 | + continue; | |
ed44ffcf WD |
219 | + } |
220 | + if (t.tm_hour != -1) | |
221 | + return -1; | |
222 | + t.tm_hour = t.tm_min; | |
223 | + t.tm_min = n; | |
224 | + if (!*cp) | |
225 | + break; | |
226 | + if (*cp != ':') | |
227 | + return -1; | |
228 | + } | |
229 | + | |
230 | + in_date = 0; | |
231 | + if (t.tm_year < 0) { | |
232 | + t.tm_year = today->tm_year; | |
233 | + in_date = 1; | |
234 | + } else if (t.tm_year < 100) { | |
235 | + while (t.tm_year < today->tm_year) | |
236 | + t.tm_year += 100; | |
237 | + } else | |
238 | + t.tm_year -= 1900; | |
239 | + if (t.tm_mon < 0) { | |
240 | + t.tm_mon = today->tm_mon; | |
241 | + in_date = 2; | |
242 | + } else | |
243 | + t.tm_mon--; | |
244 | + if (t.tm_mday < 0) { | |
ed44ffcf WD |
245 | + t.tm_mday = today->tm_mday; |
246 | + in_date = 3; | |
247 | + } | |
248 | + | |
249 | + n = 0; | |
250 | + if (t.tm_min < 0) { | |
251 | + t.tm_hour = t.tm_min = 0; | |
252 | + } else if (t.tm_hour < 0) { | |
253 | + if (in_date != 3) | |
254 | + return -1; | |
255 | + in_date = 0; | |
256 | + t.tm_hour = today->tm_hour; | |
257 | + n = 60*60; | |
258 | + } | |
259 | + | |
16711fbf WD |
260 | + if (t.tm_hour > 23 || t.tm_min > 59 |
261 | + || t.tm_mon < 0 || t.tm_mon >= 12 | |
262 | + || t.tm_mday < 1 || t.tm_mday > 31 | |
263 | + || (val = mktime(&t)) == (time_t)-1) | |
ed44ffcf WD |
264 | + return -1; |
265 | + | |
266 | + if (val <= now && in_date) { | |
267 | + tweak_date: | |
268 | + switch (in_date) { | |
269 | + case 3: | |
270 | + t.tm_mday++; | |
271 | + break; | |
272 | + case 2: | |
273 | + if (++t.tm_mon == 12) | |
274 | + t.tm_mon = 0; | |
275 | + else | |
276 | + break; | |
277 | + case 1: | |
278 | + t.tm_year++; | |
279 | + break; | |
280 | + } | |
281 | + if ((val = mktime(&t)) == (time_t)-1) { | |
282 | + if (in_date == 3 && t.tm_mday > 28) { | |
283 | + t.tm_mday = 1; | |
284 | + in_date = 2; | |
285 | + goto tweak_date; | |
286 | + } | |
287 | + return -1; | |
288 | + } | |
289 | + } | |
290 | + if (n) { | |
291 | + while (val <= now) | |
292 | + val += n; | |
293 | + } | |
294 | + return val; | |
295 | +} | |
296 | ||
297 | ||
298 | int set_modtime(char *fname, time_t modtime) |