blob: 77ac19c6fc692ac0c451e5f367c89e818de836bd [file] [log] [blame]
Martin Poola0397492000-10-26 07:24:18 +00001/* -*- c-file-style: "linux"; -*-
Wayne Davison4a7319b2003-12-15 01:45:01 +00002
Martin Pool0c5a7922001-03-19 08:19:11 +00003 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
Martin Pool18c71e92001-02-23 01:45:46 +00004 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
Wayne Davison4a7319b2003-12-15 01:45:01 +00005
Andrew Tridgell0b76cd61998-05-09 14:00:55 +00006 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
Wayne Davison4a7319b2003-12-15 01:45:01 +000010
Andrew Tridgell0b76cd61998-05-09 14:00:55 +000011 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
Wayne Davison4a7319b2003-12-15 01:45:01 +000015
Andrew Tridgell0b76cd61998-05-09 14:00:55 +000016 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
Martin Pool3e3dcd62001-02-21 07:18:50 +000022 Logging and utility functions.
Martin Pool0c5a7922001-03-19 08:19:11 +000023 tridge, May 1998
Andrew Tridgell0b76cd61998-05-09 14:00:55 +000024
Martin Pool3e3dcd62001-02-21 07:18:50 +000025 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
Andrew Tridgell0b76cd61998-05-09 14:00:55 +000027 */
28#include "rsync.h"
29
Wayne Davison41b5b5e2005-02-26 04:49:30 +000030extern int verbose;
Wayne Davison088aff12005-02-19 17:23:08 +000031extern int dry_run;
Wayne Davison548abf92004-01-13 04:50:45 +000032extern int am_daemon;
33extern int am_server;
34extern int am_sender;
Wayne Davisonc1759b92005-03-30 16:35:01 +000035extern int local_server;
Wayne Davison548abf92004-01-13 04:50:45 +000036extern int quiet;
37extern int module_id;
Wayne Davisona644fc32004-01-15 07:42:19 +000038extern int msg_fd_out;
Wayne Davison19bc8262005-02-19 02:39:26 +000039extern int protocol_version;
Wayne Davisonef74f5d2005-02-16 08:10:28 +000040extern int preserve_times;
Wayne Davisond6707172005-02-19 03:54:13 +000041extern int log_format_has_o_or_i;
Wayne Davison1eec0032005-02-19 22:16:41 +000042extern int daemon_log_format_has_o_or_i;
Wayne Davison548abf92004-01-13 04:50:45 +000043extern char *auth_user;
44extern char *log_format;
45
David Dykstra30e8c8e2002-08-30 23:27:26 +000046static int log_initialised;
Wayne Davison64c37822005-04-14 16:08:10 +000047static int logfile_was_closed;
David Dykstra45a83542000-10-24 18:50:08 +000048static char *logfname;
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000049static FILE *logfile;
Martin Poolb35d0d82002-04-08 04:10:20 +000050struct stats stats;
Andrew Tridgelle0414f41998-09-09 06:06:08 +000051
Wayne Davison8fcdc442004-05-15 19:31:16 +000052int log_got_error = 0;
Martin Poolaf642a62000-11-10 03:28:15 +000053
54struct {
55 int code;
56 char const *name;
57} const rerr_names[] = {
Wayne Davison4a7319b2003-12-15 01:45:01 +000058 { RERR_SYNTAX , "syntax or usage error" },
59 { RERR_PROTOCOL , "protocol incompatibility" },
60 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
61 { RERR_UNSUPPORTED, "requested action not supported" },
62 { RERR_STARTCLIENT, "error starting client-server protocol" },
63 { RERR_SOCKETIO , "error in socket IO" },
64 { RERR_FILEIO , "error in file IO" },
65 { RERR_STREAMIO , "error in rsync protocol data stream" },
66 { RERR_MESSAGEIO , "errors with program diagnostics" },
67 { RERR_IPC , "error in IPC code" },
68 { RERR_SIGNAL , "received SIGUSR1 or SIGINT" },
69 { RERR_WAITCHILD , "some error returned by waitpid()" },
70 { RERR_MALLOC , "error allocating core memory buffers" },
71 { RERR_PARTIAL , "some files could not be transferred" },
Wayne Davison584ba4e2004-09-05 21:30:00 +000072 { RERR_VANISHED , "some files vanished before they could be transferred" },
Wayne Davison4a7319b2003-12-15 01:45:01 +000073 { RERR_TIMEOUT , "timeout in data send/receive" },
Andrew Tridgell19b27a42001-03-23 01:26:04 +000074 { RERR_CMD_FAILED , "remote shell failed" },
75 { RERR_CMD_KILLED , "remote shell killed" },
Wayne Davison24cecf12005-03-05 18:58:35 +000076 { RERR_CMD_RUN , "remote command could not be run" },
77 { RERR_CMD_NOTFOUND,"remote command not found" },
Wayne Davison26718402005-03-15 23:23:41 +000078 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
Wayne Davison4a7319b2003-12-15 01:45:01 +000079 { 0, NULL }
Martin Poolaf642a62000-11-10 03:28:15 +000080};
81
82
83
84/*
85 * Map from rsync error code to name, or return NULL.
86 */
87static char const *rerr_name(int code)
88{
Wayne Davison4a7319b2003-12-15 01:45:01 +000089 int i;
90 for (i = 0; rerr_names[i].name; i++) {
91 if (rerr_names[i].code == code)
92 return rerr_names[i].name;
93 }
94 return NULL;
Martin Poolaf642a62000-11-10 03:28:15 +000095}
96
Wayne Davison2267efe2005-02-18 17:33:43 +000097
Andrew Tridgell4f6325c1998-09-09 05:51:42 +000098static void logit(int priority, char *buf)
99{
Wayne Davison64c37822005-04-14 16:08:10 +0000100 if (logfile_was_closed)
101 logfile_reopen();
102 if (logfile) {
Wayne Davison4a7319b2003-12-15 01:45:01 +0000103 fprintf(logfile,"%s [%d] %s",
Andrew Tridgellf7632fc1998-11-02 00:52:01 +0000104 timestring(time(NULL)), (int)getpid(), buf);
Andrew Tridgell4f6325c1998-09-09 05:51:42 +0000105 fflush(logfile);
106 } else {
107 syslog(priority, "%s", buf);
108 }
109}
Andrew Tridgelle42c9451998-05-15 09:26:01 +0000110
Wayne Davison6afb9072005-04-12 23:03:49 +0000111static void syslog_init()
112{
113 static int been_here = 0;
114 int options = LOG_PID;
115
116 if (been_here)
117 return;
118 been_here = 1;
119
120#ifdef LOG_NDELAY
121 options |= LOG_NDELAY;
122#endif
123
124#ifdef LOG_DAEMON
125 openlog("rsyncd", options, lp_syslog_facility());
126#else
127 openlog("rsyncd", options);
128#endif
129
130#ifndef LOG_NDELAY
131 logit(LOG_INFO, "rsyncd started\n");
132#endif
133}
134
Wayne Davison64c37822005-04-14 16:08:10 +0000135static void logfile_open(void)
136{
137 extern int orig_umask;
138 int old_umask = umask(022 | orig_umask);
139 logfile = fopen(logfname, "a");
140 umask(old_umask);
141 if (!logfile) {
142 int fopen_errno = errno;
143 /* Rsync falls back to using syslog on failure. */
144 syslog_init();
145 rsyserr(FERROR, fopen_errno,
146 "failed to open log-file %s", logfname);
147 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
148 }
149}
150
David Dykstra45a83542000-10-24 18:50:08 +0000151void log_init(void)
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000152{
Andrew Tridgellbcf5b131998-07-02 10:57:20 +0000153 time_t t;
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000154
Wayne Davison0bb4d172004-06-07 22:51:14 +0000155 if (log_initialised)
156 return;
David Dykstra30e8c8e2002-08-30 23:27:26 +0000157 log_initialised = 1;
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000158
Andrew Tridgell958f3731998-10-30 11:18:38 +0000159 /* this looks pointless, but it is needed in order for the
Wayne Davison4a7319b2003-12-15 01:45:01 +0000160 * C library on some systems to fetch the timezone info
161 * before the chroot */
Andrew Tridgell958f3731998-10-30 11:18:38 +0000162 t = time(NULL);
163 localtime(&t);
164
165 /* optionally use a log file instead of syslog */
David Dykstra45a83542000-10-24 18:50:08 +0000166 logfname = lp_log_file();
Wayne Davison64c37822005-04-14 16:08:10 +0000167 if (logfname && *logfname)
168 logfile_open();
169 else
170 syslog_init();
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000171}
Andrew Tridgell1a016bf1998-05-15 10:34:07 +0000172
Wayne Davison64c37822005-04-14 16:08:10 +0000173void logfile_close(void)
Wayne Davison4a239e92005-02-21 00:38:15 +0000174{
175 if (logfile) {
Wayne Davison64c37822005-04-14 16:08:10 +0000176 logfile_was_closed = 1;
Wayne Davison4a239e92005-02-21 00:38:15 +0000177 fclose(logfile);
178 logfile = NULL;
179 }
180}
181
Wayne Davison64c37822005-04-14 16:08:10 +0000182void logfile_reopen(void)
183{
184 if (logfile_was_closed) {
185 logfile_was_closed = 0;
186 logfile_open();
187 }
188}
189
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000190/* this is the underlying (unformatted) rsync debugging function. Call
Wayne Davison4a7319b2003-12-15 01:45:01 +0000191 * it with FINFO, FERROR or FLOG */
Andrew Tridgellff41a592000-01-28 15:29:59 +0000192void rwrite(enum logcode code, char *buf, int len)
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000193{
Wayne Davison8fcdc442004-05-15 19:31:16 +0000194 FILE *f = NULL;
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000195 /* recursion can happen with certain fatal conditions */
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000196
Wayne Davisona644fc32004-01-15 07:42:19 +0000197 if (quiet && code == FINFO)
198 return;
David Dykstrab86f0ce1999-02-09 17:25:35 +0000199
Wayne Davisona644fc32004-01-15 07:42:19 +0000200 if (len < 0)
201 exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000202
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000203 buf[len] = 0;
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000204
Wayne Davison0bb4d172004-06-07 22:51:14 +0000205 if (am_server && msg_fd_out >= 0) {
206 /* Pass the message to our sibling. */
207 send_msg((enum msgcode)code, buf, len);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000208 return;
209 }
210
Wayne Davison1eec0032005-02-19 22:16:41 +0000211 if (code == FCLIENT)
212 code = FINFO;
213 else if (am_daemon) {
Wayne Davison0bb4d172004-06-07 22:51:14 +0000214 static int in_block;
215 char msg[2048];
216 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
217
218 if (in_block)
219 return;
220 in_block = 1;
221 if (!log_initialised)
222 log_init();
223 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
224 logit(priority, msg);
225 in_block = 0;
226
227 if (code == FLOG || !am_server)
228 return;
229 } else if (code == FLOG)
230 return;
231
Wayne Davisona644fc32004-01-15 07:42:19 +0000232 if (am_server) {
Wayne Davison0bb4d172004-06-07 22:51:14 +0000233 /* Pass the message to the non-server side. */
234 if (io_multiplex_write((enum msgcode)code, buf, len))
235 return;
236 if (am_daemon) {
237 /* TODO: can we send the error to the user somehow? */
Wayne Davisona644fc32004-01-15 07:42:19 +0000238 return;
239 }
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000240 }
241
Andrew Tridgellff41a592000-01-28 15:29:59 +0000242 if (code == FERROR) {
Andrew Tridgellff81e802001-03-22 07:36:51 +0000243 log_got_error = 1;
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000244 f = stderr;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000245 }
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000246
Wayne Davison0bb4d172004-06-07 22:51:14 +0000247 if (code == FINFO)
248 f = am_server ? stderr : stdout;
Andrew Tridgellff8b29b1998-05-10 08:24:38 +0000249
Wayne Davison0bb4d172004-06-07 22:51:14 +0000250 if (!f)
251 exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000252
Wayne Davison0bb4d172004-06-07 22:51:14 +0000253 if (fwrite(buf, len, 1, f) != 1)
254 exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell8d9dc9f1998-05-18 08:20:26 +0000255
Wayne Davison0bb4d172004-06-07 22:51:14 +0000256 if (buf[len-1] == '\r' || buf[len-1] == '\n')
257 fflush(f);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000258}
Wayne Davison0455cd92005-03-31 03:01:26 +0000259
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000260
Martin Poola0397492000-10-26 07:24:18 +0000261/* This is the rsync debugging function. Call it with FINFO, FERROR or
262 * FLOG. */
263void rprintf(enum logcode code, const char *format, ...)
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000264{
Wayne Davison4a7319b2003-12-15 01:45:01 +0000265 va_list ap;
Wayne Davison8fcdc442004-05-15 19:31:16 +0000266 char buf[MAXPATHLEN+512];
267 size_t len;
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000268
269 va_start(ap, format);
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000270 len = vsnprintf(buf, sizeof buf, format, ap);
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000271 va_end(ap);
272
Martin Poolb2f02462001-09-04 03:12:55 +0000273 /* Deal with buffer overruns. Instead of panicking, just
Wayne Davison8fcdc442004-05-15 19:31:16 +0000274 * truncate the resulting string. (Note that configure ensures
275 * that we have a vsnprintf() that doesn't ever return -1.) */
276 if (len > sizeof buf - 1) {
Martin Poolb2f02462001-09-04 03:12:55 +0000277 const char ellipsis[] = "[...]";
278
279 /* Reset length, and zero-terminate the end of our buffer */
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000280 len = sizeof buf - 1;
Martin Poolb2f02462001-09-04 03:12:55 +0000281 buf[len] = '\0';
282
283 /* Copy the ellipsis to the end of the string, but give
284 * us one extra character:
285 *
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000286 * v--- null byte at buf[sizeof buf - 1]
Martin Poolb2f02462001-09-04 03:12:55 +0000287 * abcdefghij0
288 * -> abcd[...]00 <-- now two null bytes at end
289 *
290 * If the input format string has a trailing newline,
291 * we copy it into that extra null; if it doesn't, well,
292 * all we lose is one byte. */
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000293 strncpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
Martin Poolb2f02462001-09-04 03:12:55 +0000294 if (format[strlen(format)-1] == '\n') {
295 buf[len-1] = '\n';
296 }
297 }
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000298
Andrew Tridgellff41a592000-01-28 15:29:59 +0000299 rwrite(code, buf, len);
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000300}
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000301
Martin Poola0397492000-10-26 07:24:18 +0000302
303/* This is like rprintf, but it also tries to print some
304 * representation of the error code. Normally errcode = errno.
305 *
306 * Unlike rprintf, this always adds a newline and there should not be
307 * one in the format string.
308 *
309 * Note that since strerror might involve dynamically loading a
310 * message catalog we need to call it once before chroot-ing. */
311void rsyserr(enum logcode code, int errcode, const char *format, ...)
312{
Wayne Davison4a7319b2003-12-15 01:45:01 +0000313 va_list ap;
Wayne Davison8fcdc442004-05-15 19:31:16 +0000314 char buf[MAXPATHLEN+512];
315 size_t len;
316
317 strcpy(buf, RSYNC_NAME ": ");
318 len = (sizeof RSYNC_NAME ": ") - 1;
Martin Poola0397492000-10-26 07:24:18 +0000319
320 va_start(ap, format);
Wayne Davison8fcdc442004-05-15 19:31:16 +0000321 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
Martin Poola0397492000-10-26 07:24:18 +0000322 va_end(ap);
323
Wayne Davison8fcdc442004-05-15 19:31:16 +0000324 if (len < sizeof buf) {
325 len += snprintf(buf + len, sizeof buf - len,
326 ": %s (%d)\n", strerror(errcode), errcode);
327 }
328 if (len >= sizeof buf)
Martin Pool59ee7432002-01-23 08:04:54 +0000329 exit_cleanup(RERR_MESSAGEIO);
Martin Poola0397492000-10-26 07:24:18 +0000330
Martin Poola0397492000-10-26 07:24:18 +0000331 rwrite(code, buf, len);
332}
333
334
335
Andrew Tridgellff41a592000-01-28 15:29:59 +0000336void rflush(enum logcode code)
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000337{
338 FILE *f = NULL;
Wayne Davison0455cd92005-03-31 03:01:26 +0000339
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000340 if (am_daemon) {
341 return;
342 }
343
Andrew Tridgellff41a592000-01-28 15:29:59 +0000344 if (code == FLOG) {
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000345 return;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000346 }
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000347
Andrew Tridgellff41a592000-01-28 15:29:59 +0000348 if (code == FERROR) {
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000349 f = stderr;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000350 }
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000351
Andrew Tridgellff41a592000-01-28 15:29:59 +0000352 if (code == FINFO) {
Wayne Davison4a7319b2003-12-15 01:45:01 +0000353 if (am_server)
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000354 f = stderr;
355 else
356 f = stdout;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000357 }
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000358
Andrew Tridgell65417571998-11-03 07:08:27 +0000359 if (!f) exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgell0b76cd61998-05-09 14:00:55 +0000360 fflush(f);
361}
362
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000363
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000364
365/* a generic logging routine for send/recv, with parameter
Wayne Davison4a7319b2003-12-15 01:45:01 +0000366 * substitiution */
Wayne Davisonafc65a52005-03-04 16:07:50 +0000367static void log_formatted(enum logcode code, char *format, char *op,
368 struct file_struct *file, struct stats *initial_stats,
369 int iflags, char *hlink)
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000370{
Wayne Davisonddd74b62005-03-30 18:27:09 +0000371 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
372 char *p, *s, *n;
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000373 size_t len, total;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000374 int64 b;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000375
Wayne Davisonddd74b62005-03-30 18:27:09 +0000376 *fmt = '%';
377
Martin Poolaa126972002-01-24 02:41:38 +0000378 /* We expand % codes one by one in place in buf. We don't
Wayne Davison126e7af2005-03-31 00:50:58 +0000379 * copy in the terminating null of the inserted strings, but
380 * rather keep going until we reach the null of the format. */
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000381 total = strlcpy(buf, format, sizeof buf);
Wayne Davisond9c00512005-03-31 01:09:18 +0000382 if (total > MAXPATHLEN) {
383 rprintf(FERROR, "log-format string is WAY too long!\n");
384 exit_cleanup(RERR_MESSAGEIO);
385 }
386 buf[total++] = '\n';
387 buf[total] = '\0';
Wayne Davison0455cd92005-03-31 03:01:26 +0000388
Wayne Davison16f960f2005-03-31 00:19:13 +0000389 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
Wayne Davisonddd74b62005-03-30 18:27:09 +0000390 s = p++;
391 n = fmt + 1;
Wayne Davison9baed762005-03-30 19:44:29 +0000392 if (*p == '-')
393 *n++ = *p++;
Wayne Davisonb4bf2b52005-03-30 20:18:48 +0000394 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
Wayne Davisonddd74b62005-03-30 18:27:09 +0000395 *n++ = *p++;
Wayne Davisone145d512005-03-30 23:39:00 +0000396 if (!*p)
397 break;
Wayne Davisonddd74b62005-03-30 18:27:09 +0000398 *n = '\0';
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000399 n = NULL;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000400
Wayne Davison0455cd92005-03-31 03:01:26 +0000401 switch (*p) {
Andrew Tridgellaf77cc61998-11-16 23:50:28 +0000402 case 'h': if (am_daemon) n = client_name(0); break;
403 case 'a': if (am_daemon) n = client_addr(0); break;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000404 case 'l':
Wayne Davisonddd74b62005-03-30 18:27:09 +0000405 strlcat(fmt, ".0f", sizeof fmt);
406 snprintf(buf2, sizeof buf2, fmt,
Wayne Davison4a7319b2003-12-15 01:45:01 +0000407 (double)file->length);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000408 n = buf2;
409 break;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000410 case 'p':
Wayne Davison126e7af2005-03-31 00:50:58 +0000411 strlcat(fmt, "ld", sizeof fmt);
Wayne Davisonddd74b62005-03-30 18:27:09 +0000412 snprintf(buf2, sizeof buf2, fmt,
Wayne Davison126e7af2005-03-31 00:50:58 +0000413 (long)getpid());
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000414 n = buf2;
415 break;
416 case 'o': n = op; break;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000417 case 'f':
Wayne Davison9baed762005-03-30 19:44:29 +0000418 n = safe_fname(f_name(file));
419 if (am_sender && file->dir.root) {
420 pathjoin(buf2, sizeof buf2,
421 file->dir.root, n);
422 /* The buffer from safe_fname() has more
423 * room than MAXPATHLEN, so this is safe. */
Wayne Davisondcbae652005-03-30 20:41:11 +0000424 if (fmt[1])
425 strcpy(n, buf2);
426 else
427 n = buf2;
Wayne Davison9baed762005-03-30 19:44:29 +0000428 }
429 clean_fname(n, 0);
430 if (*n == '/')
431 n++;
Andrew Tridgellab7104d1998-11-02 06:40:11 +0000432 break;
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000433 case 'n':
Wayne Davison9baed762005-03-30 19:44:29 +0000434 n = safe_fname(f_name(file));
Wayne Davisone66d70e2005-02-18 20:17:17 +0000435 if (S_ISDIR(file->mode)) {
436 /* The buffer from safe_fname() has more
437 * room than MAXPATHLEN, so this is safe. */
438 strcat(n, "/");
439 }
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000440 break;
441 case 'L':
Wayne Davisonafc65a52005-03-04 16:07:50 +0000442 if (hlink && *hlink) {
Wayne Davison126e7af2005-03-31 00:50:58 +0000443 n = safe_fname(hlink);
444 strcpy(buf2, " => ");
Wayne Davisonafc65a52005-03-04 16:07:50 +0000445 } else if (S_ISLNK(file->mode) && file->u.link) {
Wayne Davison126e7af2005-03-31 00:50:58 +0000446 n = safe_fname(file->u.link);
447 strcpy(buf2, " -> ");
448 } else {
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000449 n = "";
Wayne Davison126e7af2005-03-31 00:50:58 +0000450 if (!fmt[1])
451 break;
452 strcpy(buf2, " ");
453 }
454 strlcat(fmt, "s", sizeof fmt);
455 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
456 n = buf2;
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000457 break;
Andrew Tridgell97cb8dc1998-10-30 05:53:05 +0000458 case 'm': n = lp_name(module_id); break;
Andrew Tridgellb6062651998-11-03 03:48:47 +0000459 case 't': n = timestring(time(NULL)); break;
Andrew Tridgell97cb8dc1998-10-30 05:53:05 +0000460 case 'P': n = lp_path(module_id); break;
461 case 'u': n = auth_user; break;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000462 case 'b':
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000463 if (am_sender) {
Wayne Davison4a7319b2003-12-15 01:45:01 +0000464 b = stats.total_written -
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000465 initial_stats->total_written;
466 } else {
Wayne Davison4a7319b2003-12-15 01:45:01 +0000467 b = stats.total_read -
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000468 initial_stats->total_read;
469 }
Wayne Davison0455cd92005-03-31 03:01:26 +0000470 strlcat(fmt, ".0f", sizeof fmt);
Wayne Davisonddd74b62005-03-30 18:27:09 +0000471 snprintf(buf2, sizeof buf2, fmt, (double)b);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000472 n = buf2;
473 break;
Wayne Davison4a7319b2003-12-15 01:45:01 +0000474 case 'c':
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000475 if (!am_sender) {
Wayne Davison4a7319b2003-12-15 01:45:01 +0000476 b = stats.total_written -
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000477 initial_stats->total_written;
478 } else {
Wayne Davison4a7319b2003-12-15 01:45:01 +0000479 b = stats.total_read -
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000480 initial_stats->total_read;
481 }
Wayne Davison0455cd92005-03-31 03:01:26 +0000482 strlcat(fmt, ".0f", sizeof fmt);
Wayne Davisonddd74b62005-03-30 18:27:09 +0000483 snprintf(buf2, sizeof buf2, fmt, (double)b);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000484 n = buf2;
485 break;
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000486 case 'i':
Wayne Davison19bc8262005-02-19 02:39:26 +0000487 if (iflags & ITEM_DELETED) {
Wayne Davisond5609e92005-03-13 05:35:49 +0000488 n = "*deleting";
Wayne Davison19bc8262005-02-19 02:39:26 +0000489 break;
490 }
Wayne Davisonb4bf2b52005-03-30 20:18:48 +0000491 n = buf2 + MAXPATHLEN - 32;
Wayne Davisonfd846732005-03-09 18:53:58 +0000492 n[0] = iflags & ITEM_LOCAL_CHANGE
493 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
Wayne Davisonca62acc2005-03-05 00:21:50 +0000494 : !(iflags & ITEM_TRANSFER) ? '.'
Wayne Davisonc1759b92005-03-30 16:35:01 +0000495 : !local_server && *op == 's' ? '<' : '>';
Wayne Davison19bc8262005-02-19 02:39:26 +0000496 n[1] = S_ISDIR(file->mode) ? 'd'
497 : IS_DEVICE(file->mode) ? 'D'
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000498 : S_ISLNK(file->mode) ? 'L' : 'f';
Wayne Davisond4e01962005-02-17 09:16:23 +0000499 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
500 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
501 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000502 : !preserve_times || IS_DEVICE(file->mode)
503 || S_ISLNK(file->mode) ? 'T' : 't';
Wayne Davisond4e01962005-02-17 09:16:23 +0000504 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
505 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
506 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
Wayne Davisonca62acc2005-03-05 00:21:50 +0000507 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
Wayne Davisonafc65a52005-03-04 16:07:50 +0000508 n[9] = '\0';
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000509
510 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
511 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
512 int i;
513 for (i = 2; n[i]; i++)
514 n[i] = ch;
Wayne Davisonca62acc2005-03-05 00:21:50 +0000515 } else if (!(iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE))) {
Wayne Davison19bc8262005-02-19 02:39:26 +0000516 int i;
517 for (i = 2; n[i]; i++) {
518 if (n[i] != '.')
519 break;
520 }
521 if (!n[i]) {
522 for (i = 2; n[i]; i++)
523 n[i] = ' ';
Wayne Davison19bc8262005-02-19 02:39:26 +0000524 }
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000525 }
526 break;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000527 }
528
Wayne Davisonddd74b62005-03-30 18:27:09 +0000529 /* "n" is the string to be inserted in place of this % code. */
530 if (!n)
531 continue;
Wayne Davison9baed762005-03-30 19:44:29 +0000532 if (n != buf2 && fmt[1]) {
533 strlcat(fmt, "s", sizeof fmt);
534 snprintf(buf2, sizeof buf2, fmt, n);
535 n = buf2;
536 }
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000537 len = strlen(n);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000538
Wayne Davisone145d512005-03-30 23:39:00 +0000539 /* Subtract the length of the escape from the string's size. */
Wayne Davison0455cd92005-03-31 03:01:26 +0000540 total -= p - s + 1;
Wayne Davisone145d512005-03-30 23:39:00 +0000541
Wayne Davisond9c00512005-03-31 01:09:18 +0000542 if (len + total >= (size_t)sizeof buf) {
Wayne Davison1e7098b2005-02-15 03:38:04 +0000543 rprintf(FERROR,
544 "buffer overflow expanding %%%c -- exiting\n",
Wayne Davison0455cd92005-03-31 03:01:26 +0000545 p[0]);
Andrew Tridgell65417571998-11-03 07:08:27 +0000546 exit_cleanup(RERR_MESSAGEIO);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000547 }
548
Martin Poolaa126972002-01-24 02:41:38 +0000549 /* Shuffle the rest of the string along to make space for n */
Wayne Davison0455cd92005-03-31 03:01:26 +0000550 if (len != (size_t)(p - s + 1))
551 memmove(s + len, p + 1, total - (s - buf) + 1);
Wayne Davisonddd74b62005-03-30 18:27:09 +0000552 total += len;
Martin Poolaa126972002-01-24 02:41:38 +0000553
Wayne Davisonddd74b62005-03-30 18:27:09 +0000554 /* Insert the contents of string "n", but NOT its null. */
Wayne Davisonef74f5d2005-02-16 08:10:28 +0000555 if (len)
Wayne Davisonddd74b62005-03-30 18:27:09 +0000556 memcpy(s, n, len);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000557
Martin Poolaa126972002-01-24 02:41:38 +0000558 /* Skip over inserted string; continue looking */
Wayne Davisonddd74b62005-03-30 18:27:09 +0000559 p = s + len;
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000560 }
561
Wayne Davisond9c00512005-03-31 01:09:18 +0000562 rwrite(code, buf, total);
Andrew Tridgelle08bfe11998-10-30 02:24:47 +0000563}
564
Wayne Davison0455cd92005-03-31 03:01:26 +0000565/* Return 1 if the format escape is in the log-format string (e.g. look for
566 * the 'b' in the "%9b" format escape). */
Wayne Davison16f960f2005-03-31 00:19:13 +0000567int log_format_has(const char *format, char esc)
568{
569 const char *p;
570
571 if (!format)
572 return 0;
573
574 for (p = format; (p = strchr(p, '%')) != NULL; ) {
575 if (*++p == '-')
576 p++;
577 while (isdigit(*(uchar*)p))
578 p++;
579 if (!*p)
580 break;
581 if (*p == esc)
582 return 1;
583 }
584 return 0;
585}
586
Wayne Davisonafc65a52005-03-04 16:07:50 +0000587/* log the transfer of a file */
588void log_item(struct file_struct *file, struct stats *initial_stats,
589 int iflags, char *hlink)
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000590{
Wayne Davisonafc65a52005-03-04 16:07:50 +0000591 char *s_or_r = am_sender ? "send" : "recv";
592
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000593 if (lp_transfer_logging(module_id)) {
Wayne Davisonafc65a52005-03-04 16:07:50 +0000594 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
595 file, initial_stats, iflags, hlink);
Andrew Tridgellb6062651998-11-03 03:48:47 +0000596 } else if (log_format && !am_server) {
Wayne Davisonafc65a52005-03-04 16:07:50 +0000597 log_formatted(FINFO, log_format, s_or_r,
598 file, initial_stats, iflags, hlink);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000599 }
600}
601
Wayne Davison1c3e3672005-03-04 16:52:00 +0000602void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
603 char *buf)
604{
605 int see_item = itemizing && (iflags || verbose > 1);
606 if (am_server) {
607 if (am_daemon && !dry_run && see_item)
608 log_item(file, &stats, iflags, buf);
Wayne Davisonca62acc2005-03-05 00:21:50 +0000609 } else if (see_item || iflags & ITEM_LOCAL_CHANGE || *buf
Wayne Davison9b9dd062005-03-09 04:00:20 +0000610 || (S_ISDIR(file->mode) && iflags & SIGNIFICANT_ITEM_FLAGS))
Wayne Davison1c3e3672005-03-04 16:52:00 +0000611 log_item(file, &stats, iflags, buf);
612}
613
Wayne Davison19bc8262005-02-19 02:39:26 +0000614void log_delete(char *fname, int mode)
615{
616 static struct file_struct file;
617 int len = strlen(fname);
Wayne Davison19bc8262005-02-19 02:39:26 +0000618 char *fmt;
619
620 file.mode = mode;
621 file.basename = fname;
622
Wayne Davison41b5b5e2005-02-26 04:49:30 +0000623 if (!verbose && !log_format)
624 ;
625 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
Wayne Davison19bc8262005-02-19 02:39:26 +0000626 if (S_ISDIR(mode))
627 len++; /* directories include trailing null */
628 send_msg(MSG_DELETED, fname, len);
Wayne Davison19bc8262005-02-19 02:39:26 +0000629 } else {
Wayne Davisond5609e92005-03-13 05:35:49 +0000630 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
Wayne Davisonafc65a52005-03-04 16:07:50 +0000631 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
632 ITEM_DELETED, NULL);
Wayne Davison19bc8262005-02-19 02:39:26 +0000633 }
634
Wayne Davison41b5b5e2005-02-26 04:49:30 +0000635 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
Wayne Davison1eec0032005-02-19 22:16:41 +0000636 return;
637
Wayne Davisond5609e92005-03-13 05:35:49 +0000638 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
Wayne Davisonafc65a52005-03-04 16:07:50 +0000639 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
Wayne Davison19bc8262005-02-19 02:39:26 +0000640}
Martin Poolaf642a62000-11-10 03:28:15 +0000641
642
643/*
644 * Called when the transfer is interrupted for some reason.
645 *
646 * Code is one of the RERR_* codes from errcode.h, or terminating
647 * successfully.
648 */
Andrew Tridgella9766ef1998-11-15 01:21:42 +0000649void log_exit(int code, const char *file, int line)
Andrew Tridgell9b73d1c1998-10-29 11:11:38 +0000650{
651 if (code == 0) {
Wayne Davison088aff12005-02-19 17:23:08 +0000652 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
Andrew Tridgell9b73d1c1998-10-29 11:11:38 +0000653 (double)stats.total_written,
654 (double)stats.total_read,
655 (double)stats.total_size);
656 } else {
Wayne Davison4a7319b2003-12-15 01:45:01 +0000657 const char *name;
Martin Poolaf642a62000-11-10 03:28:15 +0000658
Wayne Davison4a7319b2003-12-15 01:45:01 +0000659 name = rerr_name(code);
660 if (!name)
661 name = "unexplained error";
662
Wayne Davison1bca1de2003-12-15 08:06:52 +0000663 /* VANISHED is not an error, only a warning */
664 if (code == RERR_VANISHED) {
665 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
666 name, code, file, line);
667 } else {
668 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
669 name, code, file, line);
670 }
Andrew Tridgell9b73d1c1998-10-29 11:11:38 +0000671 }
672}