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)
14 @@ -50,6 +50,7 @@ extern int remove_source_files;
15 extern int preserve_hard_links;
16 extern char *filesfrom_host;
17 extern struct stats stats;
18 +extern time_t stop_at_utime;
19 extern struct file_list *the_file_list;
21 const char phase_unknown[] = "unknown";
22 @@ -147,16 +148,24 @@ static void check_timeout(void)
26 + if ((!io_timeout || ignore_timeout) && !stop_at_utime)
31 + if (stop_at_utime && t >= stop_at_utime) {
32 + rprintf(FERROR, "run-time limit exceeded\n");
33 + exit_cleanup(RERR_TIMEOUT);
36 if (!io_timeout || ignore_timeout)
40 - last_io_in = time(NULL);
47 if (t - last_io_in >= io_timeout) {
48 if (!am_server && !am_daemon) {
49 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
52 @@ -116,6 +116,7 @@ int checksum_seed = 0;
54 int delay_updates = 0;
55 long block_size = 0; /* "long" because popt can't set an int32. */
56 +time_t stop_at_utime = 0;
59 /** Network address family. **/
60 @@ -377,6 +378,8 @@ void usage(enum logcode F)
61 rprintf(F," --password-file=FILE read password from FILE\n");
62 rprintf(F," --list-only list the files instead of copying them\n");
63 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
64 + rprintf(F," --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute\n");
65 + rprintf(F," --time-limit=MINS Stop rsync after MINS minutes have elapsed\n");
66 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
67 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
68 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
69 @@ -398,7 +401,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
70 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
71 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
72 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
74 + OPT_NO_D, OPT_STOP_AT, OPT_TIME_LIMIT,
75 OPT_SERVER, OPT_REFUSED_BASE = 9000};
77 static struct poptOption long_options[] = {
78 @@ -516,6 +519,8 @@ static struct poptOption long_options[]
79 {"log-format", 0, POPT_ARG_STRING, &stdout_format, 0, 0, 0 }, /* DEPRECATED */
80 {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 },
81 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
82 + {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
83 + {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
84 {"backup", 'b', POPT_ARG_NONE, &make_backups, 0, 0, 0 },
85 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
86 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
87 @@ -1089,6 +1094,36 @@ int parse_arguments(int *argc, const cha
92 + arg = poptGetOptArg(pc);
93 + if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
94 + snprintf(err_buf, sizeof err_buf,
95 + "invalid --stop-at format: %s\n",
97 + rprintf(FERROR, "ERROR: %s", err_buf);
100 + if (stop_at_utime < time(NULL)) {
101 + snprintf(err_buf, sizeof err_buf,
102 + "--stop-at time is in the past: %s\n",
104 + rprintf(FERROR, "ERROR: %s", err_buf);
109 + case OPT_TIME_LIMIT:
110 + arg = poptGetOptArg(pc);
111 + if ((stop_at_utime = atol(arg) * 60) <= 0) {
112 + snprintf(err_buf, sizeof err_buf,
113 + "invalid --time-limit value: %s\n",
115 + rprintf(FERROR, "ERROR: %s", err_buf);
118 + stop_at_utime += time(NULL);
122 /* A large opt value means that set_refuse_options()
123 * turned this option off. */
124 @@ -1642,6 +1677,15 @@ void server_options(char **args,int *arg
128 + if (stop_at_utime) {
129 + long mins = (stop_at_utime - time(NULL)) / 60;
132 + if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
138 args[ac++] = "--backup-dir";
139 args[ac++] = backup_dir;
142 @@ -394,6 +394,8 @@ to the detailed description below for a
143 --password-file=FILE read password from FILE
144 --list-only list the files instead of copying them
145 --bwlimit=KBPS limit I/O bandwidth; KBytes per second
146 + --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute
147 + --time-limit=MINS Stop rsync after MINS minutes have elapsed
148 --write-batch=FILE write a batched update to FILE
149 --only-write-batch=FILE like --write-batch but w/o updating dest
150 --read-batch=FILE read a batched update from FILE
151 @@ -1733,6 +1735,19 @@ transfer was too fast, it will wait befo
152 result is an average transfer rate equaling the specified limit. A value
153 of zero specifies no limit.
155 +dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what
156 +time to stop rsync, in year-month-dayThour:minute numeric format (e.g.
157 +2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also
158 +leave off various items and the result will be the next possible time
159 +that matches the specified data. For example, "1-30" specifies the next
160 +January 30th (at midnight), "04:00" specifies the next 4am, "1"
161 +specifies the next 1st of the month at midnight, and ":59" specifies the
162 +next 59th minute after the hour. If you prefer, you may separate the
163 +date numbers using slashes instead of dashes.
165 +dit(bf(--time-limit=MINS)) This option allows you to specify the maximum
166 +number of minutes rsync will run for.
168 dit(bf(--write-batch=FILE)) Record a file that can later be applied to
169 another identical destination with bf(--read-batch). See the "BATCH MODE"
170 section for details, and also the bf(--only-write-batch) option.
173 @@ -121,6 +121,133 @@ NORETURN void overflow_exit(char *str)
174 exit_cleanup(RERR_MALLOC);
177 +/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
178 + * also allowing abbreviated data. For instance, if the time is omitted,
179 + * it defaults to midnight. If the date is omitted, it defaults to the
180 + * next possible date in the future with the specified time. Even the
181 + * year or year-month can be omitted, again defaulting to the next date
182 + * in the future that matches the specified information. A 2-digit year
183 + * is also OK, as is using '/' instead of '-'. */
184 +time_t parse_time(const char *arg)
187 + time_t val, now = time(NULL);
188 + struct tm t, *today = localtime(&now);
191 + memset(&t, 0, sizeof t);
192 + t.tm_year = t.tm_mon = t.tm_mday = -1;
193 + t.tm_hour = t.tm_min = t.tm_isdst = -1;
195 + if (*cp == 'T' || *cp == 't' || *cp == ':') {
206 + n = n * 10 + *cp++ - '0';
207 + } while (isdigit(*cp));
212 + if (t.tm_year != -1)
214 + t.tm_year = t.tm_mon;
215 + t.tm_mon = t.tm_mday;
219 + if (*cp == 'T' || *cp == 't') {
223 + } else if (*cp != '-' && *cp != '/')
227 + if (t.tm_hour != -1)
229 + t.tm_hour = t.tm_min;
238 + if (t.tm_year < 0) {
239 + t.tm_year = today->tm_year;
241 + } else if (t.tm_year < 100) {
242 + while (t.tm_year < today->tm_year)
246 + if (t.tm_mon < 0) {
247 + t.tm_mon = today->tm_mon;
251 + if (t.tm_mday < 0) {
252 + t.tm_mday = today->tm_mday;
257 + if (t.tm_min < 0) {
258 + t.tm_hour = t.tm_min = 0;
259 + } else if (t.tm_hour < 0) {
263 + t.tm_hour = today->tm_hour;
267 + if (t.tm_hour > 23 || t.tm_min > 59
268 + || t.tm_mon < 0 || t.tm_mon >= 12
269 + || t.tm_mday < 1 || t.tm_mday > 31
270 + || (val = mktime(&t)) == (time_t)-1)
273 + if (val <= now && in_date) {
280 + if (++t.tm_mon == 12)
288 + if ((val = mktime(&t)) == (time_t)-1) {
289 + if (in_date == 3 && t.tm_mday > 28) {
304 int set_modtime(char *fname, time_t modtime, mode_t mode)
306 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES