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