1 To use this patch, run these commands for a successful build:
3 patch -p1 <patches/atimes.diff
4 ./configure (optional if already run)
7 based-on: 181c9faf928faad08ef095f4667afe460ec3bef6
8 diff --git a/compat.c b/compat.c
11 @@ -43,6 +43,7 @@ extern int protocol_version;
12 extern int protect_args;
13 extern int preserve_uid;
14 extern int preserve_gid;
15 +extern int preserve_atimes;
16 extern int preserve_acls;
17 extern int preserve_xattrs;
18 extern int need_messages_from_generator;
19 @@ -60,7 +61,7 @@ extern char *iconv_opt;
22 /* These index values are for the file-list's extra-attribute array. */
23 -int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
24 +int uid_ndx, gid_ndx, atimes_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
26 int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
27 int sender_symlink_iconv = 0; /* sender should convert symlink content */
28 @@ -136,6 +137,8 @@ void setup_protocol(int f_out,int f_in)
29 uid_ndx = ++file_extra_cnt;
31 gid_ndx = ++file_extra_cnt;
32 + if (preserve_atimes)
33 + atimes_ndx = (file_extra_cnt += TIME_EXTRA_CNT);
34 if (preserve_acls && !am_sender)
35 acls_ndx = ++file_extra_cnt;
37 diff --git a/flist.c b/flist.c
40 @@ -55,6 +55,7 @@ extern int missing_args;
44 +extern int atimes_ndx;
45 extern int relative_paths;
46 extern int implied_dirs;
47 extern int file_extra_cnt;
48 @@ -396,7 +397,7 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
50 int ndx, int first_ndx)
52 - static time_t modtime;
53 + static time_t modtime, atime;
55 #ifdef SUPPORT_HARD_LINKS
57 @@ -494,6 +495,13 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
58 xflags |= XMIT_SAME_TIME;
60 modtime = file->modtime;
61 + if (atimes_ndx && !S_ISDIR(mode)) {
62 + time_t file_atime = f_atime(file);
63 + if (file_atime == atime)
64 + xflags |= XMIT_SAME_ATIME;
69 #ifdef SUPPORT_HARD_LINKS
71 @@ -578,6 +586,8 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
73 if (!(xflags & XMIT_SAME_MODE))
74 write_int(f, to_wire_mode(mode));
75 + if (atimes_ndx && !S_ISDIR(mode) && !(xflags & XMIT_SAME_ATIME))
76 + write_varlong(f, atime, 4);
77 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
78 if (protocol_version < 30)
80 @@ -664,7 +674,7 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
81 static struct file_struct *recv_file_entry(struct file_list *flist,
84 - static int64 modtime;
85 + static int64 modtime, atime;
87 #ifdef SUPPORT_HARD_LINKS
89 @@ -802,6 +812,16 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
91 if (!(xflags & XMIT_SAME_MODE))
92 mode = from_wire_mode(read_int(f));
93 + if (atimes_ndx && !S_ISDIR(mode) && !(xflags & XMIT_SAME_ATIME)) {
94 + atime = read_varlong(f, 4);
95 +#if SIZEOF_TIME_T < SIZEOF_INT64
96 + if (!am_generator && (int64)(time_t)atime != atime) {
97 + rprintf(FERROR_XFER,
98 + "Access time value of %s truncated on receiver.\n",
104 if (chmod_modes && !S_ISLNK(mode) && mode)
105 mode = tweak_mode(mode, chmod_modes);
106 @@ -952,6 +972,8 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
108 file->flags |= gid_flags;
111 + f_atime_set(file, (time_t)atime);
113 F_NDX(file) = flist->used + flist->ndx_start;
115 @@ -1337,6 +1359,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
116 F_OWNER(file) = st.st_uid;
117 if (gid_ndx) /* Check gid_ndx instead of preserve_gid for del support */
118 F_GROUP(file) = st.st_gid;
120 + f_atime_set(file, st.st_atime);
122 if (basename != thisname)
123 file->dirname = lastdir;
124 diff --git a/generator.c b/generator.c
127 @@ -455,6 +455,9 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
128 : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
129 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
130 iflags |= ITEM_REPORT_TIME;
131 + if (atimes_ndx && !S_ISDIR(file->mode) && !S_ISLNK(file->mode)
132 + && cmp_time(f_atime(file), sxp->st.st_atime) != 0)
133 + iflags |= ITEM_REPORT_ATIME;
134 #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
135 if (S_ISLNK(file->mode)) {
137 @@ -825,6 +828,8 @@ static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
139 if (!hard_link_one(file, fname, cmpbuf, 1))
142 + set_file_attrs(fname, file, sxp, NULL, 0);
143 if (preserve_hard_links && F_IS_HLINKED(file))
144 finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
145 if (!maybe_ATTRS_REPORT && (INFO_GTE(NAME, 2) || stdout_format_has_i > 1)) {
146 @@ -1010,6 +1015,7 @@ static int try_dests_non(struct file_struct *file, char *fname, int ndx,
147 static void list_file_entry(struct file_struct *f)
149 char permbuf[PERMSTRING_SIZE];
150 + time_t atime = atimes_ndx ? f_atime(f) : 0;
152 int colwidth = human_readable ? 14 : 11;
154 @@ -1025,10 +1031,11 @@ static void list_file_entry(struct file_struct *f)
157 if (preserve_links && S_ISLNK(f->mode)) {
158 - rprintf(FINFO, "%s %*s %s %s -> %s\n",
159 + rprintf(FINFO, "%s %*s %s %s %s -> %s\n",
160 permbuf, colwidth, comma_num(len),
161 - timestring(f->modtime), f_name(f, NULL),
163 + timestring(f->modtime),
164 + atimes_ndx ? timestring(atime) : "",
165 + f_name(f, NULL), F_SYMLINK(f));
168 if (missing_args == 2 && f->mode == 0) {
169 @@ -1036,9 +1043,11 @@ static void list_file_entry(struct file_struct *f)
170 colwidth + 31, "*missing",
173 - rprintf(FINFO, "%s %*s %s %s\n",
174 + rprintf(FINFO, "%s %*s %s %s %s\n",
175 permbuf, colwidth, comma_num(len),
176 - timestring(f->modtime), f_name(f, NULL));
177 + timestring(f->modtime),
178 + atimes_ndx ? timestring(atime) : "",
183 @@ -1870,7 +1879,7 @@ static void touch_up_dirs(struct file_list *flist, int ndx)
185 if (link_stat(fname, &st, 0) == 0
186 && cmp_time(st.st_mtime, file->modtime) != 0)
187 - set_modtime(fname, file->modtime, file->mode);
188 + set_times(fname, file->modtime, file->modtime, file->mode);
190 if (counter >= loopchk_limit) {
192 diff --git a/ifuncs.h b/ifuncs.h
195 @@ -35,6 +35,28 @@ realloc_xbuf(xbuf *xb, size_t sz)
199 +static inline time_t
200 +f_atime(struct file_struct *fp)
202 +#if SIZEOF_TIME_T > 4
204 + memcpy(&atime, &REQ_EXTRA(fp, atimes_ndx)->unum, SIZEOF_TIME_T);
207 + return REQ_EXTRA(fp, atimes_ndx)->unum;
212 +f_atime_set(struct file_struct *fp, time_t atime)
214 +#if SIZEOF_TIME_T > 4
215 + memcpy(&REQ_EXTRA(fp, atimes_ndx)->unum, &atime, SIZEOF_TIME_T);
217 + REQ_EXTRA(fp, atimes_ndx)->unum = (uint32)atime;
222 to_wire_mode(mode_t mode)
224 diff --git a/log.c b/log.c
227 @@ -715,7 +715,8 @@ static void log_formatted(enum logcode code, const char *format, const char *op,
228 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
229 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
230 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
231 - c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
232 + c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.'
233 + : S_ISLNK(file->mode) ? 'U' : 'u';
234 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
235 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
237 diff --git a/options.c b/options.c
240 @@ -59,6 +59,7 @@ int preserve_specials = 0;
241 int preserve_uid = 0;
242 int preserve_gid = 0;
243 int preserve_times = 0;
244 +int preserve_atimes = 0;
248 @@ -698,6 +699,7 @@ void usage(enum logcode F)
249 rprintf(F," -D same as --devices --specials\n");
250 rprintf(F," -t, --times preserve modification times\n");
251 rprintf(F," -O, --omit-dir-times omit directories from --times\n");
252 + rprintf(F," -U, --atimes preserve access (last-used) times\n");
253 rprintf(F," --super receiver attempts super-user activities\n");
254 #ifdef SUPPORT_XATTRS
255 rprintf(F," --fake-super store/recover privileged attrs using xattrs\n");
256 @@ -845,6 +847,9 @@ static struct poptOption long_options[] = {
257 {"times", 't', POPT_ARG_VAL, &preserve_times, 2, 0, 0 },
258 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
259 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
260 + {"atimes", 'U', POPT_ARG_VAL, &preserve_atimes, 1, 0, 0 },
261 + {"no-atimes", 0, POPT_ARG_VAL, &preserve_atimes, 0, 0, 0 },
262 + {"no-U", 0, POPT_ARG_VAL, &preserve_atimes, 0, 0, 0 },
263 {"omit-dir-times", 'O', POPT_ARG_VAL, &omit_dir_times, 1, 0, 0 },
264 {"no-omit-dir-times",0, POPT_ARG_VAL, &omit_dir_times, 0, 0, 0 },
265 {"no-O", 0, POPT_ARG_VAL, &omit_dir_times, 0, 0, 0 },
266 @@ -2306,6 +2311,8 @@ void server_options(char **args, int *argc_p)
270 + if (preserve_atimes)
274 else if (preserve_executability && am_sender)
275 diff --git a/rsync.c b/rsync.c
278 @@ -384,6 +384,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
281 int change_uid, change_gid;
282 + time_t atime, mtime;
283 mode_t new_mode = file->mode;
286 @@ -422,20 +423,38 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
287 set_xattr(fname, file, fnamecmp, sxp);
290 + /* This code must be the first update in the function due to
291 + * how it uses the "updated" variable. */
292 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1))
293 flags |= ATTRS_SKIP_MTIME;
294 + if (!atimes_ndx || S_ISDIR(sxp->st.st_mode))
295 + flags |= ATTRS_SKIP_ATIME;
296 if (!(flags & ATTRS_SKIP_MTIME)
297 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
298 - int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
299 + mtime = file->modtime;
302 + mtime = sxp->st.st_mtime;
303 + if (!(flags & ATTRS_SKIP_ATIME)) {
304 + time_t file_atime = f_atime(file);
305 + if (cmp_time(sxp->st.st_atime, file_atime) != 0) {
306 + atime = file_atime;
309 + atime = sxp->st.st_atime;
311 + atime = sxp->st.st_atime;
313 + int ret = set_times(fname, mtime, atime, sxp->st.st_mode);
315 rsyserr(FERROR_XFER, errno, "failed to set times on %s",
319 - if (ret == 0) /* ret == 1 if symlink could not be set */
322 + if (ret > 0) { /* ret == 1 if symlink could not be set */
324 file->flags |= FLAG_TIME_FAILED;
328 change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
329 @@ -570,7 +589,7 @@ int finish_transfer(const char *fname, const char *fnametmp,
331 /* Change permissions before putting the file into place. */
332 set_file_attrs(fnametmp, file, NULL, fnamecmp,
333 - ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
334 + ok_to_set_time ? 0 : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME);
336 /* move tmp file over real file */
337 if (DEBUG_GTE(RECV, 1))
338 @@ -597,7 +616,7 @@ int finish_transfer(const char *fname, const char *fnametmp,
341 set_file_attrs(fnametmp, file, NULL, fnamecmp,
342 - ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
343 + ok_to_set_time ? 0 : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME);
345 if (temp_copy_name) {
346 if (do_rename(fnametmp, fname) < 0) {
347 diff --git a/rsync.h b/rsync.h
351 #define XMIT_GROUP_NAME_FOLLOWS (1<<11) /* protocols 30 - now */
352 #define XMIT_HLINK_FIRST (1<<12) /* protocols 30 - now (HLINKED files only) */
353 #define XMIT_IO_ERROR_ENDLIST (1<<12) /* protocols 31 - now (w/XMIT_EXTENDED_FLAGS) */
354 +#define XMIT_SAME_ATIME (1<<13) /* protocols ?? - now */
356 /* These flags are used in the live flist data. */
360 #define ATTRS_REPORT (1<<0)
361 #define ATTRS_SKIP_MTIME (1<<1)
362 +#define ATTRS_SKIP_ATIME (1<<2)
365 #define NORMAL_FLUSH 0
366 @@ -656,12 +658,14 @@ extern int file_extra_cnt;
367 extern int inc_recurse;
370 +extern int atimes_ndx;
372 extern int xattrs_ndx;
374 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
375 #define EXTRA_LEN (sizeof (union file_extras))
376 #define PTR_EXTRA_CNT ((sizeof (char *) + EXTRA_LEN - 1) / EXTRA_LEN)
377 +#define TIME_EXTRA_CNT ((SIZEOF_TIME_T + EXTRA_LEN - 1) / EXTRA_LEN)
378 #define DEV_EXTRA_CNT 2
379 #define DIRNODE_EXTRA_CNT 3
380 #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN)
381 diff --git a/rsync.yo b/rsync.yo
384 @@ -356,6 +356,7 @@ to the detailed description below for a complete description. verb(
385 -D same as --devices --specials
386 -t, --times preserve modification times
387 -O, --omit-dir-times omit directories from --times
388 + -U, --atimes preserve access (use) times
389 --super receiver attempts super-user activities
390 --fake-super store/recover privileged attrs using xattrs
391 -S, --sparse handle sparse files efficiently
392 @@ -1078,6 +1079,12 @@ it is preserving modification times (see bf(--times)). If NFS is sharing
393 the directories on the receiving side, it is a good idea to use bf(-O).
394 This option is inferred if you use bf(--backup) without bf(--backup-dir).
396 +dit(bf(-U, --atimes)) This tells rsync to set the access (use) times of the
397 +destination files to the same value as the source files. Note that the
398 +reading of the source file may update the atime of the source files, so
399 +repeated rsync runs with --atimes may be needed if you want to force the
400 +access-time values to be 100% identical on the two systems.
402 dit(bf(--super)) This tells the receiving side to attempt super-user
403 activities even if the receiving rsync wasn't run by the super-user. These
404 activities include: preserving users via the bf(--owner) option, preserving
405 @@ -1907,7 +1914,10 @@ quote(itemization(
406 sender's value (requires bf(--owner) and super-user privileges).
407 it() A bf(g) means the group is different and is being updated to the
408 sender's value (requires bf(--group) and the authority to set the group).
409 - it() The bf(u) slot is reserved for future use.
410 + it() A bf(u) means the access (use) time is different and is being updated to
411 + the sender's value (requires bf(--atimes)). An alternate value of bf(U)
412 + means that the access time will be set to the transfer time, which happens
413 + when a symlink or directory is updated.
414 it() The bf(a) means that the ACL information changed.
415 it() The bf(x) means that the extended attribute information changed.
417 diff --git a/testsuite/atimes.test b/testsuite/atimes.test
420 +++ b/testsuite/atimes.test
424 +# Test rsync copying atimes
426 +. "$suitedir/rsync.fns"
430 +touch "$fromdir/foo"
431 +touch -a -t 200102031717.42 "$fromdir/foo"
435 +checkit "$RSYNC -rtUgvvv \"$fromdir/\" \"$todir/\"" "$fromdir" "$todir"
437 +# The script would have aborted on error, so getting here means we've won.
439 diff --git a/testsuite/rsync.fns b/testsuite/rsync.fns
440 --- a/testsuite/rsync.fns
441 +++ b/testsuite/rsync.fns
442 @@ -215,6 +215,10 @@ checkit() {
443 # We can just write everything to stdout/stderr, because the
444 # wrapper hides it unless there is a problem.
446 + if test x$TLS_ARGS = x--atimes; then
447 + ( cd "$2" && rsync_ls_lR . ) > "$tmpdir/ls-from"
450 echo "Running: \"$1\""
453 @@ -222,10 +226,13 @@ checkit() {
457 + if test x$TLS_ARGS != x--atimes; then
458 + ( cd "$2" && rsync_ls_lR . ) > "$tmpdir/ls-from"
462 echo "check how the directory listings compare with diff:"
464 - ( cd "$2" && rsync_ls_lR . ) > "$tmpdir/ls-from"
465 ( cd "$3" && rsync_ls_lR . ) > "$tmpdir/ls-to"
466 diff $diffopt "$tmpdir/ls-from" "$tmpdir/ls-to" || failed=YES
468 diff --git a/tls.c b/tls.c
471 @@ -108,6 +108,8 @@ static int stat_xattr(const char *fname, STRUCT_STAT *fst)
475 +static int display_atimes = 0;
477 static void failed(char const *what, char const *where)
479 fprintf(stderr, PROGRAM ": %s %s: %s\n",
480 @@ -115,12 +117,29 @@ static void failed(char const *what, char const *where)
484 +static void storetime(char *dest, time_t t, size_t destsize)
487 + struct tm *mt = gmtime(&t);
489 + snprintf(dest, destsize,
490 + "%04d-%02d-%02d %02d:%02d:%02d ",
491 + (int)mt->tm_year + 1900,
492 + (int)mt->tm_mon + 1,
498 + strlcpy(dest, " ", destsize);
501 static void list_file(const char *fname)
504 char permbuf[PERMSTRING_SIZE];
511 if (do_lstat(fname, &buf) < 0)
512 @@ -159,19 +178,11 @@ static void list_file(const char *fname)
514 permstring(permbuf, buf.st_mode);
516 - if (buf.st_mtime) {
517 - mt = gmtime(&buf.st_mtime);
519 - snprintf(datebuf, sizeof datebuf,
520 - "%04d-%02d-%02d %02d:%02d:%02d",
521 - (int)mt->tm_year + 1900,
522 - (int)mt->tm_mon + 1,
528 - strlcpy(datebuf, " ", sizeof datebuf);
529 + storetime(mtimebuf, buf.st_mtime, sizeof mtimebuf);
530 + if (display_atimes)
531 + storetime(atimebuf, S_ISDIR(buf.st_mode) ? 0 : buf.st_atime, sizeof atimebuf);
533 + atimebuf[0] = '\0';
535 /* TODO: Perhaps escape special characters in fname? */
537 @@ -182,13 +193,14 @@ static void list_file(const char *fname)
538 (long)minor(buf.st_rdev));
540 printf("%15s", do_big_num(buf.st_size, 1, NULL));
541 - printf(" %6ld.%-6ld %6ld %s %s%s\n",
542 + printf(" %6ld.%-6ld %6ld %s%s%s%s\n",
543 (long)buf.st_uid, (long)buf.st_gid, (long)buf.st_nlink,
544 - datebuf, fname, linkbuf);
545 + mtimebuf, atimebuf, fname, linkbuf);
548 static struct poptOption long_options[] = {
549 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
550 + {"atimes", 'U', POPT_ARG_NONE, &display_atimes, 0, 0, 0},
551 {"link-times", 'l', POPT_ARG_NONE, &link_times, 0, 0, 0 },
552 {"link-owner", 'L', POPT_ARG_NONE, &link_owner, 0, 0, 0 },
553 #ifdef SUPPORT_XATTRS
554 @@ -204,6 +216,7 @@ static void tls_usage(int ret)
555 fprintf(F,"usage: " PROGRAM " [OPTIONS] FILE ...\n");
556 fprintf(F,"Trivial file listing program for portably checking rsync\n");
557 fprintf(F,"\nOptions:\n");
558 + fprintf(F," -U, --atimes display access (last-used) times\n");
559 fprintf(F," -l, --link-times display the time on a symlink\n");
560 fprintf(F," -L, --link-owner display the owner+group on a symlink\n");
561 #ifdef SUPPORT_XATTRS
562 diff --git a/util.c b/util.c
565 @@ -123,7 +123,7 @@ NORETURN void overflow_exit(const char *str)
566 exit_cleanup(RERR_MALLOC);
569 -int set_modtime(const char *fname, time_t modtime, mode_t mode)
570 +int set_times(const char *fname, time_t modtime, time_t atime, mode_t mode)
572 #if !defined HAVE_LUTIMES || !defined HAVE_UTIMES
574 @@ -131,9 +131,13 @@ int set_modtime(const char *fname, time_t modtime, mode_t mode)
577 if (DEBUG_GTE(TIME, 1)) {
578 - rprintf(FINFO, "set modtime of %s to (%ld) %s",
579 + char mtimebuf[200];
581 + strlcpy(mtimebuf, timestring(modtime), sizeof mtimebuf);
583 + "set modtime, atime of %s to (%ld) %s, (%ld) %s\n",
584 fname, (long)modtime,
585 - asctime(localtime(&modtime)));
586 + mtimebuf, (long)atime, timestring(atime));
590 @@ -142,7 +146,7 @@ int set_modtime(const char *fname, time_t modtime, mode_t mode)
594 - t[0].tv_sec = time(NULL);
595 + t[0].tv_sec = atime;
597 t[1].tv_sec = modtime;
599 @@ -156,12 +160,12 @@ int set_modtime(const char *fname, time_t modtime, mode_t mode)
600 return utimes(fname, t);
601 #elif defined HAVE_STRUCT_UTIMBUF
603 - tbuf.actime = time(NULL);
604 + tbuf.actime = atime;
605 tbuf.modtime = modtime;
606 return utime(fname,&tbuf);
607 #elif defined HAVE_UTIME
612 return utime(fname,t);