The patches for 3.3.0.
[rsync-patches.git] / db.diff
1 Added some DB-access routines to help rsync keep extra filesystem info
2 about the files it is dealing with.  This adds both the --db=CONFIG_FILE
3 option and the "db config" daemon parameter.
4
5 Future improvements may include:
6
7  - Updating of MD4 checksums when transferring any file, even w/o -c.
8    To make that work we'd need to make the sender force checksum_seed to
9    0 when using a DB and having the receiving side check to see if it
10    got a 0 checksum_seed.
11
12  - Caching of path info that allows for the finding of files to use for
13    moving/linking/copying/alternate-basis-use.
14
15  - Extend DB support beyond MySQL and SQLite (PostgreSQL?).
16
17 To use this patch, run these commands for a successful build:
18
19     patch -p1 <patches/db.diff
20     ./prepare-source
21     ./configure
22     make
23
24 based-on: d1a1fec1340254926e17f5d83f848f7574286a33
25 diff --git a/.gitignore b/.gitignore
26 --- a/.gitignore
27 +++ b/.gitignore
28 @@ -16,6 +16,7 @@ aclocal.m4
29  /proto.h
30  /proto.h-tstamp
31  /rsync.1
32 +/rsyncdb.1
33  /rsyncd.conf.5
34  /autom4te*.cache
35  /confdefs.h
36 @@ -24,6 +25,7 @@ aclocal.m4
37  /getgroups
38  /gmon.out
39  /rsync
40 +/rsyncdb
41  /rsync-ssl
42  /stunnel-rsync
43  /stunnel-rsyncd.conf
44 diff --git a/Makefile.in b/Makefile.in
45 --- a/Makefile.in
46 +++ b/Makefile.in
47 @@ -6,6 +6,7 @@ datarootdir=@datarootdir@
48  exec_prefix=@exec_prefix@
49  stunnel4=@STUNNEL4@
50  bindir=@bindir@
51 +sbindir=@sbindir@
52  mandir=@mandir@
53  
54  LIBS=@LIBS@
55 @@ -29,7 +30,7 @@ VERSION=@RSYNC_VERSION@
56  .SUFFIXES:
57  .SUFFIXES: .c .o
58  
59 -GENFILES=configure.sh aclocal.m4 config.h.in proto.h proto.h-tstamp rsync.1 rsyncd.conf.5
60 +GENFILES=configure.sh aclocal.m4 config.h.in proto.h proto.h-tstamp rsync.1 rsyncdb.1 rsyncd.conf.5
61  HEADERS=byteorder.h config.h errcode.h proto.h rsync.h ifuncs.h itypes.h inums.h \
62         lib/pool_alloc.h
63  LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o lib/md5.o \
64 @@ -39,7 +40,7 @@ zlib_OBJS=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
65  OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
66         util.o util2.o main.o checksum.o match.o syscall.o log.o backup.o delete.o
67  OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o hashtable.o \
68 -       fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
69 +       fileio.o batch.o clientname.o chmod.o db.o acls.o xattrs.o
70  OBJS3=progress.o pipe.o
71  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
72  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
73 @@ -63,14 +64,17 @@ CHECK_OBJS=tls.o testrun.o getgroups.o getfsdev.o t_stub.o t_unsafe.o trimslash.
74         $(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) -c $< @CC_SHOBJ_FLAG@
75  @OBJ_RESTORE@
76  
77 -all: Makefile rsync$(EXEEXT) rsync-ssl stunnel-rsync stunnel-rsyncd.conf @MAKE_MAN@
78 +all: Makefile rsync$(EXEEXT) rsyncdb$(EXEEXT) rsync-ssl stunnel-rsync stunnel-rsyncd.conf @MAKE_MAN@
79  
80  install: all
81 -       -${MKDIR_P} ${DESTDIR}${bindir}
82 +       -${MKDIR_P} ${DESTDIR}${bindir} ${DESTDIR}${sbindir}
83         ${INSTALLCMD} ${INSTALL_STRIP} -m 755 rsync$(EXEEXT) ${DESTDIR}${bindir}
84 +       rsync -ilt rsyncdb$(EXEEXT) ${DESTDIR}${bindir}/
85 +       ${INSTALLCMD} -m 755 rsyncdb-mountinfo ${DESTDIR}${sbindir}
86         -${MKDIR_P} ${DESTDIR}${mandir}/man1
87         -${MKDIR_P} ${DESTDIR}${mandir}/man5
88         if test -f rsync.1; then ${INSTALLMAN} -m 644 rsync.1 ${DESTDIR}${mandir}/man1; fi
89 +       if test -f rsyncdb.1; then ${INSTALLMAN} -m 644 rsyncdb.1 ${DESTDIR}${mandir}/man1; fi
90         if test -f rsyncd.conf.5; then ${INSTALLMAN} -m 644 rsyncd.conf.5 ${DESTDIR}${mandir}/man5; fi
91  
92  install-ssl-client: rsync-ssl stunnel-rsync
93 @@ -93,6 +97,9 @@ install-strip:
94  rsync$(EXEEXT): $(OBJS)
95         $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
96  
97 +rsyncdb$(EXEEXT): rsync$(EXEEXT)
98 +       ln -s rsync$(EXEEXT) rsyncdb$(EXEEXT)
99 +
100  $(OBJS): $(HEADERS)
101  $(CHECK_OBJS): $(HEADERS)
102  
103 @@ -212,22 +219,27 @@ proto.h: proto.h-tstamp
104  proto.h-tstamp: $(srcdir)/*.c $(srcdir)/lib/compat.c config.h
105         perl $(srcdir)/mkproto.pl $(srcdir)/*.c $(srcdir)/lib/compat.c
106  
107 -man: rsync.1 rsyncd.conf.5 man-copy
108 +man: rsync.1 rsyncdb.1 rsyncd.conf.5 man-copy
109  
110  man-copy:
111         @if test -f rsync.1; then :; elif test -f $(srcdir)/rsync.1; then echo 'Copying srcdir rsync.1'; cp -p $(srcdir)/rsync.1 .; else echo "NOTE: rsync.1 cannot be created."; fi
112 +       @if test -f rsyncdb.1; then :; elif test -f $(srcdir)/rsyncdb.1; then echo 'Copying srcdir rsyncdb.1'; cp -p $(srcdir)/rsyncdb.1 .; else echo "NOTE: rsyncdb.1 cannot be created."; fi
113         @if test -f rsyncd.conf.5; then :; elif test -f $(srcdir)/rsyncd.conf.5; then echo 'Copying srcdir rsyncd.conf.5'; cp -p $(srcdir)/rsyncd.conf.5 .; else echo "NOTE: rsyncd.conf.5 cannot be created."; fi
114  
115  rsync.1: rsync.yo
116         yodl2man -o rsync.1 $(srcdir)/rsync.yo
117         -$(srcdir)/tweak_manpage rsync.1
118  
119 +rsyncdb.1: rsyncdb.yo
120 +       yodl2man -o rsyncdb.1 $(srcdir)/rsyncdb.yo
121 +       -$(srcdir)/tweak_manpage rsyncdb.1
122 +
123  rsyncd.conf.5: rsyncd.conf.yo
124         yodl2man -o rsyncd.conf.5 $(srcdir)/rsyncd.conf.yo
125         -$(srcdir)/tweak_manpage rsyncd.conf.5
126  
127  clean: cleantests
128 -       rm -f *~ $(OBJS) $(CHECK_PROGS) $(CHECK_OBJS) $(CHECK_SYMLINKS) \
129 +       rm -f *~ $(OBJS) $(CHECK_PROGS) $(CHECK_OBJS) $(CHECK_SYMLINKS) rsyncdb$(EXEEXT) \
130                 rounding rounding.h *.old
131  
132  cleantests:
133 diff --git a/checksum.c b/checksum.c
134 --- a/checksum.c
135 +++ b/checksum.c
136 @@ -24,6 +24,7 @@
137  extern int checksum_seed;
138  extern int protocol_version;
139  extern int proper_seed_order;
140 +extern int use_db;
141  extern char *checksum_choice;
142  
143  #define CSUM_NONE 0
144 @@ -213,6 +214,9 @@ void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
145                         md5_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
146  
147                 md5_result(&m, (uchar *)sum);
148 +
149 +               if (use_db)
150 +                       db_set_checksum(5, st_p, sum);
151                 break;
152           case CSUM_MD4:
153           case CSUM_MD4_OLD:
154 @@ -233,6 +237,9 @@ void file_checksum(const char *fname, const STRUCT_STAT *st_p, char *sum)
155                         mdfour_update(&m, (uchar *)map_ptr(buf, i, remainder), remainder);
156  
157                 mdfour_result(&m, (uchar *)sum);
158 +
159 +               if (use_db)
160 +                       db_set_checksum(4, st_p, sum);
161                 break;
162           default:
163                 rprintf(FERROR, "invalid checksum-choice for the --checksum option (%d)\n", checksum_type);
164 diff --git a/cleanup.c b/cleanup.c
165 --- a/cleanup.c
166 +++ b/cleanup.c
167 @@ -27,6 +27,7 @@ extern int am_server;
168  extern int am_daemon;
169  extern int am_receiver;
170  extern int io_error;
171 +extern int use_db;
172  extern int keep_partial;
173  extern int got_xfer_error;
174  extern int protocol_version;
175 @@ -142,6 +143,12 @@ NORETURN void _exit_cleanup(int code, const char *file, int line)
176  #include "case_N.h"
177                 switch_step++;
178  
179 +               if (use_db)
180 +                       db_disconnect(False);
181 +
182 +               /* FALLTHROUGH */
183 +#include "case_N.h"
184 +
185                 if (cleanup_child_pid != -1) {
186                         int status;
187                         int pid = wait_process(cleanup_child_pid, &status, WNOHANG);
188 diff --git a/clientserver.c b/clientserver.c
189 --- a/clientserver.c
190 +++ b/clientserver.c
191 @@ -42,12 +42,15 @@ extern int numeric_ids;
192  extern int filesfrom_fd;
193  extern int remote_protocol;
194  extern int protocol_version;
195 +extern int always_checksum;
196 +extern int db_lax;
197  extern int io_timeout;
198  extern int no_detach;
199  extern int write_batch;
200  extern int default_af_hint;
201  extern int logfile_format_has_i;
202  extern int logfile_format_has_o_or_i;
203 +extern char *db_config;
204  extern char *bind_address;
205  extern char *config_file;
206  extern char *logfile_format;
207 @@ -685,6 +688,11 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
208  
209         log_init(1);
210  
211 +       if (*lp_db_config(i)) {
212 +               db_read_config(FLOG, lp_db_config(i));
213 +               db_lax = lp_db_lax(i);
214 +       }
215 +
216  #ifdef HAVE_PUTENV
217         if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
218                 int status;
219 @@ -890,6 +898,8 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
220  
221         am_server = 1; /* Don't let someone try to be tricky. */
222         quiet = 0;
223 +       db_config = NULL;
224 +
225         if (lp_ignore_errors(module_id))
226                 ignore_errors = 1;
227         if (write_batch < 0)
228 diff --git a/configure.ac b/configure.ac
229 --- a/configure.ac
230 +++ b/configure.ac
231 @@ -344,6 +344,7 @@ AC_CHECK_HEADERS(sys/fcntl.h sys/select.h fcntl.h sys/time.h sys/unistd.h \
232      unistd.h utime.h grp.h compat.h sys/param.h ctype.h sys/wait.h \
233      sys/ioctl.h sys/filio.h string.h stdlib.h sys/socket.h sys/mode.h \
234      sys/un.h sys/attr.h mcheck.h arpa/inet.h arpa/nameser.h locale.h \
235 +    mysql/mysql.h sqlite3.h \
236      netdb.h malloc.h float.h limits.h iconv.h libcharset.h langinfo.h \
237      sys/acl.h acl/libacl.h attr/xattr.h sys/xattr.h sys/extattr.h \
238      popt.h popt/popt.h linux/falloc.h netinet/in_systm.h netinet/ip.h \
239 @@ -1098,6 +1099,48 @@ if test x"$enable_acl_support" = x"no" -o x"$enable_xattr_support" = x"no" -o x"
240      fi
241  fi
242  
243 +AC_MSG_CHECKING([whether to include mysql DB support])
244 +AC_ARG_ENABLE(mysql,
245 +       AC_HELP_STRING([--disable-mysql],
246 +               [disable mysql DB support]))
247 +
248 +if test x"$enable_mysql" = x"no"; then
249 +    AC_MSG_RESULT(no)
250 +else
251 +    AC_MSG_RESULT([yes])
252 +    AC_CHECK_PROG(MYSQL_CONFIG, mysql_config, 1, 0)
253 +    if test x$MYSQL_CONFIG = x1; then
254 +       AC_MSG_CHECKING(for mysql version >= 4)
255 +       mysql_version=`mysql_config --version`
256 +       mysql_major_version=`echo $mysql_version | sed 's/\..*//'`
257 +       if test $mysql_major_version -lt 4; then
258 +           AC_MSG_RESULT(no.. skipping MySQL)
259 +       else
260 +           AC_MSG_RESULT(yes)
261 +
262 +           MYSQL_CFLAGS=`mysql_config --cflags`
263 +           MYSQL_LIBS=`mysql_config --libs`
264 +
265 +           CPPFLAGS="$CPPFLAGS $MYSQL_CFLAGS"
266 +           LIBS="$MYSQL_LIBS $LIBS"
267 +
268 +           AC_CHECK_LIB(mysqlclient, mysql_init)
269 +       fi
270 +    fi
271 +fi
272 +
273 +AC_MSG_CHECKING([whether to include sqlite DB support])
274 +AC_ARG_ENABLE(sqlite,
275 +       AC_HELP_STRING([--disable-sqlite],
276 +               [disable sqlite DB support]))
277 +
278 +if test x"$enable_sqlite" = x"no"; then
279 +    AC_MSG_RESULT(no)
280 +else
281 +    AC_CHECK_LIB(sqlite3, sqlite3_open)
282 +    AC_CHECK_FUNCS(sqlite3_open_v2 sqlite3_prepare_v2)
283 +fi
284 +
285  case "$CC" in
286  ' checker'*|checker*)
287      AC_DEFINE(FORCE_FD_ZERO_MEMSET, 1, [Used to make "checker" understand that FD_ZERO() clears memory.])
288 diff --git a/db.c b/db.c
289 new file mode 100644
290 --- /dev/null
291 +++ b/db.c
292 @@ -0,0 +1,1912 @@
293 +/*
294 + * Routines to access extended file info via DB.
295 + *
296 + * Copyright (C) 2008-2013 Wayne Davison
297 + *
298 + * This program is free software; you can redistribute it and/or modify
299 + * it under the terms of the GNU General Public License as published by
300 + * the Free Software Foundation; either version 3 of the License, or
301 + * (at your option) any later version.
302 + *
303 + * This program is distributed in the hope that it will be useful,
304 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
305 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
306 + * GNU General Public License for more details.
307 + *
308 + * You should have received a copy of the GNU General Public License along
309 + * with this program; if not, visit the http://fsf.org website.
310 + */
311 +
312 +#include "rsync.h"
313 +#include "ifuncs.h"
314 +#include "itypes.h"
315 +#include "inums.h"
316 +
317 +extern int protocol_version;
318 +extern int recurse;
319 +extern int same_db;
320 +extern int am_receiver;
321 +extern int am_generator;
322 +extern int db_clean, db_check, db_do_md4, db_do_md5, db_update, db_lax, db_init, db_mounts;
323 +extern int db_output_name, db_output_sum, db_output_info, db_output_unchanged, db_output_dirs, db_output_msgs;
324 +extern int saw_db_output_opt, saw_db_sum_opt;
325 +extern char *db_config;
326 +
327 +/* TODO: make this configurable */
328 +#define RSYNCDB_MOUNTS "/usr/sbin/rsyncdb-mountinfo"
329 +
330 +#if defined HAVE_MYSQL_MYSQL_H && defined HAVE_LIBMYSQLCLIENT
331 +#define USE_MYSQL
332 +#include <mysql/mysql.h>
333 +#include <mysql/errmsg.h>
334 +#endif
335 +
336 +#if defined HAVE_SQLITE3_H && defined HAVE_LIBSQLITE3
337 +#define USE_SQLITE
338 +#include <sqlite3.h>
339 +#ifndef HAVE_SQLITE3_OPEN_V2
340 +#define sqlite3_open_v2(dbname, dbhptr, flags, vfs) \
341 +       sqlite3_open(dbname, dbhptr)
342 +#endif
343 +#ifndef HAVE_SQLITE3_PREPARE_V2
344 +#define sqlite3_prepare_v2 sqlite3_prepare
345 +#endif
346 +#define MAX_LOCK_FAILURES 10
347 +#define LOCK_FAIL_MSLEEP 100
348 +#endif
349 +
350 +#define DB_TYPE_NONE 0
351 +#define DB_TYPE_MYSQL 1
352 +#define DB_TYPE_SQLITE 2
353 +
354 +int use_db = DB_TYPE_NONE;
355 +int select_many_sums = 0;
356 +
357 +#define PREP_NORM 0
358 +#define PREP_MOUNT 1
359 +
360 +static const char *dbhost = NULL, *dbuser = NULL, *dbpass = NULL, *dbname = NULL;
361 +static unsigned int dbport = 0;
362 +static int transaction_state = -1;
363 +
364 +static union {
365 +#ifdef USE_MYSQL
366 +    MYSQL *mysql;
367 +#endif
368 +#ifdef USE_SQLITE
369 +    sqlite3 *sqlite;
370 +#endif
371 +    void *all;
372 +} dbh;
373 +
374 +#define SEL_DEV 0
375 +#define SEL_SUM 1
376 +#define REP_SUM 2
377 +#define UPD_CTIME 3
378 +#define INS_MOUNT 4
379 +#define UPD_MOUNT 5 /* SQLite only */
380 +#define SEL_MOUNT 6
381 +#define UN_MOUNT 7
382 +#define DEL_SUMS 8
383 +#define INS_PRESENT 9
384 +#define MAX_PREP_CNT 10
385 +
386 +#define MAX_BIND_CNT 7
387 +#define MAX_RESULT_BINDS 32
388 +
389 +static union {
390 +#ifdef USE_MYSQL
391 +    MYSQL_STMT *mysql;
392 +#endif
393 +#ifdef USE_SQLITE
394 +    sqlite3_stmt *sqlite;
395 +#endif
396 +    void *all;
397 +} statements[MAX_PREP_CNT];
398 +
399 +static int md_num;
400 +static enum logcode log_code;
401 +
402 +#ifdef USE_MYSQL
403 +static unsigned int bind_disk_id, bind_mdnum;
404 +static int64 bind_devno, bind_ino, bind_size, bind_mtime, bind_ctime;
405 +static char bind_sum[MAX_DIGEST_LEN];
406 +static unsigned long result_length[MAX_RESULT_BINDS];
407 +static my_bool result_is_null[MAX_RESULT_BINDS], result_error[MAX_RESULT_BINDS];
408 +#else
409 +static int64 bind_mtime;
410 +#endif
411 +static char bind_thishost[128+1], bind_mount_uniq[128+1];
412 +static unsigned long bind_thishost_len, bind_mount_uniq_len;
413 +
414 +static char *error_log;
415 +#if defined USE_SQLITE && defined SQLITE_CONFIG_LOG
416 +static FILE *error_log_fp;
417 +#endif
418 +
419 +#define PTR_SIZE (sizeof (struct file_struct *))
420 +
421 +static void update_mounts(void);
422 +
423 +struct name_list {
424 +       struct name_list *next;
425 +       char name[1];
426 +} *dirs_list;
427 +
428 +int db_read_config(enum logcode code, const char *config_file)
429 +{
430 +       char buf[2048], *cp;
431 +       FILE *fp;
432 +       int lineno = 0;
433 +
434 +       log_code = code;
435 +
436 +       bind_thishost_len = strlcpy(bind_thishost, "localhost", sizeof bind_thishost);
437 +
438 +       if (!(fp = fopen(config_file, "r"))) {
439 +               rsyserr(log_code, errno, "unable to open %s", config_file);
440 +               return 0;
441 +       }
442 +       if (DEBUG_GTE(DB, 1))
443 +               rprintf(FCLIENT, "[%s] Reading DB config from %s\n", who_am_i(), config_file);
444 +       while (fgets(buf, sizeof buf, fp)) {
445 +               lineno++;
446 +               if ((cp = strchr(buf, '#')) == NULL
447 +                && (cp = strchr(buf, '\r')) == NULL
448 +                && (cp = strchr(buf, '\n')) == NULL)
449 +                       cp = buf + strlen(buf);
450 +               while (cp != buf && isSpace(cp-1)) cp--;
451 +               *cp = '\0';
452 +
453 +               if (!*buf)
454 +                       continue;
455 +
456 +               if (!(cp = strchr(buf, ':')))
457 +                       goto invalid_line;
458 +               *cp++ = '\0';
459 +
460 +               while (isSpace(cp)) cp++;
461 +               if (strcasecmp(buf, "dbhost") == 0)
462 +                       dbhost = strdup(cp);
463 +               else if (strcasecmp(buf, "dbuser") == 0)
464 +                       dbuser = strdup(cp);
465 +               else if (strcasecmp(buf, "dbpass") == 0)
466 +                       dbpass = strdup(cp);
467 +               else if (strcasecmp(buf, "dbname") == 0)
468 +                       dbname = strdup(cp);
469 +               else if (strcasecmp(buf, "dbport") == 0)
470 +                       dbport = atoi(cp);
471 +               else if (strcasecmp(buf, "transaction") == 0)
472 +                       transaction_state = atoi(cp) ? 0 : -1;
473 +               else if (strcasecmp(buf, "errlog") == 0)
474 +                       error_log = strdup(cp);
475 +               else if (strcasecmp(buf, "thishost") == 0)
476 +                       bind_thishost_len = strlcpy(bind_thishost, cp, sizeof bind_thishost);
477 +               else if (strcasecmp(buf, "dbtype") == 0) {
478 +#ifdef USE_MYSQL
479 +                       if (strcasecmp(cp, "mysql") == 0) {
480 +                               use_db = DB_TYPE_MYSQL;
481 +                               continue;
482 +                       }
483 +#endif
484 +#ifdef USE_SQLITE
485 +                       if (strcasecmp(cp, "sqlite") == 0) {
486 +                               use_db = DB_TYPE_SQLITE;
487 +                               continue;
488 +                       }
489 +#endif
490 +                       rprintf(log_code,
491 +                           "Unsupported dbtype on line #%d in %s.\n",
492 +                           lineno, config_file);
493 +                       use_db = DB_TYPE_NONE;
494 +                       return 0;
495 +               } else {
496 +                 invalid_line:
497 +                       rprintf(log_code, "Invalid line #%d in %s\n",
498 +                               lineno, config_file);
499 +                       use_db = DB_TYPE_NONE;
500 +                       return 0;
501 +               }
502 +       }
503 +       fclose(fp);
504 +
505 +       if (bind_thishost_len >= (int)sizeof bind_thishost)
506 +               bind_thishost_len = sizeof bind_thishost - 1;
507 +
508 +       if (!use_db || !dbname) {
509 +               rprintf(log_code, "Please specify at least dbtype and dbname in %s.\n", config_file);
510 +               use_db = DB_TYPE_NONE;
511 +               return 0;
512 +       }
513 +
514 +       md_num = protocol_version >= 30 ? 5 : 4;
515 +
516 +       if (error_log) {
517 +               if (use_db != DB_TYPE_SQLITE)
518 +                       rprintf(log_code, "Ignoring errlog setting for non-SQLite DB.\n");
519 +#ifndef SQLITE_CONFIG_LOG
520 +               else
521 +                       rprintf(log_code, "Your sqlite doesn't support SQLITE_CONFIG_LOG.\n");
522 +#endif
523 +       }
524 +
525 +       return 1;
526 +}
527 +
528 +#if defined USE_SQLITE && defined SQLITE_CONFIG_LOG
529 +static void errorLogCallback(UNUSED(void *pArg), int iErrCode, const char *zMsg)
530 +{
531 +       fprintf(error_log_fp, "[%d] %s (%d)\n", (int)getpid(), zMsg, iErrCode);
532 +}
533 +#endif
534 +
535 +static int run_sql(const char *fmt, ...)
536 +{
537 +       va_list ap;
538 +       char *query;
539 +       int ok = 0, qlen;
540 +
541 +       va_start(ap, fmt);
542 +       qlen = vasprintf(&query, fmt, ap);
543 +       va_end(ap);
544 +       if (qlen < 0)
545 +               out_of_memory("run_sql");
546 +       if (DEBUG_GTE(DB, 3))
547 +               rprintf(FCLIENT, "[%s] SQL being run: %s\n", who_am_i(), query);
548 +
549 +       switch (use_db) {
550 +#ifdef USE_MYSQL
551 +       case DB_TYPE_MYSQL:
552 +               if (mysql_query(dbh.mysql, query) < 0) {
553 +                       rprintf(FERROR, "Failed to run sql: %s\n", mysql_error(dbh.mysql));
554 +                       rprintf(FERROR, "%s\n", query);
555 +               } else
556 +                       ok = 1;
557 +               break;
558 +#endif
559 +#ifdef USE_SQLITE
560 +       case DB_TYPE_SQLITE: {
561 +               int rc, lock_failures = 0;
562 +               while (1) {
563 +                       if ((rc = sqlite3_exec(dbh.sqlite, query, NULL, NULL, NULL)) == 0)
564 +                               break;
565 +                       if (rc != SQLITE_BUSY && rc != SQLITE_LOCKED)
566 +                               break;
567 +                       if (++lock_failures > MAX_LOCK_FAILURES)
568 +                               break;
569 +                       msleep(LOCK_FAIL_MSLEEP);
570 +               }
571 +               if (rc) {
572 +                       rprintf(FERROR, "[%s] Failed to run sql: %s\n", who_am_i(), sqlite3_errmsg(dbh.sqlite));
573 +                       rprintf(FERROR, "%s\n", query);
574 +               } else
575 +                       ok = 1;
576 +               break;
577 +           }
578 +#endif
579 +       }
580 +
581 +       free(query);
582 +
583 +       return ok;
584 +}
585 +
586 +#ifdef USE_MYSQL
587 +static int prepare_mysql(int ndx, MYSQL_BIND *binds, int bind_cnt, const char *fmt, ...)
588 +{
589 +       va_list ap;
590 +       char *query;
591 +       int qlen, param_cnt;
592 +       MYSQL_STMT *stmt = mysql_stmt_init(dbh.mysql);
593 +
594 +       if (stmt == NULL)
595 +               out_of_memory("prepare_mysql");
596 +
597 +       va_start(ap, fmt);
598 +       qlen = vasprintf(&query, fmt, ap);
599 +       va_end(ap);
600 +       if (qlen < 0)
601 +               out_of_memory("prepare_mysql");
602 +       if (DEBUG_GTE(DB, 3))
603 +               rprintf(FCLIENT, "[%s] SQL being prepared: %s\n", who_am_i(), query);
604 +
605 +       if (mysql_stmt_prepare(stmt, query, qlen) != 0) {
606 +               rprintf(log_code, "[%s] Prepare failed: %s\n", who_am_i(), mysql_stmt_error(stmt));
607 +               rprintf(log_code, "%s\n", query);
608 +               free(query);
609 +               return 0;
610 +       }
611 +
612 +       if ((param_cnt = mysql_stmt_param_count(stmt)) != bind_cnt) {
613 +               rprintf(log_code, "[%s] Parameters in statement = %d, bind vars = %d\n",
614 +                       who_am_i(), param_cnt, bind_cnt);
615 +               rprintf(log_code, "%s\n", query);
616 +               free(query);
617 +               return 0;
618 +       }
619 +       if (bind_cnt)
620 +               mysql_stmt_bind_param(stmt, binds);
621 +
622 +       statements[ndx].mysql = stmt;
623 +       free(query);
624 +
625 +       return 1;
626 +}
627 +#endif
628 +
629 +#ifdef USE_MYSQL
630 +static int prepare_mysql_queries(int type)
631 +{
632 +       MYSQL_BIND binds[MAX_BIND_CNT];
633 +       char *sql;
634 +
635 +       switch (type) {
636 +       case PREP_NORM:
637 +               sql="SELECT disk_id"
638 +                   " FROM disk"
639 +                   " WHERE host = ? AND devno = ?";
640 +               memset(binds, 0, sizeof binds);
641 +               binds[0].buffer_type = MYSQL_TYPE_STRING;
642 +               binds[0].buffer = &bind_thishost;
643 +               binds[0].buffer_length = bind_thishost_len;
644 +               binds[1].buffer_type = MYSQL_TYPE_LONGLONG;
645 +               binds[1].buffer = &bind_devno;
646 +               if (!prepare_mysql(SEL_DEV, binds, 2, sql))
647 +                       return 0;
648 +
649 +               memset(binds, 0, sizeof binds);
650 +               binds[0].buffer_type = MYSQL_TYPE_LONG;
651 +               binds[0].buffer = &bind_disk_id;
652 +               binds[1].buffer_type = MYSQL_TYPE_LONGLONG;
653 +               binds[1].buffer = &bind_ino;
654 +               if (select_many_sums) {
655 +                       sql="SELECT checksum, sum_type, size, mtime, ctime"
656 +                           " FROM inode_map"
657 +                           " WHERE disk_id = ? AND ino = ?";
658 +                       if (!prepare_mysql(SEL_SUM, binds, 2, sql))
659 +                               return 0;
660 +               } else {
661 +                       sql="SELECT checksum"
662 +                           " FROM inode_map"
663 +                           " WHERE disk_id = ? AND ino = ? AND sum_type = %d"
664 +                           "   AND size = ? AND mtime = ? %s";
665 +                       binds[2].buffer_type = MYSQL_TYPE_LONGLONG;
666 +                       binds[2].buffer = &bind_size;
667 +                       binds[3].buffer_type = MYSQL_TYPE_LONGLONG;
668 +                       binds[3].buffer = &bind_mtime;
669 +                       if (!db_lax) {
670 +                               binds[4].buffer_type = MYSQL_TYPE_LONGLONG;
671 +                               binds[4].buffer = &bind_ctime;
672 +                       }
673 +                       if (!prepare_mysql(SEL_SUM, binds, 4 + !db_lax, sql,    md_num, db_lax ? "" : "AND ctime = ?"))
674 +                               return 0;
675 +               }
676 +
677 +               sql="INSERT INTO inode_map"
678 +                   " SET disk_id = ?, ino = ?, sum_type = ?,"
679 +                   "     size = ?, mtime = ?, ctime = ?, checksum = ?"
680 +                   " ON DUPLICATE KEY"
681 +                   " UPDATE size = VALUES(size), mtime = VALUES(mtime),"
682 +                   "        ctime = VALUES(ctime), checksum = VALUES(checksum)";
683 +               memset(binds, 0, sizeof binds);
684 +               binds[0].buffer_type = MYSQL_TYPE_LONG;
685 +               binds[0].buffer = &bind_disk_id;
686 +               binds[1].buffer_type = MYSQL_TYPE_LONGLONG;
687 +               binds[1].buffer = &bind_ino;
688 +               binds[2].buffer_type = MYSQL_TYPE_LONG;
689 +               binds[2].buffer = &bind_mdnum;
690 +               binds[3].buffer_type = MYSQL_TYPE_LONGLONG;
691 +               binds[3].buffer = &bind_size;
692 +               binds[4].buffer_type = MYSQL_TYPE_LONGLONG;
693 +               binds[4].buffer = &bind_mtime;
694 +               binds[5].buffer_type = MYSQL_TYPE_LONGLONG;
695 +               binds[5].buffer = &bind_ctime;
696 +               binds[6].buffer_type = MYSQL_TYPE_BLOB;
697 +               binds[6].buffer = &bind_sum;
698 +               binds[6].buffer_length = MD5_DIGEST_LEN; /* Same as MD4_DIGEST_LEN */
699 +               if (!prepare_mysql(REP_SUM, binds, 7, sql))
700 +                       return 0;
701 +
702 +               sql="UPDATE inode_map"
703 +                   " SET ctime = ?"
704 +                   " WHERE disk_id = ? AND ino = ? AND sum_type = ? AND size = ? AND mtime = ?";
705 +               memset(binds, 0, sizeof binds);
706 +               binds[0].buffer_type = MYSQL_TYPE_LONGLONG;
707 +               binds[0].buffer = &bind_ctime;
708 +               binds[1].buffer_type = MYSQL_TYPE_LONG;
709 +               binds[1].buffer = &bind_disk_id;
710 +               binds[2].buffer_type = MYSQL_TYPE_LONGLONG;
711 +               binds[2].buffer = &bind_ino;
712 +               binds[3].buffer_type = MYSQL_TYPE_LONG;
713 +               binds[3].buffer = &bind_mdnum;
714 +               binds[4].buffer_type = MYSQL_TYPE_LONGLONG;
715 +               binds[4].buffer = &bind_size;
716 +               binds[5].buffer_type = MYSQL_TYPE_LONGLONG;
717 +               binds[5].buffer = &bind_mtime;
718 +               if (!prepare_mysql(UPD_CTIME, binds, 6, sql))
719 +                       return 0;
720 +               break;
721 +
722 +       case PREP_MOUNT:
723 +               sql="INSERT INTO disk"
724 +                   " SET host = ?, last_seen = ?, mount_uniq = ?, devno = ?"
725 +                   " ON DUPLICATE KEY"
726 +                   " UPDATE last_seen = VALUES(last_seen), devno = VALUES(devno)";
727 +               memset(binds, 0, sizeof binds);
728 +               binds[0].buffer_type = MYSQL_TYPE_STRING;
729 +               binds[0].buffer = &bind_thishost;
730 +               binds[0].buffer_length = bind_thishost_len;
731 +               binds[1].buffer_type = MYSQL_TYPE_LONGLONG;
732 +               binds[1].buffer = &bind_mtime; /* we abuse mtime to hold the last_seen value */
733 +               binds[2].buffer_type = MYSQL_TYPE_STRING;
734 +               binds[2].buffer = &bind_mount_uniq;
735 +               binds[2].buffer_length = sizeof bind_mount_uniq;
736 +               binds[2].length = &bind_mount_uniq_len;
737 +               binds[3].buffer_type = MYSQL_TYPE_LONGLONG;
738 +               binds[3].buffer = &bind_devno;
739 +               if (!prepare_mysql(INS_MOUNT, binds, 4, sql))
740 +                       return 0;
741 +
742 +               sql="SELECT mount_uniq"
743 +                   " FROM disk"
744 +                   " WHERE host = ? AND last_seen < ? AND devno != 0";
745 +               /* Reusing first 2 binds from INS_MOUNT */
746 +               if (!prepare_mysql(SEL_MOUNT, binds, 2, sql))
747 +                       return 0;
748 +
749 +               sql="UPDATE disk"
750 +                   " SET devno = 0"
751 +                   " WHERE host = ? AND last_seen < ? AND devno != 0";
752 +               /* Reusing binds from SEL_MOUNT */
753 +               if (!prepare_mysql(UN_MOUNT, binds, 2, sql))
754 +                       return 0;
755 +               break;
756 +       }
757 +
758 +       return 1;
759 +}
760 +#endif
761 +
762 +#ifdef USE_MYSQL
763 +static int db_connect_mysql(void)
764 +{
765 +       const char *open_dbname = db_init ? "mysql" : dbname;
766 +
767 +       if (!(dbh.mysql = mysql_init(NULL)))
768 +               out_of_memory("db_read_config");
769 +
770 +       if (DEBUG_GTE(DB, 1)) {
771 +               rprintf(FCLIENT, "[%s] connecting: host=%s user=%s db=%s port=%d\n",
772 +                       who_am_i(), dbhost, dbuser, open_dbname, dbport);
773 +       }
774 +       if (!mysql_real_connect(dbh.mysql, dbhost, dbuser, dbpass, open_dbname, dbport, NULL, 0)) {
775 +               rprintf(log_code, "[%s] Unable to connect to DB: %s\n", who_am_i(), mysql_error(dbh.mysql));
776 +               return 0;
777 +       }
778 +
779 +       if (db_init) {
780 +               if (db_output_msgs)
781 +                       rprintf(FCLIENT, "Creating DB %s (if it does not exist)\n", dbname);
782 +               if (!run_sql("CREATE DATABASE IF NOT EXISTS `%s`", dbname)
783 +                || !run_sql("USE `%s`", dbname))
784 +                       exit_cleanup(RERR_IPC);
785 +
786 +               if (db_output_msgs)
787 +                       rprintf(FCLIENT, "Dropping old tables (if they exist))\n");
788 +               if (!run_sql("DROP TABLE IF EXISTS disk")
789 +                || !run_sql("DROP TABLE IF EXISTS inode_map"))
790 +                       exit_cleanup(RERR_IPC);
791 +
792 +               if (db_output_msgs)
793 +                       rprintf(FCLIENT, "Creating empty tables ...\n");
794 +               if (!run_sql(
795 +                   "CREATE TABLE disk (\n"
796 +                   "  disk_id integer unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,\n"
797 +                   "  host varchar(128) NOT NULL default 'localhost',\n"
798 +                   "  mount_uniq varchar(128) default NULL,\n"
799 +                   "  devno bigint unsigned NOT NULL,\n" /* This is 0 when not mounted */
800 +                   "  last_seen bigint NOT NULL,\n"
801 +                   "  UNIQUE KEY mount_lookup (host, mount_uniq),\n"
802 +                   "  KEY dev_lookup (devno, host)\n"
803 +                   ")"))
804 +                       exit_cleanup(RERR_IPC);
805 +
806 +               if (!run_sql(
807 +                   "CREATE TABLE inode_map (\n"
808 +                   "  disk_id integer unsigned NOT NULL,\n"
809 +                   "  ino bigint unsigned NOT NULL,\n"
810 +                   "  sum_type tinyint NOT NULL default '0',\n"
811 +                   "  size bigint unsigned NOT NULL,\n"
812 +                   "  mtime bigint NOT NULL,\n"
813 +                   "  ctime bigint NOT NULL,\n"
814 +                   "  checksum binary(16) NOT NULL,\n"
815 +                   "  PRIMARY KEY (disk_id,ino,sum_type)\n"
816 +                   ")"))
817 +                       exit_cleanup(RERR_IPC);
818 +
819 +               if (!db_mounts)
820 +                       exit_cleanup(0);
821 +       }
822 +
823 +       if (db_mounts) {
824 +               if (!prepare_mysql_queries(PREP_MOUNT))
825 +                       exit_cleanup(RERR_IPC);
826 +               update_mounts();
827 +               exit_cleanup(0);
828 +       }
829 +
830 +       if (!prepare_mysql_queries(PREP_NORM))
831 +               return 0;
832 +
833 +       return 1;
834 +}
835 +#endif
836 +
837 +#ifdef USE_SQLITE
838 +static int prepare_sqlite(int ndx, const char *fmt, ...)
839 +{
840 +       va_list ap;
841 +       char *query;
842 +       int rc, qlen, lock_failures = 0;
843 +
844 +       va_start(ap, fmt);
845 +       qlen = vasprintf(&query, fmt, ap);
846 +       va_end(ap);
847 +       if (qlen < 0)
848 +               out_of_memory("prepare_sqlite");
849 +       if (DEBUG_GTE(DB, 3))
850 +               rprintf(FCLIENT, "[%s] SQL being prepared: %s\n", who_am_i(), query);
851 +
852 +       while ((rc = sqlite3_prepare_v2(dbh.sqlite, query, -1, &statements[ndx].sqlite, NULL)) != 0) {
853 +               if (DEBUG_GTE(DB, 4)) {
854 +                       rprintf(FCLIENT, "[%s] sqlite3_prepare_v2(,%s,,) returned %d\n",
855 +                               who_am_i(), query, rc);
856 +               }
857 +               if (rc != SQLITE_BUSY && rc != SQLITE_LOCKED)
858 +                       break;
859 +               if (++lock_failures > MAX_LOCK_FAILURES)
860 +                       break;
861 +               msleep(LOCK_FAIL_MSLEEP);
862 +       }
863 +       if (rc) {
864 +               rprintf(log_code, "[%s] Failed to prepare SQL: %s (%d)\n", who_am_i(), sqlite3_errmsg(dbh.sqlite), rc);
865 +               rprintf(log_code, "%s\n", query);
866 +               free(query);
867 +               return 0;
868 +       }
869 +       free(query);
870 +
871 +       return 1;
872 +}
873 +#endif
874 +
875 +#ifdef USE_SQLITE
876 +static int prepare_sqlite_queries(int type)
877 +{
878 +       char *sql;
879 +
880 +       switch (type) {
881 +       case PREP_NORM:
882 +               sql="SELECT disk_id"
883 +                   " FROM disk"
884 +                   " WHERE host = ? AND devno = ?";
885 +               if (!prepare_sqlite(SEL_DEV, sql))
886 +                       return 0;
887 +
888 +               if (select_many_sums) {
889 +                       sql="SELECT checksum, sum_type, size, mtime, ctime"
890 +                           " FROM inode_map"
891 +                           " WHERE disk_id = ? AND ino = ?";
892 +                       if (!prepare_sqlite(SEL_SUM, sql))
893 +                               return 0;
894 +               } else {
895 +                       sql="SELECT checksum"
896 +                           " FROM inode_map"
897 +                           " WHERE disk_id = ? AND ino = ? AND sum_type = %d"
898 +                           "   AND size = ? AND mtime = ? %s";
899 +                       if (!prepare_sqlite(SEL_SUM, sql, md_num, db_lax ? "" : "AND ctime = ?"))
900 +                               return 0;
901 +               }
902 +
903 +               sql="INSERT OR REPLACE INTO inode_map"
904 +                   " (disk_id, ino, sum_type, size, mtime, ctime, checksum)"
905 +                   " VALUES (?, ?, ?, ?, ?, ?, ?)";
906 +               if (!prepare_sqlite(REP_SUM, sql))
907 +                       return 0;
908 +
909 +               sql="UPDATE inode_map"
910 +                   " SET ctime = ?"
911 +                   " WHERE disk_id = ? AND ino = ? AND sum_type = ? AND size = ? AND mtime = ?";
912 +               if (!prepare_sqlite(UPD_CTIME, sql))
913 +                       return 0;
914 +               break;
915 +
916 +       case PREP_MOUNT:
917 +               sql="INSERT OR IGNORE INTO disk"
918 +                   " (host, last_seen, mount_uniq, devno)"
919 +                   " VALUES (?, ?, ?, ?)";
920 +               if (!prepare_sqlite(INS_MOUNT, sql))
921 +                       return 0;
922 +
923 +               sql="UPDATE disk"
924 +                   " SET last_seen = ?, devno = ?"
925 +                   " WHERE host = ? AND mount_uniq = ?";
926 +               if (!prepare_sqlite(UPD_MOUNT, sql))
927 +                       return 0;
928 +
929 +               sql="SELECT mount_uniq"
930 +                   " FROM disk"
931 +                   " WHERE host = ? AND last_seen < ? AND devno != 0";
932 +               if (!prepare_sqlite(SEL_MOUNT, sql))
933 +                       return 0;
934 +
935 +               sql="UPDATE disk"
936 +                   " SET devno = 0"
937 +                   " WHERE host = ? AND last_seen < ? AND devno != 0";
938 +               if (!prepare_sqlite(UN_MOUNT, sql))
939 +                       return 0;
940 +               break;
941 +       }
942 +
943 +       return 1;
944 +}
945 +#endif
946 +
947 +#ifdef USE_SQLITE
948 +static int db_connect_sqlite(void)
949 +{
950 +       int lock_failures = 0;
951 +       int rc;
952 +
953 +#ifdef SQLITE_CONFIG_LOG
954 +       if (error_log) {
955 +               if (DEBUG_GTE(DB, 1))
956 +                       rprintf(FCLIENT, "[%s] Setting sqlite errlog to %s\n", who_am_i(), error_log);
957 +               if (!(error_log_fp = fopen(error_log, "a"))) {
958 +                       rsyserr(log_code, errno, "unable to append to logfile %s", error_log);
959 +                       error_log = NULL;
960 +               } else if (sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, NULL) != 0)
961 +                       rprintf(log_code, "Failed to set errorLogCallback: %s\n", sqlite3_errmsg(dbh.sqlite));
962 +       }
963 +#endif
964 +
965 +       while (1) {
966 +               int open_flags = SQLITE_OPEN_READWRITE;
967 +               if (db_init)
968 +                       open_flags |= SQLITE_OPEN_CREATE;
969 +               if (DEBUG_GTE(DB, 1))
970 +                       rprintf(FCLIENT, "[%s] opening %s (%d)\n", who_am_i(), dbname, open_flags);
971 +               if ((rc = sqlite3_open_v2(dbname, &dbh.sqlite, open_flags, NULL)) == 0) {
972 +                       break;
973 +               }
974 +               if (DEBUG_GTE(DB, 4)) {
975 +                       rprintf(FCLIENT, "[%s] sqlite3_open_v2(%s,,%d,NULL) returned %d\n",
976 +                               who_am_i(), dbname, open_flags, rc);
977 +               }
978 +               if (rc != SQLITE_BUSY && rc != SQLITE_LOCKED)
979 +                       break;
980 +               if (++lock_failures > MAX_LOCK_FAILURES)
981 +                       break;
982 +               msleep(LOCK_FAIL_MSLEEP);
983 +       }
984 +
985 +       if (rc) {
986 +               rprintf(log_code, "Unable to connect to DB: %s (%d)\n", sqlite3_errmsg(dbh.sqlite), rc);
987 +               return 0;
988 +       }
989 +
990 +       if (db_init) {
991 +               char *sql;
992 +               if (db_output_msgs)
993 +                       rprintf(FCLIENT, "Dropping old tables (if they exist) ...\n");
994 +               if (!run_sql("DROP TABLE IF EXISTS disk")
995 +                || !run_sql("DROP TABLE IF EXISTS inode_map"))
996 +                       exit_cleanup(RERR_IPC);
997 +
998 +               if (db_output_msgs)
999 +                       rprintf(FCLIENT, "Creating empty tables ...\n");
1000 +               sql="CREATE TABLE disk (\n"
1001 +                   "  disk_id integer NOT NULL PRIMARY KEY AUTOINCREMENT,\n"
1002 +                   "  host varchar(128) NOT NULL default 'localhost',\n"
1003 +                   "  mount_uniq varchar(128) default NULL,\n"
1004 +                   "  devno bigint NOT NULL,\n" /* This is 0 when not mounted */
1005 +                   "  last_seen bigint NOT NULL,\n"
1006 +                   "  UNIQUE (host, mount_uniq)\n"
1007 +                   ")";
1008 +               if (!run_sql(sql))
1009 +                       exit_cleanup(RERR_IPC);
1010 +
1011 +               sql="CREATE TABLE inode_map (\n"
1012 +                   "  disk_id integer NOT NULL,\n"
1013 +                   "  ino bigint NOT NULL,\n"
1014 +                   "  size bigint NOT NULL,\n"
1015 +                   "  mtime bigint NOT NULL,\n"
1016 +                   "  ctime bigint NOT NULL,\n"
1017 +                   "  sum_type tinyint NOT NULL default '0',\n"
1018 +                   "  checksum binary(16) NOT NULL,\n"
1019 +                   "  PRIMARY KEY (disk_id,ino,sum_type)\n"
1020 +                   ")";
1021 +               if (!run_sql(sql))
1022 +                       exit_cleanup(RERR_IPC);
1023 +
1024 +#if SQLITE_VERSION_NUMBER >= 3007000
1025 +               /* Using WAL locking makes concurrency much better (requires sqlite 3.7.0). */
1026 +               sql="PRAGMA journal_mode = wal";
1027 +               run_sql(sql); /* We don't check this for success. */
1028 +#endif
1029 +
1030 +               if (!db_mounts)
1031 +                       exit_cleanup(0);
1032 +       }
1033 +
1034 +       if (db_mounts) {
1035 +               if (!prepare_sqlite_queries(PREP_MOUNT))
1036 +                       exit_cleanup(RERR_IPC);
1037 +               update_mounts();
1038 +               exit_cleanup(0);
1039 +       }
1040 +
1041 +       if (!prepare_sqlite_queries(PREP_NORM)) {
1042 +               db_disconnect(False);
1043 +               return 0;
1044 +       }
1045 +
1046 +       return 1;
1047 +}
1048 +#endif
1049 +
1050 +int db_connect(int select_many)
1051 +{
1052 +       select_many_sums = select_many;
1053 +
1054 +       switch (use_db) {
1055 +#ifdef USE_MYSQL
1056 +       case DB_TYPE_MYSQL:
1057 +               if (db_connect_mysql())
1058 +                       return 1;
1059 +               break;
1060 +#endif
1061 +#ifdef USE_SQLITE
1062 +       case DB_TYPE_SQLITE:
1063 +               if (db_connect_sqlite())
1064 +                       return 1;
1065 +               break;
1066 +#endif
1067 +       }
1068 +
1069 +       db_disconnect(False);
1070 +
1071 +       return 0;
1072 +}
1073 +
1074 +void db_disconnect(BOOL commit)
1075 +{
1076 +       int ndx;
1077 +
1078 +       if (!dbh.all)
1079 +               return;
1080 +
1081 +       if (transaction_state > 0) {
1082 +               if (DEBUG_GTE(DB, 1)) {
1083 +                       rprintf(FCLIENT, "[%s] %s our DB transaction\n",
1084 +                               who_am_i(), commit ? "Committing" : "Rolling back");
1085 +               }
1086 +               transaction_state = 0;
1087 +               if (commit)
1088 +                       run_sql("COMMIT");
1089 +               else
1090 +                       run_sql("ROLLBACK");
1091 +       }
1092 +
1093 +       if (DEBUG_GTE(DB, 1))
1094 +               rprintf(FCLIENT, "[%s] Disconnecting from the DB\n", who_am_i());
1095 +
1096 +       for (ndx = 0; ndx < MAX_PREP_CNT; ndx++) {
1097 +               if (statements[ndx].all) {
1098 +                       switch (use_db) {
1099 +#ifdef USE_MYSQL
1100 +                       case DB_TYPE_MYSQL:
1101 +                               mysql_stmt_close(statements[ndx].mysql);
1102 +                               break;
1103 +#endif
1104 +#ifdef USE_SQLITE
1105 +                       case DB_TYPE_SQLITE:
1106 +                               sqlite3_finalize(statements[ndx].sqlite);
1107 +                               break;
1108 +#endif
1109 +                       }
1110 +                       statements[ndx].all = NULL;
1111 +               }
1112 +       }
1113 +
1114 +       switch (use_db) {
1115 +#ifdef USE_MYSQL
1116 +       case DB_TYPE_MYSQL:
1117 +               mysql_close(dbh.mysql);
1118 +               break;
1119 +#endif
1120 +#ifdef USE_SQLITE
1121 +       case DB_TYPE_SQLITE:
1122 +               sqlite3_close(dbh.sqlite);
1123 +               break;
1124 +#endif
1125 +       }
1126 +
1127 +       dbh.all = NULL;
1128 +       use_db = DB_TYPE_NONE;
1129 +}
1130 +
1131 +#ifdef USE_MYSQL
1132 +static MYSQL_STMT *exec_mysql(int ndx)
1133 +{
1134 +       MYSQL_STMT *stmt = statements[ndx].mysql;
1135 +       int rc;
1136 +
1137 +       if ((rc = mysql_stmt_execute(stmt)) == CR_SERVER_LOST) {
1138 +               db_disconnect(False);
1139 +               use_db = DB_TYPE_MYSQL;
1140 +               if (db_connect(select_many_sums)) {
1141 +                       stmt = statements[ndx].mysql;
1142 +                       rc = mysql_stmt_execute(stmt);
1143 +               }
1144 +       }
1145 +       if (rc != 0) {
1146 +               rprintf(log_code, "SQL execute failed: %s\n", mysql_stmt_error(stmt));
1147 +               return NULL;
1148 +       }
1149 +
1150 +       return stmt;
1151 +}
1152 +#endif
1153 +
1154 +#ifdef USE_MYSQL
1155 +/* This stores up to max_rows into the values pointed to by the bind data arrays.
1156 + * If max_rows is > 1, then all the buffer pointers MUST be set to an array long
1157 + * enough to hold the max count of rows.  The buffer pointer will be incremented
1158 + * to read additional rows (but never past the end).  If stmt_ptr is non-NULL, it
1159 + * will be set to the "stmt" pointer IFF we didn't run out of rows before hitting
1160 + * the max.  In this case, the caller should call mysql_stmt_fetch() to read any
1161 + * remaining rows (the buffer pointers will point at the final array element) and
1162 + * then call mysql_stmt_free_result().  If *stmt_ptr is a NULL value, there were
1163 + * not enough rows to fill the max_rows arrays, and the stmt was already freed. */
1164 +static int fetch_mysql(MYSQL_BIND *binds, int bind_cnt, int ndx, int max_rows, MYSQL_STMT **stmt_ptr)
1165 +{
1166 +       MYSQL_STMT *stmt;
1167 +       int i, rc, rows = 0;
1168 +
1169 +       if (bind_cnt > MAX_RESULT_BINDS) {
1170 +               fprintf(stderr, "Internal error: MAX_RESULT_BINDS overflow\n");
1171 +               exit_cleanup(RERR_UNSUPPORTED);
1172 +       }
1173 +
1174 +       if ((stmt = exec_mysql(ndx)) == NULL)
1175 +               return 0;
1176 +
1177 +       for (i = 0; i < bind_cnt; i++) {
1178 +               binds[i].is_null = &result_is_null[i];
1179 +               binds[i].length = &result_length[i];
1180 +               binds[i].error = &result_error[i];
1181 +       }
1182 +       mysql_stmt_bind_result(stmt, binds);
1183 +
1184 +       while (rows < max_rows) {
1185 +               if ((rc = mysql_stmt_fetch(stmt)) != 0) {
1186 +                       if (rc != MYSQL_NO_DATA)
1187 +                               rprintf(log_code, "SELECT fetch failed: %s\n", mysql_stmt_error(stmt));
1188 +                       break;
1189 +               }
1190 +               if (++rows >= max_rows)
1191 +                       break;
1192 +               for (i = 0; i < bind_cnt; i++) {
1193 +                       switch (binds[i].buffer_type) {
1194 +                       case MYSQL_TYPE_BLOB:
1195 +                       case MYSQL_TYPE_STRING:
1196 +                           binds[i].buffer += binds[i].buffer_length;
1197 +                           break;
1198 +                       case MYSQL_TYPE_LONG:
1199 +                           binds[i].buffer += sizeof (int);
1200 +                           break;
1201 +                       case MYSQL_TYPE_LONGLONG:
1202 +                           binds[i].buffer += sizeof (int64);
1203 +                           break;
1204 +                       default:
1205 +                           fprintf(stderr, "Unknown MYSQL_TYPE_* in multi-row read: %d.\n", binds[i].buffer_type);
1206 +                           exit_cleanup(RERR_UNSUPPORTED);
1207 +                       }
1208 +               }
1209 +       }
1210 +
1211 +       if (!stmt_ptr || rows < max_rows) {
1212 +               mysql_stmt_free_result(stmt);
1213 +               stmt = NULL;
1214 +       }
1215 +       if (stmt_ptr)
1216 +               *stmt_ptr = stmt;
1217 +
1218 +       return rows;
1219 +}
1220 +#endif
1221 +
1222 +static void update_mounts(void)
1223 +{
1224 +       char buf[2048], *argv[2];
1225 +       int f_from, f_to, len;
1226 +       STRUCT_STAT st;
1227 +       int pid, status;
1228 +
1229 +       if (DEBUG_GTE(DB, 2))
1230 +               printf("Running %s to grab mount info\n", RSYNCDB_MOUNTS);
1231 +       argv[0] = RSYNCDB_MOUNTS;
1232 +       argv[1] = NULL;
1233 +       pid = piped_child(argv, &f_from, &f_to);
1234 +       close(f_to);
1235 +
1236 +       bind_mtime = time(NULL); /* abuse mtime slightly to hold our last_seen value */
1237 +
1238 +       /* Strict format has 2 items with one tab as separator: MOUNT_UNIQ\tPATH */
1239 +       while ((len = read_line(f_from, buf, sizeof buf, 0)) > 0) {
1240 +               char *mount_uniq, *path;
1241 +
1242 +               if (DEBUG_GTE(DB, 3))
1243 +                       printf("Parsing mount info: %s\n", buf);
1244 +               mount_uniq = strtok(buf, "\t");
1245 +               path = mount_uniq ? strtok(NULL, "\r\n") : NULL;
1246 +               if (!path) {
1247 +                       fprintf(stderr, "Failed to parse line from %s output\n", RSYNCDB_MOUNTS);
1248 +                       exit_cleanup(RERR_SYNTAX);
1249 +               }
1250 +
1251 +               if (lstat(path, &st) < 0) {
1252 +                       fprintf(stderr, "Failed to lstat(%s): %s\n", path, strerror(errno));
1253 +                       exit_cleanup(RERR_IPC);
1254 +               }
1255 +
1256 +               bind_mount_uniq_len = strlcpy(bind_mount_uniq, mount_uniq, sizeof bind_mount_uniq);
1257 +               if (bind_mount_uniq_len >= (int)sizeof bind_mount_uniq)
1258 +                       bind_mount_uniq_len = sizeof bind_mount_uniq - 1;
1259 +
1260 +               if (db_output_msgs) {
1261 +                       printf("Marking mount \"%s\" (%s) as a recent mount\n",
1262 +                               bind_mount_uniq, big_num(st.st_dev));
1263 +               }
1264 +               switch (use_db) {
1265 +#ifdef USE_MYSQL
1266 +               case DB_TYPE_MYSQL:
1267 +                       bind_devno = st.st_dev;
1268 +                       if (exec_mysql(INS_MOUNT) == NULL) {
1269 +                               fprintf(stderr, "Failed to update mount info for \"%s\" - %s\n",
1270 +                                       bind_mount_uniq, mysql_error(dbh.mysql));
1271 +                               exit_cleanup(RERR_IPC);
1272 +                       }
1273 +                       break;
1274 +#endif
1275 +#ifdef USE_SQLITE
1276 +               case DB_TYPE_SQLITE: {
1277 +                       int rc, change_cnt;
1278 +                       sqlite3_stmt *stmt = statements[INS_MOUNT].sqlite;
1279 +                       sqlite3_bind_text(stmt, 1, bind_thishost, bind_thishost_len, SQLITE_STATIC);
1280 +                       sqlite3_bind_int64(stmt, 2, bind_mtime);
1281 +                       sqlite3_bind_text(stmt, 3, bind_mount_uniq, bind_mount_uniq_len, SQLITE_STATIC);
1282 +                       sqlite3_bind_int64(stmt, 4, st.st_dev);
1283 +                       rc = sqlite3_step(stmt);
1284 +                       if (rc != SQLITE_DONE) {
1285 +                               fprintf(stderr, "Failed to insert mount info for \"%s\" - %s (%d)\n",
1286 +                                       bind_mount_uniq, sqlite3_errmsg(dbh.sqlite), rc);
1287 +                               exit_cleanup(RERR_IPC);
1288 +                       }
1289 +                       change_cnt = sqlite3_changes(dbh.sqlite);
1290 +                       sqlite3_reset(stmt);
1291 +                       if (change_cnt == 0) {
1292 +                               stmt = statements[UPD_MOUNT].sqlite;
1293 +                               sqlite3_bind_int64(stmt, 1, bind_mtime);
1294 +                               sqlite3_bind_int64(stmt, 2, st.st_dev);
1295 +                               sqlite3_bind_text(stmt, 3, bind_thishost, bind_thishost_len, SQLITE_STATIC);
1296 +                               sqlite3_bind_text(stmt, 4, bind_mount_uniq, bind_mount_uniq_len, SQLITE_STATIC);
1297 +                               rc = sqlite3_step(stmt);
1298 +                               if (rc != SQLITE_DONE) {
1299 +                                       fprintf(stderr, "Failed to update mount info for \"%s\" - %s (%d)\n",
1300 +                                               bind_mount_uniq, sqlite3_errmsg(dbh.sqlite), rc);
1301 +                                       exit_cleanup(RERR_IPC);
1302 +                               }
1303 +                               sqlite3_reset(stmt);
1304 +                       }
1305 +                       break;
1306 +                   }
1307 +#endif
1308 +               }
1309 +       }
1310 +       close(f_from);
1311 +
1312 +       waitpid(pid, &status, 0);
1313 +
1314 +       switch (use_db) {
1315 +#ifdef USE_MYSQL
1316 +       case DB_TYPE_MYSQL: {
1317 +               if (db_output_msgs) {
1318 +                       MYSQL_BIND binds[1];
1319 +                       MYSQL_STMT *stmt;
1320 +
1321 +                       binds[0].buffer_type = MYSQL_TYPE_BLOB;
1322 +                       binds[0].buffer = bind_mount_uniq;
1323 +                       binds[0].buffer_length = sizeof bind_mount_uniq;
1324 +                       if (fetch_mysql(binds, 1, SEL_MOUNT, 1, &stmt)) {
1325 +                               while (1) {
1326 +                                       printf("Marking mount \"%s\" as unmounted.\n", bind_mount_uniq);
1327 +                                       if (mysql_stmt_fetch(stmt) != 0)
1328 +                                               break;
1329 +                               }
1330 +                               mysql_stmt_free_result(stmt);
1331 +                       }
1332 +               }
1333 +
1334 +               if (exec_mysql(UN_MOUNT) == NULL) {
1335 +                       fprintf(stderr, "Failed to update old mount info - %s\n",
1336 +                               mysql_error(dbh.mysql));
1337 +                       exit_cleanup(RERR_IPC);
1338 +               }
1339 +               break;
1340 +           }
1341 +#endif
1342 +#ifdef USE_SQLITE
1343 +       case DB_TYPE_SQLITE: {
1344 +               sqlite3_stmt *stmt;
1345 +               int rc;
1346 +
1347 +               if (db_output_msgs) {
1348 +                       stmt = statements[SEL_MOUNT].sqlite;
1349 +                       sqlite3_bind_text(stmt, 1, bind_thishost, bind_thishost_len, SQLITE_STATIC);
1350 +                       sqlite3_bind_int64(stmt, 2, bind_mtime);
1351 +                       while (1) {
1352 +                               if (sqlite3_step(stmt) != SQLITE_ROW)
1353 +                                       break;
1354 +                               printf("Marking mount \"%s\" as unmounted.\n", sqlite3_column_text(stmt, 0));
1355 +                       }
1356 +                       sqlite3_reset(stmt);
1357 +               }
1358 +
1359 +               stmt = statements[UN_MOUNT].sqlite;
1360 +               sqlite3_bind_text(stmt, 1, bind_thishost, bind_thishost_len, SQLITE_STATIC);
1361 +               sqlite3_bind_int64(stmt, 2, bind_mtime);
1362 +               rc = sqlite3_step(stmt);
1363 +               sqlite3_reset(stmt);
1364 +               if (rc != SQLITE_DONE) {
1365 +                       fprintf(stderr, "Failed to update old mount info - %s (%d)\n",
1366 +                               sqlite3_errmsg(dbh.sqlite), rc);
1367 +                       exit_cleanup(RERR_IPC);
1368 +               }
1369 +               break;
1370 +           }
1371 +#endif
1372 +       }
1373 +}
1374 +
1375 +unsigned int get_disk_id(int64 devno)
1376 +{
1377 +       static unsigned int prior_disk_id = 0;
1378 +       static int64 prior_devno = 0;
1379 +
1380 +       if (prior_devno == devno && prior_disk_id) {
1381 +               if (DEBUG_GTE(DB, 5))
1382 +                       rprintf(FCLIENT, "get_disk_id(%s,%s) = %d (cached)\n", bind_thishost, big_num(devno), prior_disk_id);
1383 +               return prior_disk_id;
1384 +       }
1385 +       prior_devno = devno;
1386 +
1387 +       switch (use_db) {
1388 +#ifdef USE_MYSQL
1389 +       case DB_TYPE_MYSQL: {
1390 +               MYSQL_BIND binds[1];
1391 +
1392 +               bind_devno = devno; /* The one changing SEL_DEV input value. */
1393 +
1394 +               /* Bind where to put the output. */
1395 +               binds[0].buffer_type = MYSQL_TYPE_LONG;
1396 +               binds[0].buffer = &prior_disk_id;
1397 +               if (!fetch_mysql(binds, 1, SEL_DEV, 1, NULL))
1398 +                       prior_disk_id = 0;
1399 +               break;
1400 +           }
1401 +#endif
1402 +#ifdef USE_SQLITE
1403 +       case DB_TYPE_SQLITE: {
1404 +               sqlite3_stmt *stmt = statements[SEL_DEV].sqlite;
1405 +               sqlite3_bind_text(stmt, 1, bind_thishost, bind_thishost_len, SQLITE_STATIC);
1406 +               sqlite3_bind_int64(stmt, 2, devno);
1407 +               if (sqlite3_step(stmt) == SQLITE_ROW)
1408 +                       prior_disk_id = sqlite3_column_int(stmt, 0);
1409 +               else
1410 +                       prior_disk_id = 0;
1411 +               sqlite3_reset(stmt);
1412 +               break;
1413 +           }
1414 +#endif
1415 +       }
1416 +
1417 +       if (DEBUG_GTE(DB, 2))
1418 +               rprintf(FCLIENT, "get_disk_id(%s,%s) = %d\n", bind_thishost, big_num(devno), prior_disk_id);
1419 +       return prior_disk_id;
1420 +}
1421 +
1422 +int db_get_checksum(const STRUCT_STAT *st_p, char *sum)
1423 +{
1424 +       unsigned int disk_id = get_disk_id(st_p->st_dev);
1425 +       int ok = 0;
1426 +
1427 +       if (disk_id == 0)
1428 +               return 0;
1429 +
1430 +       switch (use_db) {
1431 +#ifdef USE_MYSQL
1432 +       case DB_TYPE_MYSQL: {
1433 +               MYSQL_BIND binds[1];
1434 +
1435 +               bind_disk_id = disk_id;
1436 +               bind_ino = st_p->st_ino;
1437 +               bind_size = st_p->st_size;
1438 +               bind_mtime = st_p->st_mtime;
1439 +               if (!db_lax)
1440 +                       bind_ctime = st_p->st_ctime;
1441 +
1442 +               binds[0].buffer_type = MYSQL_TYPE_BLOB;
1443 +               binds[0].buffer = sum;
1444 +               binds[0].buffer_length = MD5_DIGEST_LEN;
1445 +               ok = fetch_mysql(binds, 1, SEL_SUM, 1, NULL);
1446 +               break;
1447 +           }
1448 +#endif
1449 +#ifdef USE_SQLITE
1450 +       case DB_TYPE_SQLITE: {
1451 +               sqlite3_stmt *stmt = statements[SEL_SUM].sqlite;
1452 +               sqlite3_bind_int(stmt, 1, disk_id);
1453 +               sqlite3_bind_int64(stmt, 2, st_p->st_ino);
1454 +               sqlite3_bind_int64(stmt, 3, st_p->st_size);
1455 +               sqlite3_bind_int64(stmt, 4, st_p->st_mtime);
1456 +               if (!db_lax)
1457 +                       sqlite3_bind_int64(stmt, 5, st_p->st_ctime);
1458 +               if (sqlite3_step(stmt) == SQLITE_ROW) {
1459 +                       int len = sqlite3_column_bytes(stmt, 0);
1460 +                       if (len > MAX_DIGEST_LEN)
1461 +                               len = MAX_DIGEST_LEN;
1462 +                       memcpy(sum, sqlite3_column_blob(stmt, 0), len);
1463 +                       ok = 1;
1464 +               }
1465 +               sqlite3_reset(stmt);
1466 +               break;
1467 +           }
1468 +#endif
1469 +       }
1470 +
1471 +       if (DEBUG_GTE(DB, 2)) {
1472 +               if (ok) {
1473 +                       rprintf(FCLIENT, "[%s] Found DB checksum for %s,%s,%d: %s\n",
1474 +                               who_am_i(), big_num(st_p->st_dev),
1475 +                               big_num(st_p->st_ino), md_num, sum_as_hex(md_num, sum));
1476 +               } else {
1477 +                       rprintf(FCLIENT, "[%s] No DB checksum for %s,%s,%d\n",
1478 +                               who_am_i(), big_num(st_p->st_dev),
1479 +                               big_num(st_p->st_ino), md_num);
1480 +               }
1481 +       }
1482 +
1483 +       return ok;
1484 +}
1485 +
1486 +int db_get_both_checksums(const STRUCT_STAT *st_p, int *right_sum_cnt, int *wrong_sum_cnt, char **sum4, char **sum5)
1487 +{
1488 +       static char dbsum[MD5_DIGEST_LEN*2];
1489 +       int rows, j, sum_type[2];
1490 +       int64 dbsize[2], dbmtime[2], dbctime[2];
1491 +       unsigned int disk_id = get_disk_id(st_p->st_dev);
1492 +
1493 +       if (disk_id == 0)
1494 +               return 0;
1495 +
1496 +       switch (use_db) {
1497 +#ifdef USE_MYSQL
1498 +       case DB_TYPE_MYSQL: {
1499 +               MYSQL_BIND binds[5];
1500 +
1501 +               bind_disk_id = disk_id;
1502 +               bind_ino = st_p->st_ino;
1503 +
1504 +               binds[0].buffer_type = MYSQL_TYPE_BLOB;
1505 +               binds[0].buffer = dbsum;
1506 +               binds[0].buffer_length = MD5_DIGEST_LEN;
1507 +               binds[1].buffer_type = MYSQL_TYPE_LONG;
1508 +               binds[1].buffer = (char*)sum_type;
1509 +               binds[2].buffer_type = MYSQL_TYPE_LONGLONG;
1510 +               binds[2].buffer = (char*)dbsize;
1511 +               binds[3].buffer_type = MYSQL_TYPE_LONGLONG;
1512 +               binds[3].buffer = (char*)dbmtime;
1513 +               binds[4].buffer_type = MYSQL_TYPE_LONGLONG;
1514 +               binds[4].buffer = (char*)dbctime;
1515 +               rows = fetch_mysql(binds, 5, SEL_SUM, 2, NULL);
1516 +               break;
1517 +           }
1518 +#endif
1519 +#ifdef USE_SQLITE
1520 +       case DB_TYPE_SQLITE: {
1521 +               sqlite3_stmt *stmt = statements[SEL_SUM].sqlite;
1522 +               sqlite3_bind_int(stmt, 1, disk_id);
1523 +               sqlite3_bind_int64(stmt, 2, st_p->st_ino);
1524 +               for (j = 0; j < 2; j++) {
1525 +                       int len;
1526 +                       if (sqlite3_step(stmt) != SQLITE_ROW)
1527 +                               break;
1528 +                       len = sqlite3_column_bytes(stmt, 0);
1529 +                       if (len > MD5_DIGEST_LEN)
1530 +                               len = MD5_DIGEST_LEN;
1531 +                       memcpy(dbsum + MD5_DIGEST_LEN*j, sqlite3_column_blob(stmt, 0), len);
1532 +                       sum_type[j] = sqlite3_column_int(stmt, 1);
1533 +                       dbsize[j] = sqlite3_column_int(stmt, 2);
1534 +                       dbmtime[j] = sqlite3_column_int64(stmt, 3);
1535 +                       dbctime[j] = sqlite3_column_int64(stmt, 4);
1536 +               }
1537 +               sqlite3_reset(stmt);
1538 +               rows = j;
1539 +               break;
1540 +           }
1541 +#endif
1542 +       default:
1543 +               return 0;
1544 +       }
1545 +
1546 +       if (sum4)
1547 +               *sum4 = NULL;
1548 +       if (sum5)
1549 +               *sum5 = NULL;
1550 +       *right_sum_cnt = *wrong_sum_cnt = 0;
1551 +       for (j = 0; j < rows; j++) {
1552 +               if (DEBUG_GTE(DB, 3)) {
1553 +                       rprintf(FCLIENT, "DB checksum for %s,%s,%d: %s\n",
1554 +                               big_num(st_p->st_dev), big_num(st_p->st_ino), sum_type[j],
1555 +                               sum_as_hex(sum_type[j], dbsum + MD5_DIGEST_LEN*j));
1556 +               }
1557 +
1558 +               if (sum_type[j] == 4) {
1559 +                       if (!sum4)
1560 +                               continue;
1561 +                       *sum4 = dbsum + MD5_DIGEST_LEN*j;
1562 +               } else {
1563 +                       if (!sum5)
1564 +                               continue;
1565 +                       *sum5 = dbsum + MD5_DIGEST_LEN*j;
1566 +               }
1567 +               if (st_p->st_size == dbsize[j] && st_p->st_mtime == dbmtime[j] && (db_lax || st_p->st_ctime == dbctime[j]))
1568 +                       ++*right_sum_cnt;
1569 +               else
1570 +                       ++*wrong_sum_cnt;
1571 +       }
1572 +
1573 +       return rows;
1574 +}
1575 +
1576 +int db_set_checksum(int mdnum, const STRUCT_STAT *st_p, const char *sum)
1577 +{
1578 +       unsigned int disk_id;
1579 +       const char *errmsg = NULL;
1580 +       int rc = 0;
1581 +
1582 +       if (am_receiver || (am_generator && same_db)) {
1583 +               /* Forward the setting to a single process.  The receiver always
1584 +                * forward to the generator, and the generator will forward to
1585 +                * the receiver ONLY if this is a local transfer. */
1586 +               char data[MSG_CHECKSUM_LEN];
1587 +               SIVAL64(data, 0, st_p->st_dev);
1588 +               SIVAL64(data, 8, st_p->st_ino);
1589 +               SIVAL64(data, 16, st_p->st_size);
1590 +               SIVAL64(data, 24, st_p->st_mtime);
1591 +               SIVAL64(data, 32, st_p->st_ctime);
1592 +#if MSG_CHECKSUM_LONGS != 5
1593 +#error Fix the setting of checksum long values
1594 +#endif
1595 +               SIVAL(data, MSG_CHECKSUM_LONGS*8, mdnum);
1596 +               memcpy(data + MSG_CHECKSUM_LONGS*8 + 4, sum, MAX_DIGEST_LEN);
1597 +               return send_msg(MSG_CHECKSUM, data, sizeof data, 0);
1598 +       }
1599 +
1600 +       if ((disk_id = get_disk_id(st_p->st_dev)) == 0)
1601 +               return 0;
1602 +
1603 +       switch (use_db) {
1604 +#ifdef USE_MYSQL
1605 +       case DB_TYPE_MYSQL:
1606 +               if (transaction_state == 0) {
1607 +                       if (!run_sql("BEGIN"))
1608 +                               return 0;
1609 +                       transaction_state = 1;
1610 +               }
1611 +
1612 +               bind_disk_id = disk_id;
1613 +               bind_ino = st_p->st_ino;
1614 +               bind_mdnum = mdnum;
1615 +               bind_size = st_p->st_size;
1616 +               bind_mtime = st_p->st_mtime;
1617 +               bind_ctime = st_p->st_ctime;
1618 +               memcpy(bind_sum, sum, MD5_DIGEST_LEN);
1619 +               if (exec_mysql(REP_SUM) == NULL)
1620 +                       errmsg = mysql_error(dbh.mysql);
1621 +               break;
1622 +#endif
1623 +#ifdef USE_SQLITE
1624 +       case DB_TYPE_SQLITE: {
1625 +               sqlite3_stmt *stmt = statements[REP_SUM].sqlite;
1626 +               int lock_failures = 0;
1627 +
1628 +               if (transaction_state == 0) {
1629 +                       if (!run_sql("BEGIN"))
1630 +                               return 0;
1631 +                       transaction_state = 1;
1632 +               }
1633 +
1634 +               sqlite3_bind_int(stmt, 1, disk_id);
1635 +               sqlite3_bind_int64(stmt, 2, st_p->st_ino);
1636 +               sqlite3_bind_int(stmt, 3, mdnum);
1637 +               sqlite3_bind_int64(stmt, 4, st_p->st_size);
1638 +               sqlite3_bind_int64(stmt, 5, st_p->st_mtime);
1639 +               sqlite3_bind_int64(stmt, 6, st_p->st_ctime);
1640 +               sqlite3_bind_blob(stmt, 7, sum, MD5_DIGEST_LEN, SQLITE_TRANSIENT);
1641 +               while (1) {
1642 +                       rc = sqlite3_step(stmt);
1643 +                       if (rc != SQLITE_BUSY && rc != SQLITE_LOCKED)
1644 +                               break;
1645 +                       if (++lock_failures > MAX_LOCK_FAILURES)
1646 +                               break;
1647 +                       sqlite3_reset(stmt);
1648 +                       msleep(LOCK_FAIL_MSLEEP);
1649 +               }
1650 +               if (rc != SQLITE_DONE)
1651 +                       errmsg = sqlite3_errmsg(dbh.sqlite);
1652 +               sqlite3_reset(stmt);
1653 +               break;
1654 +           }
1655 +#endif
1656 +       }
1657 +
1658 +       if (!errmsg) {
1659 +               if (DEBUG_GTE(DB, 2)) {
1660 +                       rprintf(FCLIENT, "[%s] Set DB checksum for %s,%s,%d: %s\n",
1661 +                               who_am_i(), big_num(st_p->st_dev), big_num(st_p->st_ino),
1662 +                               md_num, sum_as_hex(md_num, sum));
1663 +               }
1664 +       } else {
1665 +               rprintf(log_code, "[%s] Failed to set checksum for %s,%s,%d: %s (%d) -- closing DB\n",
1666 +                       who_am_i(), big_num(st_p->st_dev), big_num(st_p->st_ino),
1667 +                       md_num, errmsg, rc);
1668 +               db_disconnect(False);
1669 +       }
1670 +
1671 +       return errmsg ? 0 : 1;
1672 +}
1673 +
1674 +/* For a delayed-update copy, we set the checksum on the file when it was
1675 + * inside the partial-dir.  Since renaming the file changes its ctime, we need
1676 + * to update the ctime to its new value (we can skip this in db_lax mode). */
1677 +int db_update_ctime(int mdnum, const STRUCT_STAT *st_p)
1678 +{
1679 +       unsigned int disk_id = get_disk_id(st_p->st_dev);
1680 +
1681 +       if (disk_id == 0)
1682 +               return 0;
1683 +
1684 +       switch (use_db) {
1685 +#ifdef USE_MYSQL
1686 +       case DB_TYPE_MYSQL:
1687 +               bind_ctime = st_p->st_ctime;
1688 +               bind_disk_id = disk_id;
1689 +               bind_ino = st_p->st_ino;
1690 +               bind_mdnum = mdnum;
1691 +               bind_size = st_p->st_size;
1692 +               bind_mtime = st_p->st_mtime;
1693 +               return exec_mysql(UPD_CTIME) != NULL;
1694 +#endif
1695 +#ifdef USE_SQLITE
1696 +       case DB_TYPE_SQLITE: {
1697 +               int rc;
1698 +
1699 +               sqlite3_stmt *stmt = statements[UPD_CTIME].sqlite;
1700 +               if (stmt == NULL)
1701 +                       return 0;
1702 +               sqlite3_bind_int64(stmt, 1, st_p->st_ctime);
1703 +               sqlite3_bind_int(stmt, 2, disk_id);
1704 +               sqlite3_bind_int64(stmt, 3, st_p->st_ino);
1705 +               sqlite3_bind_int(stmt, 4, mdnum);
1706 +               sqlite3_bind_int64(stmt, 5, st_p->st_size);
1707 +               sqlite3_bind_int64(stmt, 6, st_p->st_mtime);
1708 +               rc = sqlite3_step(stmt);
1709 +               sqlite3_reset(stmt);
1710 +               return rc == SQLITE_DONE;
1711 +           }
1712 +#endif
1713 +       }
1714 +
1715 +       return 0;
1716 +}
1717 +
1718 +int db_clean_init(void)
1719 +{
1720 +       switch (use_db) {
1721 +#ifdef USE_MYSQL
1722 +       case DB_TYPE_MYSQL: {
1723 +               MYSQL_BIND binds[MAX_BIND_CNT];
1724 +               char *sql;
1725 +
1726 +               mysql_query(dbh.mysql,
1727 +                       "CREATE TEMPORARY TABLE inode_present ("
1728 +                       " disk_id integer unsigned NOT NULL,"
1729 +                       " ino bigint unsigned NOT NULL,"
1730 +                       " present tinyint NOT NULL default '1',"
1731 +                       " PRIMARY KEY (disk_id,ino)"
1732 +                       ") ENGINE=MEMORY"
1733 +                       );
1734 +
1735 +               sql="INSERT IGNORE INTO inode_present"
1736 +                   " SET disk_id = ?, ino = ?, present = 1";
1737 +               memset(binds, 0, sizeof binds);
1738 +               binds[0].buffer_type = MYSQL_TYPE_LONG;
1739 +               binds[0].buffer = &bind_disk_id;
1740 +               binds[1].buffer_type = MYSQL_TYPE_LONGLONG;
1741 +               binds[1].buffer = &bind_ino;
1742 +               if (!prepare_mysql(INS_PRESENT, binds, 2, sql))
1743 +                       exit_cleanup(RERR_SYNTAX);
1744 +
1745 +               sql="DELETE m.*"
1746 +                   " FROM inode_map AS m"
1747 +                   " LEFT JOIN inode_present USING(disk_id, ino)"
1748 +                   " JOIN disk AS d ON(m.disk_id = d.disk_id)"
1749 +                   " WHERE host = ? AND devno != 0 AND present IS NULL";
1750 +               memset(binds, 0, sizeof binds);
1751 +               binds[0].buffer_type = MYSQL_TYPE_STRING;
1752 +               binds[0].buffer = &bind_thishost;
1753 +               binds[0].buffer_length = bind_thishost_len;
1754 +               if (!prepare_mysql(DEL_SUMS, binds, 1, sql))
1755 +                       exit_cleanup(RERR_SYNTAX);
1756 +
1757 +               return 1;
1758 +           }
1759 +#endif
1760 +#ifdef USE_SQLITE
1761 +       case DB_TYPE_SQLITE: {
1762 +               char *sql;
1763 +               sql="ATTACH DATABASE '' AS aux1;"; /* Private temp DB, probably in-memory */
1764 +               if (!run_sql(sql))
1765 +                       exit_cleanup(RERR_IPC);
1766 +
1767 +               sql="CREATE TABLE aux1.inode_present ("
1768 +                   " disk_id integer NOT NULL,"
1769 +                   " ino bigint NOT NULL,"
1770 +                   " present tinyint NOT NULL default '1',"
1771 +                   " PRIMARY KEY (disk_id,ino)"
1772 +                   ")";
1773 +               if (!run_sql(sql))
1774 +                       exit_cleanup(RERR_IPC);
1775 +
1776 +               sql="INSERT OR IGNORE INTO aux1.inode_present"
1777 +                   " (disk_id, ino, present)"
1778 +                   " VALUES (?, ?, 1)";
1779 +               if (!prepare_sqlite(INS_PRESENT, sql))
1780 +                       exit_cleanup(RERR_IPC);
1781 +
1782 +               sql="DELETE FROM inode_map"
1783 +                   " WHERE ROWID IN ("
1784 +                   "  SELECT m.ROWID"
1785 +                   "  FROM inode_map AS m"
1786 +                   "  LEFT JOIN aux1.inode_present USING(disk_id, ino)"
1787 +                   "  JOIN disk AS d ON(m.disk_id = d.disk_id)"
1788 +                   "  WHERE host = ? AND devno != 0 AND present IS NULL"
1789 +                   " )";
1790 +               if (!prepare_sqlite(DEL_SUMS, sql))
1791 +                       exit_cleanup(RERR_IPC);
1792 +
1793 +               transaction_state = -1; /* bug work-around -- force transaction off when cleaning XXX */
1794 +
1795 +               return 1;
1796 +           }
1797 +#endif
1798 +       }
1799 +
1800 +       return 0;
1801 +}
1802 +
1803 +int db_note_present(int disk_id, int64 ino)
1804 +{
1805 +       switch (use_db) {
1806 +#ifdef USE_MYSQL
1807 +       case DB_TYPE_MYSQL:
1808 +               bind_disk_id = disk_id;
1809 +               bind_ino = ino;
1810 +               return exec_mysql(INS_PRESENT) != NULL;
1811 +#endif
1812 +#ifdef USE_SQLITE
1813 +       case DB_TYPE_SQLITE: {
1814 +               int rc;
1815 +               sqlite3_stmt *stmt = statements[INS_PRESENT].sqlite;
1816 +               sqlite3_bind_int(stmt, 1, disk_id);
1817 +               sqlite3_bind_int64(stmt, 2, ino);
1818 +               rc = sqlite3_step(stmt);
1819 +               sqlite3_reset(stmt);
1820 +               return rc == SQLITE_DONE;
1821 +           }
1822 +#endif
1823 +       }
1824 +
1825 +       return 0;
1826 +}
1827 +
1828 +/* This function requires the user to have populated all disk_id+inode pairs
1829 + * into the inode_present table. */
1830 +int db_clean_inodes(void)
1831 +{
1832 +       int del_cnt = 0;
1833 +
1834 +       switch (use_db) {
1835 +#ifdef USE_MYSQL
1836 +       case DB_TYPE_MYSQL: {
1837 +               MYSQL_STMT *stmt = exec_mysql(DEL_SUMS);
1838 +               if (stmt != NULL)
1839 +                       del_cnt = mysql_affected_rows(dbh.mysql);
1840 +               break;
1841 +           }
1842 +#endif
1843 +#ifdef USE_SQLITE
1844 +       case DB_TYPE_SQLITE: {
1845 +               int rc;
1846 +               sqlite3_stmt *stmt = statements[DEL_SUMS].sqlite;
1847 +               sqlite3_bind_text(stmt, 1, bind_thishost, bind_thishost_len, SQLITE_STATIC);
1848 +               rc = sqlite3_step(stmt);
1849 +               if (rc == SQLITE_DONE)
1850 +                       del_cnt = sqlite3_changes(dbh.sqlite);
1851 +               sqlite3_reset(stmt);
1852 +               break;
1853 +           }
1854 +#endif
1855 +       }
1856 +
1857 +       return del_cnt;
1858 +}
1859 +
1860 +static int abs_path(char *buf, int bufsiz, const char *curdir, const char *dir)
1861 +{
1862 +       if (*dir == '/')
1863 +               strlcpy(buf, dir, bufsiz);
1864 +       else
1865 +               snprintf(buf, bufsiz, "%s/%s", curdir, dir);
1866 +
1867 +       return clean_fname(buf, CFN_DROP_TRAILING_DOT_DIR | CFN_COLLAPSE_DOT_DOT_DIRS);
1868 +}
1869 +
1870 +static struct name_list *new_name(const char *basename, const char *filename)
1871 +{
1872 +       struct name_list *n;
1873 +       int blen = strlen(basename);
1874 +       int slen = filename ? (int)strlen(filename) : -1;
1875 +       int len = blen + 1 + slen;
1876 +
1877 +       if (len >= MAXPATHLEN) {
1878 +               if (filename)
1879 +                       rprintf(FERROR, "Filename too long: %s/%s\n", basename, filename);
1880 +               else
1881 +                       rprintf(FERROR, "Filename too long: %s\n", basename);
1882 +               return NULL;
1883 +       }
1884 +
1885 +       if (!(n = (struct name_list *)malloc(sizeof (struct name_list) + len)))
1886 +               out_of_memory("new_name");
1887 +
1888 +       memcpy(n->name, basename, blen);
1889 +       if (filename) {
1890 +               n->name[blen] = '/';
1891 +               memcpy(n->name + 1 + blen, filename, slen);
1892 +       }
1893 +       n->name[len] = '\0';
1894 +       n->next = NULL;
1895 +
1896 +       return n;
1897 +}
1898 +
1899 +static int name_compare(const void *n1, const void *n2)
1900 +{
1901 +       struct name_list *p1 = *(struct name_list **)n1;
1902 +       struct name_list *p2 = *(struct name_list **)n2;
1903 +       return strcmp(p1->name, p2->name);
1904 +}
1905 +
1906 +static struct name_list *get_sorted_names(const char *dir)
1907 +{
1908 +       struct name_list *add, **sortbuf, *names = NULL, *prior_name = NULL;
1909 +       struct dirent *di;
1910 +       int cnt = 0;
1911 +       DIR *d;
1912 +
1913 +       if (!(d = opendir("."))) {
1914 +               rprintf(FERROR, "Unable to opendir %s: %s\n", dir, strerror(errno));
1915 +               return NULL;
1916 +       }
1917 +       while ((di = readdir(d)) != NULL) {
1918 +               char *dname = d_name(di);
1919 +               if (dname[0] == '.' && (dname[1] == '\0' || (dname[1] == '.' && dname[2] == '\0')))
1920 +                       continue;
1921 +               if (!(add = new_name(dname, NULL)))
1922 +                       continue;
1923 +               if (prior_name)
1924 +                       prior_name->next = add;
1925 +               else
1926 +                       names = add;
1927 +               prior_name = add;
1928 +               cnt++;
1929 +       }
1930 +       closedir(d);
1931 +
1932 +       if (cnt) {
1933 +               int j;
1934 +
1935 +               if (!(sortbuf = new_array(struct name_list *, cnt)))
1936 +                       out_of_memory("get_sorted_names");
1937 +               for (j = 0; j < cnt; j++) {
1938 +                       sortbuf[j] = names;
1939 +                       names = names->next;
1940 +               }
1941 +
1942 +               qsort(sortbuf, cnt, PTR_SIZE, name_compare);
1943 +
1944 +               names = prior_name = NULL;
1945 +               for (j = 0; j < cnt; j++) {
1946 +                       add = sortbuf[j];
1947 +                       if (prior_name)
1948 +                               prior_name->next = add;
1949 +                       else
1950 +                               names = add;
1951 +                       prior_name = add;
1952 +               }
1953 +
1954 +               if (prior_name)
1955 +                       prior_name->next = NULL;
1956 +               free(sortbuf);
1957 +       }
1958 +
1959 +       return names;
1960 +}
1961 +
1962 +static inline int sums_ne(const char *sum1, const char *sum2)
1963 +{
1964 +       return memcmp(sum1, sum2, MD5_DIGEST_LEN) != 0;
1965 +}
1966 +
1967 +/* Returns 1 if there is a checksum change, else 0. */
1968 +static int mention_file(const char *dir, const char *name, int right_cnt, int wrong_cnt,
1969 +                       const char *dbsum4, const char *dbsum5, const char *sum4, const char *sum5)
1970 +{
1971 +       char *info_str = wrong_cnt && !right_cnt ? "!i " : "   ";
1972 +       char *md4_str = !db_do_md4 ? NULL : !dbsum4 ? "+4 " : !sum4 ? "?4 " : sums_ne(sum4, dbsum4) ? "!4 " : "   ";
1973 +       char *md5_str = !db_do_md5 ? NULL : !dbsum5 ? "+5 " : !sum5 ? "?5 " : sums_ne(sum5, dbsum5) ? "!5 " : "   ";
1974 +       int chg = *info_str != ' ' || (md4_str && *md4_str != ' ') || (md5_str && *md5_str != ' ');
1975 +       if (chg || db_output_unchanged) {
1976 +               if (db_output_info) {
1977 +                       fputs(info_str, stdout);
1978 +                       if (md4_str)
1979 +                               fputs(md4_str, stdout);
1980 +                       if (md5_str)
1981 +                               fputs(md5_str, stdout);
1982 +               }
1983 +               if (db_output_sum) {
1984 +                       if (db_do_md4)
1985 +                               printf("%s ", sum_as_hex(4, sum4));
1986 +                       if (db_do_md5)
1987 +                               printf("%s ", sum_as_hex(5, sum5));
1988 +               }
1989 +               if (db_output_name) {
1990 +                       if (db_output_sum)
1991 +                               putchar(' '); /* We want 2 spaces, like md5sum. */
1992 +                       if (*dir != '.' || dir[1]) {
1993 +                               fputs(dir, stdout);
1994 +                               putchar('/');
1995 +                       }
1996 +                       puts(name);
1997 +               }
1998 +       }
1999 +
2000 +       return chg;
2001 +}
2002 +
2003 +NORETURN void run_dbonly(const char **args)
2004 +{
2005 +       char start_dir[MAXPATHLEN], dirbuf[MAXPATHLEN];
2006 +       int need_sum_cnt, start_dir_len;
2007 +       struct name_list *prior_dir;
2008 +       struct name_list *names;
2009 +       int exit_code = 0;
2010 +
2011 +       protocol_version = 31;
2012 +
2013 +       need_sum_cnt = db_do_md4 + db_do_md5;
2014 +
2015 +       if (!db_read_config(FERROR, db_config) || !db_connect(1))
2016 +               exit_cleanup(RERR_FILEIO);
2017 +
2018 +       if (db_clean)
2019 +               db_clean_init();
2020 +
2021 +       if (getcwd(start_dir, sizeof start_dir - 1) == NULL) {
2022 +               rsyserr(FERROR, errno, "getcwd()");
2023 +               exit_cleanup(RERR_FILESELECT);
2024 +       }
2025 +       start_dir_len = strlen(start_dir);
2026 +
2027 +       if (args) {
2028 +               prior_dir = NULL;
2029 +               while (*args) {
2030 +                       struct name_list *add;
2031 +                       if (abs_path(dirbuf, sizeof dirbuf, start_dir, *args++) <= 0)
2032 +                               continue;
2033 +                       if (!(add = new_name(dirbuf, NULL)))
2034 +                               continue;
2035 +                       if (prior_dir)
2036 +                               prior_dir->next = add;
2037 +                       else
2038 +                               dirs_list = add;
2039 +                       prior_dir = add;
2040 +               }
2041 +       } else
2042 +               dirs_list = new_name(start_dir, NULL);
2043 +
2044 +       prior_dir = NULL;
2045 +       while (dirs_list) {
2046 +               struct name_list *subdirs, *prior_subdir, *prior_name;
2047 +               const char *dir = dirs_list->name;
2048 +               const char *reldir = dir;
2049 +
2050 +               if (prior_dir)
2051 +                       free((void*)prior_dir);
2052 +               prior_dir = dirs_list;
2053 +               dirs_list = dirs_list->next;
2054 +
2055 +               if (strncmp(reldir, start_dir, start_dir_len) == 0) {
2056 +                       if (reldir[start_dir_len] == '\0')
2057 +                               reldir = ".";
2058 +                       else if (reldir[start_dir_len] == '/')
2059 +                               reldir += start_dir_len + 1;
2060 +               }
2061 +               if (db_output_dirs)
2062 +                       printf("... %s/ ...\n", reldir);
2063 +
2064 +               if (chdir(dir) < 0) {
2065 +                       rprintf(FERROR, "Unable to chdir to %s: %s\n", dir, strerror(errno));
2066 +                       continue;
2067 +               }
2068 +               if (!(names = get_sorted_names(dir)))
2069 +                       continue;
2070 +
2071 +               subdirs = prior_subdir = prior_name = NULL;
2072 +               while (names) {
2073 +                       STRUCT_STAT st;
2074 +                       char *dbsum4, *sum4, sumbuf4[MD5_DIGEST_LEN];
2075 +                       char *dbsum5, *sum5, sumbuf5[MD5_DIGEST_LEN];
2076 +                       int right_sum_cnt, wrong_sum_cnt;
2077 +                       const char *name = names->name;
2078 +                       unsigned int disk_id;
2079 +
2080 +                       if (prior_name)
2081 +                               free((void*)prior_name);
2082 +                       prior_name = names;
2083 +                       names = names->next;
2084 +
2085 +                       dbsum4 = dbsum5 = sum4 = sum5 = NULL;
2086 +
2087 +                       if (lstat(name, &st) < 0) {
2088 +                               rprintf(FERROR, "Failed to lstat(%s): %s\n", name, strerror(errno));
2089 +                               continue;
2090 +                       }
2091 +                       if (S_ISLNK(st.st_mode))
2092 +                               continue;
2093 +                       if (S_ISDIR(st.st_mode)) {
2094 +                               /* add optional excluding of things like /^(CVS|\.svn|\.git|\.bzr)$/; */
2095 +                               if (recurse) {
2096 +                                       struct name_list *add = new_name(dir, name);
2097 +                                       if (add) {
2098 +                                               if (prior_subdir)
2099 +                                                       prior_subdir->next = add;
2100 +                                               else
2101 +                                                       subdirs = add;
2102 +                                               prior_subdir = add;
2103 +                                       }
2104 +                               }
2105 +                               continue;
2106 +                       }
2107 +                       if (!S_ISREG(st.st_mode))
2108 +                               continue;
2109 +
2110 +                       if (!(disk_id = get_disk_id(st.st_dev)))
2111 +                               continue;
2112 +                       if (db_clean) {
2113 +                               db_note_present(disk_id, st.st_ino);
2114 +                               if (!db_update && !db_check)
2115 +                                       continue;
2116 +                       }
2117 +                       db_get_both_checksums(&st, &right_sum_cnt, &wrong_sum_cnt,
2118 +                                             db_do_md4 ? &dbsum4 : NULL, db_do_md5 ? &dbsum5 : NULL);
2119 +
2120 +                       if (!db_check && right_sum_cnt == need_sum_cnt) {
2121 +                               mention_file(reldir, name, right_sum_cnt, wrong_sum_cnt, dbsum4, dbsum5, dbsum4, dbsum5);
2122 +                               continue;
2123 +                       }
2124 +
2125 +                       if (db_update || (db_check && right_sum_cnt) || db_output_sum) {
2126 +                               uchar *data;
2127 +                               int32 remainder;
2128 +                               md_context m4, m5;
2129 +                               struct map_struct *buf;
2130 +                               OFF_T off, len = st.st_size;
2131 +                               int fd = do_open(name, O_RDONLY, 0);
2132 +
2133 +                               if (fd < 0) {
2134 +                                       rprintf(FERROR, "ERROR: unable to read %s: %s\n", name, strerror(errno));
2135 +                                       continue;
2136 +                               }
2137 +
2138 +                               if (db_do_md4)
2139 +                                       mdfour_begin(&m4);
2140 +                               if (db_do_md5)
2141 +                                       md5_begin(&m5);
2142 +
2143 +                               buf = map_file(fd, len, MAX_MAP_SIZE, CSUM_CHUNK);
2144 +
2145 +                               for (off = 0; off + CSUM_CHUNK <= len; off += CSUM_CHUNK) {
2146 +                                       data = (uchar*)map_ptr(buf, off, CSUM_CHUNK);
2147 +                                       if (db_do_md4)
2148 +                                               mdfour_update(&m4, data, CSUM_CHUNK);
2149 +                                       if (db_do_md5)
2150 +                                               md5_update(&m5, data, CSUM_CHUNK);
2151 +                               }
2152 +
2153 +                               remainder = (int32)(len - off);
2154 +                               data = (uchar*)map_ptr(buf, off, remainder);
2155 +                               if (db_do_md4) {
2156 +                                       mdfour_update(&m4, data, remainder);
2157 +                                       mdfour_result(&m4, (uchar*)(sum4 = sumbuf4));
2158 +                               }
2159 +                               if (db_do_md5) {
2160 +                                       md5_update(&m5, data, remainder);
2161 +                                       md5_result(&m5, (uchar*)(sum5 = sumbuf5));
2162 +                               }
2163 +
2164 +                               close(fd);
2165 +                               unmap_file(buf);
2166 +                       }
2167 +
2168 +                       int chg = mention_file(reldir, name, right_sum_cnt, wrong_sum_cnt, dbsum4, dbsum5, sum4, sum5);
2169 +                       if (!chg) {
2170 +                               /* Only db_check should get here... */
2171 +                       } else if (!db_update) {
2172 +                               exit_code = 1;
2173 +                       } else {
2174 +                               int fail = 0;
2175 +                               if (db_do_md4 && !db_set_checksum(4, &st, sum4))
2176 +                                       fail = 1;
2177 +                               if (db_do_md5 && !db_set_checksum(5, &st, sum5))
2178 +                                       fail = 1;
2179 +                               if (fail) {
2180 +                                       fprintf(stderr, "Failed to set checksum on %s/%s\n", reldir, name);
2181 +                                       exit_cleanup(RERR_FILEIO);
2182 +                               }
2183 +                       }
2184 +               }
2185 +               if (prior_name)
2186 +                       free((void*)prior_name);
2187 +
2188 +               if (recurse && subdirs) {
2189 +                       prior_subdir->next = dirs_list;
2190 +                       dirs_list = subdirs;
2191 +               }
2192 +       }
2193 +       if (prior_dir)
2194 +               free((void*)prior_dir);
2195 +
2196 +       if (db_clean) {
2197 +               int rows = db_clean_inodes();
2198 +               if (db_output_msgs)
2199 +                       printf("Cleaned out %d old inode%s.\n", rows, rows == 1 ? "" : "s");
2200 +       }
2201 +
2202 +       db_disconnect(True);
2203 +       exit(exit_code);
2204 +}
2205 diff --git a/flist.c b/flist.c
2206 --- a/flist.c
2207 +++ b/flist.c
2208 @@ -53,6 +53,7 @@ extern int preserve_devices;
2209  extern int preserve_specials;
2210  extern int delete_during;
2211  extern int missing_args;
2212 +extern int use_db;
2213  extern int eol_nulls;
2214  extern int relative_paths;
2215  extern int implied_dirs;
2216 @@ -1312,11 +1313,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
2217                 extra_len += EXTRA_LEN;
2218  #endif
2219  
2220 -       if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
2221 -               file_checksum(thisname, &st, tmp_sum);
2222 -               if (sender_keeps_checksum)
2223 -                       extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
2224 -       }
2225 +       if (sender_keeps_checksum && S_ISREG(st.st_mode))
2226 +               extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
2227  
2228  #if EXTRA_ROUNDING > 0
2229         if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
2230 @@ -1401,8 +1399,12 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
2231                 return NULL;
2232         }
2233  
2234 -       if (sender_keeps_checksum && S_ISREG(st.st_mode))
2235 -               memcpy(F_SUM(file), tmp_sum, checksum_len);
2236 +       if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
2237 +               if (!use_db || !db_get_checksum(&st, tmp_sum))
2238 +                       file_checksum(thisname, &st, tmp_sum);
2239 +               if (sender_keeps_checksum)
2240 +                       memcpy(F_SUM(file), tmp_sum, checksum_len);
2241 +       }
2242  
2243         if (unsort_ndx)
2244                 F_NDX(file) = stats.num_dirs;
2245 @@ -2063,6 +2065,9 @@ void send_extra_file_list(int f, int at_least)
2246    finish:
2247         if (io_error != save_io_error && protocol_version == 30 && !ignore_errors)
2248                 send_msg_int(MSG_IO_ERROR, io_error);
2249 +
2250 +       if (use_db && flist_eof)
2251 +               db_disconnect(True);
2252  }
2253  
2254  struct file_list *send_file_list(int f, int argc, char *argv[])
2255 @@ -2086,6 +2091,13 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
2256                      | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
2257         int implied_dot_dir = 0;
2258  
2259 +       if (use_db) {
2260 +               if (always_checksum)
2261 +                       db_connect(0); /* Will reset use_db on error. */
2262 +               else
2263 +                       use_db = 0;
2264 +       }
2265 +
2266         rprintf(FLOG, "building file list\n");
2267         if (show_filelist_p())
2268                 start_filelist_progress("building file list");
2269 @@ -2432,6 +2444,9 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
2270                         rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i());
2271         }
2272  
2273 +       if (use_db && (!inc_recurse || flist_eof))
2274 +               db_disconnect(True);
2275 +
2276         return flist;
2277  }
2278  
2279 diff --git a/generator.c b/generator.c
2280 --- a/generator.c
2281 +++ b/generator.c
2282 @@ -59,6 +59,7 @@ extern int ignore_existing;
2283  extern int ignore_non_existing;
2284  extern int want_xattr_optim;
2285  extern int inplace;
2286 +extern int use_db;
2287  extern int append_mode;
2288  extern int make_backups;
2289  extern int csum_length;
2290 @@ -582,7 +583,8 @@ int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
2291            of the file time to determine whether to sync */
2292         if (always_checksum > 0 && S_ISREG(st->st_mode)) {
2293                 char sum[MAX_DIGEST_LEN];
2294 -               file_checksum(fn, st, sum);
2295 +               if (!use_db || !db_get_checksum(st, sum))
2296 +                       file_checksum(fn, st, sum);
2297                 return memcmp(sum, F_SUM(file), checksum_len) == 0;
2298         }
2299  
2300 @@ -2225,6 +2227,13 @@ void generate_files(int f_out, const char *local_name)
2301                         : "enabled");
2302         }
2303  
2304 +       if (use_db) {
2305 +               if (always_checksum || (append_mode != 1 && protocol_version >= 30))
2306 +                       db_connect(0); /* Will reset use_db on error. */
2307 +               else
2308 +                       use_db = 0;
2309 +       }
2310 +
2311         dflt_perms = (ACCESSPERMS & ~orig_umask);
2312  
2313         do {
2314 @@ -2350,6 +2359,9 @@ void generate_files(int f_out, const char *local_name)
2315                         wait_for_receiver();
2316         }
2317  
2318 +       if (use_db)
2319 +               db_disconnect(True);
2320 +
2321         info_levels[INFO_FLIST] = save_info_flist;
2322         info_levels[INFO_PROGRESS] = save_info_progress;
2323  
2324 diff --git a/io.c b/io.c
2325 --- a/io.c
2326 +++ b/io.c
2327 @@ -41,8 +41,10 @@ extern int am_server;
2328  extern int am_sender;
2329  extern int am_receiver;
2330  extern int am_generator;
2331 +extern int local_server;
2332  extern int msgs2stderr;
2333  extern int inc_recurse;
2334 +extern int same_db;
2335  extern int io_error;
2336  extern int eol_nulls;
2337  extern int flist_eof;
2338 @@ -1481,6 +1483,32 @@ static void read_a_msg(void)
2339                 if (am_sender)
2340                         maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
2341                 break;
2342 +       case MSG_CHECKSUM:
2343 +               /* This receives some checksum info that we want to make a note of
2344 +                * (which allows a single process to do all the writing to the db). */
2345 +               if (msg_bytes != MSG_CHECKSUM_LEN)
2346 +                       goto overflow;
2347 +               raw_read_buf(data, MSG_CHECKSUM_LEN);
2348 +               if (am_generator && same_db) {
2349 +                       iobuf.in_multiplexed = 1;
2350 +                       send_msg(MSG_CHECKSUM, data, MSG_CHECKSUM_LEN, 0);
2351 +               } if (am_receiver || (am_sender && !local_server))
2352 +                       goto unexpected;
2353 +               else {
2354 +                       /* The received data is a set of numbers followed by the checksum. */
2355 +                       STRUCT_STAT st;
2356 +                       st.st_dev = IVAL64(data, 0);
2357 +                       st.st_ino = IVAL64(data, 8);
2358 +                       st.st_size = IVAL64(data, 16);
2359 +                       st.st_mtime = IVAL64(data, 24);
2360 +                       st.st_ctime = IVAL64(data, 32);
2361 +#if MSG_CHECKSUM_LONGS != 5
2362 +#error Fix the parsing of checksum long values
2363 +#endif
2364 +                       iobuf.in_multiplexed = 1;
2365 +                       db_set_checksum(IVAL(data, MSG_CHECKSUM_LONGS*8), &st, data + MSG_CHECKSUM_LONGS*8 + 4);
2366 +               }
2367 +               break;
2368         case MSG_DELETED:
2369                 if (msg_bytes >= sizeof data)
2370                         goto overflow;
2371 @@ -1632,6 +1660,7 @@ static void read_a_msg(void)
2372                  * with a duplicate exit message. */
2373                 _exit_cleanup(val, __FILE__, 0 - __LINE__);
2374         default:
2375 +       unexpected:
2376                 rprintf(FERROR, "unexpected tag %d [%s%s]\n",
2377                         tag, who_am_i(), inc_recurse ? "/inc" : "");
2378                 exit_cleanup(RERR_STREAMIO);
2379 diff --git a/loadparm.c b/loadparm.c
2380 --- a/loadparm.c
2381 +++ b/loadparm.c
2382 @@ -109,6 +109,7 @@ typedef struct {
2383         char *auth_users;
2384         char *charset;
2385         char *comment;
2386 +       char *db_config;
2387         char *dont_compress;
2388         char *exclude;
2389         char *exclude_from;
2390 @@ -139,6 +140,7 @@ typedef struct {
2391         int syslog_facility;
2392         int timeout;
2393  
2394 +       BOOL db_lax;
2395         BOOL fake_super;
2396         BOOL forward_lookup;
2397         BOOL ignore_errors;
2398 @@ -185,6 +187,7 @@ static const all_vars Defaults = {
2399   /* auth_users; */             NULL,
2400   /* charset; */                NULL,
2401   /* comment; */                NULL,
2402 + /* db_config; */              NULL,
2403   /* dont_compress; */          DEFAULT_DONT_COMPRESS,
2404   /* exclude; */                        NULL,
2405   /* exclude_from; */           NULL,
2406 @@ -213,6 +216,7 @@ static const all_vars Defaults = {
2407   /* syslog_facility; */                LOG_DAEMON,
2408   /* timeout; */                        0,
2409  
2410 + /* db_lax; */                 False,
2411   /* fake_super; */             False,
2412   /* forward_lookup; */         True,
2413   /* ignore_errors; */          False,
2414 @@ -322,6 +326,8 @@ static struct parm_struct parm_table[] =
2415   {"auth users",        P_STRING, P_LOCAL, &Vars.l.auth_users,          NULL,0},
2416   {"charset",           P_STRING, P_LOCAL, &Vars.l.charset,             NULL,0},
2417   {"comment",           P_STRING, P_LOCAL, &Vars.l.comment,             NULL,0},
2418 + {"db config",         P_STRING, P_LOCAL, &Vars.l.db_config,           NULL,0},
2419 + {"db lax",            P_BOOL,   P_LOCAL, &Vars.l.db_lax,              NULL,0},
2420   {"dont compress",     P_STRING, P_LOCAL, &Vars.l.dont_compress,       NULL,0},
2421   {"exclude from",      P_STRING, P_LOCAL, &Vars.l.exclude_from,        NULL,0},
2422   {"exclude",           P_STRING, P_LOCAL, &Vars.l.exclude,             NULL,0},
2423 @@ -454,6 +460,7 @@ FN_GLOBAL_INTEGER(lp_rsync_port, &Vars.g.rsync_port)
2424  FN_LOCAL_STRING(lp_auth_users, auth_users)
2425  FN_LOCAL_STRING(lp_charset, charset)
2426  FN_LOCAL_STRING(lp_comment, comment)
2427 +FN_LOCAL_STRING(lp_db_config, db_config)
2428  FN_LOCAL_STRING(lp_dont_compress, dont_compress)
2429  FN_LOCAL_STRING(lp_exclude, exclude)
2430  FN_LOCAL_STRING(lp_exclude_from, exclude_from)
2431 @@ -482,6 +489,7 @@ FN_LOCAL_INTEGER(lp_max_verbosity, max_verbosity)
2432  FN_LOCAL_INTEGER(lp_syslog_facility, syslog_facility)
2433  FN_LOCAL_INTEGER(lp_timeout, timeout)
2434  
2435 +FN_LOCAL_BOOL(lp_db_lax, db_lax)
2436  FN_LOCAL_BOOL(lp_fake_super, fake_super)
2437  FN_LOCAL_BOOL(lp_forward_lookup, forward_lookup)
2438  FN_LOCAL_BOOL(lp_ignore_errors, ignore_errors)
2439 diff --git a/main.c b/main.c
2440 --- a/main.c
2441 +++ b/main.c
2442 @@ -34,6 +34,7 @@ extern int am_root;
2443  extern int am_server;
2444  extern int am_sender;
2445  extern int am_daemon;
2446 +extern int am_dbadmin;
2447  extern int inc_recurse;
2448  extern int blocking_io;
2449  extern int always_checksum;
2450 @@ -51,6 +52,7 @@ extern int copy_unsafe_links;
2451  extern int keep_dirlinks;
2452  extern int preserve_hard_links;
2453  extern int protocol_version;
2454 +extern int always_checksum;
2455  extern int file_total;
2456  extern int recurse;
2457  extern int xfer_dirs;
2458 @@ -85,6 +87,7 @@ extern char *filesfrom_host;
2459  extern char *partial_dir;
2460  extern char *dest_option;
2461  extern char *rsync_path;
2462 +extern char *db_config;
2463  extern char *shell_cmd;
2464  extern char *batch_name;
2465  extern char *password_file;
2466 @@ -1100,6 +1103,9 @@ void start_server(int f_in, int f_out, int argc, char *argv[])
2467         if (am_daemon && io_timeout && protocol_version >= 31)
2468                 send_msg_int(MSG_IO_TIMEOUT, io_timeout);
2469  
2470 +       if (db_config)
2471 +               db_read_config(FERROR, db_config);
2472 +
2473         if (am_sender) {
2474                 keep_dirlinks = 0; /* Must be disabled on the sender. */
2475                 if (need_messages_from_generator)
2476 @@ -1381,6 +1387,9 @@ static int start_client(int argc, char *argv[])
2477                 }
2478         }
2479  
2480 +       if (db_config)
2481 +               db_read_config(FERROR, db_config);
2482 +
2483         if (daemon_over_rsh < 0)
2484                 return start_socket_client(shell_machine, remote_argc, remote_argv, argc, argv);
2485  
2486 diff --git a/mkproto.pl b/mkproto.pl
2487 --- a/mkproto.pl
2488 +++ b/mkproto.pl
2489 @@ -13,6 +13,8 @@ if (open(IN, 'proto.h')) {
2490      STRING => 'char *',
2491  );
2492  
2493 +@ARGV = grep !m{/rsyncdb\.c$}, @ARGV;
2494 +
2495  $inheader = 0;
2496  $protos = qq|/* This file is automatically generated with "make proto". DO NOT EDIT */\n\n|;
2497  
2498 diff --git a/options.c b/options.c
2499 --- a/options.c
2500 +++ b/options.c
2501 @@ -82,6 +82,7 @@ int am_root = 0; /* 0 = normal, 1 = root, 2 = --super, -1 = --fake-super */
2502  int am_server = 0;
2503  int am_sender = 0;
2504  int am_starting_up = 1;
2505 +int am_dbadmin = 0;
2506  int relative_paths = -1;
2507  int implied_dirs = 1;
2508  int missing_args = 0; /* 0 = FERROR_XFER, 1 = ignore, 2 = delete */
2509 @@ -95,6 +96,7 @@ int use_qsort = 0;
2510  char *files_from = NULL;
2511  int filesfrom_fd = -1;
2512  char *filesfrom_host = NULL;
2513 +char *db_config = NULL;
2514  int eol_nulls = 0;
2515  int protect_args = -1;
2516  int human_readable = 1;
2517 @@ -102,6 +104,9 @@ int recurse = 0;
2518  int allow_inc_recurse = 1;
2519  int xfer_dirs = -1;
2520  int am_daemon = 0;
2521 +int db_clean, db_check, db_do_md4, db_do_md5, db_update = 1, db_lax, db_init, db_mounts;
2522 +int db_output_name, db_output_sum, db_output_info, db_output_unchanged, db_output_dirs, db_output_msgs;
2523 +int saw_db_output_opt, saw_db_sum_opt;
2524  int connect_timeout = 0;
2525  int keep_partial = 0;
2526  int safe_symlinks = 0;
2527 @@ -271,6 +276,7 @@ static struct output_struct debug_words[COUNT_DEBUG+1] = {
2528         DEBUG_WORD(CHDIR, W_CLI|W_SRV, "Debug when the current directory changes"),
2529         DEBUG_WORD(CONNECT, W_CLI, "Debug connection events (levels 1-2)"),
2530         DEBUG_WORD(CMD, W_CLI, "Debug commands+options that are issued (levels 1-2)"),
2531 +       DEBUG_WORD(DB, W_SND|W_REC, "Debug DB operations (levels 1-5)"),
2532         DEBUG_WORD(DEL, W_REC, "Debug delete actions (levels 1-3)"),
2533         DEBUG_WORD(DELTASUM, W_SND|W_REC, "Debug delta-transfer checksumming (levels 1-4)"),
2534         DEBUG_WORD(DUP, W_REC, "Debug weeding of duplicate names"),
2535 @@ -573,6 +579,7 @@ static void print_rsync_version(enum logcode f)
2536         char const *links = "no ";
2537         char const *iconv = "no ";
2538         char const *ipv6 = "no ";
2539 +       char const *db = "no ";
2540         STRUCT_STAT *dumstat;
2541  
2542  #if SUBPROTOCOL_VERSION != 0
2543 @@ -609,6 +616,11 @@ static void print_rsync_version(enum logcode f)
2544  #ifdef CAN_SET_SYMLINK_TIMES
2545         symtimes = "";
2546  #endif
2547 +#if defined HAVE_MYSQL_MYSQL_H && defined HAVE_LIBMYSQLCLIENT
2548 +       db = "";
2549 +#elif defined HAVE_SQLITE3_H && defined HAVE_LIBSQLITE3
2550 +       db = "";
2551 +#endif
2552  
2553         rprintf(f, "%s  version %s  protocol version %d%s\n",
2554                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
2555 @@ -622,8 +634,8 @@ static void print_rsync_version(enum logcode f)
2556                 (int)(sizeof (int64) * 8));
2557         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
2558                 got_socketpair, hardlinks, links, ipv6, have_inplace);
2559 -       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc\n",
2560 -               have_inplace, acls, xattrs, iconv, symtimes, prealloc);
2561 +       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc, %sdb\n",
2562 +               have_inplace, acls, xattrs, iconv, symtimes, prealloc, db);
2563  
2564  #ifdef MAINTAINER_MODE
2565         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
2566 @@ -672,6 +684,9 @@ void usage(enum logcode F)
2567    rprintf(F," -q, --quiet                 suppress non-error messages\n");
2568    rprintf(F,"     --no-motd               suppress daemon-mode MOTD (see manpage caveat)\n");
2569    rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
2570 +  rprintf(F,"     --db=CONFIG_FILE        specify a CONFIG_FILE for DB checksums\n");
2571 +  rprintf(F,"     --db-only=CONFIG_FILE   behave like rsyncdb\n");
2572 +  rprintf(F,"     --db-lax                ignore ctime changes (use with CAUTION)\n");
2573    rprintf(F," -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)\n");
2574    rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
2575    rprintf(F," -r, --recursive             recurse into directories\n");
2576 @@ -820,6 +835,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
2577        OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
2578        OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
2579        OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG,
2580 +      OPT_NO_DB, OPT_DBONLY,
2581        OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT,
2582        OPT_SERVER, OPT_REFUSED_BASE = 9000};
2583  
2584 @@ -960,6 +976,10 @@ static struct poptOption long_options[] = {
2585    {"checksum",        'c', POPT_ARG_VAL,    &always_checksum, 1, 0, 0 },
2586    {"no-checksum",      0,  POPT_ARG_VAL,    &always_checksum, 0, 0, 0 },
2587    {"no-c",             0,  POPT_ARG_VAL,    &always_checksum, 0, 0, 0 },
2588 +  {"db",               0,  POPT_ARG_STRING, &db_config, 0, 0, 0 },
2589 +  {"no-db",            0,  POPT_ARG_NONE,   0, OPT_NO_DB, 0, 0 },
2590 +  {"db-lax",           0,  POPT_ARG_VAL,    &db_lax, 1, 0, 0 },
2591 +  {"no-db-lax",        0,  POPT_ARG_VAL,    &db_lax, 0, 0, 0 },
2592    {"block-size",      'B', POPT_ARG_LONG,   &block_size, 0, 0, 0 },
2593    {"compare-dest",     0,  POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
2594    {"copy-dest",        0,  POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
2595 @@ -1050,6 +1070,9 @@ static struct poptOption long_options[] = {
2596    {"dparam",           0,  POPT_ARG_STRING, 0, OPT_DAEMON, 0, 0 },
2597    {"detach",           0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
2598    {"no-detach",        0,  POPT_ARG_NONE,   0, OPT_DAEMON, 0, 0 },
2599 +  /* All the following options switch us into DB-admin option-parsing. */
2600 +  {"db-help",          0,  POPT_ARG_NONE,   0, OPT_DBONLY, 0, 0 },
2601 +  {"db-only",          0,  POPT_ARG_STRING, 0, OPT_DBONLY, 0, 0 },
2602    {0,0,0,0, 0, 0, 0}
2603  };
2604  
2605 @@ -1103,6 +1126,50 @@ static struct poptOption long_daemon_options[] = {
2606    {0,0,0,0, 0, 0, 0}
2607  };
2608  
2609 +static void dbonly_usage(enum logcode F)
2610 +{
2611 +  rprintf(F,"Usage: rsyncdb --db=CONFIG_FILE [OPTIONS] [DIRS]\n");
2612 +  rprintf(F,"\n");
2613 +  rprintf(F,"Options:\n");
2614 +  rprintf(F,"    --db=CONFIG   Specify the CONFIG file to read for the DB info.\n");
2615 +  rprintf(F,"    --db-lax      Ignore ctime changes (use with CAUTION).\n");
2616 +  rprintf(F,"-r, --recursive   Scan files in subdirs (the default w/o --no-recursive).\n");
2617 +  rprintf(F,"-s, --sums=SUMS   List which checksums to update (default: 4,5).\n");
2618 +  rprintf(F,"-o, --output=STR  One or more letters of what to output (default is nothing).\n");
2619 +  rprintf(F,"-c, --check       Check the checksums (by reading the files) and fix issues.\n");
2620 +  rprintf(F,"    --clean       Note all inodes in the DIRS and remove DB extras.\n");
2621 +  rprintf(F,"-N, --no-update   Avoids updating/adding info with --check and/or --clean.\n");
2622 +  rprintf(F,"    --init        Initialize a DB by (re-)creating its tables.\n");
2623 +  rprintf(F,"    --mounts      Scan for mounted filesystems and update the DB.\n");
2624 +  rprintf(F,"-q, --quiet       Disable the default non-error output.\n");
2625 +  rprintf(F,"-h, --help        Display this help message.\n");
2626 +}
2627 +
2628 +static struct poptOption long_dbonly_options[] = {
2629 +  /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2630 +  {"check",           'c', POPT_ARG_NONE,   &db_check, 0, 0, 0},
2631 +  {"clean",            0,  POPT_ARG_NONE,   &db_clean, 0, 0, 0},
2632 +  {"db",               0,  POPT_ARG_STRING, &db_config, 0, 0, 0 },
2633 +  {"db-only",          0,  POPT_ARG_STRING, &db_config, 0, 0, 0 },
2634 +  {"db-lax",           0,  POPT_ARG_VAL,    &db_lax, 1, 0, 0 },
2635 +  {"no-db-lax",        0,  POPT_ARG_VAL,    &db_lax, 0, 0, 0 },
2636 +  {"info",             0,  POPT_ARG_STRING, 0, OPT_INFO, 0, 0 },
2637 +  {"debug",            0,  POPT_ARG_STRING, 0, OPT_DEBUG, 0, 0 },
2638 +  {"update",          'u', POPT_ARG_VAL,    &db_update, 1, 0, 0 },
2639 +  {"no-update",       'N', POPT_ARG_VAL,    &db_update, 0, 0, 0 },
2640 +  {"no-u",             0,  POPT_ARG_VAL,    &db_update, 0, 0, 0 },
2641 +  {"output",          'o', POPT_ARG_STRING, 0, 'o', 0, 0 },
2642 +  {"recursive",       'r', POPT_ARG_VAL,    &recurse, 1, 0, 0 },
2643 +  {"no-recursive",     0,  POPT_ARG_VAL,    &recurse, 0, 0, 0 },
2644 +  {"no-r",             0,  POPT_ARG_VAL,    &recurse, 0, 0, 0 },
2645 +  {"sums",            's', POPT_ARG_STRING, 0, 's', 0, 0 },
2646 +  {"init",             0,  POPT_ARG_NONE,   &db_init, 0, 0, 0 },
2647 +  {"mounts",           0,  POPT_ARG_NONE,   &db_mounts, 0, 0, 0 },
2648 +  {"quiet",           'q', POPT_ARG_NONE,   &quiet, 0, 0, 0 },
2649 +  {"help",            'h', POPT_ARG_NONE,   0, 'h', 0, 0 },
2650 +  {"db-help",          0,  POPT_ARG_NONE,   0, 'h', 0, 0 },
2651 +  {0,0,0,0, 0, 0, 0}
2652 +};
2653  
2654  static char err_buf[200];
2655  
2656 @@ -1281,6 +1348,100 @@ static void create_refuse_error(int which)
2657         }
2658  }
2659  
2660 +static NORETURN void parse_dbonly_args(int argc, const char **argv)
2661 +{
2662 +       poptContext pc = poptGetContext(RSYNC_NAME, argc, argv, long_dbonly_options, 0);
2663 +       const char *arg;
2664 +       int opt;
2665 +
2666 +       recurse = 1;
2667 +       am_dbadmin = 1;
2668 +
2669 +       while ((opt = poptGetNextOpt(pc)) != -1) {
2670 +               const char *cp;
2671 +               switch (opt) {
2672 +               case 'o':
2673 +                       for (cp = poptGetOptArg(pc); *cp; cp++) {
2674 +                               switch (toLower(cp)) {
2675 +                               case 'n':
2676 +                                       db_output_name = 1;
2677 +                                       break;
2678 +                               case 's':
2679 +                               case 'c':
2680 +                                       db_output_sum = db_output_name = 1;
2681 +                                       break;
2682 +                               case 'i':
2683 +                                       db_output_info = db_output_name = 1;
2684 +                                       break;
2685 +                               case 'u':
2686 +                                       db_output_unchanged = db_output_name = 1;
2687 +                                       break;
2688 +                               case 'd':
2689 +                                       db_output_dirs = 1;
2690 +                                       break;
2691 +                               }
2692 +                       }
2693 +                       saw_db_output_opt = 1;
2694 +                       break;
2695 +
2696 +               case 's':
2697 +                       for (cp = poptGetOptArg(pc); *cp; cp++) {
2698 +                               switch (*cp) {
2699 +                               case '4':
2700 +                                       db_do_md4 = 1;
2701 +                                       break;
2702 +                               case '5':
2703 +                                       db_do_md5 = 1;
2704 +                                       break;
2705 +                               }
2706 +                       }
2707 +                       saw_db_sum_opt = 1;
2708 +                       break;
2709 +
2710 +               case 'h':
2711 +                       dbonly_usage(FINFO);
2712 +                       exit_cleanup(0);
2713 +
2714 +               case OPT_INFO:
2715 +                       arg = poptGetOptArg(pc);
2716 +                       parse_output_words(info_words, info_levels, arg, USER_PRIORITY);
2717 +                       break;
2718 +
2719 +               case OPT_DEBUG:
2720 +                       arg = poptGetOptArg(pc);
2721 +                       parse_output_words(debug_words, debug_levels, arg, USER_PRIORITY);
2722 +                       break;
2723 +
2724 +               default:
2725 +                       rprintf(FERROR,
2726 +                               "rsyncdb: %s: %s\n",
2727 +                               poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2728 +                               poptStrerror(opt));
2729 +                       goto dbonly_usage;
2730 +               }
2731 +       }
2732 +
2733 +       if (!db_config) {
2734 +               rprintf(FERROR, "You must specify the --db=FILE option.\n");
2735 +         dbonly_usage:
2736 +               rprintf(FERROR,
2737 +                       "(Type \"rsyncdb --help\" for assistance.)\n");
2738 +               exit_cleanup(RERR_SYNTAX);
2739 +       }
2740 +
2741 +       if (db_check)
2742 +               db_output_info = 1;
2743 +       if (!saw_db_output_opt && !quiet)
2744 +               db_output_dirs = db_output_name = 1;
2745 +       if (!quiet)
2746 +               db_output_msgs = 1;
2747 +       if (!saw_db_sum_opt)
2748 +               db_do_md5 = 1;
2749 +
2750 +       am_starting_up = 0;
2751 +       run_dbonly(poptGetArgs(pc));
2752 +       exit(42); /* NOT REACHED */
2753 +}
2754  
2755  /**
2756   * Process command line arguments.  Called on both local and remote.
2757 @@ -1298,10 +1459,18 @@ int parse_arguments(int *argc_p, const char ***argv_p)
2758         int argc = *argc_p;
2759         int opt;
2760  
2761 +       arg = *argv + strlen(*argv);
2762 +       if (arg - *argv > 2 && strcmp(arg-2, "db") == 0) {
2763 +               parse_dbonly_args(argc, argv);
2764 +               /* NOT REACHED */
2765 +       }
2766 +
2767         if (ref && *ref)
2768                 set_refuse_options(ref);
2769         if (am_daemon) {
2770                 set_refuse_options("log-file*");
2771 +               set_refuse_options("db");
2772 +               set_refuse_options("db-lax");
2773  #ifdef ICONV_OPTION
2774                 if (!*lp_charset(module_id))
2775                         set_refuse_options("iconv");
2776 @@ -1424,6 +1593,12 @@ int parse_arguments(int *argc_p, const char ***argv_p)
2777                         am_daemon = 1;
2778                         return 1;
2779  
2780 +               case OPT_DBONLY:
2781 +                       protect_args = 0;
2782 +                       poptFreeContext(pc);
2783 +                       parse_dbonly_args(argc, argv);
2784 +                       break; /* NOT REACHED */
2785 +
2786                 case OPT_MODIFY_WINDOW:
2787                         /* The value has already been set by popt, but
2788                          * we need to remember that we're using a
2789 @@ -1498,6 +1673,10 @@ int parse_arguments(int *argc_p, const char ***argv_p)
2790                         preserve_devices = preserve_specials = 0;
2791                         break;
2792  
2793 +               case OPT_NO_DB:
2794 +                       db_config = NULL;
2795 +                       break;
2796 +
2797                 case 'h':
2798                         human_readable++;
2799                         break;
2800 diff --git a/pipe.c b/pipe.c
2801 --- a/pipe.c
2802 +++ b/pipe.c
2803 @@ -27,11 +27,16 @@ extern int am_server;
2804  extern int blocking_io;
2805  extern int filesfrom_fd;
2806  extern int munge_symlinks;
2807 +extern int always_checksum;
2808 +extern int use_db;
2809 +extern char *db_config;
2810  extern char *logfile_name;
2811  extern int remote_option_cnt;
2812  extern const char **remote_options;
2813  extern struct chmod_mode_struct *chmod_modes;
2814  
2815 +int same_db = 0;
2816 +
2817  /**
2818   * Create a child connected to us via its stdin/stdout.
2819   *
2820 @@ -142,13 +147,22 @@ pid_t local_child(int argc, char **argv, int *f_in, int *f_out,
2821                 }
2822  
2823                 if (remote_option_cnt) {
2824 +                       const char *db_config_save = db_config;
2825                         int rc = remote_option_cnt + 1;
2826                         const char **rv = remote_options;
2827                         if (!parse_arguments(&rc, &rv)) {
2828                                 option_error();
2829                                 exit_cleanup(RERR_SYNTAX);
2830                         }
2831 -               }
2832 +                       if (db_config == db_config_save)
2833 +                               same_db = db_config != NULL;
2834 +                       else if (!db_config || !db_config_save || strcmp(db_config, db_config_save) != 0) {
2835 +                               use_db = 0;
2836 +                               if (db_config)
2837 +                                       db_read_config(FERROR, db_config);
2838 +                       }
2839 +               } else if (use_db)
2840 +                       same_db = 1;
2841  
2842                 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
2843                     close(to_child_pipe[1]) < 0 ||
2844 diff --git a/receiver.c b/receiver.c
2845 --- a/receiver.c
2846 +++ b/receiver.c
2847 @@ -24,6 +24,8 @@
2848  
2849  extern int dry_run;
2850  extern int do_xfers;
2851 +extern int use_db;
2852 +extern int db_lax;
2853  extern int am_root;
2854  extern int am_server;
2855  extern int inc_recurse;
2856 @@ -431,6 +433,11 @@ static void handle_delayed_updates(char *local_name)
2857                                         "rename failed for %s (from %s)",
2858                                         full_fname(fname), partialptr);
2859                         } else {
2860 +                               if (use_db && !db_lax) {
2861 +                                       STRUCT_STAT st;
2862 +                                       if (do_lstat(fname, &st) == 0)
2863 +                                               db_update_ctime(5, &st);
2864 +                               }
2865                                 if (remove_source_files
2866                                  || (preserve_hard_links && F_IS_HLINKED(file)))
2867                                         send_msg_int(MSG_SUCCESS, ndx);
2868 @@ -537,6 +544,9 @@ int recv_files(int f_in, int f_out, char *local_name)
2869         if (delay_updates)
2870                 delayed_bits = bitbag_create(cur_flist->used + 1);
2871  
2872 +       if (use_db && (append_mode == 1 || protocol_version < 30))
2873 +               use_db = 0; /* We can't note finished md5 values */
2874 +
2875         while (1) {
2876                 cleanup_disable();
2877  
2878 @@ -865,6 +875,8 @@ int recv_files(int f_in, int f_out, char *local_name)
2879                                 do_unlink(partialptr);
2880                                 handle_partial_dir(partialptr, PDIR_DELETE);
2881                         }
2882 +                       if (use_db && do_lstat(fname, &st) == 0)
2883 +                               db_set_checksum(5, &st, sender_file_sum);
2884                 } else if (keep_partial && partialptr) {
2885                         if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
2886                                 rprintf(FERROR,
2887 @@ -878,6 +890,8 @@ int recv_files(int f_in, int f_out, char *local_name)
2888                                 recv_ok = -1;
2889                         else if (delay_updates && recv_ok) {
2890                                 bitbag_set_bit(delayed_bits, ndx);
2891 +                               if (use_db && do_lstat(partialptr, &st) == 0)
2892 +                                       db_set_checksum(5, &st, sender_file_sum);
2893                                 recv_ok = 2;
2894                         } else
2895                                 partialptr = NULL;
2896 diff --git a/rsync.c b/rsync.c
2897 --- a/rsync.c
2898 +++ b/rsync.c
2899 @@ -39,6 +39,7 @@ extern int am_daemon;
2900  extern int am_sender;
2901  extern int am_receiver;
2902  extern int am_generator;
2903 +extern int am_dbadmin;
2904  extern int am_starting_up;
2905  extern int allow_8bit_chars;
2906  extern int protocol_version;
2907 @@ -742,6 +743,8 @@ struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc)
2908  
2909  const char *who_am_i(void)
2910  {
2911 +       if (am_dbadmin)
2912 +               return "rsyncdb";
2913         if (am_starting_up)
2914                 return am_server ? "server" : "client";
2915         return am_sender ? "sender"
2916 diff --git a/rsync.h b/rsync.h
2917 --- a/rsync.h
2918 +++ b/rsync.h
2919 @@ -241,12 +241,16 @@ enum msgcode {
2920         MSG_IO_ERROR=22,/* the sending side had an I/O error */
2921         MSG_IO_TIMEOUT=33,/* tell client about a daemon's timeout value */
2922         MSG_NOOP=42,    /* a do-nothing message (legacy protocol-30 only) */
2923 +       MSG_CHECKSUM=55,/* sent via rcvr -> gen pipe and local-host-only gen -> sender */
2924         MSG_ERROR_EXIT=86, /* synchronize an error exit (siblings and protocol >= 31) */
2925         MSG_SUCCESS=100,/* successfully updated indicated flist index */
2926         MSG_DELETED=101,/* successfully deleted a file on receiving side */
2927         MSG_NO_SEND=102,/* sender failed to open a file we wanted */
2928  };
2929  
2930 +#define MSG_CHECKSUM_LONGS 5
2931 +#define MSG_CHECKSUM_LEN (MSG_CHECKSUM_LONGS*8 + 4 + MAX_DIGEST_LEN)
2932 +
2933  #define NDX_DONE -1
2934  #define NDX_FLIST_EOF -2
2935  #define NDX_DEL_STATS -3
2936 @@ -1256,7 +1260,8 @@ extern short info_levels[], debug_levels[];
2937  #define DEBUG_CHDIR (DEBUG_BIND+1)
2938  #define DEBUG_CONNECT (DEBUG_CHDIR+1)
2939  #define DEBUG_CMD (DEBUG_CONNECT+1)
2940 -#define DEBUG_DEL (DEBUG_CMD+1)
2941 +#define DEBUG_DB (DEBUG_CMD+1)
2942 +#define DEBUG_DEL (DEBUG_DB+1)
2943  #define DEBUG_DELTASUM (DEBUG_DEL+1)
2944  #define DEBUG_DUP (DEBUG_DELTASUM+1)
2945  #define DEBUG_EXIT (DEBUG_DUP+1)
2946 diff --git a/rsync.yo b/rsync.yo
2947 --- a/rsync.yo
2948 +++ b/rsync.yo
2949 @@ -340,6 +340,9 @@ to the detailed description below for a complete description.  verb(
2950   -q, --quiet                 suppress non-error messages
2951       --no-motd               suppress daemon-mode MOTD (see caveat)
2952   -c, --checksum              skip based on checksum, not mod-time & size
2953 +     --db=CONFIG_FILE        specify a CONFIG_FILE for DB checksums
2954 +     --db-only=CONFIG_FILE   Behave like rsyncdb (see that manpage).
2955 +     --db-lax                Ignore ctime changes (use with CAUTION).
2956   -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
2957       --no-OPTION             turn off an implied OPTION (e.g. --no-D)
2958   -r, --recursive             recurse into directories
2959 @@ -649,6 +652,67 @@ option's before-the-transfer "Does this file need to be updated?" check.
2960  For protocol 30 and beyond (first supported in 3.0.0), the checksum used is
2961  MD5.  For older protocols, the checksum used is MD4.
2962  
2963 +dit(bf(--db=CONFIG_FILE))  This option specifies a CONFIG_FILE to read
2964 +that holds connection details for a database of checksum information.
2965 +When combined with the bf(--checksum) (bf(-c)) option, rsync will try to
2966 +use cached checksum information from the DB, and will update it if it is
2967 +missing.
2968 +
2969 +The currently supported DB choices are MySQL and SQLite.  For example, a
2970 +MySQL configuration might look like this:
2971 +
2972 +verb(    dbtype: mysql
2973 +    dbhost: 127.0.0.1
2974 +    dbname: rsyncdb
2975 +    dbuser: rsyncuser
2976 +    dbpass: somepass
2977 +    port: 3306
2978 +    thishost: hostname )
2979 +
2980 +And a SQLite configuration might look like this:
2981 +
2982 +verb(    dbtype: SQLite
2983 +    dbname: /var/cache/rsync/sum.db
2984 +    transaction: 1)
2985 +
2986 +Both the bf(--db) and bf(--db-lax) options only affect the side where the
2987 +option is used.  To affect the remote side of a remote-shell connection,
2988 +use the bf(--remote-option) (bf(-M)) option.  For example, to specify the
2989 +same options on both sides, you could specify something like this:
2990 +
2991 +verb(    rsync -avc {-M,}--db=/etc/rsyncdb.conf src/ host:dest/ )
2992 +
2993 +For a local copy, this option affects both the source and the destination.
2994 +If you wish a local copy to enable this option just for the destination
2995 +files, specify bf(-M--db=CONFIG) (the same for bf(-M--db-lax).  If you wish
2996 +a local copy to enable this option just for the source files, combine
2997 +bf(--db=CONFIG) with bf(-M--no-db) (similarly use bf(-M--no-db-lax)).
2998 +
2999 +See the perl script "rsyncdb" in the support directory of the source code
3000 +(which may also be installed in /usr/bin) for a way to create the tables,
3001 +populate the mounted-disk information, check files against their checksums,
3002 +and update both the MD4 and MD5 checksums for files at the same time (since
3003 +an rsync copy will only update one or the other).
3004 +
3005 +You can use a single MySQL DB for all your hosts if you give each one
3006 +their own "thishost" name and setup their device-mapping data.  Or feel
3007 +free to use separate databases, separate servers, etc.  See the rsync
3008 +daemon's "db config" parameter for how to configure a daemon to use a DB
3009 +(since a client cannot control this parameter on a daemon).
3010 +
3011 +dit(bf(--db-lax)) This option can be used to modify the inode-matching
3012 +algorithm used by bf(--db) to one that ignores the ctime.  This can be very
3013 +DANGEROUS unless your files are known to ALWAYS be updated in a safe manner.
3014 +If unsure, don't use it.
3015 +
3016 +The reason you might want to use it is that the ctime (inode change time) is
3017 +changed by an added hard-link, or the file being moving around.  To use this
3018 +option safely you must be CERTAIN that either rsync w/--db is the only program
3019 +adding files into the cached hierarchies, OR that all new files will have new
3020 +modify times (never a historical mtime that might match an orphaned inode).
3021 +So, for certain applications, such as mirrors of new tar releases, this option
3022 +can save a lot of unneeded checksum re-computation due to ctime changes.
3023 +
3024  dit(bf(-a, --archive)) This is equivalent to bf(-rlptgoD). It is a quick
3025  way of saying you want recursion and want to preserve almost
3026  everything (with -H being a notable omission).
3027 diff --git a/rsyncd.conf.yo b/rsyncd.conf.yo
3028 --- a/rsyncd.conf.yo
3029 +++ b/rsyncd.conf.yo
3030 @@ -316,6 +316,22 @@ is daemon.  This setting has no effect if the "log file" setting is a
3031  non-empty string (either set in the per-modules settings, or inherited
3032  from the global settings).
3033  
3034 +dit(bf(db config)) This parameter specifies a config file to read that
3035 +holds connection details for a database of checksum information.
3036 +
3037 +The config file will be read-in prior to any chroot restrictions, but
3038 +the connection occurs from inside the chroot.  This means that you
3039 +should use a socket connection (e.g. 127.0.0.1 rather than localhost)
3040 +for a MySQL config from inside a chroot.  For SQLite, the DB file must
3041 +be placed inside the chroot (though it can be placed outside the
3042 +transfer dir if you configured an inside-chroot path).
3043 +
3044 +See the bf(--db=CONFIG_FILE) option for full details.
3045 +
3046 +dit(bf(db lax)) This parameter specifies that a "db config" setup should use
3047 +lax (no ctime) lookups.  See the rsync manpage's section -n bf(--db-lax) for
3048 +some warnings about using this setting.
3049 +
3050  dit(bf(max verbosity)) This parameter allows you to control
3051  the maximum amount of verbose information that you'll allow the daemon to
3052  generate (since the information goes into the log file). The default is 1,
3053 diff --git a/rsyncdb-mountinfo b/rsyncdb-mountinfo
3054 new file mode 100755
3055 --- /dev/null
3056 +++ b/rsyncdb-mountinfo
3057 @@ -0,0 +1,60 @@
3058 +#!/usr/bin/perl
3059 +
3060 +# This script outputs data for rsyncdb --mounts.  It must output a complete
3061 +# list of the mounts for the current host in a strict format -- 2 fields
3062 +# with a Tab between:  $MOUNT_UNIQ\t$PATH
3063 +#
3064 +# The list of mounts MUST NOT contain any entry that has the same devnum
3065 +# (st_dev) as any other entry in the list (as checked via its PATH).
3066 +#
3067 +# MOUNT_UNIQ is a unique string that identifies the mount on this host.
3068 +# This cannot be the devnum (st_dev) because that can vary depending on the
3069 +# mount order or be reused for different mounts if they are not mounted at
3070 +# the same time.  By default the value is "Mount of $devname", which should
3071 +# be adequate for situations that don't want removable media in the DB
3072 +# (though you may need to take steps to weed-out removable media from the
3073 +# list to ensure that such inodes stay out of the DB).
3074 +#
3075 +# You can override the MOUNT_UNIQ value by putting a .rsyndb_mount_uniq
3076 +# file in the root directory of any mount, at which point it is up to you
3077 +# to make sure that the value stays unique (note that all sequences of
3078 +# whitespace are transformed into a single space, and leading/trailing
3079 +# whitespace is removed).
3080 +#
3081 +# MOUNT_UNIQ may never contain a Tab but it would be legal for PATH to have
3082 +# a Tab (just really weird).  Neither may have a CR or LF in it.
3083 +#
3084 +# The maximum size for MOUNT_UNIQ is 256 characters.
3085 +#
3086 +# If this script doesn't meet your needs, feel free to edit it and choose
3087 +# some other method of finding a unique value for each mount.  If you come
3088 +# up with a good idiom that might be useful to others, please share it back
3089 +# to me.
3090 +
3091 +use strict;
3092 +use warnings;
3093 +
3094 +my $MOUNT_FILE = '/etc/mtab';
3095 +my $VALID_DEVICE_REGEX = qr{^/dev};
3096 +
3097 +my %hash;
3098 +
3099 +open MOUNTS, $MOUNT_FILE or die "Unable to open $MOUNT_FILE: $!\n";
3100 +while (<MOUNTS>) {
3101 +    my ($devname, $path) = (split)[0,1];
3102 +    next unless $devname =~ /$VALID_DEVICE_REGEX/;
3103 +
3104 +    my ($devno) = (stat($path))[0];
3105 +    next unless defined $devno; # Skip if mount is invalid.
3106 +    next if $hash{$devno}++; # SKip if we've seen this devno earlier.
3107 +
3108 +    my $mount_uniq = "Mount of $devname";
3109 +    if (open UNIQ, '<', "$path/.rsyndb_mount_uniq") {
3110 +       $mount_uniq = <UNIQ>;
3111 +       close UNIQ;
3112 +       $mount_uniq =~ s/\s+/ /g; # This ensures no tab, CR, nor LF.
3113 +       $mount_uniq =~ s/^ | $//g; # .. and no leading or trailing whitespace.
3114 +    }
3115 +    print $mount_uniq, "\t", $path, "\n";
3116 +}
3117 +close MOUNTS;
3118 diff --git a/rsyncdb.yo b/rsyncdb.yo
3119 new file mode 100644
3120 --- /dev/null
3121 +++ b/rsyncdb.yo
3122 @@ -0,0 +1,186 @@
3123 +mailto(rsync-bugs@samba.org)
3124 +manpage(rsync)(1)(23 Jun 2013)()()
3125 +manpagename(rsyncdb)(Maintain an rsync checksum DB)
3126 +manpagesynopsis()
3127 +
3128 +verb(rsyncdb --db=CONFIG [OPTION...] [DIR...])
3129 +
3130 +manpagedescription()
3131 +
3132 +Rsyncdb can maintain a checksum-caching DB that rsync can use to make its
3133 +bf(--checksum) option more optimal.  You must specify a config file via
3134 +the bf(--db=CONFIG_FILE) option in order for rsyncdb to know what DB to
3135 +manipulate.  See the rsync manpage's bf(--db) option for full details on
3136 +the file's format.
3137 +
3138 +You can specify one or more directory args for rsyncdb to scan.  If no
3139 +DIR args are specified, the current directory is assumed to be the spot
3140 +to start scanning.
3141 +
3142 +Note that the rsyncdb program is usually just a symlink to the rsync program.
3143 +You can force rsync to behave as rsyncdb either by having a symlink (or
3144 +hardlink) name that ends with "db" or by bf(starting) the rsync args with
3145 +bf(--db-only=CONFIG) (and that option works just like bf(--db=CONFIG) to
3146 +a program named rsyncdb).
3147 +
3148 +manpagesection(EXAMPLES)
3149 +
3150 +The following command will update checksum information in the database
3151 +described in the /etc/db.conf file:
3152 +
3153 +verb(    rsyncdb --db=/etc/db.conf -o n --clean /dir1 /dir2)
3154 +
3155 +It scans 2 directory hierarchies (/dir1 & /dir2) and cleans out any
3156 +checksums whose inodes are no longer found in those directories (so that
3157 +directory args are presumed to be complete for this host's DB contents).
3158 +
3159 +The following command will scan all the files in the /dir2 directory (without
3160 +recursive scanning, due to the bf(--no-r) option) and check them against
3161 +the DB:
3162 +
3163 +verb(    rsyncdb --db=/etc/db.conf --check --no-r /dir2)
3164 +
3165 +Any errors found are output as well as being fixed in the DB.  (See
3166 +bf(--no-update) for how to check without updating.)
3167 +
3168 +The following command will output MD5 sums for all the files found in the
3169 +directories mentioned, even if they are unchanged (due to the
3170 +bf(--output=u) option):
3171 +
3172 +verb(    rsyncdb --db=/etc/db.conf -rous /dir* >/tmp/md5sums.txt)
3173 +
3174 +This is just like running md5sum, only faster.  Unlike md5sum, you can't
3175 +specify a single file, so use bf(--no-r) and grep the output if you just
3176 +want to see a single file's value.
3177 +
3178 +The following command initializes a new DB, and is required for any new DB:
3179 +
3180 +verb(    rsyncdb --db=/etc/db.conf --init --mounts)
3181 +
3182 +The bf(--init) option should only be used once (unless you want to
3183 +destroy existing data).  The bf(--mounts) option may need to be used
3184 +periodically.
3185 +
3186 +manpagesection(OPTIONS SUMMARY)
3187 +
3188 +Rsyncdb accepts the following options: verb(
3189 +     --db=CONFIG     Specify the CONFIG file to read for the DB info.
3190 +     --db-lax        Ignore ctime changes (use with CAUTION).
3191 +     --no-recursive  Avoid the default --recursive (-r) scanning behavior.
3192 + -s, --sums=SUMS     List which checksums to update (default: md5).
3193 + -o, --output=STR    One or more letters of what to output (default is nothing).
3194 + -c, --check         Check the checksums (by reading the files) and fix any
3195 +                     issues.  Enables --output=i.
3196 +     --clean         Note all inodes in the DIRS and remove DB extras.
3197 + -N, --no-update     Avoids updating/adding info with --check and/or --clean.
3198 +     --init          Initialize a DB by (re-)creating its tables.
3199 +     --mounts        Scan for mounted filesystems and update the DB.
3200 + -q, --quiet         Disables the default non-error output.
3201 + -h, --help          Display this help message.)
3202 +
3203 +manpageoptions()
3204 +
3205 +Rsyncdb accepts both long (double-dash + word) and short (single-dash + letter)
3206 +options.  The full list of the available options are described below.  If an
3207 +option can be specified in more than one way, the choices are comma-separated.
3208 +Some options only have a long variant, not a short.  If the option takes a
3209 +parameter, the parameter is only listed after the long variant, even though it
3210 +must also be specified for the short.  When specifying a parameter, you can
3211 +either use the form --option=param or replace the '=' with whitespace.  The
3212 +parameter may need to be quoted in some manner for it to survive the shell's
3213 +command-line parsing.
3214 +
3215 +startdit()
3216 +dit(bf(--db=CONFIG_FILE)) This tells rsyncdb what DB-config file to read
3217 +for the DB setup.  This is the same as the option in rsync, so refer to
3218 +that manpage for full details.
3219 +
3220 +dit(bf(--db-lax)) This option works just like it does in rsync, so refer to
3221 +that manpage for full details.
3222 +
3223 +dit(bf(--no-recursive, --no-r)) This disables the default recursive
3224 +directory scan that is performed on the listed directory args.  The
3225 +options bf(--recursive) and bf(-r) are also accepted, if someone wants
3226 +to override an earlier bf(--no-r) override.
3227 +
3228 +dit(bf(--sums=SUMS, -s)) Only output/update the listed checksum types. By
3229 +default we deal with just the newer md5 checksums (i.e.  bf(--sums=5)).
3230 +
3231 +Note that this option does NOT affect the order that checksums are output
3232 +if "-o s" is enabled, so bf(-s5,4) is the same as bf(-s4,5).
3233 +
3234 +dit(bf(--output=STR, -o)) The output option lets you specify one or more
3235 +letters indicating what information should be output.  The default is to
3236 +output "d" and "n" if bf(--output) is not specified.
3237 +
3238 +The following letters are accepted:
3239 +
3240 +quote(itemization(
3241 +  it() bf(d) outputs "... dir_name ..." lines for each directory in our scan.
3242 +  it() bf(n) outputs the names of files with changes (implied by all but "d").
3243 +  it() bf(s) outputs checksum info for changes (implies bf(n)).
3244 +  it() bf(u) outputs unchanged files too (implies bf(n)).
3245 +  it() bf(i) outputs prefixed change info.  The output strings are:
3246 +  quote(itemization(
3247 +    it() bf(!i) indicates that the time and/or size is wrong.
3248 +    it() bf(+4) indicates the MD4 sum is missing.
3249 +    it() bf(+5) indicates the MD5 sum is missing.
3250 +    it() bf(!4) indicates the MD4 sum is wrong.
3251 +    it() bf(!5) indicates the MD5 sum is wrong.
3252 +    it() bf(?4) indicates an unknown MD4 difference.  This can happen if we
3253 +    didn't need to read the file; i.e. if the time/size is wrong and no sum
3254 +    info was requested.
3255 +    it() bf(?5) indicates an unknown MD5 difference.
3256 +  ))
3257 +))
3258 +
3259 +dit(bf(--check, -c)) Check the checksums (forcing the reading of all the
3260 +files) and fix any issues that are found.  Forces bf(--output=ni) on.
3261 +
3262 +dit(bf(--clean)) Makes a temp-DB of all the inodes that we find in all the
3263 +listed directories and removes any extraneous checksums from the DB.  You
3264 +will need to specify all the mounted directories that are present (and
3265 +listed as mounted) in the DB on this host or else the checksums from the
3266 +unvisited directories will be discarded from the DB.  If you want to just
3267 +--clean without adding or updating the info of new or changed files,
3268 +specify bf(--no-update) as well.
3269 +
3270 +See the bf(--mount)
3271 +
3272 +dit(bf(--no-update, -N)) Avoids updating/adding info with bf(--check)
3273 +and/or bf(--clean).
3274 +
3275 +dit(bf(--quiet, -q)) Disable the default (non-error) output settings.  This
3276 +turns off the messages that bf(--init), bf(--mount), and bf(--clean) output,
3277 +and makes the default for bf(--output) be nothing (though an explicit
3278 +bf(--output) option is not affected).
3279 +
3280 +dit(bf(--init)) Create the tables in the DB.  If it is used on an existing
3281 +DB, all the existing tables are dropped and re-created.
3282 +
3283 +This option disables scanning for checksum information, but may be combined
3284 +with bf(--mounts).
3285 +
3286 +dit(bf(--mounts)) Populate the "disk" DB with the available device numbers
3287 +and change any mounted/unmount information for devices.  This should be run
3288 +every time a mount-change happens that may affect a directory hierarchy in
3289 +the DB.  Rsyncdb will not save any checksums for a device that is not
3290 +listed in the "disk" table.
3291 +
3292 +Some advanced users may want to maintain the disk table themselves in order
3293 +to support mounting a drive in different (or multiple) locations, etc.
3294 +
3295 +This option disables scanning for checksum information, but may be combined
3296 +with bf(--init).
3297 +
3298 +dit(bf(--help, -h)) Display a summary of the options.
3299 +
3300 +enddit()
3301 +
3302 +manpageseealso()
3303 +
3304 +bf(rsync)(1)
3305 +
3306 +manpageauthor()
3307 +
3308 +Rsyncdb was written by Wayne Davison.