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 diff --git a/io.c b/io.c
15 @@ -54,6 +54,7 @@ extern int protocol_version;
16 extern int remove_source_files;
17 extern int preserve_hard_links;
18 extern struct stats stats;
19 +extern time_t stop_at_utime;
20 extern struct file_list *cur_flist;
22 extern int filesfrom_convert;
23 @@ -221,16 +222,24 @@ static void check_timeout(void)
27 + if ((!io_timeout || ignore_timeout) && !stop_at_utime)
32 + if (stop_at_utime && t >= stop_at_utime) {
33 + rprintf(FERROR, "run-time limit exceeded\n");
34 + exit_cleanup(RERR_TIMEOUT);
37 if (!io_timeout || ignore_timeout)
41 - last_io_in = time(NULL);
48 if (t - last_io_in >= io_timeout) {
49 if (!am_server && !am_daemon) {
50 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
51 diff --git a/options.c b/options.c
54 @@ -113,6 +113,7 @@ size_t bwlimit_writemax = 0;
55 int ignore_existing = 0;
56 int ignore_non_existing = 0;
57 int need_messages_from_generator = 0;
58 +time_t stop_at_utime = 0;
59 int max_delete = INT_MIN;
62 @@ -424,6 +425,8 @@ void usage(enum logcode F)
63 rprintf(F," --password-file=FILE read daemon-access password from FILE\n");
64 rprintf(F," --list-only list the files instead of copying them\n");
65 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
66 + rprintf(F," --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute\n");
67 + rprintf(F," --time-limit=MINS Stop rsync after MINS minutes have elapsed\n");
68 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
69 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
70 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
71 @@ -446,7 +449,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
72 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
73 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
74 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
75 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV,
76 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_STOP_AT, OPT_TIME_LIMIT,
77 OPT_SERVER, OPT_REFUSED_BASE = 9000};
79 static struct poptOption long_options[] = {
80 @@ -626,6 +629,8 @@ static struct poptOption long_options[] = {
81 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
82 {"contimeout", 0, POPT_ARG_INT, &connect_timeout, 0, 0, 0 },
83 {"no-contimeout", 0, POPT_ARG_VAL, &connect_timeout, 0, 0, 0 },
84 + {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
85 + {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
86 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
87 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
88 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
89 @@ -1260,6 +1265,36 @@ int parse_arguments(int *argc_p, const char ***argv_p)
94 + arg = poptGetOptArg(pc);
95 + if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
96 + snprintf(err_buf, sizeof err_buf,
97 + "invalid --stop-at format: %s\n",
99 + rprintf(FERROR, "ERROR: %s", err_buf);
102 + if (stop_at_utime < time(NULL)) {
103 + snprintf(err_buf, sizeof err_buf,
104 + "--stop-at time is in the past: %s\n",
106 + rprintf(FERROR, "ERROR: %s", err_buf);
111 + case OPT_TIME_LIMIT:
112 + arg = poptGetOptArg(pc);
113 + if ((stop_at_utime = atol(arg) * 60) <= 0) {
114 + snprintf(err_buf, sizeof err_buf,
115 + "invalid --time-limit value: %s\n",
117 + rprintf(FERROR, "ERROR: %s", err_buf);
120 + stop_at_utime += time(NULL);
124 /* A large opt value means that set_refuse_options()
125 * turned this option off. */
126 @@ -1897,6 +1932,15 @@ void server_options(char **args, int *argc_p)
130 + if (stop_at_utime) {
131 + long mins = (stop_at_utime - time(NULL)) / 60;
134 + if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
140 args[ac++] = "--backup-dir";
141 args[ac++] = backup_dir;
142 diff --git a/rsync.yo b/rsync.yo
145 @@ -418,6 +418,8 @@ to the detailed description below for a complete description. verb(
146 --password-file=FILE read daemon-access password from FILE
147 --list-only list the files instead of copying them
148 --bwlimit=KBPS limit I/O bandwidth; KBytes per second
149 + --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute
150 + --time-limit=MINS Stop rsync after MINS minutes have elapsed
151 --write-batch=FILE write a batched update to FILE
152 --only-write-batch=FILE like --write-batch but w/o updating dest
153 --read-batch=FILE read a batched update from FILE
154 @@ -2013,6 +2015,19 @@ transfer was too fast, it will wait before sending the next data block. The
155 result is an average transfer rate equaling the specified limit. A value
156 of zero specifies no limit.
158 +dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what
159 +time to stop rsync, in year-month-dayThour:minute numeric format (e.g.
160 +2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also
161 +leave off various items and the result will be the next possible time
162 +that matches the specified data. For example, "1-30" specifies the next
163 +January 30th (at midnight), "04:00" specifies the next 4am, "1"
164 +specifies the next 1st of the month at midnight, and ":59" specifies the
165 +next 59th minute after the hour. If you prefer, you may separate the
166 +date numbers using slashes instead of dashes.
168 +dit(bf(--time-limit=MINS)) This option allows you to specify the maximum
169 +number of minutes rsync will run for.
171 dit(bf(--write-batch=FILE)) Record a file that can later be applied to
172 another identical destination with bf(--read-batch). See the "BATCH MODE"
173 section for details, and also the bf(--only-write-batch) option.
174 diff --git a/util.c b/util.c
177 @@ -123,6 +123,133 @@ NORETURN void overflow_exit(const char *str)
178 exit_cleanup(RERR_MALLOC);
181 +/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
182 + * also allowing abbreviated data. For instance, if the time is omitted,
183 + * it defaults to midnight. If the date is omitted, it defaults to the
184 + * next possible date in the future with the specified time. Even the
185 + * year or year-month can be omitted, again defaulting to the next date
186 + * in the future that matches the specified information. A 2-digit year
187 + * is also OK, as is using '/' instead of '-'. */
188 +time_t parse_time(const char *arg)
191 + time_t val, now = time(NULL);
192 + struct tm t, *today = localtime(&now);
195 + memset(&t, 0, sizeof t);
196 + t.tm_year = t.tm_mon = t.tm_mday = -1;
197 + t.tm_hour = t.tm_min = t.tm_isdst = -1;
199 + if (*cp == 'T' || *cp == 't' || *cp == ':') {
210 + n = n * 10 + *cp++ - '0';
211 + } while (isDigit(cp));
216 + if (t.tm_year != -1)
218 + t.tm_year = t.tm_mon;
219 + t.tm_mon = t.tm_mday;
223 + if (*cp == 'T' || *cp == 't') {
227 + } else if (*cp != '-' && *cp != '/')
231 + if (t.tm_hour != -1)
233 + t.tm_hour = t.tm_min;
242 + if (t.tm_year < 0) {
243 + t.tm_year = today->tm_year;
245 + } else if (t.tm_year < 100) {
246 + while (t.tm_year < today->tm_year)
250 + if (t.tm_mon < 0) {
251 + t.tm_mon = today->tm_mon;
255 + if (t.tm_mday < 0) {
256 + t.tm_mday = today->tm_mday;
261 + if (t.tm_min < 0) {
262 + t.tm_hour = t.tm_min = 0;
263 + } else if (t.tm_hour < 0) {
267 + t.tm_hour = today->tm_hour;
271 + if (t.tm_hour > 23 || t.tm_min > 59
272 + || t.tm_mon < 0 || t.tm_mon >= 12
273 + || t.tm_mday < 1 || t.tm_mday > 31
274 + || (val = mktime(&t)) == (time_t)-1)
277 + if (val <= now && in_date) {
284 + if (++t.tm_mon == 12)
292 + if ((val = mktime(&t)) == (time_t)-1) {
293 + if (in_date == 3 && t.tm_mday > 28) {
308 int set_modtime(const char *fname, time_t modtime, mode_t mode)
310 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES