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