++static void write_checksums(const char *next_dirname, int whole_dir)
++{
++ static const char *dirname_save;
++ char fbuf[MAXPATHLEN];
++ const char *dirname;
++ int count, new_entries, counts_match, no_skipped;
++ time_t latest_time = 0;
++ FILE *out_fp;
++ int i;
++
++ dirname = dirname_save;
++ dirname_save = next_dirname;
++
++ if (!dirname)
++ return;
++
++ count = checksum_flist->count;
++ new_entries = checksum_updates != 0;
++ counts_match = count == checksum_matches;
++ no_skipped = whole_dir && regular_skipped == 0;
++
++ clean_flist(checksum_flist, 0);
++
++ checksum_flist->count = 0;
++ checksum_matches = 0;
++ checksum_updates = 0;
++ regular_skipped = 0;
++
++ if (dry_run)
++ return;
++
++ if (*dirname) {
++ if (pathjoin(fbuf, sizeof fbuf, dirname, ".rsyncsums") >= sizeof fbuf)
++ return;
++ } else
++ strlcpy(fbuf, ".rsyncsums", sizeof fbuf);
++
++ if (checksum_flist->high - checksum_flist->low < 0 && no_skipped) {
++ unlink(fbuf);
++ return;
++ }
++
++ if (!new_entries && (counts_match || !whole_dir))
++ return;
++
++ if (!(out_fp = fopen(fbuf, "w")))
++ return;
++
++ new_entries = 0;
++ 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;
++ const char *alt_sum = file->basename + strlen(file->basename) + 1;
++ time_t ctime;
++ if (whole_dir && !(file->flags & FLAG_SUM_KEEP))
++ continue;
++#if SIZEOF_TIME_T == 4
++ ctime = OPT_EXTRA(file, LEN64_BUMP(file) + SUM_EXTRA_CNT)->num;
++#else
++ memcpy(&ctime, cp - SIZEOF_TIME_T, SIZEOF_TIME_T);
++#endif
++ if (protocol_version >= 30)
++ fprintf(out_fp, "%s ", alt_sum);
++ if (file->flags & FLAG_SUM_MISSING) {
++ new_entries++;
++ do {
++ fprintf(out_fp, "==");
++ } while (++cp != end);
++ } else {
++ do {
++ fprintf(out_fp, "%02x", CVAL(cp, 0));
++ } while (++cp != end);
++ }
++ if (protocol_version < 30)
++ fprintf(out_fp, " %s", alt_sum);
++ if (*alt_sum == '=')
++ new_entries++;
++ fprintf(out_fp, " %10.0f %10.0f %10.0f %s\n",
++ (double)F_LENGTH(file), (double)file->modtime,
++ (double)ctime, file->basename);
++ if (file->modtime > ctime)
++ ctime = file->modtime;
++ if (ctime > latest_time)
++ latest_time = ctime;
++ }
++
++ fclose(out_fp);
++
++ if (whole_dir && new_entries == 0)
++ set_modtime(fbuf, latest_time, latest_time);
++ else
++ set_modtime(fbuf, latest_time-1, latest_time-1);
++}
++