generator.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  1. /*
  2. * Routines that are exclusive to the generator process.
  3. *
  4. * Copyright (C) 1996-2000 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2002 Martin Pool <mbp@samba.org>
  7. * Copyright (C) 2003-2009 Wayne Davison
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, visit the http://fsf.org website.
  21. */
  22. #include "rsync.h"
  23. extern int verbose;
  24. extern int dry_run;
  25. extern int do_xfers;
  26. extern int stdout_format_has_i;
  27. extern int logfile_format_has_i;
  28. extern int am_root;
  29. extern int am_server;
  30. extern int am_daemon;
  31. extern int inc_recurse;
  32. extern int do_progress;
  33. extern int relative_paths;
  34. extern int implied_dirs;
  35. extern int keep_dirlinks;
  36. extern int preserve_acls;
  37. extern int preserve_xattrs;
  38. extern int preserve_links;
  39. extern int preserve_devices;
  40. extern int preserve_specials;
  41. extern int preserve_hard_links;
  42. extern int preserve_executability;
  43. extern int preserve_perms;
  44. extern int preserve_times;
  45. extern int delete_mode;
  46. extern int delete_before;
  47. extern int delete_during;
  48. extern int delete_after;
  49. extern int msgdone_cnt;
  50. extern int ignore_errors;
  51. extern int remove_source_files;
  52. extern int delay_updates;
  53. extern int update_only;
  54. extern int ignore_existing;
  55. extern int ignore_non_existing;
  56. extern int inplace;
  57. extern int append_mode;
  58. extern int make_backups;
  59. extern int csum_length;
  60. extern int ignore_times;
  61. extern int size_only;
  62. extern OFF_T max_size;
  63. extern OFF_T min_size;
  64. extern int io_error;
  65. extern int flist_eof;
  66. extern int allowed_lull;
  67. extern int sock_f_out;
  68. extern int ignore_timeout;
  69. extern int protocol_version;
  70. extern int file_total;
  71. extern int fuzzy_basis;
  72. extern int always_checksum;
  73. extern int checksum_len;
  74. extern char *partial_dir;
  75. extern char *basis_dir[MAX_BASIS_DIRS+1];
  76. extern int compare_dest;
  77. extern int copy_dest;
  78. extern int link_dest;
  79. extern int whole_file;
  80. extern int list_only;
  81. extern int read_batch;
  82. extern int safe_symlinks;
  83. extern long block_size; /* "long" because popt can't set an int32. */
  84. extern int unsort_ndx;
  85. extern int max_delete;
  86. extern int force_delete;
  87. extern int one_file_system;
  88. extern struct stats stats;
  89. extern dev_t filesystem_dev;
  90. extern mode_t orig_umask;
  91. extern uid_t our_uid;
  92. extern char *backup_dir;
  93. extern char *backup_suffix;
  94. extern int backup_suffix_len;
  95. extern struct file_list *cur_flist, *first_flist, *dir_flist;
  96. extern struct filter_list_struct daemon_filter_list;
  97. int ignore_perishable = 0;
  98. int non_perishable_cnt = 0;
  99. int maybe_ATTRS_REPORT = 0;
  100. static dev_t dev_zero;
  101. static int deletion_count = 0; /* used to implement --max-delete */
  102. static int deldelay_size = 0, deldelay_cnt = 0;
  103. static char *deldelay_buf = NULL;
  104. static int deldelay_fd = -1;
  105. static int loopchk_limit;
  106. static int dir_tweaking;
  107. static int symlink_timeset_failed_flags;
  108. static int need_retouch_dir_times;
  109. static int need_retouch_dir_perms;
  110. static const char *solo_file = NULL;
  111. /* For calling delete_item() and delete_dir_contents(). */
  112. #define DEL_NO_UID_WRITE (1<<0) /* file/dir has our uid w/o write perm */
  113. #define DEL_RECURSE (1<<1) /* if dir, delete all contents */
  114. #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
  115. #define DEL_FOR_FILE (1<<3) /* making room for a replacement file */
  116. #define DEL_FOR_DIR (1<<4) /* making room for a replacement dir */
  117. #define DEL_FOR_SYMLINK (1<<5) /* making room for a replacement symlink */
  118. #define DEL_FOR_DEVICE (1<<6) /* making room for a replacement device */
  119. #define DEL_FOR_SPECIAL (1<<7) /* making room for a replacement special */
  120. #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
  121. enum nonregtype {
  122. TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
  123. };
  124. enum delret {
  125. DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
  126. };
  127. /* Forward declarations. */
  128. static enum delret delete_dir_contents(char *fname, uint16 flags);
  129. #ifdef SUPPORT_HARD_LINKS
  130. static void handle_skipped_hlink(struct file_struct *file, int itemizing,
  131. enum logcode code, int f_out);
  132. #endif
  133. static int is_backup_file(char *fn)
  134. {
  135. int k = strlen(fn) - backup_suffix_len;
  136. return k > 0 && strcmp(fn+k, backup_suffix) == 0;
  137. }
  138. /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
  139. * delete recursively.
  140. *
  141. * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
  142. * a directory! (The buffer is used for recursion, but returned unchanged.)
  143. */
  144. static enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
  145. {
  146. enum delret ret;
  147. char *what;
  148. int ok;
  149. if (verbose > 2) {
  150. rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
  151. fbuf, (int)mode, (int)flags);
  152. }
  153. if (flags & DEL_NO_UID_WRITE)
  154. do_chmod(fbuf, mode | S_IWUSR);
  155. if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
  156. /* This only happens on the first call to delete_item() since
  157. * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
  158. ignore_perishable = 1;
  159. /* If DEL_RECURSE is not set, this just reports emptiness. */
  160. ret = delete_dir_contents(fbuf, flags);
  161. ignore_perishable = 0;
  162. if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
  163. goto check_ret;
  164. /* OK: try to delete the directory. */
  165. }
  166. if (!(flags & DEL_MAKE_ROOM) && max_delete >= 0 && ++deletion_count > max_delete)
  167. return DR_AT_LIMIT;
  168. if (S_ISDIR(mode)) {
  169. what = "rmdir";
  170. ok = do_rmdir(fbuf) == 0;
  171. } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
  172. what = "make_backup";
  173. ok = make_backup(fbuf);
  174. } else {
  175. what = "unlink";
  176. ok = robust_unlink(fbuf) == 0;
  177. }
  178. if (ok) {
  179. if (!(flags & DEL_MAKE_ROOM))
  180. log_delete(fbuf, mode);
  181. ret = DR_SUCCESS;
  182. } else {
  183. if (S_ISDIR(mode) && errno == ENOTEMPTY) {
  184. rprintf(FINFO, "cannot delete non-empty directory: %s\n",
  185. fbuf);
  186. ret = DR_NOT_EMPTY;
  187. } else if (errno != ENOENT) {
  188. rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
  189. what, fbuf);
  190. ret = DR_FAILURE;
  191. } else {
  192. deletion_count--;
  193. ret = DR_SUCCESS;
  194. }
  195. }
  196. check_ret:
  197. if (ret != DR_SUCCESS && flags & DEL_MAKE_ROOM) {
  198. const char *desc;
  199. switch (flags & DEL_MAKE_ROOM) {
  200. case DEL_FOR_FILE: desc = "regular file"; break;
  201. case DEL_FOR_DIR: desc = "directory"; break;
  202. case DEL_FOR_SYMLINK: desc = "symlink"; break;
  203. case DEL_FOR_DEVICE: desc = "device file"; break;
  204. case DEL_FOR_SPECIAL: desc = "special file"; break;
  205. default: exit_cleanup(RERR_UNSUPPORTED); /* IMPOSSIBLE */
  206. }
  207. rprintf(FERROR_XFER, "could not make way for new %s: %s\n",
  208. desc, fbuf);
  209. }
  210. return ret;
  211. }
  212. /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
  213. * its contents, otherwise just checks for content. Returns DR_SUCCESS or
  214. * DR_NOT_EMPTY. Note that fname must point to a MAXPATHLEN buffer! (The
  215. * buffer is used for recursion, but returned unchanged.)
  216. */
  217. static enum delret delete_dir_contents(char *fname, uint16 flags)
  218. {
  219. struct file_list *dirlist;
  220. enum delret ret;
  221. unsigned remainder;
  222. void *save_filters;
  223. int j, dlen;
  224. char *p;
  225. if (verbose > 3) {
  226. rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
  227. fname, flags);
  228. }
  229. dlen = strlen(fname);
  230. save_filters = push_local_filters(fname, dlen);
  231. non_perishable_cnt = 0;
  232. dirlist = get_dirlist(fname, dlen, 0);
  233. ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
  234. if (!dirlist->used)
  235. goto done;
  236. if (!(flags & DEL_RECURSE)) {
  237. ret = DR_NOT_EMPTY;
  238. goto done;
  239. }
  240. p = fname + dlen;
  241. if (dlen != 1 || *fname != '/')
  242. *p++ = '/';
  243. remainder = MAXPATHLEN - (p - fname);
  244. /* We do our own recursion, so make delete_item() non-recursive. */
  245. flags = (flags & ~(DEL_RECURSE|DEL_MAKE_ROOM|DEL_NO_UID_WRITE))
  246. | DEL_DIR_IS_EMPTY;
  247. for (j = dirlist->used; j--; ) {
  248. struct file_struct *fp = dirlist->files[j];
  249. if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
  250. if (verbose > 1) {
  251. rprintf(FINFO,
  252. "mount point, %s, pins parent directory\n",
  253. f_name(fp, NULL));
  254. }
  255. ret = DR_NOT_EMPTY;
  256. continue;
  257. }
  258. strlcpy(p, fp->basename, remainder);
  259. if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
  260. do_chmod(fname, fp->mode | S_IWUSR);
  261. /* Save stack by recursing to ourself directly. */
  262. if (S_ISDIR(fp->mode)) {
  263. if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
  264. ret = DR_NOT_EMPTY;
  265. }
  266. if (delete_item(fname, fp->mode, flags) != DR_SUCCESS)
  267. ret = DR_NOT_EMPTY;
  268. }
  269. fname[dlen] = '\0';
  270. done:
  271. flist_free(dirlist);
  272. pop_local_filters(save_filters);
  273. if (ret == DR_NOT_EMPTY) {
  274. rprintf(FINFO, "cannot delete non-empty directory: %s\n",
  275. fname);
  276. }
  277. return ret;
  278. }
  279. static int start_delete_delay_temp(void)
  280. {
  281. char fnametmp[MAXPATHLEN];
  282. int save_dry_run = dry_run;
  283. dry_run = 0;
  284. if (!get_tmpname(fnametmp, "deldelay")
  285. || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
  286. rprintf(FINFO, "NOTE: Unable to create delete-delay temp file%s.\n",
  287. inc_recurse ? "" : " -- switching to --delete-after");
  288. delete_during = 0;
  289. delete_after = !inc_recurse;
  290. dry_run = save_dry_run;
  291. return 0;
  292. }
  293. unlink(fnametmp);
  294. dry_run = save_dry_run;
  295. return 1;
  296. }
  297. static int flush_delete_delay(void)
  298. {
  299. if (deldelay_fd < 0 && !start_delete_delay_temp())
  300. return 0;
  301. if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
  302. rsyserr(FERROR, errno, "flush of delete-delay buffer");
  303. delete_during = 0;
  304. delete_after = !inc_recurse;
  305. close(deldelay_fd);
  306. return 0;
  307. }
  308. deldelay_cnt = 0;
  309. return 1;
  310. }
  311. static int remember_delete(struct file_struct *file, const char *fname, int flags)
  312. {
  313. int len;
  314. if (deldelay_cnt == deldelay_size && !flush_delete_delay())
  315. return 0;
  316. if (flags & DEL_NO_UID_WRITE)
  317. deldelay_buf[deldelay_cnt++] = '!';
  318. while (1) {
  319. len = snprintf(deldelay_buf + deldelay_cnt,
  320. deldelay_size - deldelay_cnt,
  321. "%x %s%c",
  322. (int)file->mode, fname, '\0');
  323. if ((deldelay_cnt += len) <= deldelay_size)
  324. break;
  325. deldelay_cnt -= len;
  326. if (!flush_delete_delay())
  327. return 0;
  328. }
  329. return 1;
  330. }
  331. static int read_delay_line(char *buf, int *flags_p)
  332. {
  333. static int read_pos = 0;
  334. int j, len, mode;
  335. char *bp, *past_space;
  336. while (1) {
  337. for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
  338. if (j < deldelay_cnt)
  339. break;
  340. if (deldelay_fd < 0) {
  341. if (j > read_pos)
  342. goto invalid_data;
  343. return -1;
  344. }
  345. deldelay_cnt -= read_pos;
  346. if (deldelay_cnt == deldelay_size)
  347. goto invalid_data;
  348. if (deldelay_cnt && read_pos) {
  349. memmove(deldelay_buf, deldelay_buf + read_pos,
  350. deldelay_cnt);
  351. }
  352. len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
  353. deldelay_size - deldelay_cnt);
  354. if (len == 0) {
  355. if (deldelay_cnt) {
  356. rprintf(FERROR,
  357. "ERROR: unexpected EOF in delete-delay file.\n");
  358. }
  359. return -1;
  360. }
  361. if (len < 0) {
  362. rsyserr(FERROR, errno,
  363. "reading delete-delay file");
  364. return -1;
  365. }
  366. deldelay_cnt += len;
  367. read_pos = 0;
  368. }
  369. bp = deldelay_buf + read_pos;
  370. if (*bp == '!') {
  371. bp++;
  372. *flags_p = DEL_NO_UID_WRITE;
  373. } else
  374. *flags_p = 0;
  375. if (sscanf(bp, "%x ", &mode) != 1) {
  376. invalid_data:
  377. rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
  378. return -1;
  379. }
  380. past_space = strchr(bp, ' ') + 1;
  381. len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
  382. read_pos = j + 1;
  383. if (len > MAXPATHLEN) {
  384. rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
  385. return -1;
  386. }
  387. /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
  388. * instead of returning a pointer to our buffer. */
  389. memcpy(buf, past_space, len);
  390. return mode;
  391. }
  392. static void do_delayed_deletions(char *delbuf)
  393. {
  394. int mode, flags;
  395. if (deldelay_fd >= 0) {
  396. if (deldelay_cnt && !flush_delete_delay())
  397. return;
  398. lseek(deldelay_fd, 0, 0);
  399. }
  400. while ((mode = read_delay_line(delbuf, &flags)) >= 0)
  401. delete_item(delbuf, mode, flags | DEL_RECURSE);
  402. if (deldelay_fd >= 0)
  403. close(deldelay_fd);
  404. }
  405. /* This function is used to implement per-directory deletion, and is used by
  406. * all the --delete-WHEN options. Note that the fbuf pointer must point to a
  407. * MAXPATHLEN buffer with the name of the directory in it (the functions we
  408. * call will append names onto the end, but the old dir value will be restored
  409. * on exit). */
  410. static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
  411. {
  412. static int already_warned = 0;
  413. struct file_list *dirlist;
  414. char delbuf[MAXPATHLEN];
  415. int dlen, i;
  416. if (!fbuf) {
  417. change_local_filter_dir(NULL, 0, 0);
  418. return;
  419. }
  420. if (verbose > 2)
  421. rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
  422. if (allowed_lull)
  423. maybe_send_keepalive();
  424. if (io_error && !ignore_errors) {
  425. if (already_warned)
  426. return;
  427. rprintf(FINFO,
  428. "IO error encountered -- skipping file deletion\n");
  429. already_warned = 1;
  430. return;
  431. }
  432. dlen = strlen(fbuf);
  433. change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
  434. if (one_file_system) {
  435. if (file->flags & FLAG_TOP_DIR)
  436. filesystem_dev = *fs_dev;
  437. else if (filesystem_dev != *fs_dev)
  438. return;
  439. }
  440. dirlist = get_dirlist(fbuf, dlen, 0);
  441. /* If an item in dirlist is not found in flist, delete it
  442. * from the filesystem. */
  443. for (i = dirlist->used; i--; ) {
  444. struct file_struct *fp = dirlist->files[i];
  445. if (!F_IS_ACTIVE(fp))
  446. continue;
  447. if (fp->flags & FLAG_MOUNT_DIR && S_ISDIR(fp->mode)) {
  448. if (verbose > 1)
  449. rprintf(FINFO, "cannot delete mount point: %s\n",
  450. f_name(fp, NULL));
  451. continue;
  452. }
  453. /* Here we want to match regardless of file type. Replacement
  454. * of a file with one of another type is handled separately by
  455. * a delete_item call with a DEL_MAKE_ROOM flag. */
  456. if (flist_find_ignore_dirness(cur_flist, fp) < 0) {
  457. int flags = DEL_RECURSE;
  458. if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
  459. flags |= DEL_NO_UID_WRITE;
  460. f_name(fp, delbuf);
  461. if (delete_during == 2) {
  462. if (!remember_delete(fp, delbuf, flags))
  463. break;
  464. } else
  465. delete_item(delbuf, fp->mode, flags);
  466. }
  467. }
  468. flist_free(dirlist);
  469. }
  470. /* This deletes any files on the receiving side that are not present on the
  471. * sending side. This is used by --delete-before and --delete-after. */
  472. static void do_delete_pass(void)
  473. {
  474. char fbuf[MAXPATHLEN];
  475. STRUCT_STAT st;
  476. int j;
  477. /* dry_run is incremented when the destination doesn't exist yet. */
  478. if (dry_run > 1 || list_only)
  479. return;
  480. for (j = 0; j < cur_flist->used; j++) {
  481. struct file_struct *file = cur_flist->sorted[j];
  482. f_name(file, fbuf);
  483. if (!(file->flags & FLAG_CONTENT_DIR)) {
  484. change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(file));
  485. continue;
  486. }
  487. if (verbose > 1 && file->flags & FLAG_TOP_DIR)
  488. rprintf(FINFO, "deleting in %s\n", fbuf);
  489. if (link_stat(fbuf, &st, keep_dirlinks) < 0
  490. || !S_ISDIR(st.st_mode))
  491. continue;
  492. delete_in_dir(fbuf, file, &st.st_dev);
  493. }
  494. delete_in_dir(NULL, NULL, &dev_zero);
  495. if (do_progress && !am_server)
  496. rprintf(FINFO, " \r");
  497. }
  498. static inline int time_differs(struct file_struct *file, stat_x *sxp)
  499. {
  500. return cmp_time(sxp->st.st_mtime, file->modtime);
  501. }
  502. static inline int perms_differ(struct file_struct *file, stat_x *sxp)
  503. {
  504. if (preserve_perms)
  505. return !BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS);
  506. if (preserve_executability)
  507. return (sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0);
  508. return 0;
  509. }
  510. static inline int ownership_differs(struct file_struct *file, stat_x *sxp)
  511. {
  512. if (am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file))
  513. return 1;
  514. if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
  515. return 1;
  516. return 0;
  517. }
  518. #ifdef SUPPORT_ACLS
  519. static inline int acls_differ(const char *fname, struct file_struct *file, stat_x *sxp)
  520. {
  521. if (preserve_acls) {
  522. if (!ACL_READY(*sxp))
  523. get_acl(fname, sxp);
  524. if (set_acl(NULL, file, sxp, file->mode))
  525. return 1;
  526. }
  527. return 0;
  528. }
  529. #endif
  530. #ifdef SUPPORT_XATTRS
  531. static inline int xattrs_differ(const char *fname, struct file_struct *file, stat_x *sxp)
  532. {
  533. if (preserve_xattrs) {
  534. if (!XATTR_READY(*sxp))
  535. get_xattr(fname, sxp);
  536. if (xattr_diff(file, sxp, 0))
  537. return 1;
  538. }
  539. return 0;
  540. }
  541. #endif
  542. int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
  543. {
  544. if (S_ISLNK(file->mode)) {
  545. #ifdef CAN_SET_SYMLINK_TIMES
  546. if (preserve_times & PRESERVE_LINK_TIMES && time_differs(file, sxp))
  547. return 0;
  548. #endif
  549. #ifdef CAN_CHMOD_SYMLINK
  550. if (perms_differ(file, sxp))
  551. return 0;
  552. #endif
  553. #ifndef CAN_CHOWN_SYMLINK
  554. if (ownership_differs(file, sxp))
  555. return 0;
  556. #endif
  557. #if defined SUPPORT_ACLS && 0 /* no current symlink-ACL support */
  558. if (acls_differ(fname, file, sxp))
  559. return 0;
  560. #endif
  561. #if defined SUPPORT_XATTRS && !defined NO_SYMLINK_XATTRS
  562. if (xattrs_differ(fname, file, sxp))
  563. return 0;
  564. #endif
  565. } else {
  566. if (preserve_times && time_differs(file, sxp))
  567. return 0;
  568. if (perms_differ(file, sxp))
  569. return 0;
  570. if (ownership_differs(file, sxp))
  571. return 0;
  572. #ifdef SUPPORT_ACLS
  573. if (acls_differ(fname, file, sxp))
  574. return 0;
  575. #endif
  576. #ifdef SUPPORT_XATTRS
  577. if (xattrs_differ(fname, file, sxp))
  578. return 0;
  579. #endif
  580. }
  581. return 1;
  582. }
  583. void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
  584. stat_x *sxp, int32 iflags, uchar fnamecmp_type,
  585. const char *xname)
  586. {
  587. if (statret >= 0) { /* A from-dest-dir statret can == 1! */
  588. int keep_time = !preserve_times ? 0
  589. : S_ISDIR(file->mode) ? preserve_times & PRESERVE_DIR_TIMES
  590. : S_ISLNK(file->mode) ? preserve_times & PRESERVE_LINK_TIMES
  591. : 1;
  592. if (S_ISREG(file->mode) && F_LENGTH(file) != sxp->st.st_size)
  593. iflags |= ITEM_REPORT_SIZE;
  594. if (file->flags & FLAG_TIME_FAILED) { /* symlinks only */
  595. if (iflags & ITEM_LOCAL_CHANGE)
  596. iflags |= symlink_timeset_failed_flags;
  597. } else if (keep_time
  598. ? cmp_time(file->modtime, sxp->st.st_mtime) != 0
  599. : iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !(iflags & ITEM_MATCHED)
  600. && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
  601. iflags |= ITEM_REPORT_TIME;
  602. #if !defined HAVE_LCHMOD && !defined HAVE_SETATTRLIST
  603. if (S_ISLNK(file->mode)) {
  604. ;
  605. } else
  606. #endif
  607. if (preserve_perms) {
  608. if (!BITS_EQUAL(sxp->st.st_mode, file->mode, CHMOD_BITS))
  609. iflags |= ITEM_REPORT_PERMS;
  610. } else if (preserve_executability
  611. && ((sxp->st.st_mode & 0111 ? 1 : 0) ^ (file->mode & 0111 ? 1 : 0)))
  612. iflags |= ITEM_REPORT_PERMS;
  613. if (uid_ndx && am_root && (uid_t)F_OWNER(file) != sxp->st.st_uid)
  614. iflags |= ITEM_REPORT_OWNER;
  615. if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
  616. && sxp->st.st_gid != (gid_t)F_GROUP(file))
  617. iflags |= ITEM_REPORT_GROUP;
  618. #ifdef SUPPORT_ACLS
  619. if (preserve_acls && !S_ISLNK(file->mode)) {
  620. if (!ACL_READY(*sxp))
  621. get_acl(fnamecmp, sxp);
  622. if (set_acl(NULL, file, sxp, file->mode))
  623. iflags |= ITEM_REPORT_ACL;
  624. }
  625. #endif
  626. #ifdef SUPPORT_XATTRS
  627. if (preserve_xattrs) {
  628. if (!XATTR_READY(*sxp))
  629. get_xattr(fnamecmp, sxp);
  630. if (xattr_diff(file, sxp, 1))
  631. iflags |= ITEM_REPORT_XATTR;
  632. }
  633. #endif
  634. } else {
  635. #ifdef SUPPORT_XATTRS
  636. if (preserve_xattrs && xattr_diff(file, NULL, 1))
  637. iflags |= ITEM_REPORT_XATTR;
  638. #endif
  639. iflags |= ITEM_IS_NEW;
  640. }
  641. iflags &= 0xffff;
  642. if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
  643. || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
  644. if (protocol_version >= 29) {
  645. if (ndx >= 0)
  646. write_ndx(sock_f_out, ndx);
  647. write_shortint(sock_f_out, iflags);
  648. if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
  649. write_byte(sock_f_out, fnamecmp_type);
  650. if (iflags & ITEM_XNAME_FOLLOWS)
  651. write_vstring(sock_f_out, xname, strlen(xname));
  652. #ifdef SUPPORT_XATTRS
  653. if (preserve_xattrs && do_xfers
  654. && iflags & (ITEM_REPORT_XATTR|ITEM_TRANSFER)) {
  655. send_xattr_request(NULL, file,
  656. iflags & ITEM_REPORT_XATTR ? sock_f_out : -1);
  657. }
  658. #endif
  659. } else if (ndx >= 0) {
  660. enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
  661. log_item(code, file, &stats, iflags, xname);
  662. }
  663. }
  664. }
  665. /* Perform our quick-check heuristic for determining if a file is unchanged. */
  666. int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
  667. {
  668. if (st->st_size != F_LENGTH(file))
  669. return 0;
  670. /* if always checksum is set then we use the checksum instead
  671. of the file time to determine whether to sync */
  672. if (always_checksum > 0 && S_ISREG(st->st_mode)) {
  673. char sum[MAX_DIGEST_LEN];
  674. file_checksum(fn, sum, st->st_size);
  675. return memcmp(sum, F_SUM(file), checksum_len) == 0;
  676. }
  677. if (size_only > 0)
  678. return 1;
  679. if (ignore_times)
  680. return 0;
  681. return cmp_time(st->st_mtime, file->modtime) == 0;
  682. }
  683. /*
  684. * set (initialize) the size entries in the per-file sum_struct
  685. * calculating dynamic block and checksum sizes.
  686. *
  687. * This is only called from generate_and_send_sums() but is a separate
  688. * function to encapsulate the logic.
  689. *
  690. * The block size is a rounded square root of file length.
  691. *
  692. * The checksum size is determined according to:
  693. * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
  694. * provided by Donovan Baarda which gives a probability of rsync
  695. * algorithm corrupting data and falling back using the whole md4
  696. * checksums.
  697. *
  698. * This might be made one of several selectable heuristics.
  699. */
  700. static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
  701. {
  702. int32 blength;
  703. int s2length;
  704. int64 l;
  705. if (len < 0) {
  706. /* The file length overflowed our int64 var, so we can't process this file. */
  707. sum->count = -1; /* indicate overflow error */
  708. return;
  709. }
  710. if (block_size)
  711. blength = block_size;
  712. else if (len <= BLOCK_SIZE * BLOCK_SIZE)
  713. blength = BLOCK_SIZE;
  714. else {
  715. int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
  716. int32 c;
  717. int cnt;
  718. for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
  719. if (c < 0 || c >= max_blength)
  720. blength = max_blength;
  721. else {
  722. blength = 0;
  723. do {
  724. blength |= c;
  725. if (len < (int64)blength * blength)
  726. blength &= ~c;
  727. c >>= 1;
  728. } while (c >= 8); /* round to multiple of 8 */
  729. blength = MAX(blength, BLOCK_SIZE);
  730. }
  731. }
  732. if (protocol_version < 27) {
  733. s2length = csum_length;
  734. } else if (csum_length == SUM_LENGTH) {
  735. s2length = SUM_LENGTH;
  736. } else {
  737. int32 c;
  738. int b = BLOCKSUM_BIAS;
  739. for (l = len; l >>= 1; b += 2) {}
  740. for (c = blength; (c >>= 1) && b; b--) {}
  741. /* add a bit, subtract rollsum, round up. */
  742. s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
  743. s2length = MAX(s2length, csum_length);
  744. s2length = MIN(s2length, SUM_LENGTH);
  745. }
  746. sum->flength = len;
  747. sum->blength = blength;
  748. sum->s2length = s2length;
  749. sum->remainder = (int32)(len % blength);
  750. sum->count = (int32)(l = (len / blength) + (sum->remainder != 0));
  751. if ((int64)sum->count != l)
  752. sum->count = -1;
  753. if (sum->count && verbose > 2) {
  754. rprintf(FINFO,
  755. "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
  756. (double)sum->count, (long)sum->remainder, (long)sum->blength,
  757. sum->s2length, (double)sum->flength);
  758. }
  759. }
  760. /*
  761. * Generate and send a stream of signatures/checksums that describe a buffer
  762. *
  763. * Generate approximately one checksum every block_len bytes.
  764. */
  765. static int generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
  766. {
  767. int32 i;
  768. struct map_struct *mapbuf;
  769. struct sum_struct sum;
  770. OFF_T offset = 0;
  771. sum_sizes_sqroot(&sum, len);
  772. if (sum.count < 0)
  773. return -1;
  774. write_sum_head(f_out, &sum);
  775. if (append_mode > 0 && f_copy < 0)
  776. return 0;
  777. if (len > 0)
  778. mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
  779. else
  780. mapbuf = NULL;
  781. for (i = 0; i < sum.count; i++) {
  782. int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
  783. char *map = map_ptr(mapbuf, offset, n1);
  784. char sum2[SUM_LENGTH];
  785. uint32 sum1;
  786. len -= n1;
  787. offset += n1;
  788. if (f_copy >= 0) {
  789. full_write(f_copy, map, n1);
  790. if (append_mode > 0)
  791. continue;
  792. }
  793. sum1 = get_checksum1(map, n1);
  794. get_checksum2(map, n1, sum2);
  795. if (verbose > 3) {
  796. rprintf(FINFO,
  797. "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
  798. (double)i, (double)offset - n1, (long)n1,
  799. (unsigned long)sum1);
  800. }
  801. write_int(f_out, sum1);
  802. write_buf(f_out, sum2, sum.s2length);
  803. }
  804. if (mapbuf)
  805. unmap_file(mapbuf);
  806. return 0;
  807. }
  808. /* Try to find a filename in the same dir as "fname" with a similar name. */
  809. static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
  810. {
  811. int fname_len, fname_suf_len;
  812. const char *fname_suf, *fname = file->basename;
  813. uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
  814. int j, lowest_j = -1;
  815. fname_len = strlen(fname);
  816. fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
  817. for (j = 0; j < dirlist->used; j++) {
  818. struct file_struct *fp = dirlist->files[j];
  819. const char *suf, *name;
  820. int len, suf_len;
  821. uint32 dist;
  822. if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
  823. || fp->flags & FLAG_FILE_SENT)
  824. continue;
  825. name = fp->basename;
  826. if (F_LENGTH(fp) == F_LENGTH(file)
  827. && cmp_time(fp->modtime, file->modtime) == 0) {
  828. if (verbose > 4) {
  829. rprintf(FINFO,
  830. "fuzzy size/modtime match for %s\n",
  831. name);
  832. }
  833. return j;
  834. }
  835. len = strlen(name);
  836. suf = find_filename_suffix(name, len, &suf_len);
  837. dist = fuzzy_distance(name, len, fname, fname_len);
  838. /* Add some extra weight to how well the suffixes match. */
  839. dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
  840. * 10;
  841. if (verbose > 4) {
  842. rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
  843. name, (int)(dist>>16), (int)(dist&0xFFFF));
  844. }
  845. if (dist <= lowest_dist) {
  846. lowest_dist = dist;
  847. lowest_j = j;
  848. }
  849. }
  850. return lowest_j;
  851. }
  852. /* Copy a file found in our --copy-dest handling. */
  853. static int copy_altdest_file(const char *src, const char *dest, struct file_struct *file)
  854. {
  855. char buf[MAXPATHLEN];
  856. const char *copy_to, *partialptr;
  857. int save_preserve_xattrs = preserve_xattrs;
  858. int ok, fd_w;
  859. if (inplace) {
  860. /* Let copy_file open the destination in place. */
  861. fd_w = -1;
  862. copy_to = dest;
  863. } else {
  864. fd_w = open_tmpfile(buf, dest, file);
  865. if (fd_w < 0)
  866. return -1;
  867. copy_to = buf;
  868. }
  869. cleanup_set(copy_to, NULL, NULL, -1, -1);
  870. if (copy_file(src, copy_to, fd_w, file->mode, 0) < 0) {
  871. if (verbose) {
  872. rsyserr(FINFO, errno, "copy_file %s => %s",
  873. full_fname(src), copy_to);
  874. }
  875. /* Try to clean up. */
  876. unlink(copy_to);
  877. cleanup_disable();
  878. return -1;
  879. }
  880. partialptr = partial_dir ? partial_dir_fname(dest) : NULL;
  881. preserve_xattrs = 0; /* xattrs were copied with file */
  882. ok = finish_transfer(dest, copy_to, src, partialptr, file, 1, 0);
  883. preserve_xattrs = save_preserve_xattrs;
  884. cleanup_disable();
  885. return ok ? 0 : -1;
  886. }
  887. /* This is only called for regular files. We return -2 if we've finished
  888. * handling the file, -1 if no dest-linking occurred, or a non-negative
  889. * value if we found an alternate basis file. */
  890. static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
  891. char *cmpbuf, stat_x *sxp, int itemizing,
  892. enum logcode code)
  893. {
  894. int best_match = -1;
  895. int match_level = 0;
  896. int j = 0;
  897. do {
  898. pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
  899. if (link_stat(cmpbuf, &sxp->st, 0) < 0 || !S_ISREG(sxp->st.st_mode))
  900. continue;
  901. switch (match_level) {
  902. case 0:
  903. best_match = j;
  904. match_level = 1;
  905. /* FALL THROUGH */
  906. case 1:
  907. if (!unchanged_file(cmpbuf, file, &sxp->st))
  908. continue;
  909. best_match = j;
  910. match_level = 2;
  911. /* FALL THROUGH */
  912. case 2:
  913. if (!unchanged_attrs(cmpbuf, file, sxp))
  914. continue;
  915. best_match = j;
  916. match_level = 3;
  917. break;
  918. }
  919. break;
  920. } while (basis_dir[++j] != NULL);
  921. if (!match_level)
  922. return -1;
  923. if (j != best_match) {
  924. j = best_match;
  925. pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
  926. if (link_stat(cmpbuf, &sxp->st, 0) < 0)
  927. return -1;
  928. }
  929. if (match_level == 3 && !copy_dest) {
  930. #ifdef SUPPORT_HARD_LINKS
  931. if (link_dest) {
  932. if (!hard_link_one(file, fname, cmpbuf, 1))
  933. goto try_a_copy;
  934. if (preserve_hard_links && F_IS_HLINKED(file))
  935. finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, j);
  936. if (!maybe_ATTRS_REPORT && (verbose > 1 || stdout_format_has_i > 1)) {
  937. itemize(cmpbuf, file, ndx, 1, sxp,
  938. ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
  939. 0, "");
  940. }
  941. } else
  942. #endif
  943. if (itemizing)
  944. itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
  945. if (verbose > 1 && maybe_ATTRS_REPORT)
  946. rprintf(FCLIENT, "%s is uptodate\n", fname);
  947. return -2;
  948. }
  949. if (match_level >= 2) {
  950. #ifdef SUPPORT_HARD_LINKS
  951. try_a_copy: /* Copy the file locally. */
  952. #endif
  953. if (!dry_run && copy_altdest_file(cmpbuf, fname, file) < 0)
  954. return -1;
  955. if (itemizing)
  956. itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
  957. if (maybe_ATTRS_REPORT
  958. && ((!itemizing && verbose && match_level == 2)
  959. || (verbose > 1 && match_level == 3))) {
  960. code = match_level == 3 ? FCLIENT : FINFO;
  961. rprintf(code, "%s%s\n", fname,
  962. match_level == 3 ? " is uptodate" : "");
  963. }
  964. #ifdef SUPPORT_HARD_LINKS
  965. if (preserve_hard_links && F_IS_HLINKED(file))
  966. finish_hard_link(file, fname, ndx, &sxp->st, itemizing, code, -1);
  967. #endif
  968. return -2;
  969. }
  970. return FNAMECMP_BASIS_DIR_LOW + j;
  971. }
  972. /* This is only called for non-regular files. We return -2 if we've finished
  973. * handling the file, or -1 if no dest-linking occurred, or a non-negative
  974. * value if we found an alternate basis file. */
  975. static int try_dests_non(struct file_struct *file, char *fname, int ndx,
  976. char *cmpbuf, stat_x *sxp, int itemizing,
  977. enum logcode code)
  978. {
  979. char lnk[MAXPATHLEN];
  980. int best_match = -1;
  981. int match_level = 0;
  982. enum nonregtype type;
  983. uint32 *devp;
  984. int len, j = 0;
  985. #ifndef SUPPORT_LINKS
  986. if (S_ISLNK(file->mode))
  987. return -1;
  988. #endif
  989. if (S_ISDIR(file->mode)) {
  990. type = TYPE_DIR;
  991. } else if (IS_SPECIAL(file->mode))
  992. type = TYPE_SPECIAL;
  993. else if (IS_DEVICE(file->mode))
  994. type = TYPE_DEVICE;
  995. #ifdef SUPPORT_LINKS
  996. else if (S_ISLNK(file->mode))
  997. type = TYPE_SYMLINK;
  998. #endif
  999. else {
  1000. rprintf(FERROR,
  1001. "internal: try_dests_non() called with invalid mode (%o)\n",
  1002. (int)file->mode);
  1003. exit_cleanup(RERR_UNSUPPORTED);
  1004. }
  1005. do {
  1006. pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
  1007. if (link_stat(cmpbuf, &sxp->st, 0) < 0)
  1008. continue;
  1009. switch (type) {
  1010. case TYPE_DIR:
  1011. if (!S_ISDIR(sxp->st.st_mode))
  1012. continue;
  1013. break;
  1014. case TYPE_SPECIAL:
  1015. if (!IS_SPECIAL(sxp->st.st_mode))
  1016. continue;
  1017. break;
  1018. case TYPE_DEVICE:
  1019. if (!IS_DEVICE(sxp->st.st_mode))
  1020. continue;
  1021. break;
  1022. #ifdef SUPPORT_LINKS
  1023. case TYPE_SYMLINK:
  1024. if (!S_ISLNK(sxp->st.st_mode))
  1025. continue;
  1026. break;
  1027. #endif
  1028. }
  1029. if (match_level < 1) {
  1030. match_level = 1;
  1031. best_match = j;
  1032. }
  1033. switch (type) {
  1034. case TYPE_DIR:
  1035. case TYPE_SPECIAL:
  1036. break;
  1037. case TYPE_DEVICE:
  1038. devp = F_RDEV_P(file);
  1039. if (sxp->st.st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
  1040. continue;
  1041. break;
  1042. #ifdef SUPPORT_LINKS
  1043. case TYPE_SYMLINK:
  1044. if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
  1045. continue;
  1046. lnk[len] = '\0';
  1047. if (strcmp(lnk, F_SYMLINK(file)) != 0)
  1048. continue;
  1049. break;
  1050. #endif
  1051. }
  1052. if (match_level < 2) {
  1053. match_level = 2;
  1054. best_match = j;
  1055. }
  1056. if (unchanged_attrs(cmpbuf, file, sxp)) {
  1057. match_level = 3;
  1058. best_match = j;
  1059. break;
  1060. }
  1061. } while (basis_dir[++j] != NULL);
  1062. if (!match_level)
  1063. return -1;
  1064. if (j != best_match) {
  1065. j = best_match;
  1066. pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
  1067. if (link_stat(cmpbuf, &sxp->st, 0) < 0)
  1068. return -1;
  1069. }
  1070. if (match_level == 3) {
  1071. #ifdef SUPPORT_HARD_LINKS
  1072. if (link_dest
  1073. #ifndef CAN_HARDLINK_SYMLINK
  1074. && !S_ISLNK(file->mode)
  1075. #endif
  1076. #ifndef CAN_HARDLINK_SPECIAL
  1077. && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
  1078. #endif
  1079. && !S_ISDIR(file->mode)) {
  1080. if (do_link(cmpbuf, fname) < 0) {
  1081. rsyserr(FERROR_XFER, errno,
  1082. "failed to hard-link %s with %s",
  1083. cmpbuf, fname);
  1084. return j;
  1085. }
  1086. if (preserve_hard_links && F_IS_HLINKED(file))
  1087. finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
  1088. } else
  1089. #endif
  1090. match_level = 2;
  1091. if (itemizing && stdout_format_has_i
  1092. && (verbose > 1 || stdout_format_has_i > 1)) {
  1093. int chg = compare_dest && type != TYPE_DIR ? 0
  1094. : ITEM_LOCAL_CHANGE + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
  1095. char *lp = match_level == 3 ? "" : NULL;
  1096. itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
  1097. }
  1098. if (verbose > 1 && maybe_ATTRS_REPORT) {
  1099. rprintf(FCLIENT, "%s%s is uptodate\n",
  1100. fname, type == TYPE_DIR ? "/" : "");
  1101. }
  1102. return -2;
  1103. }
  1104. return j;
  1105. }
  1106. static void list_file_entry(struct file_struct *f)
  1107. {
  1108. char permbuf[PERMSTRING_SIZE];
  1109. double len;
  1110. if (!F_IS_ACTIVE(f)) {
  1111. /* this can happen if duplicate names were removed */
  1112. return;
  1113. }
  1114. permstring(permbuf, f->mode);
  1115. len = F_LENGTH(f);
  1116. /* TODO: indicate '+' if the entry has an ACL. */
  1117. #ifdef SUPPORT_LINKS
  1118. if (preserve_links && S_ISLNK(f->mode)) {
  1119. rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
  1120. permbuf, len, timestring(f->modtime),
  1121. f_name(f, NULL), F_SYMLINK(f));
  1122. } else
  1123. #endif
  1124. {
  1125. rprintf(FINFO, "%s %11.0f %s %s\n",
  1126. permbuf, len, timestring(f->modtime),
  1127. f_name(f, NULL));
  1128. }
  1129. }
  1130. static int phase = 0;
  1131. static int dflt_perms;
  1132. static int implied_dirs_are_missing;
  1133. /* Helper for recv_generator's skip_dir and dry_missing_dir tests. */
  1134. static BOOL is_below(struct file_struct *file, struct file_struct *subtree)
  1135. {
  1136. return F_DEPTH(file) > F_DEPTH(subtree)
  1137. && (!implied_dirs_are_missing || f_name_has_prefix(file, subtree));
  1138. }
  1139. /* Acts on the indicated item in cur_flist whose name is fname. If a dir,
  1140. * make sure it exists, and has the right permissions/timestamp info. For
  1141. * all other non-regular files (symlinks, etc.) we create them here. For
  1142. * regular files that have changed, we try to find a basis file and then
  1143. * start sending checksums. The ndx is the file's unique index value.
  1144. *
  1145. * The fname parameter must point to a MAXPATHLEN buffer! (e.g it gets
  1146. * passed to delete_item(), which can use it during a recursive delete.)
  1147. *
  1148. * Note that f_out is set to -1 when doing final directory-permission and
  1149. * modification-time repair. */
  1150. static void recv_generator(char *fname, struct file_struct *file, int ndx,
  1151. int itemizing, enum logcode code, int f_out)
  1152. {
  1153. static const char *parent_dirname = "";
  1154. /* Missing dir not created due to --dry-run; will still be scanned. */
  1155. static struct file_struct *dry_missing_dir = NULL;
  1156. /* Missing dir whose contents are skipped altogether due to
  1157. * --ignore-non-existing, daemon exclude, or mkdir failure. */
  1158. static struct file_struct *skip_dir = NULL;
  1159. static struct file_list *fuzzy_dirlist = NULL;
  1160. static int need_fuzzy_dirlist = 0;
  1161. struct file_struct *fuzzy_file = NULL;
  1162. int fd = -1, f_copy = -1;
  1163. stat_x sx, real_sx;
  1164. STRUCT_STAT partial_st;
  1165. struct file_struct *back_file = NULL;
  1166. int statret, real_ret, stat_errno;
  1167. char *fnamecmp, *partialptr, *backupptr = NULL;
  1168. char fnamecmpbuf[MAXPATHLEN];
  1169. uchar fnamecmp_type;
  1170. int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
  1171. int is_dir = !S_ISDIR(file->mode) ? 0
  1172. : inc_recurse && ndx != cur_flist->ndx_start - 1 ? -1
  1173. : 1;
  1174. if (verbose > 2)
  1175. rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
  1176. if (list_only) {
  1177. if (is_dir < 0
  1178. || (is_dir && !implied_dirs && file->flags & FLAG_IMPLIED_DIR))
  1179. return;
  1180. list_file_entry(file);
  1181. return;
  1182. }
  1183. if (skip_dir) {
  1184. if (is_below(file, skip_dir)) {
  1185. if (is_dir)
  1186. file->flags |= FLAG_MISSING_DIR;
  1187. #ifdef SUPPORT_HARD_LINKS
  1188. else if (F_IS_HLINKED(file))
  1189. handle_skipped_hlink(file, itemizing, code, f_out);
  1190. #endif
  1191. return;
  1192. }
  1193. skip_dir = NULL;
  1194. }
  1195. #ifdef SUPPORT_ACLS
  1196. sx.acc_acl = sx.def_acl = NULL;
  1197. #endif
  1198. #ifdef SUPPORT_XATTRS
  1199. sx.xattr = NULL;
  1200. #endif
  1201. if (daemon_filter_list.head && (*fname != '.' || fname[1])) {
  1202. if (check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
  1203. if (is_dir < 0)
  1204. return;
  1205. #ifdef SUPPORT_HARD_LINKS
  1206. if (F_IS_HLINKED(file))
  1207. handle_skipped_hlink(file, itemizing, code, f_out);
  1208. #endif
  1209. rprintf(FERROR_XFER,
  1210. "skipping daemon-excluded %s \"%s\"\n",
  1211. is_dir ? "directory" : "file", fname);
  1212. if (is_dir)
  1213. goto skipping_dir_contents;
  1214. return;
  1215. }
  1216. }
  1217. if (dry_run > 1 || (dry_missing_dir && is_below(file, dry_missing_dir))) {
  1218. parent_is_dry_missing:
  1219. if (fuzzy_dirlist) {
  1220. flist_free(fuzzy_dirlist);
  1221. fuzzy_dirlist = NULL;
  1222. }
  1223. parent_dirname = "";
  1224. statret = -1;
  1225. stat_errno = ENOENT;
  1226. } else {
  1227. const char *dn = file->dirname ? file->dirname : ".";
  1228. dry_missing_dir = NULL;
  1229. if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
  1230. if (relative_paths && !implied_dirs
  1231. && do_stat(dn, &sx.st) < 0) {
  1232. if (dry_run)
  1233. goto parent_is_dry_missing;
  1234. if (create_directory_path(fname) < 0) {
  1235. rsyserr(FERROR_XFER, errno,
  1236. "recv_generator: mkdir %s failed",
  1237. full_fname(dn));
  1238. }
  1239. }
  1240. if (fuzzy_dirlist) {
  1241. flist_free(fuzzy_dirlist);
  1242. fuzzy_dirlist = NULL;
  1243. }
  1244. if (fuzzy_basis)
  1245. need_fuzzy_dirlist = 1;
  1246. #ifdef SUPPORT_ACLS
  1247. if (!preserve_perms)
  1248. dflt_perms = default_perms_for_dir(dn);
  1249. #endif
  1250. }
  1251. parent_dirname = dn;
  1252. if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
  1253. strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
  1254. fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES);
  1255. need_fuzzy_dirlist = 0;
  1256. }
  1257. statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
  1258. stat_errno = errno;
  1259. }
  1260. if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
  1261. if (is_dir) {
  1262. if (is_dir < 0)
  1263. return;
  1264. skip_dir = file;
  1265. file->flags |= FLAG_MISSING_DIR;
  1266. }
  1267. #ifdef SUPPORT_HARD_LINKS
  1268. else if (F_IS_HLINKED(file))
  1269. handle_skipped_hlink(file, itemizing, code, f_out);
  1270. #endif
  1271. if (verbose > 1) {
  1272. rprintf(FINFO, "not creating new %s \"%s\"\n",
  1273. is_dir ? "directory" : "file", fname);
  1274. }
  1275. return;
  1276. }
  1277. if (statret == 0 && !(sx.st.st_mode & S_IWUSR)
  1278. && !am_root && sx.st.st_uid == our_uid)
  1279. del_opts |= DEL_NO_UID_WRITE;
  1280. if (ignore_existing > 0 && statret == 0
  1281. && (!is_dir || !S_ISDIR(sx.st.st_mode))) {
  1282. if (verbose > 1 && is_dir >= 0)
  1283. rprintf(FINFO, "%s exists\n", fname);
  1284. #ifdef SUPPORT_HARD_LINKS
  1285. if (F_IS_HLINKED(file))
  1286. handle_skipped_hlink(file, itemizing, code, f_out);
  1287. #endif
  1288. goto cleanup;
  1289. }
  1290. fnamecmp = fname;
  1291. if (is_dir) {
  1292. mode_t added_perms;
  1293. if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
  1294. goto cleanup;
  1295. if (am_root < 0) {
  1296. /* For --fake-super, the dir must be useable by the copying
  1297. * user, just like it would be for root. */
  1298. added_perms = S_IRUSR|S_IWUSR|S_IXUSR;
  1299. } else
  1300. added_perms = 0;
  1301. if (is_dir < 0) {
  1302. /* In inc_recurse mode we want to make sure any missing
  1303. * directories get created while we're still processing
  1304. * the parent dir (which allows us to touch the parent
  1305. * dir's mtime right away). We will handle the dir in
  1306. * full later (right before we handle its contents). */
  1307. if (statret == 0
  1308. && (S_ISDIR(sx.st.st_mode)
  1309. || delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0))
  1310. goto cleanup; /* Any errors get reported later. */
  1311. if (do_mkdir(fname, (file->mode|added_perms) & 0700) == 0)
  1312. file->flags |= FLAG_DIR_CREATED;
  1313. goto cleanup;
  1314. }
  1315. /* The file to be received is a directory, so we need
  1316. * to prepare appropriately. If there is already a
  1317. * file of that name and it is *not* a directory, then
  1318. * we need to delete it. If it doesn't exist, then
  1319. * (perhaps recursively) create it. */
  1320. if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
  1321. if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_DIR) != 0)
  1322. goto skipping_dir_contents;
  1323. statret = -1;
  1324. }
  1325. if (dry_run && statret != 0) {
  1326. if (!dry_missing_dir)
  1327. dry_missing_dir = file;
  1328. file->flags |= FLAG_MISSING_DIR;
  1329. }
  1330. real_ret = statret;
  1331. real_sx = sx;
  1332. if (file->flags & FLAG_DIR_CREATED)
  1333. statret = -1;
  1334. if (!preserve_perms) { /* See comment in non-dir code below. */
  1335. file->mode = dest_mode(file->mode, sx.st.st_mode,
  1336. dflt_perms, statret == 0);
  1337. }
  1338. if (statret != 0 && basis_dir[0] != NULL) {
  1339. int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
  1340. itemizing, code);
  1341. if (j == -2) {
  1342. itemizing = 0;
  1343. code = FNONE;
  1344. statret = 1;
  1345. } else if (j >= 0) {
  1346. statret = 1;
  1347. fnamecmp = fnamecmpbuf;
  1348. }
  1349. }
  1350. if (itemizing && f_out != -1) {
  1351. itemize(fnamecmp, file, ndx, statret, &sx,
  1352. statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
  1353. }
  1354. if (real_ret != 0 && do_mkdir(fname,file->mode|added_perms) < 0 && errno != EEXIST) {
  1355. if (!relative_paths || errno != ENOENT
  1356. || create_directory_path(fname) < 0
  1357. || (do_mkdir(fname, file->mode|added_perms) < 0 && errno != EEXIST)) {
  1358. rsyserr(FERROR_XFER, errno,
  1359. "recv_generator: mkdir %s failed",
  1360. full_fname(fname));
  1361. skipping_dir_contents:
  1362. rprintf(FERROR,
  1363. "*** Skipping any contents from this failed directory ***\n");
  1364. skip_dir = file;
  1365. file->flags |= FLAG_MISSING_DIR;
  1366. goto cleanup;
  1367. }
  1368. }
  1369. /* We need to ensure that the dirs in the transfer have writable
  1370. * permissions during the time we are putting files within them.
  1371. * This is then fixed after the transfer is done. */
  1372. #ifdef HAVE_CHMOD
  1373. if (!am_root && !(file->mode & S_IWUSR) && dir_tweaking) {
  1374. mode_t mode = file->mode | S_IWUSR;
  1375. if (do_chmod(fname, mode) < 0) {
  1376. rsyserr(FERROR_XFER, errno,
  1377. "failed to modify permissions on %s",
  1378. full_fname(fname));
  1379. }
  1380. need_retouch_dir_perms = 1;
  1381. }
  1382. #endif
  1383. #ifdef SUPPORT_XATTRS
  1384. if (preserve_xattrs && statret == 1)
  1385. copy_xattrs(fnamecmpbuf, fname);
  1386. #endif
  1387. if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
  1388. && verbose && code != FNONE && f_out != -1)
  1389. rprintf(code, "%s/\n", fname);
  1390. if (real_ret != 0 && one_file_system)
  1391. real_sx.st.st_dev = filesystem_dev;
  1392. if (inc_recurse) {
  1393. if (one_file_system) {
  1394. uint32 *devp = F_DIR_DEV_P(file);
  1395. DEV_MAJOR(devp) = major(real_sx.st.st_dev);
  1396. DEV_MINOR(devp) = minor(real_sx.st.st_dev);
  1397. }
  1398. }
  1399. else if (delete_during && f_out != -1 && !phase
  1400. && !(file->flags & FLAG_MISSING_DIR)) {
  1401. if (file->flags & FLAG_CONTENT_DIR)
  1402. delete_in_dir(fname, file, &real_sx.st.st_dev);
  1403. else
  1404. change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
  1405. }
  1406. goto cleanup;
  1407. }
  1408. /* If we're not preserving permissions, change the file-list's
  1409. * mode based on the local permissions and some heuristics. */
  1410. if (!preserve_perms) {
  1411. int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
  1412. file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
  1413. exists);
  1414. }
  1415. #ifdef SUPPORT_HARD_LINKS
  1416. if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
  1417. && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
  1418. goto cleanup;
  1419. #endif
  1420. if (preserve_links && S_ISLNK(file->mode)) {
  1421. #ifdef SUPPORT_LINKS
  1422. const char *sl = F_SYMLINK(file);
  1423. if (safe_symlinks && unsafe_symlink(sl, fname)) {
  1424. if (verbose) {
  1425. if (solo_file) {
  1426. /* fname contains the destination path, but we
  1427. * want to report the source path. */
  1428. fname = f_name(file, NULL);
  1429. }
  1430. rprintf(FINFO,
  1431. "ignoring unsafe symlink \"%s\" -> \"%s\"\n",
  1432. fname, sl);
  1433. }
  1434. return;
  1435. }
  1436. if (statret == 0) {
  1437. char lnk[MAXPATHLEN];
  1438. int len;
  1439. if (!S_ISLNK(sx.st.st_mode))
  1440. statret = -1;
  1441. else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
  1442. && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
  1443. /* The link is pointing to the right place. */
  1444. set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
  1445. if (itemizing)
  1446. itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
  1447. #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
  1448. if (preserve_hard_links && F_IS_HLINKED(file))
  1449. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1450. #endif
  1451. if (remove_source_files == 1)
  1452. goto return_with_success;
  1453. goto cleanup;
  1454. }
  1455. /* Not the right symlink (or not a symlink), so
  1456. * delete it. */
  1457. if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
  1458. goto cleanup;
  1459. } else if (basis_dir[0] != NULL) {
  1460. int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
  1461. itemizing, code);
  1462. if (j == -2) {
  1463. #ifndef CAN_HARDLINK_SYMLINK
  1464. if (link_dest) {
  1465. /* Resort to --copy-dest behavior. */
  1466. } else
  1467. #endif
  1468. if (!copy_dest)
  1469. goto cleanup;
  1470. itemizing = 0;
  1471. code = FNONE;
  1472. } else if (j >= 0)
  1473. statret = 1;
  1474. }
  1475. #ifdef SUPPORT_HARD_LINKS
  1476. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1477. cur_flist->in_progress++;
  1478. goto cleanup;
  1479. }
  1480. #endif
  1481. if (do_symlink(sl, fname) != 0) {
  1482. rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
  1483. full_fname(fname), sl);
  1484. } else {
  1485. set_file_attrs(fname, file, NULL, NULL, 0);
  1486. if (itemizing) {
  1487. itemize(fname, file, ndx, statret, &sx,
  1488. ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
  1489. }
  1490. if (code != FNONE && verbose)
  1491. rprintf(code, "%s -> %s\n", fname, sl);
  1492. #ifdef SUPPORT_HARD_LINKS
  1493. if (preserve_hard_links && F_IS_HLINKED(file))
  1494. finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
  1495. #endif
  1496. /* This does not check remove_source_files == 1
  1497. * because this is one of the items that the old
  1498. * --remove-sent-files option would remove. */
  1499. if (remove_source_files)
  1500. goto return_with_success;
  1501. }
  1502. #endif
  1503. goto cleanup;
  1504. }
  1505. if ((am_root && preserve_devices && IS_DEVICE(file->mode))
  1506. || (preserve_specials && IS_SPECIAL(file->mode))) {
  1507. dev_t rdev;
  1508. if (IS_DEVICE(file->mode)) {
  1509. uint32 *devp = F_RDEV_P(file);
  1510. rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
  1511. } else
  1512. rdev = 0;
  1513. if (statret == 0) {
  1514. int del_for_flag;
  1515. if (IS_DEVICE(file->mode)) {
  1516. if (!IS_DEVICE(sx.st.st_mode))
  1517. statret = -1;
  1518. del_for_flag = DEL_FOR_DEVICE;
  1519. } else {
  1520. if (!IS_SPECIAL(sx.st.st_mode))
  1521. statret = -1;
  1522. del_for_flag = DEL_FOR_SPECIAL;
  1523. }
  1524. if (statret == 0
  1525. && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
  1526. && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
  1527. /* The device or special file is identical. */
  1528. set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
  1529. if (itemizing)
  1530. itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
  1531. #ifdef SUPPORT_HARD_LINKS
  1532. if (preserve_hard_links && F_IS_HLINKED(file))
  1533. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1534. #endif
  1535. if (remove_source_files == 1)
  1536. goto return_with_success;
  1537. goto cleanup;
  1538. }
  1539. if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
  1540. goto cleanup;
  1541. } else if (basis_dir[0] != NULL) {
  1542. int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
  1543. itemizing, code);
  1544. if (j == -2) {
  1545. #ifndef CAN_HARDLINK_SPECIAL
  1546. if (link_dest) {
  1547. /* Resort to --copy-dest behavior. */
  1548. } else
  1549. #endif
  1550. if (!copy_dest)
  1551. goto cleanup;
  1552. itemizing = 0;
  1553. code = FNONE;
  1554. } else if (j >= 0)
  1555. statret = 1;
  1556. }
  1557. #ifdef SUPPORT_HARD_LINKS
  1558. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1559. cur_flist->in_progress++;
  1560. goto cleanup;
  1561. }
  1562. #endif
  1563. if (verbose > 2) {
  1564. rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
  1565. fname, (int)file->mode,
  1566. (long)major(rdev), (long)minor(rdev));
  1567. }
  1568. if (do_mknod(fname, file->mode, rdev) < 0) {
  1569. rsyserr(FERROR_XFER, errno, "mknod %s failed",
  1570. full_fname(fname));
  1571. } else {
  1572. set_file_attrs(fname, file, NULL, NULL, 0);
  1573. if (itemizing) {
  1574. itemize(fname, file, ndx, statret, &sx,
  1575. ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
  1576. }
  1577. if (code != FNONE && verbose)
  1578. rprintf(code, "%s\n", fname);
  1579. #ifdef SUPPORT_HARD_LINKS
  1580. if (preserve_hard_links && F_IS_HLINKED(file))
  1581. finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
  1582. #endif
  1583. if (remove_source_files == 1)
  1584. goto return_with_success;
  1585. }
  1586. goto cleanup;
  1587. }
  1588. if (!S_ISREG(file->mode)) {
  1589. if (solo_file)
  1590. fname = f_name(file, NULL);
  1591. rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
  1592. goto cleanup;
  1593. }
  1594. if (max_size > 0 && F_LENGTH(file) > max_size) {
  1595. if (verbose > 1) {
  1596. if (solo_file)
  1597. fname = f_name(file, NULL);
  1598. rprintf(FINFO, "%s is over max-size\n", fname);
  1599. }
  1600. goto cleanup;
  1601. }
  1602. if (min_size > 0 && F_LENGTH(file) < min_size) {
  1603. if (verbose > 1) {
  1604. if (solo_file)
  1605. fname = f_name(file, NULL);
  1606. rprintf(FINFO, "%s is under min-size\n", fname);
  1607. }
  1608. goto cleanup;
  1609. }
  1610. if (update_only > 0 && statret == 0
  1611. && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
  1612. if (verbose > 1)
  1613. rprintf(FINFO, "%s is newer\n", fname);
  1614. #ifdef SUPPORT_HARD_LINKS
  1615. if (F_IS_HLINKED(file))
  1616. handle_skipped_hlink(file, itemizing, code, f_out);
  1617. #endif
  1618. goto cleanup;
  1619. }
  1620. fnamecmp_type = FNAMECMP_FNAME;
  1621. if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
  1622. if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
  1623. goto cleanup;
  1624. statret = -1;
  1625. stat_errno = ENOENT;
  1626. }
  1627. if (statret != 0 && basis_dir[0] != NULL) {
  1628. int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
  1629. itemizing, code);
  1630. if (j == -2) {
  1631. if (remove_source_files == 1)
  1632. goto return_with_success;
  1633. goto cleanup;
  1634. }
  1635. if (j >= 0) {
  1636. fnamecmp = fnamecmpbuf;
  1637. fnamecmp_type = j;
  1638. statret = 0;
  1639. }
  1640. }
  1641. real_ret = statret;
  1642. real_sx = sx;
  1643. if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
  1644. && link_stat(partialptr, &partial_st, 0) == 0
  1645. && S_ISREG(partial_st.st_mode)) {
  1646. if (statret != 0)
  1647. goto prepare_to_open;
  1648. } else
  1649. partialptr = NULL;
  1650. if (statret != 0 && fuzzy_dirlist) {
  1651. int j = find_fuzzy(file, fuzzy_dirlist);
  1652. if (j >= 0) {
  1653. fuzzy_file = fuzzy_dirlist->files[j];
  1654. f_name(fuzzy_file, fnamecmpbuf);
  1655. if (verbose > 2) {
  1656. rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
  1657. fname, fnamecmpbuf);
  1658. }
  1659. sx.st.st_size = F_LENGTH(fuzzy_file);
  1660. statret = 0;
  1661. fnamecmp = fnamecmpbuf;
  1662. fnamecmp_type = FNAMECMP_FUZZY;
  1663. }
  1664. }
  1665. if (statret != 0) {
  1666. #ifdef SUPPORT_HARD_LINKS
  1667. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1668. cur_flist->in_progress++;
  1669. goto cleanup;
  1670. }
  1671. #endif
  1672. if (stat_errno == ENOENT)
  1673. goto notify_others;
  1674. rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
  1675. full_fname(fname));
  1676. goto cleanup;
  1677. }
  1678. if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
  1679. ;
  1680. else if (fnamecmp_type == FNAMECMP_FUZZY)
  1681. ;
  1682. else if (unchanged_file(fnamecmp, file, &sx.st)) {
  1683. if (partialptr) {
  1684. do_unlink(partialptr);
  1685. handle_partial_dir(partialptr, PDIR_DELETE);
  1686. }
  1687. set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
  1688. if (itemizing)
  1689. itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
  1690. #ifdef SUPPORT_HARD_LINKS
  1691. if (preserve_hard_links && F_IS_HLINKED(file))
  1692. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1693. #endif
  1694. if (remove_source_files != 1)
  1695. goto cleanup;
  1696. return_with_success:
  1697. if (!dry_run)
  1698. send_msg_int(MSG_SUCCESS, ndx);
  1699. goto cleanup;
  1700. }
  1701. if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
  1702. #ifdef SUPPORT_HARD_LINKS
  1703. if (F_IS_HLINKED(file))
  1704. handle_skipped_hlink(file, itemizing, code, f_out);
  1705. #endif
  1706. goto cleanup;
  1707. }
  1708. prepare_to_open:
  1709. if (partialptr) {
  1710. sx.st = partial_st;
  1711. fnamecmp = partialptr;
  1712. fnamecmp_type = FNAMECMP_PARTIAL_DIR;
  1713. statret = 0;
  1714. }
  1715. if (!do_xfers)
  1716. goto notify_others;
  1717. if (read_batch || whole_file) {
  1718. if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
  1719. if (!(backupptr = get_backup_name(fname)))
  1720. goto cleanup;
  1721. if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
  1722. goto pretend_missing;
  1723. if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
  1724. unmake_file(back_file);
  1725. back_file = NULL;
  1726. goto cleanup;
  1727. }
  1728. }
  1729. goto notify_others;
  1730. }
  1731. if (fuzzy_dirlist) {
  1732. int j = flist_find(fuzzy_dirlist, file);
  1733. if (j >= 0) /* don't use changing file as future fuzzy basis */
  1734. fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
  1735. }
  1736. /* open the file */
  1737. if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
  1738. rsyserr(FERROR, errno, "failed to open %s, continuing",
  1739. full_fname(fnamecmp));
  1740. pretend_missing:
  1741. /* pretend the file didn't exist */
  1742. #ifdef SUPPORT_HARD_LINKS
  1743. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1744. cur_flist->in_progress++;
  1745. goto cleanup;
  1746. }
  1747. #endif
  1748. statret = real_ret = -1;
  1749. goto notify_others;
  1750. }
  1751. if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
  1752. if (!(backupptr = get_backup_name(fname))) {
  1753. close(fd);
  1754. goto cleanup;
  1755. }
  1756. if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
  1757. close(fd);
  1758. goto pretend_missing;
  1759. }
  1760. if (robust_unlink(backupptr) && errno != ENOENT) {
  1761. rsyserr(FERROR_XFER, errno, "unlink %s",
  1762. full_fname(backupptr));
  1763. unmake_file(back_file);
  1764. back_file = NULL;
  1765. close(fd);
  1766. goto cleanup;
  1767. }
  1768. if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
  1769. int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
  1770. if (errno == ENOENT && make_bak_dir(backupptr) == 0) {
  1771. if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)
  1772. save_errno = errno ? errno : save_errno;
  1773. else
  1774. save_errno = 0;
  1775. }
  1776. if (save_errno) {
  1777. rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(backupptr));
  1778. unmake_file(back_file);
  1779. back_file = NULL;
  1780. close(fd);
  1781. goto cleanup;
  1782. }
  1783. }
  1784. fnamecmp_type = FNAMECMP_BACKUP;
  1785. }
  1786. if (verbose > 3) {
  1787. rprintf(FINFO, "gen mapped %s of size %.0f\n",
  1788. fnamecmp, (double)sx.st.st_size);
  1789. }
  1790. if (verbose > 2)
  1791. rprintf(FINFO, "generating and sending sums for %d\n", ndx);
  1792. notify_others:
  1793. if (remove_source_files && !delay_updates && !phase && !dry_run)
  1794. increment_active_files(ndx, itemizing, code);
  1795. if (inc_recurse && !dry_run)
  1796. cur_flist->in_progress++;
  1797. #ifdef SUPPORT_HARD_LINKS
  1798. if (preserve_hard_links && F_IS_HLINKED(file))
  1799. file->flags |= FLAG_FILE_SENT;
  1800. #endif
  1801. write_ndx(f_out, ndx);
  1802. if (itemizing) {
  1803. int iflags = ITEM_TRANSFER;
  1804. if (always_checksum > 0)
  1805. iflags |= ITEM_REPORT_CHANGE;
  1806. if (fnamecmp_type != FNAMECMP_FNAME)
  1807. iflags |= ITEM_BASIS_TYPE_FOLLOWS;
  1808. if (fnamecmp_type == FNAMECMP_FUZZY)
  1809. iflags |= ITEM_XNAME_FOLLOWS;
  1810. itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
  1811. fuzzy_file ? fuzzy_file->basename : NULL);
  1812. #ifdef SUPPORT_ACLS
  1813. if (preserve_acls)
  1814. free_acl(&real_sx);
  1815. #endif
  1816. #ifdef SUPPORT_XATTRS
  1817. if (preserve_xattrs)
  1818. free_xattr(&real_sx);
  1819. #endif
  1820. }
  1821. if (!do_xfers) {
  1822. #ifdef SUPPORT_HARD_LINKS
  1823. if (preserve_hard_links && F_IS_HLINKED(file))
  1824. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1825. #endif
  1826. goto cleanup;
  1827. }
  1828. if (read_batch)
  1829. goto cleanup;
  1830. if (statret != 0 || whole_file)
  1831. write_sum_head(f_out, NULL);
  1832. else if (sx.st.st_size <= 0) {
  1833. write_sum_head(f_out, NULL);
  1834. close(fd);
  1835. } else {
  1836. if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
  1837. rprintf(FWARNING,
  1838. "WARNING: file is too large for checksum sending: %s\n",
  1839. fnamecmp);
  1840. write_sum_head(f_out, NULL);
  1841. }
  1842. close(fd);
  1843. }
  1844. cleanup:
  1845. if (back_file) {
  1846. int save_preserve_xattrs = preserve_xattrs;
  1847. if (f_copy >= 0)
  1848. close(f_copy);
  1849. #ifdef SUPPORT_XATTRS
  1850. if (preserve_xattrs) {
  1851. copy_xattrs(fname, backupptr);
  1852. preserve_xattrs = 0;
  1853. }
  1854. #endif
  1855. set_file_attrs(backupptr, back_file, NULL, NULL, 0);
  1856. preserve_xattrs = save_preserve_xattrs;
  1857. if (verbose > 1) {
  1858. rprintf(FINFO, "backed up %s to %s\n",
  1859. fname, backupptr);
  1860. }
  1861. unmake_file(back_file);
  1862. }
  1863. #ifdef SUPPORT_ACLS
  1864. if (preserve_acls)
  1865. free_acl(&sx);
  1866. #endif
  1867. #ifdef SUPPORT_XATTRS
  1868. if (preserve_xattrs)
  1869. free_xattr(&sx);
  1870. #endif
  1871. return;
  1872. }
  1873. #ifdef SUPPORT_HARD_LINKS
  1874. static void handle_skipped_hlink(struct file_struct *file, int itemizing,
  1875. enum logcode code, int f_out)
  1876. {
  1877. char fbuf[MAXPATHLEN];
  1878. int new_last_ndx;
  1879. struct file_list *save_flist = cur_flist;
  1880. /* If we skip the last item in a chain of links and there was a
  1881. * prior non-skipped hard-link waiting to finish, finish it now. */
  1882. if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
  1883. return;
  1884. file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
  1885. cur_flist->in_progress--; /* undo prior increment */
  1886. f_name(file, fbuf);
  1887. recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
  1888. cur_flist = save_flist;
  1889. }
  1890. #endif
  1891. static void touch_up_dirs(struct file_list *flist, int ndx)
  1892. {
  1893. static int counter = 0;
  1894. struct file_struct *file;
  1895. char *fname;
  1896. BOOL fix_dir_perms;
  1897. int i, start, end;
  1898. if (ndx < 0) {
  1899. start = 0;
  1900. end = flist->used - 1;
  1901. } else
  1902. start = end = ndx;
  1903. /* Fix any directory permissions that were modified during the
  1904. * transfer and/or re-set any tweaked modified-time values. */
  1905. for (i = start; i <= end; i++, counter++) {
  1906. file = flist->files[i];
  1907. if (!S_ISDIR(file->mode)
  1908. || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
  1909. continue;
  1910. if (verbose > 3) {
  1911. fname = f_name(file, NULL);
  1912. rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
  1913. NS(fname), i);
  1914. }
  1915. /* Be sure not to retouch permissions with --fake-super. */
  1916. fix_dir_perms = !am_root && !(file->mode & S_IWUSR);
  1917. if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
  1918. || !(need_retouch_dir_times || fix_dir_perms))
  1919. continue;
  1920. fname = f_name(file, NULL);
  1921. if (fix_dir_perms)
  1922. do_chmod(fname, file->mode);
  1923. if (need_retouch_dir_times) {
  1924. STRUCT_STAT st;
  1925. if (link_stat(fname, &st, 0) == 0
  1926. && cmp_time(st.st_mtime, file->modtime) != 0)
  1927. set_modtime(fname, file->modtime, file->mode);
  1928. }
  1929. if (counter >= loopchk_limit) {
  1930. if (allowed_lull)
  1931. maybe_send_keepalive();
  1932. else
  1933. maybe_flush_socket(0);
  1934. counter = 0;
  1935. }
  1936. }
  1937. }
  1938. void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
  1939. {
  1940. struct file_struct *file;
  1941. struct file_list *flist;
  1942. char fbuf[MAXPATHLEN];
  1943. int ndx;
  1944. while (1) {
  1945. #ifdef SUPPORT_HARD_LINKS
  1946. if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
  1947. flist = flist_for_ndx(ndx, "check_for_finished_files.1");
  1948. file = flist->files[ndx - flist->ndx_start];
  1949. assert(file->flags & FLAG_HLINKED);
  1950. finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
  1951. flist->in_progress--;
  1952. continue;
  1953. }
  1954. #endif
  1955. if (check_redo && (ndx = get_redo_num()) != -1) {
  1956. csum_length = SUM_LENGTH;
  1957. max_size = -max_size;
  1958. min_size = -min_size;
  1959. ignore_existing = -ignore_existing;
  1960. ignore_non_existing = -ignore_non_existing;
  1961. update_only = -update_only;
  1962. always_checksum = -always_checksum;
  1963. size_only = -size_only;
  1964. append_mode = -append_mode;
  1965. make_backups = -make_backups; /* avoid dup backup w/inplace */
  1966. ignore_times++;
  1967. flist = cur_flist;
  1968. cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
  1969. file = cur_flist->files[ndx - cur_flist->ndx_start];
  1970. if (solo_file)
  1971. strlcpy(fbuf, solo_file, sizeof fbuf);
  1972. else
  1973. f_name(file, fbuf);
  1974. recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
  1975. cur_flist->to_redo--;
  1976. cur_flist = flist;
  1977. csum_length = SHORT_SUM_LENGTH;
  1978. max_size = -max_size;
  1979. min_size = -min_size;
  1980. ignore_existing = -ignore_existing;
  1981. ignore_non_existing = -ignore_non_existing;
  1982. update_only = -update_only;
  1983. always_checksum = -always_checksum;
  1984. size_only = -size_only;
  1985. append_mode = -append_mode;
  1986. make_backups = -make_backups;
  1987. ignore_times--;
  1988. continue;
  1989. }
  1990. if (cur_flist == first_flist)
  1991. break;
  1992. /* We only get here if inc_recurse is enabled. */
  1993. if (first_flist->in_progress || first_flist->to_redo)
  1994. break;
  1995. write_ndx(sock_f_out, NDX_DONE);
  1996. if (!read_batch)
  1997. maybe_flush_socket(1);
  1998. if (delete_during == 2 || !dir_tweaking) {
  1999. /* Skip directory touch-up. */
  2000. } else if (first_flist->parent_ndx >= 0)
  2001. touch_up_dirs(dir_flist, first_flist->parent_ndx);
  2002. flist_free(first_flist); /* updates first_flist */
  2003. }
  2004. }
  2005. void generate_files(int f_out, const char *local_name)
  2006. {
  2007. int i, ndx, next_loopchk = 0;
  2008. char fbuf[MAXPATHLEN];
  2009. int itemizing;
  2010. enum logcode code;
  2011. int save_do_progress = do_progress;
  2012. if (protocol_version >= 29) {
  2013. itemizing = 1;
  2014. maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
  2015. code = logfile_format_has_i ? FNONE : FLOG;
  2016. } else if (am_daemon) {
  2017. itemizing = logfile_format_has_i && do_xfers;
  2018. maybe_ATTRS_REPORT = ATTRS_REPORT;
  2019. code = itemizing || !do_xfers ? FCLIENT : FINFO;
  2020. } else if (!am_server) {
  2021. itemizing = stdout_format_has_i;
  2022. maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
  2023. code = itemizing ? FNONE : FINFO;
  2024. } else {
  2025. itemizing = 0;
  2026. maybe_ATTRS_REPORT = ATTRS_REPORT;
  2027. code = FINFO;
  2028. }
  2029. solo_file = local_name;
  2030. dir_tweaking = !(list_only || solo_file || dry_run);
  2031. need_retouch_dir_times = preserve_times & PRESERVE_DIR_TIMES;
  2032. loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
  2033. symlink_timeset_failed_flags = ITEM_REPORT_TIME
  2034. | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
  2035. implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
  2036. if (verbose > 2)
  2037. rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
  2038. if (delete_before && !solo_file && cur_flist->used > 0)
  2039. do_delete_pass();
  2040. if (delete_during == 2) {
  2041. deldelay_size = BIGPATHBUFLEN * 4;
  2042. deldelay_buf = new_array(char, deldelay_size);
  2043. if (!deldelay_buf)
  2044. out_of_memory("delete-delay");
  2045. }
  2046. do_progress = 0;
  2047. if (append_mode > 0 || whole_file < 0)
  2048. whole_file = 0;
  2049. if (verbose >= 2) {
  2050. rprintf(FINFO, "delta-transmission %s\n",
  2051. whole_file
  2052. ? "disabled for local transfer or --whole-file"
  2053. : "enabled");
  2054. }
  2055. /* Since we often fill up the outgoing socket and then just sit around
  2056. * waiting for the other 2 processes to do their thing, we don't want
  2057. * to exit on a timeout. If the data stops flowing, the receiver will
  2058. * notice that and let us know via the message pipe (or its closing). */
  2059. ignore_timeout = 1;
  2060. dflt_perms = (ACCESSPERMS & ~orig_umask);
  2061. do {
  2062. #ifdef SUPPORT_HARD_LINKS
  2063. if (preserve_hard_links && inc_recurse) {
  2064. while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
  2065. wait_for_receiver();
  2066. }
  2067. #endif
  2068. if (inc_recurse && cur_flist->parent_ndx >= 0) {
  2069. struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
  2070. if (solo_file)
  2071. strlcpy(fbuf, solo_file, sizeof fbuf);
  2072. else
  2073. f_name(fp, fbuf);
  2074. ndx = cur_flist->ndx_start - 1;
  2075. recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
  2076. if (delete_during && dry_run < 2 && !list_only
  2077. && !(fp->flags & FLAG_MISSING_DIR)) {
  2078. if (fp->flags & FLAG_CONTENT_DIR) {
  2079. dev_t dirdev;
  2080. if (one_file_system) {
  2081. uint32 *devp = F_DIR_DEV_P(fp);
  2082. dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
  2083. } else
  2084. dirdev = MAKEDEV(0, 0);
  2085. delete_in_dir(fbuf, fp, &dirdev);
  2086. } else
  2087. change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
  2088. }
  2089. }
  2090. for (i = cur_flist->low; i <= cur_flist->high; i++) {
  2091. struct file_struct *file = cur_flist->sorted[i];
  2092. if (!F_IS_ACTIVE(file))
  2093. continue;
  2094. if (unsort_ndx)
  2095. ndx = F_NDX(file);
  2096. else
  2097. ndx = i + cur_flist->ndx_start;
  2098. if (solo_file)
  2099. strlcpy(fbuf, solo_file, sizeof fbuf);
  2100. else
  2101. f_name(file, fbuf);
  2102. recv_generator(fbuf, file, ndx, itemizing, code, f_out);
  2103. check_for_finished_files(itemizing, code, 0);
  2104. if (i + cur_flist->ndx_start >= next_loopchk) {
  2105. if (allowed_lull)
  2106. maybe_send_keepalive();
  2107. else
  2108. maybe_flush_socket(0);
  2109. next_loopchk += loopchk_limit;
  2110. }
  2111. }
  2112. if (!inc_recurse) {
  2113. write_ndx(f_out, NDX_DONE);
  2114. break;
  2115. }
  2116. while (1) {
  2117. check_for_finished_files(itemizing, code, 1);
  2118. if (cur_flist->next || flist_eof)
  2119. break;
  2120. wait_for_receiver();
  2121. }
  2122. } while ((cur_flist = cur_flist->next) != NULL);
  2123. if (delete_during)
  2124. delete_in_dir(NULL, NULL, &dev_zero);
  2125. phase++;
  2126. if (verbose > 2)
  2127. rprintf(FINFO, "generate_files phase=%d\n", phase);
  2128. while (1) {
  2129. check_for_finished_files(itemizing, code, 1);
  2130. if (msgdone_cnt)
  2131. break;
  2132. wait_for_receiver();
  2133. }
  2134. phase++;
  2135. if (verbose > 2)
  2136. rprintf(FINFO, "generate_files phase=%d\n", phase);
  2137. write_ndx(f_out, NDX_DONE);
  2138. /* Reduce round-trip lag-time for a useless delay-updates phase. */
  2139. if (protocol_version >= 29 && !delay_updates)
  2140. write_ndx(f_out, NDX_DONE);
  2141. /* Read MSG_DONE for the redo phase (and any prior messages). */
  2142. while (1) {
  2143. check_for_finished_files(itemizing, code, 0);
  2144. if (msgdone_cnt > 1)
  2145. break;
  2146. wait_for_receiver();
  2147. }
  2148. if (protocol_version >= 29) {
  2149. phase++;
  2150. if (verbose > 2)
  2151. rprintf(FINFO, "generate_files phase=%d\n", phase);
  2152. if (delay_updates)
  2153. write_ndx(f_out, NDX_DONE);
  2154. /* Read MSG_DONE for delay-updates phase & prior messages. */
  2155. while (msgdone_cnt == 2)
  2156. wait_for_receiver();
  2157. }
  2158. do_progress = save_do_progress;
  2159. if (delete_during == 2)
  2160. do_delayed_deletions(fbuf);
  2161. if (delete_after && !solo_file && file_total > 0)
  2162. do_delete_pass();
  2163. if ((need_retouch_dir_perms || need_retouch_dir_times)
  2164. && dir_tweaking && (!inc_recurse || delete_during == 2))
  2165. touch_up_dirs(dir_flist, -1);
  2166. if (max_delete >= 0 && deletion_count > max_delete) {
  2167. rprintf(FWARNING,
  2168. "Deletions stopped due to --max-delete limit (%d skipped)\n",
  2169. deletion_count - max_delete);
  2170. io_error |= IOERR_DEL_LIMIT;
  2171. }
  2172. if (verbose > 2)
  2173. rprintf(FINFO, "generate_files finished\n");
  2174. }