- Cache the file's ctime too, for an even tighter sum-is-right check.
authorWayne Davison <wayned@samba.org>
Sun, 17 Jun 2007 15:22:43 +0000 (15:22 +0000)
committerWayne Davison <wayned@samba.org>
Sun, 17 Jun 2007 15:22:43 +0000 (15:22 +0000)
- Fixed a bug where a dir with regular files that were all excluded
  would get its .rsyncsums file removed.

checksum-updating.diff

index bbbceb1..301ba5e 100644 (file)
@@ -60,23 +60,24 @@ we should still update the .rsyncsums file if we compute a new checksum.
  int io_error;
  int checksum_len;
  dev_t filesystem_dev; /* used to implement -x */
-@@ -101,6 +106,8 @@ static char tmp_sum[MAX_DIGEST_LEN];
+@@ -101,6 +106,9 @@ static char tmp_sum[MAX_DIGEST_LEN];
  static char empty_sum[MAX_DIGEST_LEN];
  static int flist_count_offset; /* for --delete --progress */
  static int dir_count = 0;
 +static struct file_list *checksum_flist = NULL;
 +static int checksum_matches = 0;
++static int regular_skipped = 0;
  
  static void clean_flist(struct file_list *flist, int strip_root);
  static void output_flist(struct file_list *flist);
