Updated patches to work with the current trunk.
[rsync/rsync-patches.git] / time-limit.diff
CommitLineData
00891c37
WD
1John Taylor's patch for implementing --time-limit and --stop-at, reworked
2to be simpler and more efficient by Wayne Davison.
3
ed44ffcf 4Do we need configure support for mktime()?
00891c37 5
03019e41
WD
6To use this patch, run these commands for a successful build:
7
8 patch -p1 <patches/time-limit.diff
9 ./configure (optional if already run)
10 make
11
cc3e685d 12diff --git a/io.c b/io.c
fc557362 13index 6a89c8f..d248f0f 100644
cc3e685d
WD
14--- a/io.c
15+++ b/io.c
fc557362 16@@ -53,6 +53,7 @@ extern int protocol_version;
cdcd2137 17 extern int remove_source_files;
d608ca23 18 extern int preserve_hard_links;
9cd8d7aa 19 extern struct stats stats;
d608ca23 20+extern time_t stop_at_utime;
cdcd2137 21 extern struct file_list *cur_flist;
6cbbe66d 22 #ifdef ICONV_OPTION
cdcd2137 23 extern int filesfrom_convert;
963ca808 24@@ -182,16 +183,24 @@ static void check_timeout(void)
00891c37
WD
25 {
26 time_t t;
c70898d5 27
8ab3ce19
WD
28+ if ((!io_timeout || ignore_timeout) && !stop_at_utime)
29+ return;
30+
00891c37 31+ t = time(NULL);
c70898d5 32+
00891c37
WD
33+ if (stop_at_utime && t >= stop_at_utime) {
34+ rprintf(FERROR, "run-time limit exceeded\n");
35+ exit_cleanup(RERR_TIMEOUT);
36+ }
c70898d5 37+
8ab3ce19
WD
38 if (!io_timeout || ignore_timeout)
39 return;
40
5398d042
WD
41 if (!last_io_in) {
42- last_io_in = time(NULL);
43+ last_io_in = t;
00891c37
WD
44 return;
45 }
c70898d5 46
7ce4946a
WD
47- t = time(NULL);
48-
5398d042 49 if (t - last_io_in >= io_timeout) {
00891c37 50 if (!am_server && !am_daemon) {
a7219d20 51 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
cc3e685d 52diff --git a/options.c b/options.c
fc557362 53index e7c6c61..6e72c02 100644
cc3e685d
WD
54--- a/options.c
55+++ b/options.c
fc557362 56@@ -112,6 +112,7 @@ size_t bwlimit_writemax = 0;
70891d26
WD
57 int ignore_existing = 0;
58 int ignore_non_existing = 0;
59 int need_messages_from_generator = 0;
00891c37 60+time_t stop_at_utime = 0;
99650e0d 61 int max_delete = INT_MIN;
70891d26
WD
62 OFF_T max_size = 0;
63 OFF_T min_size = 0;
fc557362 64@@ -775,6 +776,8 @@ void usage(enum logcode F)
fc068916 65 rprintf(F," --password-file=FILE read daemon-access password from FILE\n");
be73a66e 66 rprintf(F," --list-only list the files instead of copying them\n");
79f132a1 67 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
ed44ffcf
WD
68+ rprintf(F," --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute\n");
69+ rprintf(F," --time-limit=MINS Stop rsync after MINS minutes have elapsed\n");
79f132a1 70 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
c2f72cff 71 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
79f132a1 72 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
fc557362 73@@ -798,7 +801,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
0ca6aebe 74 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
5398d042 75 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
fc557362
WD
76 OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG,
77- OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN,
78+ OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_STOP_AT, OPT_TIME_LIMIT,
0ca6aebe 79 OPT_SERVER, OPT_REFUSED_BASE = 9000};
00891c37
WD
80
81 static struct poptOption long_options[] = {
fc557362 82@@ -988,6 +991,8 @@ static struct poptOption long_options[] = {
6cbbe66d 83 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
cc3e685d 84 {"contimeout", 0, POPT_ARG_INT, &connect_timeout, 0, 0, 0 },
c0c7984e 85 {"no-contimeout", 0, POPT_ARG_VAL, &connect_timeout, 0, 0, 0 },
00891c37
WD
86+ {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
87+ {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
6cbbe66d
WD
88 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
89 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
90 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
fc557362 91@@ -1742,6 +1747,36 @@ int parse_arguments(int *argc_p, const char ***argv_p)
5ff5e82f
WD
92 return 0;
93 #endif
00891c37
WD
94
95+ case OPT_STOP_AT:
96+ arg = poptGetOptArg(pc);
ed44ffcf 97+ if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
00891c37
WD
98+ snprintf(err_buf, sizeof err_buf,
99+ "invalid --stop-at format: %s\n",
100+ arg);
101+ rprintf(FERROR, "ERROR: %s", err_buf);
102+ return 0;
103+ }
00891c37
WD
104+ if (stop_at_utime < time(NULL)) {
105+ snprintf(err_buf, sizeof err_buf,
106+ "--stop-at time is in the past: %s\n",
107+ arg);
108+ rprintf(FERROR, "ERROR: %s", err_buf);
109+ return 0;
110+ }
111+ break;
112+
113+ case OPT_TIME_LIMIT:
114+ arg = poptGetOptArg(pc);
115+ if ((stop_at_utime = atol(arg) * 60) <= 0) {
116+ snprintf(err_buf, sizeof err_buf,
117+ "invalid --time-limit value: %s\n",
118+ arg);
119+ rprintf(FERROR, "ERROR: %s", err_buf);
120+ return 0;
121+ }
122+ stop_at_utime += time(NULL);
123+ break;
124+
125 default:
126 /* A large opt value means that set_refuse_options()
27a7053c 127 * turned this option off. */
fc557362 128@@ -2428,6 +2463,15 @@ void server_options(char **args, int *argc_p)
7ce4946a
WD
129 args[ac++] = arg;
130 }
c70898d5 131
00891c37
WD
132+ if (stop_at_utime) {
133+ long mins = (stop_at_utime - time(NULL)) / 60;
134+ if (mins <= 0)
135+ mins = 1;
136+ if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
7ce4946a
WD
137+ goto oom;
138+ args[ac++] = arg;
139+ }
140+
141 if (backup_dir) {
142 args[ac++] = "--backup-dir";
143 args[ac++] = backup_dir;
cc3e685d 144diff --git a/rsync.yo b/rsync.yo
fc557362 145index 941f7a5..6945d06 100644
cc3e685d
WD
146--- a/rsync.yo
147+++ b/rsync.yo
fc557362 148@@ -431,6 +431,8 @@ to the detailed description below for a complete description. verb(
fc068916 149 --password-file=FILE read daemon-access password from FILE
be73a66e 150 --list-only list the files instead of copying them
79f132a1 151 --bwlimit=KBPS limit I/O bandwidth; KBytes per second
ed44ffcf
WD
152+ --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute
153+ --time-limit=MINS Stop rsync after MINS minutes have elapsed
a7219d20 154 --write-batch=FILE write a batched update to FILE
c2f72cff 155 --only-write-batch=FILE like --write-batch but w/o updating dest
79f132a1 156 --read-batch=FILE read a batched update from FILE
fc557362 157@@ -2230,6 +2232,19 @@ transfer was too fast, it will wait before sending the next data block. The
c70898d5
WD
158 result is an average transfer rate equaling the specified limit. A value
159 of zero specifies no limit.
7ce4946a 160
ed44ffcf
WD
161+dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what
162+time to stop rsync, in year-month-dayThour:minute numeric format (e.g.
ab74148e 163+2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also
ed44ffcf
WD
164+leave off various items and the result will be the next possible time
165+that matches the specified data. For example, "1-30" specifies the next
16711fbf
WD
166+January 30th (at midnight), "04:00" specifies the next 4am, "1"
167+specifies the next 1st of the month at midnight, and ":59" specifies the
ab74148e
WD
168+next 59th minute after the hour. If you prefer, you may separate the
169+date numbers using slashes instead of dashes.
00891c37 170+
ed44ffcf 171+dit(bf(--time-limit=MINS)) This option allows you to specify the maximum
00891c37 172+number of minutes rsync will run for.
7ce4946a 173+
9be39c35 174 dit(bf(--write-batch=FILE)) Record a file that can later be applied to
a7219d20 175 another identical destination with bf(--read-batch). See the "BATCH MODE"
388bf7cc 176 section for details, and also the bf(--only-write-batch) option.
cc3e685d 177diff --git a/util.c b/util.c
fc557362 178index 0cafed6..f3e2669 100644
cc3e685d
WD
179--- a/util.c
180+++ b/util.c
91270139 181@@ -123,6 +123,133 @@ NORETURN void overflow_exit(const char *str)
ed44ffcf
WD
182 exit_cleanup(RERR_MALLOC);
183 }
184
185+/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
186+ * also allowing abbreviated data. For instance, if the time is omitted,
187+ * it defaults to midnight. If the date is omitted, it defaults to the
188+ * next possible date in the future with the specified time. Even the
189+ * year or year-month can be omitted, again defaulting to the next date
190+ * in the future that matches the specified information. A 2-digit year
191+ * is also OK, as is using '/' instead of '-'. */
192+time_t parse_time(const char *arg)
193+{
194+ const char *cp;
195+ time_t val, now = time(NULL);
196+ struct tm t, *today = localtime(&now);
16711fbf 197+ int in_date, n;
ed44ffcf
WD
198+
199+ memset(&t, 0, sizeof t);
200+ t.tm_year = t.tm_mon = t.tm_mday = -1;
201+ t.tm_hour = t.tm_min = t.tm_isdst = -1;
16711fbf
WD
202+ cp = arg;
203+ if (*cp == 'T' || *cp == 't' || *cp == ':') {
204+ cp++;
205+ in_date = 0;
206+ } else
207+ in_date = 1;
208+ for ( ; ; cp++) {
3cff695b 209+ if (!isDigit(cp))
ed44ffcf 210+ return -1;
16711fbf
WD
211+
212+ n = 0;
213+ do {
214+ n = n * 10 + *cp++ - '0';
3cff695b 215+ } while (isDigit(cp));
16711fbf
WD
216+
217+ if (*cp == ':')
218+ in_date = 0;
ed44ffcf 219+ if (in_date) {
16711fbf
WD
220+ if (t.tm_year != -1)
221+ return -1;
222+ t.tm_year = t.tm_mon;
223+ t.tm_mon = t.tm_mday;
224+ t.tm_mday = n;
225+ if (!*cp)
226+ break;
227+ if (*cp == 'T' || *cp == 't') {
228+ if (!cp[1])
ed44ffcf 229+ break;
16711fbf
WD
230+ in_date = 0;
231+ } else if (*cp != '-' && *cp != '/')
232+ return -1;
233+ continue;
ed44ffcf
WD
234+ }
235+ if (t.tm_hour != -1)
236+ return -1;
237+ t.tm_hour = t.tm_min;
238+ t.tm_min = n;
239+ if (!*cp)
240+ break;
241+ if (*cp != ':')
242+ return -1;
243+ }
244+
245+ in_date = 0;
246+ if (t.tm_year < 0) {
247+ t.tm_year = today->tm_year;
248+ in_date = 1;
249+ } else if (t.tm_year < 100) {
250+ while (t.tm_year < today->tm_year)
251+ t.tm_year += 100;
252+ } else
253+ t.tm_year -= 1900;
254+ if (t.tm_mon < 0) {
255+ t.tm_mon = today->tm_mon;
256+ in_date = 2;
257+ } else
258+ t.tm_mon--;
259+ if (t.tm_mday < 0) {
ed44ffcf
WD
260+ t.tm_mday = today->tm_mday;
261+ in_date = 3;
262+ }
263+
264+ n = 0;
265+ if (t.tm_min < 0) {
266+ t.tm_hour = t.tm_min = 0;
267+ } else if (t.tm_hour < 0) {
268+ if (in_date != 3)
269+ return -1;
270+ in_date = 0;
271+ t.tm_hour = today->tm_hour;
272+ n = 60*60;
273+ }
274+
16711fbf
WD
275+ if (t.tm_hour > 23 || t.tm_min > 59
276+ || t.tm_mon < 0 || t.tm_mon >= 12
277+ || t.tm_mday < 1 || t.tm_mday > 31
278+ || (val = mktime(&t)) == (time_t)-1)
ed44ffcf
WD
279+ return -1;
280+
281+ if (val <= now && in_date) {
282+ tweak_date:
283+ switch (in_date) {
284+ case 3:
285+ t.tm_mday++;
286+ break;
287+ case 2:
288+ if (++t.tm_mon == 12)
289+ t.tm_mon = 0;
290+ else
291+ break;
292+ case 1:
293+ t.tm_year++;
294+ break;
295+ }
296+ if ((val = mktime(&t)) == (time_t)-1) {
297+ if (in_date == 3 && t.tm_mday > 28) {
298+ t.tm_mday = 1;
299+ in_date = 2;
300+ goto tweak_date;
301+ }
302+ return -1;
303+ }
304+ }
305+ if (n) {
306+ while (val <= now)
307+ val += n;
308+ }
309+ return val;
310+}
afcb578c 311+
fb11cdd7 312 int set_modtime(const char *fname, time_t modtime, mode_t mode)
afcb578c
WD
313 {
314 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES