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