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