log.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * Logging and utility functions.
  3. *
  4. * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
  5. * Copyright (C) 2000-2001 Martin Pool <mbp@samba.org>
  6. * Copyright (C) 2003-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. #include "ifuncs.h"
  23. extern int verbose;
  24. extern int dry_run;
  25. extern int am_daemon;
  26. extern int am_server;
  27. extern int am_sender;
  28. extern int am_generator;
  29. extern int local_server;
  30. extern int quiet;
  31. extern int module_id;
  32. extern int msg_fd_out;
  33. extern int allow_8bit_chars;
  34. extern int protocol_version;
  35. extern int preserve_times;
  36. extern int progress_is_active;
  37. extern int stdout_format_has_i;
  38. extern int stdout_format_has_o_or_i;
  39. extern int logfile_format_has_i;
  40. extern int logfile_format_has_o_or_i;
  41. extern int receiver_symlink_times;
  42. extern mode_t orig_umask;
  43. extern char *auth_user;
  44. extern char *stdout_format;
  45. extern char *logfile_format;
  46. extern char *logfile_name;
  47. #ifdef ICONV_CONST
  48. extern iconv_t ic_chck;
  49. #endif
  50. #ifdef ICONV_OPTION
  51. extern iconv_t ic_recv;
  52. #endif
  53. extern char curr_dir[MAXPATHLEN];
  54. extern char *full_module_path;
  55. extern unsigned int module_dirlen;
  56. static int log_initialised;
  57. static int logfile_was_closed;
  58. static FILE *logfile_fp;
  59. struct stats stats;
  60. int got_xfer_error = 0;
  61. struct {
  62. int code;
  63. char const *name;
  64. } const rerr_names[] = {
  65. { RERR_SYNTAX , "syntax or usage error" },
  66. { RERR_PROTOCOL , "protocol incompatibility" },
  67. { RERR_FILESELECT , "errors selecting input/output files, dirs" },
  68. { RERR_UNSUPPORTED, "requested action not supported" },
  69. { RERR_STARTCLIENT, "error starting client-server protocol" },
  70. { RERR_SOCKETIO , "error in socket IO" },
  71. { RERR_FILEIO , "error in file IO" },
  72. { RERR_STREAMIO , "error in rsync protocol data stream" },
  73. { RERR_MESSAGEIO , "errors with program diagnostics" },
  74. { RERR_IPC , "error in IPC code" },
  75. { RERR_CRASHED , "sibling process crashed" },
  76. { RERR_TERMINATED , "sibling process terminated abnormally" },
  77. { RERR_SIGNAL1 , "received SIGUSR1" },
  78. { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
  79. { RERR_WAITCHILD , "waitpid() failed" },
  80. { RERR_MALLOC , "error allocating core memory buffers" },
  81. { RERR_PARTIAL , "some files/attrs were not transferred (see previous errors)" },
  82. { RERR_VANISHED , "some files vanished before they could be transferred" },
  83. { RERR_TIMEOUT , "timeout in data send/receive" },
  84. { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
  85. { RERR_CMD_FAILED , "remote shell failed" },
  86. { RERR_CMD_KILLED , "remote shell killed" },
  87. { RERR_CMD_RUN , "remote command could not be run" },
  88. { RERR_CMD_NOTFOUND,"remote command not found" },
  89. { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
  90. { 0, NULL }
  91. };
  92. /*
  93. * Map from rsync error code to name, or return NULL.
  94. */
  95. static char const *rerr_name(int code)
  96. {
  97. int i;
  98. for (i = 0; rerr_names[i].name; i++) {
  99. if (rerr_names[i].code == code)
  100. return rerr_names[i].name;
  101. }
  102. return NULL;
  103. }
  104. static void logit(int priority, const char *buf)
  105. {
  106. if (logfile_was_closed)
  107. logfile_reopen();
  108. if (logfile_fp) {
  109. fprintf(logfile_fp, "%s [%d] %s",
  110. timestring(time(NULL)), (int)getpid(), buf);
  111. fflush(logfile_fp);
  112. } else {
  113. syslog(priority, "%s", buf);
  114. }
  115. }
  116. static void syslog_init()
  117. {
  118. static int been_here = 0;
  119. int options = LOG_PID;
  120. if (been_here)
  121. return;
  122. been_here = 1;
  123. #ifdef LOG_NDELAY
  124. options |= LOG_NDELAY;
  125. #endif
  126. #ifdef LOG_DAEMON
  127. openlog("rsyncd", options, lp_syslog_facility(module_id));
  128. #else
  129. openlog("rsyncd", options);
  130. #endif
  131. #ifndef LOG_NDELAY
  132. logit(LOG_INFO, "rsyncd started\n");
  133. #endif
  134. }
  135. static void logfile_open(void)
  136. {
  137. mode_t old_umask = umask(022 | orig_umask);
  138. logfile_fp = fopen(logfile_name, "a");
  139. umask(old_umask);
  140. if (!logfile_fp) {
  141. int fopen_errno = errno;
  142. /* Rsync falls back to using syslog on failure. */
  143. syslog_init();
  144. rsyserr(FERROR, fopen_errno,
  145. "failed to open log-file %s", logfile_name);
  146. rprintf(FINFO, "Ignoring \"log file\" setting.\n");
  147. }
  148. }
  149. void log_init(int restart)
  150. {
  151. if (log_initialised) {
  152. if (!restart)
  153. return;
  154. if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
  155. if (logfile_fp) {
  156. fclose(logfile_fp);
  157. logfile_fp = NULL;
  158. } else
  159. closelog();
  160. logfile_name = NULL;
  161. } else if (*logfile_name)
  162. return; /* unchanged, non-empty "log file" names */
  163. else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
  164. closelog();
  165. else
  166. return; /* unchanged syslog settings */
  167. } else
  168. log_initialised = 1;
  169. /* This looks pointless, but it is needed in order for the
  170. * C library on some systems to fetch the timezone info
  171. * before the chroot. */
  172. timestring(time(NULL));
  173. /* Optionally use a log file instead of syslog. (Non-daemon
  174. * rsyncs will have already set logfile_name, as needed.) */
  175. if (am_daemon && !logfile_name)
  176. logfile_name = lp_log_file(module_id);
  177. if (logfile_name && *logfile_name)
  178. logfile_open();
  179. else
  180. syslog_init();
  181. }
  182. void logfile_close(void)
  183. {
  184. if (logfile_fp) {
  185. logfile_was_closed = 1;
  186. fclose(logfile_fp);
  187. logfile_fp = NULL;
  188. }
  189. }
  190. void logfile_reopen(void)
  191. {
  192. if (logfile_was_closed) {
  193. logfile_was_closed = 0;
  194. logfile_open();
  195. }
  196. }
  197. static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
  198. {
  199. const char *s, *end = buf + len;
  200. for (s = buf; s < end; s++) {
  201. if ((s < end - 4
  202. && *s == '\\' && s[1] == '#'
  203. && isDigit(s + 2)
  204. && isDigit(s + 3)
  205. && isDigit(s + 4))
  206. || (*s != '\t'
  207. && ((use_isprint && !isPrint(s))
  208. || *(uchar*)s < ' '))) {
  209. if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
  210. exit_cleanup(RERR_MESSAGEIO);
  211. fprintf(f, "\\#%03o", *(uchar*)s);
  212. buf = s + 1;
  213. }
  214. }
  215. if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
  216. exit_cleanup(RERR_MESSAGEIO);
  217. }
  218. /* this is the underlying (unformatted) rsync debugging function. Call
  219. * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
  220. * can happen with certain fatal conditions. */
  221. void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
  222. {
  223. int trailing_CR_or_NL;
  224. FILE *f = NULL;
  225. #ifdef ICONV_OPTION
  226. iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
  227. #else
  228. #ifdef ICONV_CONST
  229. iconv_t ic = ic_chck;
  230. #endif
  231. #endif
  232. if (len < 0)
  233. exit_cleanup(RERR_MESSAGEIO);
  234. if (am_server && msg_fd_out >= 0) {
  235. assert(!is_utf8);
  236. /* Pass the message to our sibling in native charset. */
  237. send_msg((enum msgcode)code, buf, len, 0);
  238. return;
  239. }
  240. if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
  241. code = FERROR;
  242. else if (code == FERROR_UTF8) {
  243. is_utf8 = 1;
  244. code = FERROR;
  245. }
  246. if (code == FCLIENT)
  247. code = FINFO;
  248. else if (am_daemon || logfile_name) {
  249. static int in_block;
  250. char msg[2048];
  251. int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
  252. if (in_block)
  253. return;
  254. in_block = 1;
  255. if (!log_initialised)
  256. log_init(0);
  257. strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
  258. logit(priority, msg);
  259. in_block = 0;
  260. if (code == FLOG || (am_daemon && !am_server))
  261. return;
  262. } else if (code == FLOG)
  263. return;
  264. if (quiet && code == FINFO)
  265. return;
  266. if (am_server) {
  267. enum msgcode msg = (enum msgcode)code;
  268. if (protocol_version < 30) {
  269. if (msg == MSG_ERROR)
  270. msg = MSG_ERROR_XFER;
  271. else if (msg == MSG_WARNING)
  272. msg = MSG_INFO;
  273. }
  274. /* Pass the message to the non-server side. */
  275. if (send_msg(msg, buf, len, !is_utf8))
  276. return;
  277. if (am_daemon) {
  278. /* TODO: can we send the error to the user somehow? */
  279. return;
  280. }
  281. }
  282. switch (code) {
  283. case FERROR_XFER:
  284. got_xfer_error = 1;
  285. /* FALL THROUGH */
  286. case FERROR:
  287. case FWARNING:
  288. f = stderr;
  289. break;
  290. case FINFO:
  291. f = am_server ? stderr : stdout;
  292. break;
  293. default:
  294. exit_cleanup(RERR_MESSAGEIO);
  295. }
  296. if (progress_is_active && !am_server) {
  297. fputc('\n', f);
  298. progress_is_active = 0;
  299. }
  300. trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
  301. ? buf[--len] : 0;
  302. #ifdef ICONV_CONST
  303. if (ic != (iconv_t)-1) {
  304. xbuf outbuf, inbuf;
  305. char convbuf[1024];
  306. int ierrno;
  307. INIT_CONST_XBUF(outbuf, convbuf);
  308. INIT_XBUF(inbuf, (char*)buf, len, -1);
  309. while (inbuf.len) {
  310. iconvbufs(ic, &inbuf, &outbuf, 0);
  311. ierrno = errno;
  312. if (outbuf.len) {
  313. filtered_fwrite(f, convbuf, outbuf.len, 0);
  314. outbuf.len = 0;
  315. }
  316. if (!ierrno || ierrno == E2BIG)
  317. continue;
  318. fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
  319. inbuf.len--;
  320. }
  321. } else
  322. #endif
  323. filtered_fwrite(f, buf, len, !allow_8bit_chars);
  324. if (trailing_CR_or_NL) {
  325. fputc(trailing_CR_or_NL, f);
  326. fflush(f);
  327. }
  328. }
  329. /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
  330. * FWARNING, FLOG, or FCLIENT. */
  331. void rprintf(enum logcode code, const char *format, ...)
  332. {
  333. va_list ap;
  334. char buf[BIGPATHBUFLEN];
  335. size_t len;
  336. va_start(ap, format);
  337. len = vsnprintf(buf, sizeof buf, format, ap);
  338. va_end(ap);
  339. /* Deal with buffer overruns. Instead of panicking, just
  340. * truncate the resulting string. (Note that configure ensures
  341. * that we have a vsnprintf() that doesn't ever return -1.) */
  342. if (len > sizeof buf - 1) {
  343. static const char ellipsis[] = "[...]";
  344. /* Reset length, and zero-terminate the end of our buffer */
  345. len = sizeof buf - 1;
  346. buf[len] = '\0';
  347. /* Copy the ellipsis to the end of the string, but give
  348. * us one extra character:
  349. *
  350. * v--- null byte at buf[sizeof buf - 1]
  351. * abcdefghij0
  352. * -> abcd[...]00 <-- now two null bytes at end
  353. *
  354. * If the input format string has a trailing newline,
  355. * we copy it into that extra null; if it doesn't, well,
  356. * all we lose is one byte. */
  357. memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
  358. if (format[strlen(format)-1] == '\n') {
  359. buf[len-1] = '\n';
  360. }
  361. }
  362. rwrite(code, buf, len, 0);
  363. }
  364. /* This is like rprintf, but it also tries to print some
  365. * representation of the error code. Normally errcode = errno.
  366. *
  367. * Unlike rprintf, this always adds a newline and there should not be
  368. * one in the format string.
  369. *
  370. * Note that since strerror might involve dynamically loading a
  371. * message catalog we need to call it once before chroot-ing. */
  372. void rsyserr(enum logcode code, int errcode, const char *format, ...)
  373. {
  374. va_list ap;
  375. char buf[BIGPATHBUFLEN];
  376. size_t len;
  377. strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
  378. len = (sizeof RSYNC_NAME ": ") - 1;
  379. va_start(ap, format);
  380. len += vsnprintf(buf + len, sizeof buf - len, format, ap);
  381. va_end(ap);
  382. if (len < sizeof buf) {
  383. len += snprintf(buf + len, sizeof buf - len,
  384. ": %s (%d)\n", strerror(errcode), errcode);
  385. }
  386. if (len >= sizeof buf)
  387. exit_cleanup(RERR_MESSAGEIO);
  388. rwrite(code, buf, len, 0);
  389. }
  390. void rflush(enum logcode code)
  391. {
  392. FILE *f = NULL;
  393. if (am_daemon || code == FLOG)
  394. return;
  395. if (code == FINFO && !am_server)
  396. f = stdout;
  397. else
  398. f = stderr;
  399. fflush(f);
  400. }
  401. /* A generic logging routine for send/recv, with parameter substitiution. */
  402. static void log_formatted(enum logcode code, const char *format, const char *op,
  403. struct file_struct *file, const char *fname,
  404. struct stats *initial_stats, int iflags,
  405. const char *hlink)
  406. {
  407. char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
  408. char *p, *s, *c;
  409. const char *n;
  410. size_t len, total;
  411. int64 b;
  412. *fmt = '%';
  413. /* We expand % codes one by one in place in buf. We don't
  414. * copy in the terminating null of the inserted strings, but
  415. * rather keep going until we reach the null of the format. */
  416. total = strlcpy(buf, format, sizeof buf);
  417. if (total > MAXPATHLEN) {
  418. rprintf(FERROR, "log-format string is WAY too long!\n");
  419. exit_cleanup(RERR_MESSAGEIO);
  420. }
  421. buf[total++] = '\n';
  422. buf[total] = '\0';
  423. for (p = buf; (p = strchr(p, '%')) != NULL; ) {
  424. s = p++;
  425. c = fmt + 1;
  426. if (*p == '-')
  427. *c++ = *p++;
  428. while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
  429. *c++ = *p++;
  430. if (!*p)
  431. break;
  432. *c = '\0';
  433. n = NULL;
  434. switch (*p) {
  435. case 'h':
  436. if (am_daemon)
  437. n = client_name(0);
  438. break;
  439. case 'a':
  440. if (am_daemon)
  441. n = client_addr(0);
  442. break;
  443. case 'l':
  444. strlcat(fmt, ".0f", sizeof fmt);
  445. snprintf(buf2, sizeof buf2, fmt,
  446. (double)F_LENGTH(file));
  447. n = buf2;
  448. break;
  449. case 'U':
  450. strlcat(fmt, "u", sizeof fmt);
  451. snprintf(buf2, sizeof buf2, fmt,
  452. uid_ndx ? F_OWNER(file) : 0);
  453. n = buf2;
  454. break;
  455. case 'G':
  456. if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
  457. n = "DEFAULT";
  458. else {
  459. strlcat(fmt, "u", sizeof fmt);
  460. snprintf(buf2, sizeof buf2, fmt,
  461. F_GROUP(file));
  462. n = buf2;
  463. }
  464. break;
  465. case 'p':
  466. strlcat(fmt, "ld", sizeof fmt);
  467. snprintf(buf2, sizeof buf2, fmt,
  468. (long)getpid());
  469. n = buf2;
  470. break;
  471. case 'M':
  472. n = c = timestring(file->modtime);
  473. while ((c = strchr(c, ' ')) != NULL)
  474. *c = '-';
  475. break;
  476. case 'B':
  477. c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
  478. permstring(c, file->mode);
  479. n = c + 1; /* skip the type char */
  480. break;
  481. case 'o':
  482. n = op;
  483. break;
  484. case 'f':
  485. if (fname) {
  486. c = f_name_buf();
  487. strlcpy(c, fname, MAXPATHLEN);
  488. } else
  489. c = f_name(file, NULL);
  490. if (am_sender && F_PATHNAME(file)) {
  491. pathjoin(buf2, sizeof buf2,
  492. F_PATHNAME(file), c);
  493. clean_fname(buf2, 0);
  494. if (fmt[1]) {
  495. strlcpy(c, buf2, MAXPATHLEN);
  496. n = c;
  497. } else
  498. n = buf2;
  499. } else if (am_daemon && *c != '/') {
  500. pathjoin(buf2, sizeof buf2,
  501. curr_dir + module_dirlen, c);
  502. clean_fname(buf2, 0);
  503. if (fmt[1]) {
  504. strlcpy(c, buf2, MAXPATHLEN);
  505. n = c;
  506. } else
  507. n = buf2;
  508. } else {
  509. clean_fname(c, 0);
  510. n = c;
  511. }
  512. if (*n == '/')
  513. n++;
  514. break;
  515. case 'n':
  516. if (fname) {
  517. c = f_name_buf();
  518. strlcpy(c, fname, MAXPATHLEN);
  519. } else
  520. c = f_name(file, NULL);
  521. if (S_ISDIR(file->mode))
  522. strlcat(c, "/", MAXPATHLEN);
  523. n = c;
  524. break;
  525. case 'L':
  526. if (hlink && *hlink) {
  527. n = hlink;
  528. strlcpy(buf2, " => ", sizeof buf2);
  529. } else if (S_ISLNK(file->mode) && !fname) {
  530. n = F_SYMLINK(file);
  531. strlcpy(buf2, " -> ", sizeof buf2);
  532. } else {
  533. n = "";
  534. if (!fmt[1])
  535. break;
  536. strlcpy(buf2, " ", sizeof buf2);
  537. }
  538. strlcat(fmt, "s", sizeof fmt);
  539. snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
  540. n = buf2;
  541. break;
  542. case 'm':
  543. n = lp_name(module_id);
  544. break;
  545. case 't':
  546. n = timestring(time(NULL));
  547. break;
  548. case 'P':
  549. n = full_module_path;
  550. break;
  551. case 'u':
  552. n = auth_user;
  553. break;
  554. case 'b':
  555. if (am_sender) {
  556. b = stats.total_written -
  557. initial_stats->total_written;
  558. } else {
  559. b = stats.total_read -
  560. initial_stats->total_read;
  561. }
  562. strlcat(fmt, ".0f", sizeof fmt);
  563. snprintf(buf2, sizeof buf2, fmt, (double)b);
  564. n = buf2;
  565. break;
  566. case 'c':
  567. if (!am_sender) {
  568. b = stats.total_written -
  569. initial_stats->total_written;
  570. } else {
  571. b = stats.total_read -
  572. initial_stats->total_read;
  573. }
  574. strlcat(fmt, ".0f", sizeof fmt);
  575. snprintf(buf2, sizeof buf2, fmt, (double)b);
  576. n = buf2;
  577. break;
  578. case 'i':
  579. if (iflags & ITEM_DELETED) {
  580. n = "*deleting ";
  581. break;
  582. }
  583. n = c = buf2 + MAXPATHLEN - 32;
  584. c[0] = iflags & ITEM_LOCAL_CHANGE
  585. ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
  586. : !(iflags & ITEM_TRANSFER) ? '.'
  587. : !local_server && *op == 's' ? '<' : '>';
  588. if (S_ISLNK(file->mode)) {
  589. c[1] = 'L';
  590. c[3] = '.';
  591. c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
  592. : !preserve_times || !receiver_symlink_times
  593. || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
  594. } else {
  595. c[1] = S_ISDIR(file->mode) ? 'd'
  596. : IS_SPECIAL(file->mode) ? 'S'
  597. : IS_DEVICE(file->mode) ? 'D' : 'f';
  598. c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
  599. c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
  600. : !preserve_times ? 'T' : 't';
  601. }
  602. c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
  603. c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
  604. c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
  605. c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
  606. c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
  607. c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
  608. c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
  609. c[11] = '\0';
  610. if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
  611. char ch = iflags & ITEM_IS_NEW ? '+' : '?';
  612. int i;
  613. for (i = 2; c[i]; i++)
  614. c[i] = ch;
  615. } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
  616. int i;
  617. for (i = 2; c[i]; i++) {
  618. if (c[i] != '.')
  619. break;
  620. }
  621. if (!c[i]) {
  622. for (i = 2; c[i]; i++)
  623. c[i] = ' ';
  624. }
  625. }
  626. break;
  627. }
  628. /* "n" is the string to be inserted in place of this % code. */
  629. if (!n)
  630. continue;
  631. if (n != buf2 && fmt[1]) {
  632. strlcat(fmt, "s", sizeof fmt);
  633. snprintf(buf2, sizeof buf2, fmt, n);
  634. n = buf2;
  635. }
  636. len = strlen(n);
  637. /* Subtract the length of the escape from the string's size. */
  638. total -= p - s + 1;
  639. if (len + total >= (size_t)sizeof buf) {
  640. rprintf(FERROR,
  641. "buffer overflow expanding %%%c -- exiting\n",
  642. p[0]);
  643. exit_cleanup(RERR_MESSAGEIO);
  644. }
  645. /* Shuffle the rest of the string along to make space for n */
  646. if (len != (size_t)(p - s + 1))
  647. memmove(s + len, p + 1, total - (s - buf) + 1);
  648. total += len;
  649. /* Insert the contents of string "n", but NOT its null. */
  650. if (len)
  651. memcpy(s, n, len);
  652. /* Skip over inserted string; continue looking */
  653. p = s + len;
  654. }
  655. rwrite(code, buf, total, 0);
  656. }
  657. /* Return 1 if the format escape is in the log-format string (e.g. look for
  658. * the 'b' in the "%9b" format escape). */
  659. int log_format_has(const char *format, char esc)
  660. {
  661. const char *p;
  662. if (!format)
  663. return 0;
  664. for (p = format; (p = strchr(p, '%')) != NULL; ) {
  665. if (*++p == '-')
  666. p++;
  667. while (isDigit(p))
  668. p++;
  669. if (!*p)
  670. break;
  671. if (*p == esc)
  672. return 1;
  673. }
  674. return 0;
  675. }
  676. /* Log the transfer of a file. If the code is FCLIENT, the output just goes
  677. * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
  678. * output to both. */
  679. void log_item(enum logcode code, struct file_struct *file,
  680. struct stats *initial_stats, int iflags, const char *hlink)
  681. {
  682. const char *s_or_r = am_sender ? "send" : "recv";
  683. if (code != FLOG && stdout_format && !am_server) {
  684. log_formatted(FCLIENT, stdout_format, s_or_r,
  685. file, NULL, initial_stats, iflags, hlink);
  686. }
  687. if (code != FCLIENT && logfile_format && *logfile_format) {
  688. log_formatted(FLOG, logfile_format, s_or_r,
  689. file, NULL, initial_stats, iflags, hlink);
  690. }
  691. }
  692. void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
  693. const char *buf)
  694. {
  695. int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
  696. int see_item = itemizing && (significant_flags || *buf
  697. || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
  698. int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
  699. if (am_server) {
  700. if (logfile_name && !dry_run && see_item
  701. && (significant_flags || logfile_format_has_i))
  702. log_item(FLOG, file, &stats, iflags, buf);
  703. } else if (see_item || local_change || *buf
  704. || (S_ISDIR(file->mode) && significant_flags)) {
  705. enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
  706. log_item(code, file, &stats, iflags, buf);
  707. }
  708. }
  709. void log_delete(const char *fname, int mode)
  710. {
  711. static struct {
  712. union file_extras ex[4]; /* just in case... */
  713. struct file_struct file;
  714. } x;
  715. int len = strlen(fname);
  716. const char *fmt;
  717. x.file.mode = mode;
  718. if (!verbose && !stdout_format)
  719. ;
  720. else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
  721. if (S_ISDIR(mode))
  722. len++; /* directories include trailing null */
  723. send_msg(MSG_DELETED, fname, len, am_generator);
  724. } else {
  725. fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
  726. log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
  727. ITEM_DELETED, NULL);
  728. }
  729. if (!logfile_name || dry_run || !logfile_format)
  730. return;
  731. fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
  732. log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
  733. }
  734. /*
  735. * Called when the transfer is interrupted for some reason.
  736. *
  737. * Code is one of the RERR_* codes from errcode.h, or terminating
  738. * successfully.
  739. */
  740. void log_exit(int code, const char *file, int line)
  741. {
  742. if (code == 0) {
  743. rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
  744. (double)stats.total_written,
  745. (double)stats.total_read,
  746. (double)stats.total_size);
  747. } else if (am_server != 2) {
  748. const char *name;
  749. name = rerr_name(code);
  750. if (!name)
  751. name = "unexplained error";
  752. /* VANISHED is not an error, only a warning */
  753. if (code == RERR_VANISHED) {
  754. rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
  755. name, code, file, line, who_am_i(), RSYNC_VERSION);
  756. } else {
  757. rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
  758. name, code, file, line, who_am_i(), RSYNC_VERSION);
  759. }
  760. }
  761. }