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