generator.c 62 KB

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