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 --- orig/io.c 2005-01-01 21:11:00
7 +++ io.c 2004-07-17 15:28:36
8 @@ -50,6 +50,7 @@ extern int am_generator;
10 extern int checksum_seed;
11 extern int protocol_version;
12 +extern time_t stop_at_utime;
13 extern char *remote_filesfrom_file;
14 extern struct stats stats;
16 @@ -136,17 +137,22 @@ static void check_timeout(void)
21 + if (!io_timeout && !stop_at_utime)
26 + if (stop_at_utime && t >= stop_at_utime) {
27 + rprintf(FERROR, "run-time limit exceeded\n");
28 + exit_cleanup(RERR_TIMEOUT);
32 - last_io = time(NULL);
39 - if (t - last_io >= io_timeout) {
40 + if (io_timeout && t - last_io >= io_timeout) {
41 if (!am_server && !am_daemon) {
42 rprintf(FERROR, "io timeout after %d seconds - exiting\n",
44 --- orig/options.c 2005-01-01 21:11:00
45 +++ options.c 2005-01-01 21:19:56
46 @@ -98,6 +98,7 @@ int blocking_io = -1;
47 int checksum_seed = 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 @@ -306,6 +307,8 @@ void usage(enum logcode F)
55 rprintf(F," --log-format=FORMAT log file transfers using specified format\n");
56 rprintf(F," --password-file=FILE get password from FILE\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 batch to FILE\n");
61 rprintf(F," --read-batch=FILE read a batch from FILE\n");
63 @@ -324,6 +327,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
64 OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST,
65 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
66 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_TIMEOUT, OPT_MAX_SIZE,
67 + OPT_STOP_AT, OPT_TIME_LIMIT,
68 OPT_REFUSED_BASE = 9000};
70 static struct poptOption long_options[] = {
71 @@ -397,6 +401,8 @@ static struct poptOption long_options[]
72 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
73 {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 },
74 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
75 + {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
76 + {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
77 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
78 {"hard-links", 'H', POPT_ARG_NONE, &preserve_hard_links, 0, 0, 0 },
79 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
80 @@ -750,6 +756,36 @@ int parse_arguments(int *argc, const cha
81 basis_dir[basis_dir_cnt++] = (char *)arg;
85 + arg = poptGetOptArg(pc);
86 + if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
87 + snprintf(err_buf, sizeof err_buf,
88 + "invalid --stop-at format: %s\n",
90 + rprintf(FERROR, "ERROR: %s", err_buf);
93 + if (stop_at_utime < time(NULL)) {
94 + snprintf(err_buf, sizeof err_buf,
95 + "--stop-at time is in the past: %s\n",
97 + rprintf(FERROR, "ERROR: %s", err_buf);
102 + case OPT_TIME_LIMIT:
103 + arg = poptGetOptArg(pc);
104 + if ((stop_at_utime = atol(arg) * 60) <= 0) {
105 + snprintf(err_buf, sizeof err_buf,
106 + "invalid --time-limit value: %s\n",
108 + rprintf(FERROR, "ERROR: %s", err_buf);
111 + stop_at_utime += time(NULL);
115 /* A large opt value means that set_refuse_options()
116 * turned this option off (opt-BASE is its index). */
117 @@ -1141,6 +1177,15 @@ void server_options(char **args,int *arg
121 + if (stop_at_utime) {
122 + long mins = (stop_at_utime - time(NULL)) / 60;
125 + if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
131 args[ac++] = "--backup-dir";
132 args[ac++] = backup_dir;
133 --- orig/rsync.yo 2005-01-01 21:11:01
134 +++ rsync.yo 2004-07-15 02:44:40
135 @@ -376,6 +376,8 @@ verb(
136 --log-format=FORMAT log file transfers using specified format
137 --password-file=FILE get password from FILE
138 --bwlimit=KBPS limit I/O bandwidth, KBytes per second
139 + --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute
140 + --time-limit=MINS Stop rsync after MINS minutes have elapsed
141 --write-batch=FILE write a batch to FILE
142 --read-batch=FILE read a batch from FILE
143 --checksum-seed=NUM set block/file checksum seed
144 @@ -1014,6 +1016,19 @@ transfer was too fast, it will wait befo
145 result is an average transfer rate equaling the specified limit. A value
146 of zero specifies no limit.
148 +dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what
149 +time to stop rsync, in year-month-dayThour:minute numeric format (e.g.
150 +2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also
151 +leave off various items and the result will be the next possible time
152 +that matches the specified data. For example, "1-30" specifies the next
153 +January 30th (at midnight), "04:00" specifies the next 4am, "1"
154 +specifies the next 1st of the month at midnight, and ":59" specifies the
155 +next 59th minute after the hour. If you prefer, you may separate the
156 +date numbers using slashes instead of dashes.
158 +dit(bf(--time-limit=MINS)) This option allows you to specify the maximum
159 +number of minutes rsync will run for.
161 dit(bf(--write-batch=FILE)) Record a file that can later be applied to
162 another identical destination with --read-batch. See the "BATCH MODE"
164 --- orig/util.c 2004-09-07 21:45:30
165 +++ util.c 2004-07-03 20:23:22
166 @@ -126,6 +126,132 @@ void overflow(char *str)
167 exit_cleanup(RERR_MALLOC);
170 +/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
171 + * also allowing abbreviated data. For instance, if the time is omitted,
172 + * it defaults to midnight. If the date is omitted, it defaults to the
173 + * next possible date in the future with the specified time. Even the
174 + * year or year-month can be omitted, again defaulting to the next date
175 + * in the future that matches the specified information. A 2-digit year
176 + * is also OK, as is using '/' instead of '-'. */
177 +time_t parse_time(const char *arg)
180 + time_t val, now = time(NULL);
181 + struct tm t, *today = localtime(&now);
184 + memset(&t, 0, sizeof t);
185 + t.tm_year = t.tm_mon = t.tm_mday = -1;
186 + t.tm_hour = t.tm_min = t.tm_isdst = -1;
188 + if (*cp == 'T' || *cp == 't' || *cp == ':') {
199 + n = n * 10 + *cp++ - '0';
200 + } while (isdigit(*cp));
205 + if (t.tm_year != -1)
207 + t.tm_year = t.tm_mon;
208 + t.tm_mon = t.tm_mday;
212 + if (*cp == 'T' || *cp == 't') {
216 + } else if (*cp != '-' && *cp != '/')
220 + if (t.tm_hour != -1)
222 + t.tm_hour = t.tm_min;
231 + if (t.tm_year < 0) {
232 + t.tm_year = today->tm_year;
234 + } else if (t.tm_year < 100) {
235 + while (t.tm_year < today->tm_year)
239 + if (t.tm_mon < 0) {
240 + t.tm_mon = today->tm_mon;
244 + if (t.tm_mday < 0) {
245 + t.tm_mday = today->tm_mday;
250 + if (t.tm_min < 0) {
251 + t.tm_hour = t.tm_min = 0;
252 + } else if (t.tm_hour < 0) {
256 + t.tm_hour = today->tm_hour;
260 + if (t.tm_hour > 23 || t.tm_min > 59
261 + || t.tm_mon < 0 || t.tm_mon >= 12
262 + || t.tm_mday < 1 || t.tm_mday > 31
263 + || (val = mktime(&t)) == (time_t)-1)
266 + if (val <= now && in_date) {
273 + if (++t.tm_mon == 12)
281 + if ((val = mktime(&t)) == (time_t)-1) {
282 + if (in_date == 3 && t.tm_mday > 28) {
298 int set_modtime(char *fname, time_t modtime)