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()?
8 @@ -57,6 +57,7 @@ extern int remove_sent_files;
9 extern int preserve_hard_links;
10 extern char *filesfrom_host;
11 extern struct stats stats;
12 +extern time_t stop_at_utime;
13 extern struct file_list *the_file_list;
15 const char phase_unknown[] = "unknown";
16 @@ -168,16 +169,24 @@ static void check_timeout(void)
20 + if ((!io_timeout || ignore_timeout) && !stop_at_utime)
25 + if (stop_at_utime && t >= stop_at_utime) {
26 + rprintf(FERROR, "run-time limit exceeded\n");
27 + exit_cleanup(RERR_TIMEOUT);
30 if (!io_timeout || ignore_timeout)
34 - last_io_in = time(NULL);
41 if (t - last_io_in >= io_timeout) {
42 if (!am_server && !am_daemon) {
43 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
46 @@ -116,6 +116,7 @@ int checksum_seed = 0;
48 int delay_updates = 0;
49 long block_size = 0; /* "long" because popt can't set an int32. */
50 +time_t stop_at_utime = 0;
53 /** Network address family. **/
54 @@ -365,6 +366,8 @@ void usage(enum logcode F)
55 rprintf(F," --password-file=FILE read password from FILE\n");
56 rprintf(F," --list-only list the files instead of copying them\n");
57 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
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");
60 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
61 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
62 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
63 @@ -385,7 +388,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
64 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
65 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
66 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
68 + OPT_NO_D, OPT_STOP_AT, OPT_TIME_LIMIT,
69 OPT_SERVER, OPT_REFUSED_BASE = 9000};
71 static struct poptOption long_options[] = {
72 @@ -494,6 +497,8 @@ static struct poptOption long_options[]
73 {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 },
74 {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 },
75 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
76 + {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
77 + {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
78 {"backup", 'b', POPT_ARG_NONE, &make_backups, 0, 0, 0 },
79 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
80 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
81 @@ -1060,6 +1065,36 @@ int parse_arguments(int *argc, const cha
86 + arg = poptGetOptArg(pc);
87 + if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
88 + snprintf(err_buf, sizeof err_buf,
89 + "invalid --stop-at format: %s\n",
91 + rprintf(FERROR, "ERROR: %s", err_buf);
94 + if (stop_at_utime < time(NULL)) {
95 + snprintf(err_buf, sizeof err_buf,
96 + "--stop-at time is in the past: %s\n",
98 + rprintf(FERROR, "ERROR: %s", err_buf);
103 + case OPT_TIME_LIMIT:
104 + arg = poptGetOptArg(pc);
105 + if ((stop_at_utime = atol(arg) * 60) <= 0) {
106 + snprintf(err_buf, sizeof err_buf,
107 + "invalid --time-limit value: %s\n",
109 + rprintf(FERROR, "ERROR: %s", err_buf);
112 + stop_at_utime += time(NULL);
116 /* A large opt value means that set_refuse_options()
117 * turned this option off. */
118 @@ -1610,6 +1645,15 @@ void server_options(char **args,int *arg
122 + if (stop_at_utime) {
123 + long mins = (stop_at_utime - time(NULL)) / 60;
126 + if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
132 args[ac++] = "--backup-dir";
133 args[ac++] = backup_dir;
136 @@ -388,6 +388,8 @@ to the detailed description below for a
137 --password-file=FILE read password from FILE
138 --list-only list the files instead of copying them
139 --bwlimit=KBPS limit I/O bandwidth; KBytes per second
140 + --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute
141 + --time-limit=MINS Stop rsync after MINS minutes have elapsed
142 --write-batch=FILE write a batched update to FILE
143 --only-write-batch=FILE like --write-batch but w/o updating dest
144 --read-batch=FILE read a batched update from FILE
145 @@ -1584,6 +1586,19 @@ transfer was too fast, it will wait befo
146 result is an average transfer rate equaling the specified limit. A value
147 of zero specifies no limit.
149 +dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what
150 +time to stop rsync, in year-month-dayThour:minute numeric format (e.g.
151 +2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also
152 +leave off various items and the result will be the next possible time
153 +that matches the specified data. For example, "1-30" specifies the next
154 +January 30th (at midnight), "04:00" specifies the next 4am, "1"
155 +specifies the next 1st of the month at midnight, and ":59" specifies the
156 +next 59th minute after the hour. If you prefer, you may separate the
157 +date numbers using slashes instead of dashes.
159 +dit(bf(--time-limit=MINS)) This option allows you to specify the maximum
160 +number of minutes rsync will run for.
162 dit(bf(--write-batch=FILE)) Record a file that can later be applied to
163 another identical destination with bf(--read-batch). See the "BATCH MODE"
164 section for details, and also the bf(--only-write-batch) option.
167 @@ -125,6 +125,133 @@ void overflow_exit(char *str)
168 exit_cleanup(RERR_MALLOC);
171 +/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
172 + * also allowing abbreviated data. For instance, if the time is omitted,
173 + * it defaults to midnight. If the date is omitted, it defaults to the
174 + * next possible date in the future with the specified time. Even the
175 + * year or year-month can be omitted, again defaulting to the next date
176 + * in the future that matches the specified information. A 2-digit year
177 + * is also OK, as is using '/' instead of '-'. */
178 +time_t parse_time(const char *arg)
181 + time_t val, now = time(NULL);
182 + struct tm t, *today = localtime(&now);
185 + memset(&t, 0, sizeof t);
186 + t.tm_year = t.tm_mon = t.tm_mday = -1;
187 + t.tm_hour = t.tm_min = t.tm_isdst = -1;
189 + if (*cp == 'T' || *cp == 't' || *cp == ':') {
200 + n = n * 10 + *cp++ - '0';
201 + } while (isdigit(*cp));
206 + if (t.tm_year != -1)
208 + t.tm_year = t.tm_mon;
209 + t.tm_mon = t.tm_mday;
213 + if (*cp == 'T' || *cp == 't') {
217 + } else if (*cp != '-' && *cp != '/')
221 + if (t.tm_hour != -1)
223 + t.tm_hour = t.tm_min;
232 + if (t.tm_year < 0) {
233 + t.tm_year = today->tm_year;
235 + } else if (t.tm_year < 100) {
236 + while (t.tm_year < today->tm_year)
240 + if (t.tm_mon < 0) {
241 + t.tm_mon = today->tm_mon;
245 + if (t.tm_mday < 0) {
246 + t.tm_mday = today->tm_mday;
251 + if (t.tm_min < 0) {
252 + t.tm_hour = t.tm_min = 0;
253 + } else if (t.tm_hour < 0) {
257 + t.tm_hour = today->tm_hour;
261 + if (t.tm_hour > 23 || t.tm_min > 59
262 + || t.tm_mon < 0 || t.tm_mon >= 12
263 + || t.tm_mday < 1 || t.tm_mday > 31
264 + || (val = mktime(&t)) == (time_t)-1)
267 + if (val <= now && in_date) {
274 + if (++t.tm_mon == 12)
282 + if ((val = mktime(&t)) == (time_t)-1) {
283 + if (in_date == 3 && t.tm_mday > 28) {
298 int set_modtime(char *fname, time_t modtime, mode_t mode)
300 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES