My version of the --copy-atimes patch contributed by Assar.
[rsync-patches.git] / atimes.diff
1 --- backup.c    13 Mar 2004 20:18:03 -0000      1.28
2 +++ backup.c    20 Apr 2004 23:44:16 -0000
3 @@ -101,7 +101,7 @@ static int make_bak_dir(char *fullpath)
4                                     "make_bak_dir stat %s failed: %s\n",
5                                     full_fname(rel), strerror(errno));
6                         } else {
7 -                               set_modtime(fullpath, st.st_mtime);
8 +                               set_times(fullpath, st.st_mtime, time(NULL));
9                                 do_lchown(fullpath, st.st_uid, st.st_gid);
10                                 do_chmod(fullpath, st.st_mode);
11                         }
12 --- batch.c     6 Mar 2004 07:45:52 -0000       1.31
13 +++ batch.c     20 Apr 2004 23:44:16 -0000
14 @@ -342,6 +342,8 @@ void show_flist(int index, struct file_s
15                 rprintf(FINFO, "flist->flags=%#x\n", fptr[i]->flags);
16                 rprintf(FINFO, "flist->modtime=%#lx\n",
17                         (long unsigned) fptr[i]->modtime);
18 +               rprintf(FINFO, "flist->atime=%#lx\n",
19 +                       (long unsigned) fptr[i]->atime);
20                 rprintf(FINFO, "flist->length=%.0f\n",
21                         (double) fptr[i]->length);
22                 rprintf(FINFO, "flist->mode=%#o\n", (int) fptr[i]->mode);
23 --- flist.c     17 Apr 2004 17:14:12 -0000      1.214
24 +++ flist.c     20 Apr 2004 23:44:17 -0000
25 @@ -58,6 +58,7 @@ extern int relative_paths;
26  extern int implied_dirs;
27  extern int copy_links;
28  extern int copy_unsafe_links;
29 +extern int copy_atimes;
30  extern int protocol_version;
31  extern int sanitize_paths;
32  
33 @@ -140,16 +141,16 @@ static void list_file_entry(struct file_
34  
35  #if SUPPORT_LINKS
36         if (preserve_links && S_ISLNK(f->mode)) {
37 -               rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
38 +               rprintf(FINFO, "%s %11.0f %s %s %s -> %s\n",
39                         perms,
40                         (double) f->length, timestring(f->modtime),
41 -                       f_name(f), f->u.link);
42 +                       timestring(f->atime), f_name(f), f->u.link);
43         } else
44  #endif
45 -               rprintf(FINFO, "%s %11.0f %s %s\n",
46 +               rprintf(FINFO, "%s %11.0f %s %s %s\n",
47                         perms,
48                         (double) f->length, timestring(f->modtime),
49 -                       f_name(f));
50 +                       timestring(f->atime), f_name(f));
51  }
52  
53  
54 @@ -326,6 +327,7 @@ void send_file_entry(struct file_struct 
55  {
56         unsigned short flags;
57         static time_t modtime;
58 +       static time_t atime;
59         static mode_t mode;
60         static uint64 dev;
61         static dev_t rdev;
62 @@ -341,7 +343,7 @@ void send_file_entry(struct file_struct 
63  
64         if (!file) {
65                 write_byte(f, 0);
66 -               modtime = 0, mode = 0;
67 +               modtime = 0, atime = 0, mode = 0;
68                 dev = 0, rdev = makedev(0, 0);
69                 rdev_major = 0;
70                 uid = 0, gid = 0;
71 @@ -390,6 +392,12 @@ void send_file_entry(struct file_struct 
72                 flags |= XMIT_SAME_TIME;
73         else
74                 modtime = file->modtime;
75 +       if (copy_atimes && !S_ISDIR(mode)) {
76 +               if (file->atime == atime)
77 +                       flags |= XMIT_SAME_ATIME;
78 +               else
79 +                       atime = file->atime;
80 +       }
81  
82  #if SUPPORT_HARD_LINKS
83         if (file->link_u.idev) {
84 @@ -445,6 +453,8 @@ void send_file_entry(struct file_struct 
85                 write_int(f, modtime);
86         if (!(flags & XMIT_SAME_MODE))
87                 write_int(f, to_wire_mode(mode));
88 +       if (copy_atimes && !S_ISDIR(mode) && !(flags & XMIT_SAME_ATIME))
89 +               write_int(f, atime);
90         if (preserve_uid && !(flags & XMIT_SAME_UID)) {
91                 if (!numeric_ids)
92                         add_uid(uid);
93 @@ -518,6 +528,7 @@ void receive_file_entry(struct file_stru
94      struct file_list *flist, int f)
95  {
96         static time_t modtime;
97 +       static time_t atime;
98         static mode_t mode;
99         static uint64 dev;
100         static dev_t rdev;
101 @@ -534,7 +545,7 @@ void receive_file_entry(struct file_stru
102         struct file_struct *file;
103  
104         if (!fptr) {
105 -               modtime = 0, mode = 0;
106 +               modtime = 0, atime = 0, mode = 0;
107                 dev = 0, rdev = makedev(0, 0);
108                 rdev_major = 0;
109                 uid = 0, gid = 0;
110 @@ -588,6 +599,8 @@ void receive_file_entry(struct file_stru
111                 modtime = (time_t)read_int(f);
112         if (!(flags & XMIT_SAME_MODE))
113                 mode = from_wire_mode(read_int(f));
114 +       if (copy_atimes && !S_ISDIR(mode) && !(flags & XMIT_SAME_ATIME))
115 +               atime = (time_t)read_int(f);
116  
117         if (preserve_uid && !(flags & XMIT_SAME_UID))
118                 uid = (uid_t)read_int(f);
119 @@ -638,6 +651,7 @@ void receive_file_entry(struct file_stru
120  
121         file->flags = flags & XMIT_TOP_DIR ? FLAG_TOP_DIR : 0;
122         file->modtime = modtime;
123 +       file->atime = atime;
124         file->length = file_length;
125         file->mode = mode;
126         file->uid = uid;
127 @@ -852,6 +866,7 @@ skip_excludes:
128  
129         file->flags = flags;
130         file->modtime = st.st_mtime;
131 +       file->atime = st.st_atime;
132         file->length = st.st_size;
133         file->mode = st.st_mode;
134         file->uid = st.st_uid;
135 --- generator.c 15 Apr 2004 16:55:23 -0000      1.79
136 +++ generator.c 20 Apr 2004 23:44:17 -0000
137 @@ -97,7 +97,7 @@ static int skip_file(char *fname, struct
138                 return 0;
139         }
140  
141 -       return (cmp_modtime(st->st_mtime,file->modtime) == 0);
142 +       return cmp_time(st->st_mtime,file->modtime) == 0;
143  }
144  
145  
146 @@ -464,7 +464,7 @@ void recv_generator(char *fname, struct 
147                 return;
148         }
149  
150 -       if (update_only && cmp_modtime(st.st_mtime,file->modtime)>0 && fnamecmp == fname) {
151 +       if (update_only && cmp_time(st.st_mtime,file->modtime)>0 && fnamecmp == fname) {
152                 if (verbose > 1)
153                         rprintf(FINFO,"%s is newer\n",fname);
154                 return;
155 --- options.c   17 Apr 2004 17:07:23 -0000      1.147
156 +++ options.c   20 Apr 2004 23:44:17 -0000
157 @@ -46,6 +46,7 @@ int preserve_devices = 0;
158  int preserve_uid = 0;
159  int preserve_gid = 0;
160  int preserve_times = 0;
161 +int copy_atimes = 0;
162  int update_only = 0;
163  int cvs_exclude = 0;
164  int dry_run = 0;
165 @@ -241,6 +242,7 @@ void usage(enum logcode F)
166    rprintf(F," -g, --group                 preserve group\n");
167    rprintf(F," -D, --devices               preserve devices (root only)\n");
168    rprintf(F," -t, --times                 preserve times\n");
169 +  rprintf(F," -A, --copy-atimes           copy access times\n");
170    rprintf(F," -S, --sparse                handle sparse files efficiently\n");
171    rprintf(F," -n, --dry-run               show what would have been transferred\n");
172    rprintf(F," -W, --whole-file            copy whole files, no incremental checks\n");
173 @@ -346,6 +348,7 @@ static struct poptOption long_options[] 
174    {"group",           'g', POPT_ARG_NONE,   &preserve_gid, 0, 0, 0 },
175    {"devices",         'D', POPT_ARG_NONE,   &preserve_devices, 0, 0, 0 },
176    {"times",           't', POPT_ARG_NONE,   &preserve_times, 0, 0, 0 },
177 +  {"copy-atimes",     'A', POPT_ARG_NONE,   &copy_atimes, 0, 0, 0 },
178    {"checksum",        'c', POPT_ARG_NONE,   &always_checksum, 0, 0, 0 },
179    {"verbose",         'v', POPT_ARG_NONE,   0,               'v', 0, 0 },
180    {"quiet",           'q', POPT_ARG_NONE,   0,               'q', 0, 0 },
181 @@ -823,6 +826,8 @@ void server_options(char **args,int *arg
182                 argstr[x++] = 'D';
183         if (preserve_times)
184                 argstr[x++] = 't';
185 +       if (copy_atimes)
186 +               argstr[x++] = 'A';
187         if (preserve_perms)
188                 argstr[x++] = 'p';
189         if (recurse)
190 --- proto.h     14 Apr 2004 23:33:30 -0000      1.188
191 +++ proto.h     20 Apr 2004 23:44:17 -0000
192 @@ -243,7 +243,7 @@ int fd_pair(int fd[2]);
193  void print_child_argv(char **cmd);
194  void out_of_memory(char *str);
195  void overflow(char *str);
196 -int set_modtime(char *fname, time_t modtime);
197 +int set_times(char *fname, time_t modtime, time_t atime);
198  int create_directory_path(char *fname, int base_umask);
199  int copy_file(char *source, char *dest, mode_t mode);
200  int robust_unlink(char *fname);
201 @@ -267,7 +267,7 @@ int u_strcmp(const char *cs1, const char
202  int unsafe_symlink(const char *dest, const char *src);
203  char *timestring(time_t t);
204  int msleep(int t);
205 -int cmp_modtime(time_t file1, time_t file2);
206 +int cmp_time(time_t file1, time_t file2);
207  int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6);
208  void *_new_array(unsigned int size, unsigned long num);
209  void *_realloc_array(void *ptr, unsigned int size, unsigned long num);
210 --- rsync.c     23 Mar 2004 16:16:15 -0000      1.135
211 +++ rsync.c     20 Apr 2004 23:44:17 -0000
212 @@ -25,6 +25,7 @@
213  extern int verbose;
214  extern int dry_run;
215  extern int preserve_times;
216 +extern int copy_atimes;
217  extern int am_root;
218  extern int am_server;
219  extern int am_sender;
220 @@ -140,17 +141,28 @@ int set_perms(char *fname,struct file_st
221                 st = &st2;
222         }
223  
224 -       if (preserve_times && !S_ISLNK(st->st_mode) &&
225 -           cmp_modtime(st->st_mtime, file->modtime) != 0) {
226 +       if (!S_ISLNK(st->st_mode) && (preserve_times || copy_atimes)) {
227 +               time_t atime, mtime;
228 +
229 +               if (copy_atimes && !S_ISDIR(st->st_mode)
230 +                   && cmp_time(st->st_atime, file->atime) != 0) {
231 +                       atime = file->atime;
232 +                       updated = 1;
233 +               } else
234 +                       atime = st->st_atime;
235 +               if (preserve_times && cmp_time(st->st_mtime, file->modtime) != 0) {
236 +                       mtime = file->modtime;
237 +                       updated = 1;
238 +               } else
239 +                       mtime = st->st_mtime;
240                 /* don't complain about not setting times on directories
241                  * because some filesystems can't do it */
242 -               if (set_modtime(fname,file->modtime) != 0 &&
243 +               if (updated && set_times(fname, mtime, atime) != 0 &&
244                     !S_ISDIR(st->st_mode)) {
245                         rprintf(FERROR, "failed to set times on %s: %s\n",
246                                 full_fname(fname), strerror(errno));
247                         return 0;
248                 }
249 -               updated = 1;
250         }
251  
252         change_uid = am_root && preserve_uid && st->st_uid != file->uid;
253 --- rsync.h     17 Apr 2004 17:14:16 -0000      1.197
254 +++ rsync.h     20 Apr 2004 23:44:18 -0000
255 @@ -54,6 +54,7 @@
256  #define XMIT_HAS_IDEV_DATA (1<<9)
257  #define XMIT_SAME_DEV (1<<10)
258  #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
259 +#define XMIT_SAME_ATIME (1<<12)
260  
261  /* These flags are used in the live flist data. */
262  
263 @@ -419,6 +420,7 @@ struct file_struct {
264                 struct hlink *links;
265         } link_u;
266         time_t modtime;
267 +       time_t atime;
268         uid_t uid;
269         gid_t gid;
270         mode_t mode;
271 --- rsync.yo    17 Apr 2004 18:40:16 -0000      1.159
272 +++ rsync.yo    20 Apr 2004 23:44:18 -0000
273 @@ -299,6 +299,7 @@ verb(
274   -g, --group                 preserve group
275   -D, --devices               preserve devices (root only)
276   -t, --times                 preserve times
277 + -A, --copy-atimes           copy access times
278   -S, --sparse                handle sparse files efficiently
279   -n, --dry-run               show what would have been transferred
280   -W, --whole-file            copy whole files, no incremental checks
281 @@ -536,6 +537,11 @@ modified cannot be effective; in other w
282  cause the next transfer to behave as if it used -I, and all files will have
283  their checksums compared and show up in log messages even if they haven't
284  changed.
285 +
286 +dit(bf(-A, --copy-atimes)) This tells rsync to transfer access times
287 +along with the files and update them on the remote system.  Note that
288 +reading the source file may update the atime and hence repeated rsync
289 +copies with --copy-atimes may copy files unnecessarily.
290  
291  dit(bf(-n, --dry-run)) This tells rsync to not do any file transfers,
292  instead it will just report the actions it would have taken.
293 --- tls.c       9 Apr 2004 20:22:44 -0000       1.19
294 +++ tls.c       20 Apr 2004 23:44:18 -0000
295 @@ -39,6 +39,7 @@
296  
297  
298  #include "rsync.h"
299 +#include "popt.h"
300  
301  #define PROGRAM "tls"
302  
303 @@ -48,6 +49,7 @@ int read_only = 1;
304  int list_only = 0;
305  int preserve_perms = 0;
306  
307 +static int display_atime = 0;
308  
309  static void failed (char const *what,
310                     char const *where)
311 @@ -57,14 +59,29 @@ static void failed (char const *what,
312         exit (1);
313  }
314  
315 +static void storetime(char *dest, time_t t)
316 +{
317 +       if (t) {
318 +               struct tm *mt = gmtime(&t);
319  
320 +               sprintf(dest, "%04d-%02d-%02d %02d:%02d:%02d ",
321 +                       mt->tm_year + 1900,
322 +                       mt->tm_mon + 1,
323 +                       mt->tm_mday,
324 +                       mt->tm_hour,
325 +                       mt->tm_min,
326 +                       mt->tm_sec);
327 +       } else {
328 +               strcpy(dest, "                    ");
329 +       }
330 +}      
331  
332  static void list_file (const char *fname)
333  {
334         STRUCT_STAT buf;
335         char permbuf[PERMSTRING_SIZE];
336 -       struct tm *mt;
337 -       char datebuf[50];
338 +       char mtimebuf[50];
339 +       char atimebuf[50];
340         char linkbuf[4096];
341  
342         if (do_lstat(fname, &buf) == -1)
343 @@ -97,19 +114,8 @@ static void list_file (const char *fname
344  
345         permstring(permbuf, buf.st_mode);
346  
347 -       if (buf.st_mtime) {
348 -               mt = gmtime(&buf.st_mtime);
349 -
350 -               sprintf(datebuf, "%04d-%02d-%02d %02d:%02d:%02d",
351 -                       mt->tm_year + 1900,
352 -                       mt->tm_mon + 1,
353 -                       mt->tm_mday,
354 -                       mt->tm_hour,
355 -                       mt->tm_min,
356 -                       mt->tm_sec);
357 -       } else {
358 -               strcpy(datebuf, "                   ");
359 -       }
360 +       storetime(mtimebuf, buf.st_mtime);
361 +       storetime(atimebuf, buf.st_atime);
362  
363         /* TODO: Perhaps escape special characters in fname? */
364  
365 @@ -120,24 +126,55 @@ static void list_file (const char *fname
366                     (long)minor(buf.st_rdev));
367         } else /* NB: use double for size since it might not fit in a long. */
368                 printf("%12.0f", (double)buf.st_size);
369 -       printf(" %6ld.%-6ld %6ld %s %s%s\n",
370 +       printf(" %6ld.%-6ld %6ld %s%s%s%s\n",
371                (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
372 -              datebuf, fname, linkbuf);
373 +              mtimebuf, display_atime ? atimebuf : "",
374 +              fname, linkbuf);
375  }
376  
377 +static struct poptOption long_options[] = {
378 +  /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
379 +  {"atime",           'u', POPT_ARG_NONE,   &display_atime, 0,   0, 0},
380 +  {"help",            'h', POPT_ARG_NONE,   0,              'h', 0, 0},
381 +  {0,0,0,0,0,0,0}
382 +};
383 +
384 +static void tls_usage(int ret)
385 +{
386 +       fprintf(stderr, "usage: " PROGRAM " [--atime | -u] DIR ...\n"
387 +           "Trivial file listing program for portably checking rsync\n");
388 +       exit(ret);
389 +}
390  
391  int
392  main(int argc, char *argv[])
393  {
394 -       if (argc < 2) {
395 -               fprintf (stderr, "usage: " PROGRAM " DIR ...\n"
396 -                        "Trivial file listing program for portably checking rsync\n");
397 -               return 1;
398 +       poptContext pc;
399 +       const char **extra_args;
400 +       int opt;
401 +
402 +       pc = poptGetContext(PROGRAM, argc, (const char **)argv,
403 +                           long_options, 0);
404 +       while ((opt = poptGetNextOpt(pc)) != -1) {
405 +               switch (opt) {
406 +               case 'h':
407 +                       tls_usage(0);
408 +               default:
409 +                       fprintf(stderr,
410 +                               "%s: %s\n",
411 +                               poptBadOption(pc, POPT_BADOPTION_NOALIAS),
412 +                               poptStrerror(opt));
413 +                       tls_usage(1);
414 +               }
415         }
416  
417 -       for (argv++; *argv; argv++) {
418 -               list_file (*argv);
419 -       }
420 +       extra_args = poptGetArgs(pc);
421 +       if (*extra_args == NULL)
422 +               tls_usage(1);
423 +
424 +       for (; *extra_args; extra_args++)
425 +               list_file(*extra_args);
426 +       poptFreeContext(pc);
427  
428         return 0;
429  }
430 --- util.c      17 Apr 2004 17:06:03 -0000      1.136
431 +++ util.c      20 Apr 2004 23:44:19 -0000
432 @@ -124,32 +124,40 @@ void overflow(char *str)
433  
434  
435  
436 -int set_modtime(char *fname, time_t modtime)
437 +int set_times(char *fname, time_t modtime, time_t atime)
438  {
439         extern int dry_run;
440         if (dry_run)
441                 return 0;
442  
443         if (verbose > 2) {
444 -               rprintf(FINFO, "set modtime of %s to (%ld) %s",
445 +               char mtimebuf[200];
446 +               char atimebuf[200];
447 +
448 +               strlcpy(mtimebuf, timestring(modtime), sizeof(mtimebuf));
449 +               strlcpy(atimebuf, timestring(atime), sizeof(atimebuf));
450 +
451 +               rprintf(FINFO,
452 +                       "set modtime, atime of %s to (%ld) %s, (%ld) %s\n",
453                         fname, (long) modtime,
454 -                       asctime(localtime(&modtime)));
455 +                       mtimebuf,
456 +                       (long) atime, atimebuf);
457         }
458  
459         {
460  #ifdef HAVE_UTIMBUF
461                 struct utimbuf tbuf;
462 -               tbuf.actime = time(NULL);
463 +               tbuf.actime = atime;
464                 tbuf.modtime = modtime;
465                 return utime(fname,&tbuf);
466  #elif defined(HAVE_UTIME)
467                 time_t t[2];
468 -               t[0] = time(NULL);
469 +               t[0] = atime;
470                 t[1] = modtime;
471                 return utime(fname,t);
472  #else
473                 struct timeval t[2];
474 -               t[0].tv_sec = time(NULL);
475 +               t[0].tv_sec = atime;
476                 t[0].tv_usec = 0;
477                 t[1].tv_sec = modtime;
478                 t[1].tv_usec = 0;
479 @@ -1052,8 +1060,8 @@ int msleep(int t)
480  
481  
482  /**
483 - * Determine if two file modification times are equivalent (either
484 - * exact or in the modification timestamp window established by
485 + * Determine if two file  times are equivalent (either
486 + * exact or in the timestamp window established by
487   * --modify-window).
488   *
489   * @retval 0 if the times should be treated as the same
490 @@ -1062,7 +1070,7 @@ int msleep(int t)
491   *
492   * @retval -1 if the 2nd is later
493   **/
494 -int cmp_modtime(time_t file1, time_t file2)
495 +int cmp_time(time_t file1, time_t file2)
496  {
497         extern int modify_window;
498  
499 --- /dev/null   1 Jan 1970 00:00:00 -0000
500 +++ testsuite/copy-atimes.test  20 Apr 2004 23:44:19 -0000
501 @@ -0,0 +1,22 @@
502 +#! /bin/sh
503 +
504 +# Test rsync copying atimes
505 +
506 +. $srcdir/testsuite/rsync.fns
507 +
508 +set -x
509 +
510 +fromdir="$scratchdir/from"
511 +todir="$scratchdir/to"
512 +
513 +mkdir "$fromdir"
514 +
515 +touch "$fromdir/foo"
516 +touch -a -t 200102031717.42 "$fromdir/foo"
517 +
518 +TLS_ARGS=--atime
519 +
520 +checkit "$RSYNC -rtAgvvv \"$fromdir/\" \"$todir/\"" "$fromdir" "$todir"
521 +
522 +# The script would have aborted on error, so getting here means we've won.
523 +exit 0
524 --- testsuite/rsync.fns 4 Feb 2004 07:32:48 -0000       1.59
525 +++ testsuite/rsync.fns 20 Apr 2004 23:44:19 -0000
526 @@ -51,7 +51,7 @@ printmsg() {
527  
528  
529  rsync_ls_lR() {
530 -    find "$@" -print | sort | xargs "$TOOLDIR/tls"
531 +    find "$@" -print | sort | xargs "$TOOLDIR/tls" $TLS_ARGS
532  }
533  
534  rsync_getgroups() { 
535 @@ -151,6 +151,8 @@ checkit() {
536      # We can just write everything to stdout/stderr, because the
537      # wrapper hides it unless there is a problem.
538  
539 +    ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from 
540 +
541      echo "Running: \"$1\""  
542      eval "$1" 
543      status=$?
544 @@ -159,6 +161,12 @@ checkit() {
545      fi
546  
547      echo "-------------"
548 +    echo "check how the directory listings compare with diff:"
549 +    echo ""
550 +    ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to 
551 +    diff $diffopt ${TMP}/ls-from ${TMP}/ls-to || failed=YES
552 +
553 +    echo "-------------"
554      echo "check how the files compare with diff:"
555      echo ""
556      for f in `cd "$2"; find . -type f -print `
557 @@ -166,12 +174,6 @@ checkit() {
558          diff $diffopt "$2"/"$f" "$3"/"$f" || failed=YES
559      done
560  
561 -    echo "-------------"
562 -    echo "check how the directory listings compare with diff:"
563 -    echo ""
564 -    ( cd "$2" && rsync_ls_lR . ) > ${TMP}/ls-from 
565 -    ( cd "$3" && rsync_ls_lR . ) > ${TMP}/ls-to 
566 -    diff $diffopt ${TMP}/ls-from ${TMP}/ls-to || failed=YES
567      if [ -z "${failed}" ] ; then
568         return 0
569      else