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