Fixed a typo and added an extra note.
[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
ed44ffcf 6--- io.c 15 May 2004 19:31:10 -0000 1.121
7ce4946a 7+++ io.c 22 May 2004 19:33:07 -0000
00891c37
WD
8@@ -44,6 +44,7 @@ static int io_multiplexing_in;
9 static int multiplex_in_fd = -1;
10 static int multiplex_out_fd = -1;
11 static time_t last_io;
12+extern time_t stop_at_utime;
c70898d5
WD
13 static int no_flush;
14
15 extern int bwlimit;
7ce4946a 16@@ -125,16 +126,21 @@ static void check_timeout(void)
00891c37
WD
17 {
18 time_t t;
c70898d5 19
00891c37
WD
20- if (!io_timeout)
21+ if (!io_timeout && !stop_at_utime)
22 return;
23
24+ t = time(NULL);
c70898d5 25+
00891c37
WD
26+ if (stop_at_utime && t >= stop_at_utime) {
27+ rprintf(FERROR, "run-time limit exceeded\n");
28+ exit_cleanup(RERR_TIMEOUT);
29+ }
c70898d5 30+
00891c37
WD
31 if (!last_io) {
32- last_io = time(NULL);
33+ last_io = t;
34 return;
35 }
c70898d5 36
7ce4946a
WD
37- t = time(NULL);
38-
00891c37
WD
39 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
40 if (!am_server && !am_daemon) {
7ce4946a
WD
41 rprintf(FERROR, "io timeout after %d seconds - exiting\n",
42--- options.c 22 May 2004 06:09:22 -0000 1.151
43+++ options.c 22 May 2004 19:33:07 -0000
00891c37
WD
44@@ -92,6 +92,7 @@ int modify_window = 0;
45 int blocking_io = -1;
46 int checksum_seed = 0;
47 unsigned int block_size = 0;
48+time_t stop_at_utime = 0;
c70898d5 49
c70898d5 50
00891c37
WD
51 /** Network address family. **/
52@@ -288,6 +289,8 @@ void usage(enum logcode F)
c70898d5
WD
53 rprintf(F," --log-format=FORMAT log file transfers using specified format\n");
54 rprintf(F," --password-file=FILE get password from FILE\n");
55 rprintf(F," --bwlimit=KBPS limit I/O bandwidth, KBytes per second\n");
ed44ffcf
WD
56+ rprintf(F," --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute\n");
57+ rprintf(F," --time-limit=MINS Stop rsync after MINS minutes have elapsed\n");
c70898d5
WD
58 rprintf(F," --write-batch=PREFIX write batch fileset starting with PREFIX\n");
59 rprintf(F," --read-batch=PREFIX read batch fileset starting with PREFIX\n");
7ce4946a
WD
60 rprintf(F," --checksum-seed=NUM set block/file checksum seed\n");
61@@ -306,7 +309,7 @@ void usage(enum logcode F)
00891c37
WD
62 enum {OPT_VERSION = 1000, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
63 OPT_DELETE_AFTER, OPT_DELETE_EXCLUDED, OPT_LINK_DEST,
64 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW,
65- OPT_READ_BATCH, OPT_WRITE_BATCH,
66+ OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_STOP_AT, OPT_TIME_LIMIT,
67 OPT_REFUSED_BASE = 9000};
68
69 static struct poptOption long_options[] = {
7ce4946a 70@@ -378,6 +381,8 @@ static struct poptOption long_options[]
c70898d5
WD
71 {"port", 0, POPT_ARG_INT, &rsync_port, 0, 0, 0 },
72 {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 },
73 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
00891c37
WD
74+ {"stop-at", 0, POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
75+ {"time-limit", 0, POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
c70898d5
WD
76 {"address", 0, POPT_ARG_STRING, &bind_address, 0, 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 },
7ce4946a 79@@ -587,6 +592,36 @@ int parse_arguments(int *argc, const cha
00891c37
WD
80 return 0;
81 #endif
82
83+ case OPT_STOP_AT:
84+ arg = poptGetOptArg(pc);
ed44ffcf 85+ if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
00891c37
WD
86+ snprintf(err_buf, sizeof err_buf,
87+ "invalid --stop-at format: %s\n",
88+ arg);
89+ rprintf(FERROR, "ERROR: %s", err_buf);
90+ return 0;
91+ }
00891c37
WD
92+ if (stop_at_utime < time(NULL)) {
93+ snprintf(err_buf, sizeof err_buf,
94+ "--stop-at time is in the past: %s\n",
95+ arg);
96+ rprintf(FERROR, "ERROR: %s", err_buf);
97+ return 0;
98+ }
99+ break;
100+
101+ case OPT_TIME_LIMIT:
102+ arg = poptGetOptArg(pc);
103+ if ((stop_at_utime = atol(arg) * 60) <= 0) {
104+ snprintf(err_buf, sizeof err_buf,
105+ "invalid --time-limit value: %s\n",
106+ arg);
107+ rprintf(FERROR, "ERROR: %s", err_buf);
108+ return 0;
109+ }
110+ stop_at_utime += time(NULL);
111+ break;
112+
113 default:
114 /* A large opt value means that set_refuse_options()
115 * turned this option off (opt-BASE is its index). */
7ce4946a
WD
116@@ -886,6 +921,15 @@ void server_options(char **args,int *arg
117 args[ac++] = arg;
118 }
c70898d5 119
00891c37
WD
120+ if (stop_at_utime) {
121+ long mins = (stop_at_utime - time(NULL)) / 60;
122+ if (mins <= 0)
123+ mins = 1;
124+ if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
7ce4946a
WD
125+ goto oom;
126+ args[ac++] = arg;
127+ }
128+
129 if (backup_dir) {
130 args[ac++] = "--backup-dir";
131 args[ac++] = backup_dir;
132--- rsync.yo 21 May 2004 09:44:32 -0000 1.170
133+++ rsync.yo 22 May 2004 19:33:08 -0000
00891c37 134@@ -346,6 +346,8 @@ verb(
c70898d5
WD
135 --log-format=FORMAT log file transfers using specified format
136 --password-file=FILE get password from FILE
137 --bwlimit=KBPS limit I/O bandwidth, KBytes per second
ed44ffcf
WD
138+ --stop-at=y-m-dTh:m Stop rsync at year-month-dayThour:minute
139+ --time-limit=MINS Stop rsync after MINS minutes have elapsed
c70898d5
WD
140 --write-batch=PREFIX write batch fileset starting with PREFIX
141 --read-batch=PREFIX read batch fileset starting with PREFIX
7ce4946a 142 --checksum-seed=NUM set block/file checksum seed
ab74148e 143@@ -892,6 +894,19 @@ transfer was too fast, it will wait befo
c70898d5
WD
144 result is an average transfer rate equaling the specified limit. A value
145 of zero specifies no limit.
7ce4946a 146
ed44ffcf
WD
147+dit(bf(--stop-at=y-m-dTh:m)) This option allows you to specify at what
148+time to stop rsync, in year-month-dayThour:minute numeric format (e.g.
ab74148e 149+2004-12-31T23:59). You can specify a 2 or 4-digit year. You can also
ed44ffcf
WD
150+leave off various items and the result will be the next possible time
151+that matches the specified data. For example, "1-30" specifies the next
16711fbf
WD
152+January 30th (at midnight), "04:00" specifies the next 4am, "1"
153+specifies the next 1st of the month at midnight, and ":59" specifies the
ab74148e
WD
154+next 59th minute after the hour. If you prefer, you may separate the
155+date numbers using slashes instead of dashes.
00891c37 156+
ed44ffcf 157+dit(bf(--time-limit=MINS)) This option allows you to specify the maximum
00891c37 158+number of minutes rsync will run for.
7ce4946a 159+
c70898d5
WD
160 dit(bf(--write-batch=PREFIX)) Generate a set of files that can be
161 transferred as a batch update. Each filename in the set starts with
7ce4946a
WD
162 PREFIX. See the "BATCH MODE" section for details.
163--- util.c 21 May 2004 08:40:25 -0000 1.144
164+++ util.c 22 May 2004 19:33:08 -0000
165@@ -125,6 +125,132 @@ void overflow(char *str)
ed44ffcf
WD
166 exit_cleanup(RERR_MALLOC);
167 }
168
169+/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
170+ * also allowing abbreviated data. For instance, if the time is omitted,
171+ * it defaults to midnight. If the date is omitted, it defaults to the
172+ * next possible date in the future with the specified time. Even the
173+ * year or year-month can be omitted, again defaulting to the next date
174+ * in the future that matches the specified information. A 2-digit year
175+ * is also OK, as is using '/' instead of '-'. */
176+time_t parse_time(const char *arg)
177+{
178+ const char *cp;
179+ time_t val, now = time(NULL);
180+ struct tm t, *today = localtime(&now);
16711fbf 181+ int in_date, n;
ed44ffcf
WD
182+
183+ memset(&t, 0, sizeof t);
184+ t.tm_year = t.tm_mon = t.tm_mday = -1;
185+ t.tm_hour = t.tm_min = t.tm_isdst = -1;
16711fbf
WD
186+ cp = arg;
187+ if (*cp == 'T' || *cp == 't' || *cp == ':') {
188+ cp++;
189+ in_date = 0;
190+ } else
191+ in_date = 1;
192+ for ( ; ; cp++) {
193+ if (!isdigit(*cp))
ed44ffcf 194+ return -1;
16711fbf
WD
195+
196+ n = 0;
197+ do {
198+ n = n * 10 + *cp++ - '0';
199+ } while (isdigit(*cp));
200+
201+ if (*cp == ':')
202+ in_date = 0;
ed44ffcf 203+ if (in_date) {
16711fbf
WD
204+ if (t.tm_year != -1)
205+ return -1;
206+ t.tm_year = t.tm_mon;
207+ t.tm_mon = t.tm_mday;
208+ t.tm_mday = n;
209+ if (!*cp)
210+ break;
211+ if (*cp == 'T' || *cp == 't') {
212+ if (!cp[1])
ed44ffcf 213+ break;
16711fbf
WD
214+ in_date = 0;
215+ } else if (*cp != '-' && *cp != '/')
216+ return -1;
217+ continue;
ed44ffcf
WD
218+ }
219+ if (t.tm_hour != -1)
220+ return -1;
221+ t.tm_hour = t.tm_min;
222+ t.tm_min = n;
223+ if (!*cp)
224+ break;
225+ if (*cp != ':')
226+ return -1;
227+ }
228+
229+ in_date = 0;
230+ if (t.tm_year < 0) {
231+ t.tm_year = today->tm_year;
232+ in_date = 1;
233+ } else if (t.tm_year < 100) {
234+ while (t.tm_year < today->tm_year)
235+ t.tm_year += 100;
236+ } else
237+ t.tm_year -= 1900;
238+ if (t.tm_mon < 0) {
239+ t.tm_mon = today->tm_mon;
240+ in_date = 2;
241+ } else
242+ t.tm_mon--;
243+ if (t.tm_mday < 0) {
ed44ffcf
WD
244+ t.tm_mday = today->tm_mday;
245+ in_date = 3;
246+ }
247+
248+ n = 0;
249+ if (t.tm_min < 0) {
250+ t.tm_hour = t.tm_min = 0;
251+ } else if (t.tm_hour < 0) {
252+ if (in_date != 3)
253+ return -1;
254+ in_date = 0;
255+ t.tm_hour = today->tm_hour;
256+ n = 60*60;
257+ }
258+
16711fbf
WD
259+ if (t.tm_hour > 23 || t.tm_min > 59
260+ || t.tm_mon < 0 || t.tm_mon >= 12
261+ || t.tm_mday < 1 || t.tm_mday > 31
262+ || (val = mktime(&t)) == (time_t)-1)
ed44ffcf
WD
263+ return -1;
264+
265+ if (val <= now && in_date) {
266+ tweak_date:
267+ switch (in_date) {
268+ case 3:
269+ t.tm_mday++;
270+ break;
271+ case 2:
272+ if (++t.tm_mon == 12)
273+ t.tm_mon = 0;
274+ else
275+ break;
276+ case 1:
277+ t.tm_year++;
278+ break;
279+ }
280+ if ((val = mktime(&t)) == (time_t)-1) {
281+ if (in_date == 3 && t.tm_mday > 28) {
282+ t.tm_mday = 1;
283+ in_date = 2;
284+ goto tweak_date;
285+ }
286+ return -1;
287+ }
288+ }
289+ if (n) {
290+ while (val <= now)
291+ val += n;
292+ }
293+ return val;
294+}
295
296
297 int set_modtime(char *fname, time_t modtime)