Fix merge conflict.
[rsync-patches.git] / time-limit.diff
1 John Taylor's patch for implementing --time-limit and --stop-at, reworked
2 to be simpler and more efficient by Wayne Davison.
3
4 Do we need configure support for mktime()?
5
6 To 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
12 based-on: 194cee671d5e178f20c4494f41911fa8db942935
13 diff --git a/io.c b/io.c
14 --- a/io.c
15 +++ b/io.c
16 @@ -60,6 +60,7 @@ extern int preserve_hard_links;
17  extern BOOL extra_flist_sending_enabled;
18  extern BOOL flush_ok_after_signal;
19  extern struct stats stats;
20 +extern time_t stop_at_utime;
21  extern struct file_list *cur_flist;
22  #ifdef ICONV_OPTION
23  extern int filesfrom_convert;
24 @@ -170,11 +171,19 @@ static void check_timeout(BOOL allow_keepalive, int keepalive_flags)
25          * generator might be blocked trying to send checksums, it needs to
26          * know that the receiver is active).  Thus, as long as one or the
27          * other is successfully doing work, the generator will not timeout. */
28 -       if (!io_timeout)
29 +       if (!io_timeout && !stop_at_utime)
30                 return;
31  
32         t = time(NULL);
33  
34 +       if (stop_at_utime && t >= stop_at_utime) {
35 +               rprintf(FERROR, "run-time limit exceeded\n");
36 +               exit_cleanup(RERR_TIMEOUT);
37 +       }
38 +
39 +       if (!io_timeout)
40 +               return;
41 +
42         if (allow_keepalive) {
43                 /* This may put data into iobuf.msg w/o flushing. */
44                 maybe_send_keepalive(t, keepalive_flags);
45 diff --git a/options.c b/options.c
46 --- a/options.c
47 +++ b/options.c
48 @@ -119,6 +119,7 @@ size_t bwlimit_writemax = 0;
49  int ignore_existing = 0;
50  int ignore_non_existing = 0;
51  int need_messages_from_generator = 0;
52 +time_t stop_at_utime = 0;
53  int max_delete = INT_MIN;
54  OFF_T max_size = -1;
55  OFF_T min_size = -1;
56 @@ -779,6 +780,7 @@ enum {OPT_SERVER = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
57        OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG,
58        OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT,
59        OPT_OLD_COMPRESS, OPT_NEW_COMPRESS, OPT_NO_COMPRESS,
60 +      OPT_STOP_AT, OPT_TIME_LIMIT,
61        OPT_REFUSED_BASE = 9000};
62  
63  static struct poptOption long_options[] = {
64 @@ -988,6 +990,8 @@ static struct poptOption long_options[] = {
65    {"no-timeout",       0,  POPT_ARG_VAL,    &io_timeout, 0, 0, 0 },
66    {"contimeout",       0,  POPT_ARG_INT,    &connect_timeout, 0, 0, 0 },
67    {"no-contimeout",    0,  POPT_ARG_VAL,    &connect_timeout, 0, 0, 0 },
68 +  {"stop-at",          0,  POPT_ARG_STRING, 0, OPT_STOP_AT, 0, 0 },
69 +  {"time-limit",       0,  POPT_ARG_STRING, 0, OPT_TIME_LIMIT, 0, 0 },
70    {"rsh",             'e', POPT_ARG_STRING, &shell_cmd, 0, 0, 0 },
71    {"rsync-path",       0,  POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
72    {"temp-dir",        'T', POPT_ARG_STRING, &tmpdir, 0, 0, 0 },
73 @@ -1863,6 +1867,36 @@ int parse_arguments(int *argc_p, const char ***argv_p)
74                         return 0;
75  #endif
76  
77 +               case OPT_STOP_AT:
78 +                       arg = poptGetOptArg(pc);
79 +                       if ((stop_at_utime = parse_time(arg)) == (time_t)-1) {
80 +                               snprintf(err_buf, sizeof err_buf,
81 +                                   "invalid --stop-at format: %s\n",
82 +                                   arg);
83 +                               rprintf(FERROR, "ERROR: %s", err_buf);
84 +                               return 0;
85 +                       }
86 +                       if (stop_at_utime < time(NULL)) {
87 +                               snprintf(err_buf, sizeof err_buf,
88 +                                   "--stop-at time is in the past: %s\n",
89 +                                   arg);
90 +                               rprintf(FERROR, "ERROR: %s", err_buf);
91 +                               return 0;
92 +                       }
93 +                       break;
94 +
95 +               case OPT_TIME_LIMIT:
96 +                       arg = poptGetOptArg(pc);
97 +                       if ((stop_at_utime = atol(arg) * 60) <= 0) {
98 +                               snprintf(err_buf, sizeof err_buf,
99 +                                   "invalid --time-limit value: %s\n",
100 +                                   arg);
101 +                               rprintf(FERROR, "ERROR: %s", err_buf);
102 +                               return 0;
103 +                       }
104 +                       stop_at_utime += time(NULL);
105 +                       break;
106 +
107                 default:
108                         /* A large opt value means that set_refuse_options()
109                          * turned this option off. */
110 @@ -2691,6 +2725,15 @@ void server_options(char **args, int *argc_p)
111                 args[ac++] = arg;
112         }
113  
114 +       if (stop_at_utime) {
115 +               long mins = (stop_at_utime - time(NULL)) / 60;
116 +               if (mins <= 0)
117 +                       mins = 1;
118 +               if (asprintf(&arg, "--time-limit=%ld", mins) < 0)
119 +                       goto oom;
120 +               args[ac++] = arg;
121 +       }
122 +
123         if (backup_dir) {
124                 args[ac++] = "--backup-dir";
125                 args[ac++] = backup_dir;
126 diff --git a/rsync.1.md b/rsync.1.md
127 --- a/rsync.1.md
128 +++ b/rsync.1.md
129 @@ -457,6 +457,8 @@ detailed description below for a complete description.
130  --early-input=FILE       use FILE for daemon's early exec input
131  --list-only              list the files instead of copying them
132  --bwlimit=RATE           limit socket I/O bandwidth
133 +--stop-at=y-m-dTh:m      Stop rsync at year-month-dayThour:minute
134 +--time-limit=MINS        Stop rsync after MINS minutes have elapsed
135  --write-batch=FILE       write a batched update to FILE
136  --only-write-batch=FILE  like --write-batch but w/o updating dest
137  --read-batch=FILE        read a batched update from FILE
138 @@ -3107,6 +3109,22 @@ your home directory (remove the '=' for that).
139      buffered, while other can show up as very slow when the flushing of the
140      output buffer occurs.  This may be fixed in a future version.
141  
142 +0.  `--stop-at=y-m-dTh:m
143 +
144 +    This option allows you to specify at what time to stop rsync, in
145 +    year-month-dayThour:minute numeric format (e.g.  2004-12-31T23:59).  You
146 +    can specify a 2 or 4-digit year.  You can also leave off various items and
147 +    the result will be the next possible time that matches the specified data.
148 +    For example, "1-30" specifies the next January 30th (at midnight), "04:00"
149 +    specifies the next 4am, "1" specifies the next 1st of the month at
150 +    midnight, and ":59" specifies the next 59th minute after the hour.  If you
151 +    prefer, you may separate the date numbers using slashes instead of dashes.
152 +
153 +0.  `--time-limit=MINS
154 +
155 +    This option allows you to specify the maximum number of minutes rsync will
156 +    run for.
157 +
158  0.  `--write-batch=FILE`
159  
160      Record a file that can later be applied to another identical destination
161 diff --git a/util.c b/util.c
162 --- a/util.c
163 +++ b/util.c
164 @@ -115,6 +115,133 @@ void print_child_argv(const char *prefix, char **cmd)
165         rprintf(FCLIENT, " (%d args)\n", cnt);
166  }
167  
168 +/* Allow the user to specify a time in the format yyyy-mm-ddThh:mm while
169 + * also allowing abbreviated data.  For instance, if the time is omitted,
170 + * it defaults to midnight.  If the date is omitted, it defaults to the
171 + * next possible date in the future with the specified time.  Even the
172 + * year or year-month can be omitted, again defaulting to the next date
173 + * in the future that matches the specified information.  A 2-digit year
174 + * is also OK, as is using '/' instead of '-'. */
175 +time_t parse_time(const char *arg)
176 +{
177 +       const char *cp;
178 +       time_t val, now = time(NULL);
179 +       struct tm t, *today = localtime(&now);
180 +       int in_date, n;
181 +
182 +       memset(&t, 0, sizeof t);
183 +       t.tm_year = t.tm_mon = t.tm_mday = -1;
184 +       t.tm_hour = t.tm_min = t.tm_isdst = -1;
185 +       cp = arg;
186 +       if (*cp == 'T' || *cp == 't' || *cp == ':') {
187 +               cp++;
188 +               in_date = 0;
189 +       } else
190 +               in_date = 1;
191 +       for ( ; ; cp++) {
192 +               if (!isDigit(cp))
193 +                       return -1;
194 +
195 +               n = 0;
196 +               do {
197 +                       n = n * 10 + *cp++ - '0';
198 +               } while (isDigit(cp));
199 +
200 +               if (*cp == ':')
201 +                       in_date = 0;
202 +               if (in_date) {
203 +                       if (t.tm_year != -1)
204 +                               return -1;
205 +                       t.tm_year = t.tm_mon;
206 +                       t.tm_mon = t.tm_mday;
207 +                       t.tm_mday = n;
208 +                       if (!*cp)
209 +                               break;
210 +                       if (*cp == 'T' || *cp == 't') {
211 +                               if (!cp[1])
212 +                                       break;
213 +                               in_date = 0;
214 +                       } else if (*cp != '-' && *cp != '/')
215 +                               return -1;
216 +                       continue;
217 +               }
218 +               if (t.tm_hour != -1)
219 +                       return -1;
220 +               t.tm_hour = t.tm_min;
221 +               t.tm_min = n;
222 +               if (!*cp)
223 +                       break;
224 +               if (*cp != ':')
225 +                       return -1;
226 +       }
227 +
228 +       in_date = 0;
229 +       if (t.tm_year < 0) {
230 +               t.tm_year = today->tm_year;
231 +               in_date = 1;
232 +       } else if (t.tm_year < 100) {
233 +               while (t.tm_year < today->tm_year)
234 +                       t.tm_year += 100;
235 +       } else
236 +               t.tm_year -= 1900;
237 +       if (t.tm_mon < 0) {
238 +               t.tm_mon = today->tm_mon;
239 +               in_date = 2;
240 +       } else
241 +               t.tm_mon--;
242 +       if (t.tm_mday < 0) {
243 +               t.tm_mday = today->tm_mday;
244 +               in_date = 3;
245 +       }
246 +
247 +       n = 0;
248 +       if (t.tm_min < 0) {
249 +               t.tm_hour = t.tm_min = 0;
250 +       } else if (t.tm_hour < 0) {
251 +               if (in_date != 3)
252 +                       return -1;
253 +               in_date = 0;
254 +               t.tm_hour = today->tm_hour;
255 +               n = 60*60;
256 +       }
257 +
258 +       if (t.tm_hour > 23 || t.tm_min > 59
259 +           || t.tm_mon < 0 || t.tm_mon >= 12
260 +           || t.tm_mday < 1 || t.tm_mday > 31
261 +           || (val = mktime(&t)) == (time_t)-1)
262 +               return -1;
263 +
264 +       if (val <= now && in_date) {
265 +           tweak_date:
266 +               switch (in_date) {
267 +               case 3:
268 +                       t.tm_mday++;
269 +                       break;
270 +               case 2:
271 +                       if (++t.tm_mon == 12)
272 +                               t.tm_mon = 0;
273 +                       else
274 +                               break;
275 +               case 1:
276 +                       t.tm_year++;
277 +                       break;
278 +               }
279 +               if ((val = mktime(&t)) == (time_t)-1) {
280 +                       if (in_date == 3 && t.tm_mday > 28) {
281 +                               t.tm_mday = 1;
282 +                               in_date = 2;
283 +                               goto tweak_date;
284 +                       }
285 +                       return -1;
286 +               }
287 +       }
288 +       if (n) {
289 +               while (val <= now)
290 +                       val += n;
291 +       }
292 +       return val;
293 +}
294 +
295  /* This returns 0 for success, 1 for a symlink if symlink time-setting
296   * is not possible, or -1 for any other error. */
297  int set_times(const char *fname, STRUCT_STAT *stp)