Moved some misplaced code.
[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
9a7eef96
WD
12--- old/io.c
13+++ new/io.c
cdcd2137
WD
14@@ -53,6 +53,7 @@ extern int protocol_version;
15 extern int remove_source_files;
d608ca23 16 extern int preserve_hard_links;
9cd8d7aa 17 extern struct stats stats;
d608ca23 18+extern time_t stop_at_utime;
cdcd2137 19 extern struct file_list *cur_flist;
6cbbe66d 20 #ifdef ICONV_OPTION
cdcd2137
WD
21 extern int filesfrom_convert;
22@@ -215,16 +216,24 @@ static void check_timeout(void)
00891c37
WD
23 {
24 time_t t;
c70898d5 25
8ab3ce19
WD
26+ if ((!io_timeout || ignore_timeout) && !stop_at_utime)
27+ return;
28+
00891c37 29+ t = time(NULL);
c70898d5 30+
00891c37
WD
31+ if (stop_at_utime && t >= stop_at_utime) {
32+ rprintf(FERROR, "run-time limit exceeded\n");
33+ exit_cleanup(RERR_TIMEOUT);
34+ }
c70898d5 35+
8ab3ce19
WD
36 if (!io_timeout || ignore_timeout)
37 return;
38
5398d042
WD
39 if (!last_io_in) {
40- last_io_in = time(NULL);
41+ last_io_in = t;
00891c37
WD
42 return;
43 }
c70898d5 44
7ce4946a
WD
45- t = time(NULL);
46-
5398d042 47 if (t - last_io_in >= io_timeout) {
00891c37 48 if (!am_server && !am_daemon) {
a7219d20 49 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
9a7eef96
WD
50--- old/options.c
51+++ new/options.c
cdcd2137 52@@ -110,6 +110,7 @@ size_t bwlimit_writemax = 0;
70891d26
WD
53 int ignore_existing = 0;
54 int ignore_non_existing = 0;
55 int need_messages_from_generator = 0;
00891c37 56+time_t stop_at_utime = 0;
99650e0d 57 int max_delete = INT_MIN;
70891d26
WD
58 OFF_T max_size = 0;
59 OFF_T min_size = 0;
cdcd2137 60@@ -416,6 +417,8 @@ void usage(enum logcode F)
fc068916 61 rprintf(F," --password-file=FILE read daemon-access password from FILE\n");
be73a66e 62 rprintf(F," --list-only list the files instead of copying them\n");
79f132a1 63 rprintf(F," --bwlimit=KBPS limit I/O bandwidth; KBytes per second\n");
ed44ffcf
WD
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");
79f132a1 66 rprintf(F," --write-batch=FILE write a batched update to FILE\n");
c2f72cff 67 rprintf(F," --only-write-batch=FILE like --write-batch but w/o updating destination\n");
79f132a1 68 rprintf(F," --read-batch=FILE read a batched update from FILE\n");
cdcd2137 69@@ -438,7 +441,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OP
6849cd84 70 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
0ca6aebe 71 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
5398d042 72 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
5ba66156
WD
73- OPT_NO_D, OPT_APPEND,
74+ OPT_NO_D, OPT_APPEND, OPT_STOP_AT, OPT_TIME_LIMIT,
0ca6aebe 75 OPT_SERVER, OPT_REFUSED_BASE = 9000};
00891c37
WD
76
77 static struct poptOption long_options[] = {
cdcd2137 78@@ -603,6 +606,8 @@ static struct poptOption long_options[]
6cbbe66d
WD
79 {"no-numeric-ids", 0, POPT_ARG_VAL, &numeric_ids, 0, 0, 0 },
80 {"timeout", 0, POPT_ARG_INT, &io_timeout, 0, 0, 0 },
81 {"no-timeout", 0, POPT_ARG_VAL, &io_timeout, 0, 0, 0 },
00891c37
WD
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 },
6cbbe66d
WD
84 {"rsh", 'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
85 {"rsync-path", 0, POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
86 {"temp-dir", 'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
cdcd2137 87@@ -1221,6 +1226,36 @@ int parse_arguments(int *argc_p, const c
5ff5e82f
WD
88 return 0;
89 #endif
00891c37
WD
90
91+ case OPT_STOP_AT:
92+ arg = poptGetOptArg(pc);
ed44ffcf 93+ if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
00891c37
WD
94+ snprintf(err_buf, sizeof err_buf,
95+ "invalid --stop-at format: %s\n",
96+ arg);
97+ rprintf(FERROR, "ERROR: %s", err_buf);
98+ return 0;
99+ }
00891c37
WD
100+ if (stop_at_utime < time(NULL)) {
101+ snprintf(err_buf, sizeof err_buf,
102+ "--stop-at time is in the past: %s\n",
103+ arg);
104+ rprintf(FERROR, "ERROR: %s", err_buf);
105+ return 0;
106+ }
107+ break;
108+
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",
114+ arg);
115+ rprintf(FERROR, "ERROR: %s", err_buf);
116+ return 0;
117+ }
118+ stop_at_utime += time(NULL);
119+ break;
120+
121 default:
122 /* A large opt value means that set_refuse_options()
27a7053c 123 * turned this option off. */
cdcd2137 124@@ -1841,6 +1876,15 @@ void server_options(char **args, int *ar
7ce4946a
WD
125 args[ac++] = arg;
126 }
c70898d5 127
00891c37
WD
128+ if (stop_at_utime) {
129+ long mins = (stop_at_utime - time(NULL)) / 60;
130+ if (mins <= 0)
131+ mins = 1;
132+ if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
7ce4946a
WD
133+ goto oom;
134+ args[ac++] = arg;
135+ }
136+
137 if (backup_dir) {
138 args[ac++] = "--backup-dir";
139 args[ac++] = backup_dir;
9a7eef96
WD
140--- old/rsync.yo
141+++ new/rsync.yo
cdcd2137 142@@ -422,6 +422,8 @@ to the detailed description below for a
fc068916 143 --password-file=FILE read daemon-access password from FILE
be73a66e 144 --list-only list the files instead of copying them
79f132a1 145 --bwlimit=KBPS limit I/O bandwidth; KBytes per second
ed44ffcf
WD
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
a7219d20 148 --write-batch=FILE write a batched update to FILE
c2f72cff 149 --only-write-batch=FILE like --write-batch but w/o updating dest
79f132a1 150 --read-batch=FILE read a batched update from FILE
cdcd2137 151@@ -1933,6 +1935,19 @@ transfer was too fast, it will wait befo
c70898d5
WD
152 result is an average transfer rate equaling the specified limit. A value
153 of zero specifies no limit.
7ce4946a 154
ed44ffcf
WD
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.
ab74148e 157+2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also
ed44ffcf
WD
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
16711fbf
WD
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
ab74148e
WD
162+next 59th minute after the hour. If you prefer, you may separate the
163+date numbers using slashes instead of dashes.
00891c37 164+
ed44ffcf 165+dit(bf(--time-limit=MINS)) This option allows you to specify the maximum
00891c37 166+number of minutes rsync will run for.
7ce4946a 167+
9be39c35 168 dit(bf(--write-batch=FILE)) Record a file that can later be applied to
a7219d20 169 another identical destination with bf(--read-batch). See the "BATCH MODE"
388bf7cc 170 section for details, and also the bf(--only-write-batch) option.
9a7eef96
WD
171--- old/util.c
172+++ new/util.c
cdcd2137 173@@ -122,6 +122,133 @@ NORETURN void overflow_exit(const char *
ed44ffcf
WD
174 exit_cleanup(RERR_MALLOC);
175 }
176
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)
185+{
186+ const char *cp;
187+ time_t val, now = time(NULL);
188+ struct tm t, *today = localtime(&now);
16711fbf 189+ int in_date, n;
ed44ffcf
WD
190+
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;
16711fbf
WD
194+ cp = arg;
195+ if (*cp == 'T' || *cp == 't' || *cp == ':') {
196+ cp++;
197+ in_date = 0;
198+ } else
199+ in_date = 1;
200+ for ( ; ; cp++) {
3cff695b 201+ if (!isDigit(cp))
ed44ffcf 202+ return -1;
16711fbf
WD
203+
204+ n = 0;
205+ do {
206+ n = n * 10 + *cp++ - '0';
3cff695b 207+ } while (isDigit(cp));
16711fbf
WD
208+
209+ if (*cp == ':')
210+ in_date = 0;
ed44ffcf 211+ if (in_date) {
16711fbf
WD
212+ if (t.tm_year != -1)
213+ return -1;
214+ t.tm_year = t.tm_mon;
215+ t.tm_mon = t.tm_mday;
216+ t.tm_mday = n;
217+ if (!*cp)
218+ break;
219+ if (*cp == 'T' || *cp == 't') {
220+ if (!cp[1])
ed44ffcf 221+ break;
16711fbf
WD
222+ in_date = 0;
223+ } else if (*cp != '-' && *cp != '/')
224+ return -1;
225+ continue;
ed44ffcf
WD
226+ }
227+ if (t.tm_hour != -1)
228+ return -1;
229+ t.tm_hour = t.tm_min;
230+ t.tm_min = n;
231+ if (!*cp)
232+ break;
233+ if (*cp != ':')
234+ return -1;
235+ }
236+
237+ in_date = 0;
238+ if (t.tm_year < 0) {
239+ t.tm_year = today->tm_year;
240+ in_date = 1;
241+ } else if (t.tm_year < 100) {
242+ while (t.tm_year < today->tm_year)
243+ t.tm_year += 100;
244+ } else
245+ t.tm_year -= 1900;
246+ if (t.tm_mon < 0) {
247+ t.tm_mon = today->tm_mon;
248+ in_date = 2;
249+ } else
250+ t.tm_mon--;
251+ if (t.tm_mday < 0) {
ed44ffcf
WD
252+ t.tm_mday = today->tm_mday;
253+ in_date = 3;
254+ }
255+
256+ n = 0;
257+ if (t.tm_min < 0) {
258+ t.tm_hour = t.tm_min = 0;
259+ } else if (t.tm_hour < 0) {
260+ if (in_date != 3)
261+ return -1;
262+ in_date = 0;
263+ t.tm_hour = today->tm_hour;
264+ n = 60*60;
265+ }
266+
16711fbf
WD
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)
ed44ffcf
WD
271+ return -1;
272+
273+ if (val <= now && in_date) {
274+ tweak_date:
275+ switch (in_date) {
276+ case 3:
277+ t.tm_mday++;
278+ break;
279+ case 2:
280+ if (++t.tm_mon == 12)
281+ t.tm_mon = 0;
282+ else
283+ break;
284+ case 1:
285+ t.tm_year++;
286+ break;
287+ }
288+ if ((val = mktime(&t)) == (time_t)-1) {
289+ if (in_date == 3 && t.tm_mday > 28) {
290+ t.tm_mday = 1;
291+ in_date = 2;
292+ goto tweak_date;
293+ }
294+ return -1;
295+ }
296+ }
297+ if (n) {
298+ while (val <= now)
299+ val += n;
300+ }
301+ return val;
302+}
afcb578c 303+
fb11cdd7 304 int set_modtime(const char *fname, time_t modtime, mode_t mode)
afcb578c
WD
305 {
306 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES