1 John Taylor's patch for implementing --time-limit and --stop-at, reworked
2 to be simpler and more efficient by Wayne Davison.
4 Do we need configure support for mktime()?
6 To use this patch, run these commands for a successful build:
8 patch -p1 <patches/time-limit.diff
9 ./configure (optional if already run)
12 based-on: 194cee671d5e178f20c4494f41911fa8db942935
13 diff --git a/io.c b/io.c
16 @@ -60,6 +60,7 @@ extern int preserve_hard_links;
17 extern BOOL extra_flist_sending_enabled;
18 extern BOOL flush_ok_after_signal;
19 extern struct stats stats;
20 +extern time_t stop_at_utime;
21 extern struct file_list *cur_flist;
23 extern int filesfrom_convert;
24 @@ -170,11 +171,19 @@ static void check_timeout(BOOL allow_keepalive, int keepalive_flags)
25 * generator might be blocked trying to send checksums, it needs to
26 * know that the receiver is active). Thus, as long as one or the
27 * other is successfully doing work, the generator will not timeout. */
29 + if (!io_timeout && !stop_at_utime)
34 + if (stop_at_utime && t >= stop_at_utime) {
35 + rprintf(FERROR, "run-time limit exceeded\n");
36 + exit_cleanup(RERR_TIMEOUT);
42 if (allow_keepalive) {
43 /* This may put data into iobuf.msg w/o flushing. */
44 maybe_send_keepalive(t, keepalive_flags);
45 diff --git a/options.c b/options.c
48 @@ -119,6 +119,7 @@ size_t bwlimit_writemax = 0;
49 int ignore_existing = 0;
50 int ignore_non_existing = 0;
51 int need_messages_from_generator = 0;
52 +time_t stop_at_utime = 0;
53 int max_delete = INT_MIN;
56 @@ -779,6 +780,7 @@ enum {OPT_SERVER = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
57 OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG,
58 OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT,
59 OPT_OLD_COMPRESS, OPT_NEW_COMPRESS, OPT_NO_COMPRESS,
60 + OPT_STOP_AT, OPT_TIME_LIMIT,
61 OPT_REFUSED_BASE = 9000};
63 static struct poptOption long_options[] = {
64 @@ -988,6 +990,8 @@ static struct poptOption long_options[] = {
65 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
66 {"contimeout", 0, POPT_ARG_INT, &connect_timeout, 0, 0, 0 },
67 {"no-contimeout", 0, POPT_ARG_VAL, &connect_timeout, 0, 0, 0 },
68 + {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
69 + {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
70 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
71 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
72 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
73 @@ -1863,6 +1867,36 @@ int parse_arguments(int *argc_p, const char ***argv_p)
78 + arg = poptGetOptArg(pc);
79 + if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
80 + snprintf(err_buf, sizeof err_buf,
81 + "invalid --stop-at format: %s\n",
83 + rprintf(FERROR, "ERROR: %s", err_buf);
86 + if (stop_at_utime < time(NULL)) {
87 + snprintf(err_buf, sizeof err_buf,
88 + "--stop-at time is in the past: %s\n",
90 + rprintf(FERROR, "ERROR: %s", err_buf);
95 + case OPT_TIME_LIMIT:
96 + arg = poptGetOptArg(pc);
97 + if ((stop_at_utime = atol(arg) * 60) <= 0) {
98 + snprintf(err_buf, sizeof err_buf,
99 + "invalid --time-limit value: %s\n",
101 + rprintf(FERROR, "ERROR: %s", err_buf);
104 + stop_at_utime += time(NULL);
108 /* A large opt value means that set_refuse_options()
109 * turned this option off. */
110 @@ -2691,6 +2725,15 @@ void server_options(char **args, int *argc_p)
114 + if (stop_at_utime) {
115 + long mins = (stop_at_utime - time(NULL)) / 60;
118 + if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
124 args[ac++] = "--backup-dir";
125 args[ac++] = backup_dir;
126 diff --git a/rsync.1.md b/rsync.1.md
129 @@ -457,6 +457,8 @@ detailed description below for a complete description.
130 --early-input=FILE use FILE for daemon's early exec input
131 --list-only list the files instead of copying them
132 --bwlimit=RATE limit socket I/O bandwidth
133 +--stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute
134 +--time-limit=MINS Stop rsync after MINS minutes have elapsed
135 --write-batch=FILE write a batched update to FILE
136 --only-write-batch=FILE like --write-batch but w/o updating dest
137 --read-batch=FILE read a batched update from FILE
138 @@ -3107,6 +3109,22 @@ your home directory (remove the '=' for that).
139 buffered, while other can show up as very slow when the flushing of the
140 output buffer occurs. This may be fixed in a future version.
142 +0. `--stop-at=y-m-dTh:m
144 + This option allows you to specify at what time to stop rsync, in
145 + year-month-dayThour:minute numeric format (e.g. 2004-12-31T23:59). You
146 + can specify a 2 or 4-digit year. You can also leave off various items and
147 + the result will be the next possible time that matches the specified data.
148 + For example, "1-30" specifies the next January 30th (at midnight), "04:00"
149 + specifies the next 4am, "1" specifies the next 1st of the month at
150 + midnight, and ":59" specifies the next 59th minute after the hour. If you
151 + prefer, you may separate the date numbers using slashes instead of dashes.
153 +0. `--time-limit=MINS
155 + This option allows you to specify the maximum number of minutes rsync will
158 0. `--write-batch=FILE`
160 Record a file that can later be applied to another identical destination
161 diff --git a/util.c b/util.c
164 @@ -115,6 +115,133 @@ void print_child_argv(const char *prefix, char **cmd)
165 rprintf(FCLIENT, " (%d args)\n", cnt);
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)
178 + time_t val, now = time(NULL);
179 + struct tm t, *today = localtime(&now);
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;
186 + if (*cp == 'T' || *cp == 't' || *cp == ':') {
197 + n = n * 10 + *cp++ - '0';
198 + } while (isDigit(cp));
203 + if (t.tm_year != -1)
205 + t.tm_year = t.tm_mon;
206 + t.tm_mon = t.tm_mday;
210 + if (*cp == 'T' || *cp == 't') {
214 + } else if (*cp != '-' && *cp != '/')
218 + if (t.tm_hour != -1)
220 + t.tm_hour = t.tm_min;
229 + if (t.tm_year < 0) {
230 + t.tm_year = today->tm_year;
232 + } else if (t.tm_year < 100) {
233 + while (t.tm_year < today->tm_year)
237 + if (t.tm_mon < 0) {
238 + t.tm_mon = today->tm_mon;
242 + if (t.tm_mday < 0) {
243 + t.tm_mday = today->tm_mday;
248 + if (t.tm_min < 0) {
249 + t.tm_hour = t.tm_min = 0;
250 + } else if (t.tm_hour < 0) {
254 + t.tm_hour = today->tm_hour;
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)
264 + if (val <= now && in_date) {
271 + if (++t.tm_mon == 12)
279 + if ((val = mktime(&t)) == (time_t)-1) {
280 + if (in_date == 3 && t.tm_mday > 28) {
295 /* This returns 0 for success, 1 for a symlink if symlink time-setting
296 * is not possible, or -1 for any other error. */
297 int set_times(const char *fname, STRUCT_STAT *stp)