-@@ -317,6 +324,259 @@ static void flist_done_allocating(struct
+@@ -317,6 +325,275 @@ static void flist_done_allocating(struct
                flist->pool_boundary = ptr;
  }
  
 +/* The len count is the length of the basename + 1 for the null. */
 +static void add_checksum(const char *dirname, const char *basename, int len,
-+                       OFF_T file_length, time_t mtime, const char *sum,
-+                       const char *alt_sum, int flags)
++                       OFF_T file_length, time_t mtime, time_t ctime,
++                       const char *sum, const char *alt_sum, int flags)
 +{
 +      struct file_struct *file;
 +      int alloc_len, extra_len;
@@ -88,8 +89,9 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +      if (len < 0)
 +              len = strlen(basename) + 1;
 +
-+      extra_len = (file_extra_cnt + (file_length > 0xFFFFFFFFu) + SUM_EXTRA_CNT)
-+                * EXTRA_LEN;
++      extra_len = (file_extra_cnt + (file_length > 0xFFFFFFFFu)
++                 + SUM_EXTRA_CNT + TIME_EXTRA_CNT)
++                * EXTRA_LEN;
 +#if EXTRA_ROUNDING > 0
 +      if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
 +              extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
@@ -121,6 +123,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +      file->dirname = dirname;
 +      bp = (char*)F_SUM(file);
 +      memcpy(bp, sum, checksum_len);
++      memcpy(bp - SIZEOF_TIME_T, &ctime, SIZEOF_TIME_T);
 +
 +      flist_expand(checksum_flist, 1);
 +      checksum_flist->files[checksum_flist->count++] = file;
@@ -135,7 +138,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +      char line[MAXPATHLEN+1024], fbuf[MAXPATHLEN], sum[MAX_DIGEST_LEN];
 +      const char *alt_sum = NULL;
 +      OFF_T file_length;
-+      time_t mtime;
++      time_t mtime, ctime;
 +      int len, dlen, i, flags;
 +      char *cp;
 +      FILE *fp;
@@ -151,6 +154,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +      checksum_flist->low = 0;
 +      checksum_flist->high = -1;
 +      checksum_matches = 0;
++      regular_skipped = 0;
 +
 +      if (dirname) {
 +              dlen = strlcpy(fbuf, dirname, sizeof fbuf);
@@ -233,6 +237,13 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +                      break;
 +              while (*++cp == ' ') {}
 +
++              ctime = 0;
++              while (isDigit(cp))
++                      ctime = ctime * 10 + *cp++ - '0';
++              if (*cp != ' ')
++                      break;
++              while (*++cp == ' ') {}
++
 +              len = strlen(cp);
 +              while (len && (cp[len-1] == '\n' || cp[len-1] == '\r'))
 +                      len--;
@@ -248,7 +259,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +                      checksum_matches++;
 +              }
 +
-+              add_checksum(dirname, cp, len, file_length, mtime,
++              add_checksum(dirname, cp, len, file_length, mtime, ctime,
 +                           sum, alt_sum, flags);
 +      }
 +      fclose(fp);
@@ -258,49 +269,49 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +
 +static void write_checksums(const char *dirname)
 +{
-+      char buf[MAXPATHLEN+1024];
++      char fbuf[MAXPATHLEN];
 +      int count = checksum_flist->count;
 +      int new_entries = count > checksum_flist->high + 1;
-+      int orphan_entires = count != checksum_matches;
++      int counts_match = count == checksum_matches;
++      int no_skipped = regular_skipped == 0;
++      time_t latest_time = 0;
 +      FILE *out_fp;
 +      int i;
 +
-+
-+      for (i = checksum_flist->high + 1; i < count; i++) {
-+              struct file_struct *file = checksum_flist->sorted[i];
-+              file->flags |= FLAG_SUM_FOUND;
-+      }
-+
 +      clean_flist(checksum_flist, 0);
++
 +      checksum_flist->count = 0;
 +      checksum_matches = 0;
++      regular_skipped = 0;
 +
 +      if (dry_run)
 +              return;
 +
 +      if (dirname) {
-+              if (pathjoin(buf, sizeof buf, dirname, ".rsyncsums") >= sizeof buf)
++              if (pathjoin(fbuf, sizeof fbuf, dirname, ".rsyncsums") >= sizeof fbuf)
 +                      return;
 +      } else
-+              strlcpy(buf, ".rsyncsums", sizeof buf);
++              strlcpy(fbuf, ".rsyncsums", sizeof fbuf);
 +
-+      if (checksum_flist->high - checksum_flist->low < 0) {
-+              unlink(buf);
++      if (checksum_flist->high - checksum_flist->low < 0 && no_skipped) {
++              unlink(fbuf);
 +              return;
 +      }
 +
-+      if (!new_entries && !orphan_entires)
++      if (!new_entries && counts_match)
 +              return;
 +
-+      if (!(out_fp = fopen(buf, "w")))
++      if (!(out_fp = fopen(fbuf, "w")))
 +              return;
 +
 +      for (i = checksum_flist->low; i <= checksum_flist->high; i++) {
 +              struct file_struct *file = checksum_flist->sorted[i];
 +              const char *cp = F_SUM(file);
 +              const char *end = cp + checksum_len;
++              time_t ctime;
 +              if (!(file->flags & FLAG_SUM_FOUND))
 +                      continue;
++              memcpy(&ctime, cp - SIZEOF_TIME_T, SIZEOF_TIME_T);
 +              if (protocol_version >= 30) {
 +                      fprintf(out_fp, "%s ", 
 +                              file->basename + strlen(file->basename) + 1);
@@ -318,18 +329,24 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +                      fprintf(out_fp, " %s", 
 +                              file->basename + strlen(file->basename) + 1);
 +              }
-+              fprintf(out_fp, " %10.0f %10ld %s\n",
++              fprintf(out_fp, " %10.0f %10ld %10ld %s\n",
 +                      (double)F_LENGTH(file), (long)file->modtime,
-+                      file->basename);
++                      (long)ctime, file->basename);
++              if (file->modtime > ctime)
++                      ctime = file->modtime;
++              if (ctime > latest_time)
++                      latest_time = ctime;
 +      }
 +
 +      fclose(out_fp);
++
++      set_modtime(fbuf, latest_time, latest_time);
 +}
 +
  int push_pathname(const char *dir, int len)
  {
        if (dir == pathname)
-@@ -973,34 +1233,24 @@ static struct file_struct *recv_file_ent
+@@ -973,34 +1250,24 @@ static struct file_struct *recv_file_ent
        return file;
  }
  
@@ -370,7 +387,16 @@ we should still update the .rsyncsums file if we compute a new checksum.
        char *bp;
  
        if (strlcpy(thisname, fname, sizeof thisname)
-@@ -1115,9 +1365,16 @@ struct file_struct *make_file(const char
+@@ -1077,6 +1344,8 @@ struct file_struct *make_file(const char
+       if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
+               if (ignore_perishable)
+                       non_perishable_cnt++;
++              if (S_ISREG(st.st_mode))
++                      regular_skipped++;
+               return NULL;
+       }
+@@ -1115,9 +1384,16 @@ struct file_struct *make_file(const char
                        memcpy(lastdir, thisname, len);
                        lastdir[len] = '\0';
                        lastdir_len = len;
@@ -388,7 +414,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
        basename_len = strlen(basename) + 1; /* count the '\0' */
  
  #ifdef SUPPORT_LINKS
-@@ -1193,11 +1450,36 @@ struct file_struct *make_file(const char
+@@ -1193,11 +1469,40 @@ struct file_struct *make_file(const char
        }
  #endif
  
@@ -401,7 +427,10 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +              int j;
 +              if (flist && (j = flist_find(checksum_flist, file)) >= 0) {
 +                      struct file_struct *fp = checksum_flist->sorted[j];
-+                      if (fp->modtime == st.st_mtime && F_LENGTH(fp) == st.st_size) {
++                      time_t ctime;
++                      memcpy(&ctime, F_SUM(fp) - SIZEOF_TIME_T, SIZEOF_TIME_T);
++                      if (F_LENGTH(fp) == st.st_size
++                       && fp->modtime == st.st_mtime && ctime == st.st_ctime) {
 +                              if (fp->flags & FLAG_SUM_MISSING) {
 +                                      fp->flags &= ~FLAG_SUM_MISSING;
 +                                      file_checksum(thisname, tmp_sum, st.st_size);
@@ -420,7 +449,8 @@ we should still update the .rsyncsums file if we compute a new checksum.
 +                      file_checksum(thisname, tmp_sum, st.st_size);
 +                      if (checksum_updating && flist) {
 +                              add_checksum(file->dirname, basename, basename_len,
-+                                           st.st_size, st.st_mtime, tmp_sum, NULL, 0);
++                                           st.st_size, st.st_mtime, st.st_ctime,
++                                           tmp_sum, NULL, FLAG_SUM_FOUND);
 +                      }
 +              }
 +      }
@@ -428,7 +458,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
        /* This code is only used by the receiver when it is building
         * a list of files for a delete pass. */
        if (keep_dirlinks && linkname_len && flist) {
-@@ -1241,14 +1523,14 @@ void unmake_file(struct file_struct *fil
+@@ -1241,14 +1546,14 @@ void unmake_file(struct file_struct *fil
  
  static struct file_struct *send_file_name(int f, struct file_list *flist,
                                          char *fname, STRUCT_STAT *stp,
@@ -445,7 +475,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
        if (!file)
                return NULL;
  
-@@ -1442,7 +1724,7 @@ static void send_directory(int f, struct
+@@ -1442,7 +1747,7 @@ static void send_directory(int f, struct
        DIR *d;
        int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
        int start = flist->count;
@@ -454,7 +484,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
  
        assert(flist != NULL);
  
-@@ -1471,7 +1753,7 @@ static void send_directory(int f, struct
+@@ -1471,7 +1776,7 @@ static void send_directory(int f, struct
                        continue;
                }
  
@@ -463,7 +493,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
        }
  
        fbuf[len] = '\0';
-@@ -1483,6 +1765,9 @@ static void send_directory(int f, struct
+@@ -1483,6 +1788,9 @@ static void send_directory(int f, struct
  
        closedir(d);
  
@@ -473,7 +503,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
        if (f >= 0 && recurse && !divert_dirs) {
                int i, end = flist->count - 1;
                /* send_if_directory() bumps flist->count, so use "end". */
-@@ -2206,7 +2491,7 @@ void flist_free(struct file_list *flist)
+@@ -2206,7 +2514,7 @@ void flist_free(struct file_list *flist)
  
        if (!flist->prev || !flist_cnt)
                pool_destroy(flist->file_pool);
@@ -482,7 +512,7 @@ we should still update the .rsyncsums file if we compute a new checksum.
                pool_free_old(flist->file_pool, flist->pool_boundary);
  
        if (flist->sorted && flist->sorted != flist->files)
-@@ -2225,6 +2510,7 @@ static void clean_flist(struct file_list
+@@ -2225,6 +2533,7 @@ static void clean_flist(struct file_list
        if (!flist)
                return;
        if (flist->count == 0) {
@@ -563,7 +593,15 @@ we should still update the .rsyncsums file if we compute a new checksum.
                args[ac++] = "--append";
 --- old/rsync.h
 +++ new/rsync.h
-@@ -1070,6 +1070,12 @@ isDigit(const char *ptr)
+@@ -589,6 +589,7 @@ extern int preserve_xattrs;
+ #define EXTRA_LEN (sizeof (union file_extras))
+ #define PTR_EXTRA_LEN ((sizeof (char *) + EXTRA_LEN - 1) / EXTRA_LEN)
+ #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN)
++#define TIME_EXTRA_CNT ((SIZEOF_TIME_T + EXTRA_LEN - 1) / EXTRA_LEN)
+ #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
+ #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
+@@ -1070,6 +1071,12 @@ isDigit(const char *ptr)
  }
  
  static inline int
@@ -665,3 +703,190 @@ we should still update the .rsyncsums file if we compute a new checksum.
  dit(bf(read only)) The "read only" option determines whether clients
  will be able to upload files or not. If "read only" is true then any
  attempted uploads will fail. If "read only" is false then uploads will
+--- old/support/rsyncsums
++++ new/support/rsyncsums
+@@ -0,0 +1,184 @@
++#!/usr/bin/perl -w
++use strict;
++
++use Getopt::Long;
++use Cwd qw(abs_path cwd);
++use Digest::MD4;
++use Digest::MD5;
++
++our $SUMS_FILE = '.rsyncsums';
++
++our($recurse_opt, $force_reading, $help_opt);
++our $verbosity = 0;
++
++&Getopt::Long::Configure('bundling');
++&usage if !&GetOptions(
++    'recurse|r' => \$recurse_opt,
++    'force|f' => \$force_reading,
++    'verbose|v+' => \$verbosity,
++    'help|h' => \$help_opt,
++) || $help_opt;
++
++my $start_dir = cwd();
++
++my @dirs = @ARGV;
++@dirs = '.' unless @dirs;
++foreach (@dirs) {
++    $_ = abs_path($_);
++}
++
++$| = 1;
++
++my $md4 = Digest::MD4->new;
++my $md5 = Digest::MD5->new;
++
++while (@dirs) {
++    my $dir = shift @dirs;
++
++    if (!chdir($dir)) {
++      warn "Unable to chdir to $dir: $!\n";
++      next;
++    }
++    if (!opendir(DP, '.')) {
++      warn "Unable to opendir $dir: $!\n";
++      next;
++    }
++
++    if ($verbosity) {
++      my $reldir = $dir;
++      $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
++      print "$reldir ... ";
++    }
++
++    my $sums_mtime = (stat($SUMS_FILE))[9];
++    my %cache;
++    my @fp;
++    my @subdirs;
++    my $cnt = 0;
++    my $latest_time = 0;
++    while (defined(my $fn = readdir(DP))) {
++      next if $fn =~ /^\.\.?$/ || $fn =~ /^\Q$SUMS_FILE\E$/o || -l $fn;
++      if (-d _) {
++          push(@subdirs, "$dir/$fn");
++          next;
++      }
++      next unless -f _;
++
++      my($size,$mtime,$ctime) = (stat(_))[7,9,10];
++
++      $cache{$fn} = [ $size, $mtime, $ctime ];
++      $cnt++;
++
++      $latest_time = $mtime if $mtime > $latest_time;
++      $latest_time = $ctime if $ctime > $latest_time;
++    }
++
++    closedir DP;
++
++    unshift(@dirs, sort @subdirs) if $recurse_opt;
++
++    if (!$cnt) {
++      if (defined $sums_mtime) {
++          print "(removed $SUMS_FILE) " if $verbosity;
++          unlink($SUMS_FILE);
++      }
++      print "empty\n" if $verbosity;
++      next;
++    }
++
++    if (defined($sums_mtime) && $sums_mtime == $latest_time && !$force_reading) {
++      print "OK\n" if $verbosity;
++      next;
++    }
++
++    if (open(FP, '+<', $SUMS_FILE)) {
++      while (<FP>) {
++          chomp;
++          my($sum4, $sum5, $size, $mtime, $ctime, $fn) = split(' ', $_, 6);
++          my $ref = $cache{$fn};
++          if (defined $ref) {
++              if ($$ref[0] == $size
++               && $$ref[1] == $mtime && $$ref[2] == $ctime
++               && $sum4 !~ /=/ && $sum5 !~ /=/) {
++                  $$ref[3] = $sum4;
++                  $$ref[4] = $sum5;
++                  $cnt--;
++              } else {
++                  $$ref[3] = $$ref[4] = undef;
++              }
++          } else {
++              $cnt = -1; # Force rewrite due to removed line.
++          }
++      }
++    } else {
++      open(FP, '>', $SUMS_FILE) or die "Unable to write $dir/$SUMS_FILE: $!\n";
++      $cnt = -1;
++    }
++
++    if ($cnt) {
++      print "updating\n" if $verbosity;
++      while (my($fn, $ref) = each %cache) {
++          next if defined $$ref[3] && defined $$ref[4];
++          if (!open(IN, $fn)) {
++              print STDERR "Unable to read $fn: $!\n";
++              delete $cache{$fn};
++              next;
++          }
++
++          my($size,$mtime,$ctime) = (stat(IN))[7,9,10];
++          my($sum4, $sum5);
++
++          while (1) {
++              while (sysread(IN, $_, 64*1024)) {
++                  $md4->add($_);
++                  $md5->add($_);
++              }
++              $sum4 = $md4->hexdigest;
++              $sum5 = $md5->hexdigest;
++              print " $sum4 $sum5 $fn\n" if $verbosity > 1;
++              my($size2,$mtime2,$ctime2) = (stat(IN))[7,9,10];
++              last if $size == $size2 && $mtime == $mtime2 && $ctime == $ctime2;
++              $size = $size2;
++              $mtime = $mtime2;
++              $ctime = $ctime2;
++              sysseek(IN, 0, 0);
++          }
++          
++          close IN;
++
++          $cache{$fn} = [ $size, $mtime, $ctime, $sum4, $sum5 ];
++      }
++
++      $latest_time = 0;
++      seek(FP, 0, 0);
++      foreach my $fn (sort keys %cache) {
++          my $ref = $cache{$fn};
++          my($size, $mtime, $ctime, $sum4, $sum5) = @$ref;
++          printf FP '%s %s %10d %10d %s' . "\n", $sum4, $sum5, $size, $mtime, $ctime, $fn;
++
++          $latest_time = $mtime if $mtime > $latest_time;
++          $latest_time = $ctime if $ctime > $latest_time;
++      }
++      truncate(FP, tell(FP));
++    } else {
++      print "OK.\n" if $verbosity;
++    }
++
++    close FP;
++
++    utime $latest_time, $latest_time, $SUMS_FILE;
++}
++
++sub usage
++{
++    die <<EOT;
++Usage: rsyncsums [OPTIONS] [DIRS]
++
++Options:
++ -r, --recurse     Update $SUMS_FILE files in subdirectories too.
++ -f, --force       Force the reading of an $SUMS_FILE file that looks to be
++                   up-to-date.  (Useful for weeding out old entries.)
++ -v, --verbose     Mention what we're doing.  Repeat for more info.
++ -h, --help        Display this help message.
++EOT
++}