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