generator.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417
  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. #ifdef 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. "ERROR: daemon refused to receive %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. #ifdef SUPPORT_XATTRS
  1370. if (preserve_xattrs && statret == 1)
  1371. copy_xattrs(fnamecmpbuf, fname);
  1372. #endif
  1373. if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
  1374. && verbose && code != FNONE && f_out != -1)
  1375. rprintf(code, "%s/\n", fname);
  1376. /* We need to ensure that the dirs in the transfer have both
  1377. * readable and writable permissions during the time we are
  1378. * putting files within them. This is then restored to the
  1379. * former permissions after the transfer is done. */
  1380. #ifdef HAVE_CHMOD
  1381. if (!am_root && (file->mode & S_IRWXU) != S_IRWXU && dir_tweaking) {
  1382. mode_t mode = file->mode | S_IRWXU;
  1383. if (do_chmod(fname, mode) < 0) {
  1384. rsyserr(FERROR_XFER, errno,
  1385. "failed to modify permissions on %s",
  1386. full_fname(fname));
  1387. }
  1388. need_retouch_dir_perms = 1;
  1389. }
  1390. #endif
  1391. if (real_ret != 0 && one_file_system)
  1392. real_sx.st.st_dev = filesystem_dev;
  1393. if (inc_recurse) {
  1394. if (one_file_system) {
  1395. uint32 *devp = F_DIR_DEV_P(file);
  1396. DEV_MAJOR(devp) = major(real_sx.st.st_dev);
  1397. DEV_MINOR(devp) = minor(real_sx.st.st_dev);
  1398. }
  1399. }
  1400. else if (delete_during && f_out != -1 && !phase
  1401. && !(file->flags & FLAG_MISSING_DIR)) {
  1402. if (file->flags & FLAG_CONTENT_DIR)
  1403. delete_in_dir(fname, file, &real_sx.st.st_dev);
  1404. else
  1405. change_local_filter_dir(fname, strlen(fname), F_DEPTH(file));
  1406. }
  1407. goto cleanup;
  1408. }
  1409. /* If we're not preserving permissions, change the file-list's
  1410. * mode based on the local permissions and some heuristics. */
  1411. if (!preserve_perms) {
  1412. int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
  1413. file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
  1414. exists);
  1415. }
  1416. #ifdef SUPPORT_HARD_LINKS
  1417. if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
  1418. && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
  1419. goto cleanup;
  1420. #endif
  1421. if (preserve_links && S_ISLNK(file->mode)) {
  1422. #ifdef SUPPORT_LINKS
  1423. const char *sl = F_SYMLINK(file);
  1424. if (safe_symlinks && unsafe_symlink(sl, fname)) {
  1425. if (verbose) {
  1426. if (solo_file) {
  1427. /* fname contains the destination path, but we
  1428. * want to report the source path. */
  1429. fname = f_name(file, NULL);
  1430. }
  1431. rprintf(FINFO,
  1432. "ignoring unsafe symlink \"%s\" -> \"%s\"\n",
  1433. fname, sl);
  1434. }
  1435. return;
  1436. }
  1437. if (statret == 0) {
  1438. char lnk[MAXPATHLEN];
  1439. int len;
  1440. if (!S_ISLNK(sx.st.st_mode))
  1441. statret = -1;
  1442. else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
  1443. && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
  1444. /* The link is pointing to the right place. */
  1445. set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
  1446. if (itemizing)
  1447. itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
  1448. #if defined SUPPORT_HARD_LINKS && defined CAN_HARDLINK_SYMLINK
  1449. if (preserve_hard_links && F_IS_HLINKED(file))
  1450. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1451. #endif
  1452. if (remove_source_files == 1)
  1453. goto return_with_success;
  1454. goto cleanup;
  1455. }
  1456. /* Not the right symlink (or not a symlink), so
  1457. * delete it. */
  1458. if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_SYMLINK) != 0)
  1459. goto cleanup;
  1460. } else if (basis_dir[0] != NULL) {
  1461. int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
  1462. itemizing, code);
  1463. if (j == -2) {
  1464. #ifndef CAN_HARDLINK_SYMLINK
  1465. if (link_dest) {
  1466. /* Resort to --copy-dest behavior. */
  1467. } else
  1468. #endif
  1469. if (!copy_dest)
  1470. goto cleanup;
  1471. itemizing = 0;
  1472. code = FNONE;
  1473. } else if (j >= 0)
  1474. statret = 1;
  1475. }
  1476. #ifdef SUPPORT_HARD_LINKS
  1477. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1478. cur_flist->in_progress++;
  1479. goto cleanup;
  1480. }
  1481. #endif
  1482. if (do_symlink(sl, fname) != 0) {
  1483. rsyserr(FERROR_XFER, errno, "symlink %s -> \"%s\" failed",
  1484. full_fname(fname), sl);
  1485. } else {
  1486. set_file_attrs(fname, file, NULL, NULL, 0);
  1487. if (itemizing) {
  1488. itemize(fname, file, ndx, statret, &sx,
  1489. ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
  1490. }
  1491. if (code != FNONE && verbose)
  1492. rprintf(code, "%s -> %s\n", fname, sl);
  1493. #ifdef SUPPORT_HARD_LINKS
  1494. if (preserve_hard_links && F_IS_HLINKED(file))
  1495. finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
  1496. #endif
  1497. /* This does not check remove_source_files == 1
  1498. * because this is one of the items that the old
  1499. * --remove-sent-files option would remove. */
  1500. if (remove_source_files)
  1501. goto return_with_success;
  1502. }
  1503. #endif
  1504. goto cleanup;
  1505. }
  1506. if ((am_root && preserve_devices && IS_DEVICE(file->mode))
  1507. || (preserve_specials && IS_SPECIAL(file->mode))) {
  1508. dev_t rdev;
  1509. if (IS_DEVICE(file->mode)) {
  1510. uint32 *devp = F_RDEV_P(file);
  1511. rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
  1512. } else
  1513. rdev = 0;
  1514. if (statret == 0) {
  1515. int del_for_flag;
  1516. if (IS_DEVICE(file->mode)) {
  1517. if (!IS_DEVICE(sx.st.st_mode))
  1518. statret = -1;
  1519. del_for_flag = DEL_FOR_DEVICE;
  1520. } else {
  1521. if (!IS_SPECIAL(sx.st.st_mode))
  1522. statret = -1;
  1523. del_for_flag = DEL_FOR_SPECIAL;
  1524. }
  1525. if (statret == 0
  1526. && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
  1527. && (IS_SPECIAL(sx.st.st_mode) || sx.st.st_rdev == rdev)) {
  1528. /* The device or special file is identical. */
  1529. set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
  1530. if (itemizing)
  1531. itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
  1532. #ifdef SUPPORT_HARD_LINKS
  1533. if (preserve_hard_links && F_IS_HLINKED(file))
  1534. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1535. #endif
  1536. if (remove_source_files == 1)
  1537. goto return_with_success;
  1538. goto cleanup;
  1539. }
  1540. if (delete_item(fname, sx.st.st_mode, del_opts | del_for_flag) != 0)
  1541. goto cleanup;
  1542. } else if (basis_dir[0] != NULL) {
  1543. int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
  1544. itemizing, code);
  1545. if (j == -2) {
  1546. #ifndef CAN_HARDLINK_SPECIAL
  1547. if (link_dest) {
  1548. /* Resort to --copy-dest behavior. */
  1549. } else
  1550. #endif
  1551. if (!copy_dest)
  1552. goto cleanup;
  1553. itemizing = 0;
  1554. code = FNONE;
  1555. } else if (j >= 0)
  1556. statret = 1;
  1557. }
  1558. #ifdef SUPPORT_HARD_LINKS
  1559. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1560. cur_flist->in_progress++;
  1561. goto cleanup;
  1562. }
  1563. #endif
  1564. if (verbose > 2) {
  1565. rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
  1566. fname, (int)file->mode,
  1567. (long)major(rdev), (long)minor(rdev));
  1568. }
  1569. if (do_mknod(fname, file->mode, rdev) < 0) {
  1570. rsyserr(FERROR_XFER, errno, "mknod %s failed",
  1571. full_fname(fname));
  1572. } else {
  1573. set_file_attrs(fname, file, NULL, NULL, 0);
  1574. if (itemizing) {
  1575. itemize(fname, file, ndx, statret, &sx,
  1576. ITEM_LOCAL_CHANGE|ITEM_REPORT_CHANGE, 0, NULL);
  1577. }
  1578. if (code != FNONE && verbose)
  1579. rprintf(code, "%s\n", fname);
  1580. #ifdef SUPPORT_HARD_LINKS
  1581. if (preserve_hard_links && F_IS_HLINKED(file))
  1582. finish_hard_link(file, fname, ndx, NULL, itemizing, code, -1);
  1583. #endif
  1584. if (remove_source_files == 1)
  1585. goto return_with_success;
  1586. }
  1587. goto cleanup;
  1588. }
  1589. if (!S_ISREG(file->mode)) {
  1590. if (solo_file)
  1591. fname = f_name(file, NULL);
  1592. rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
  1593. goto cleanup;
  1594. }
  1595. if (max_size > 0 && F_LENGTH(file) > max_size) {
  1596. if (verbose > 1) {
  1597. if (solo_file)
  1598. fname = f_name(file, NULL);
  1599. rprintf(FINFO, "%s is over max-size\n", fname);
  1600. }
  1601. goto cleanup;
  1602. }
  1603. if (min_size > 0 && F_LENGTH(file) < min_size) {
  1604. if (verbose > 1) {
  1605. if (solo_file)
  1606. fname = f_name(file, NULL);
  1607. rprintf(FINFO, "%s is under min-size\n", fname);
  1608. }
  1609. goto cleanup;
  1610. }
  1611. if (update_only > 0 && statret == 0
  1612. && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
  1613. if (verbose > 1)
  1614. rprintf(FINFO, "%s is newer\n", fname);
  1615. #ifdef SUPPORT_HARD_LINKS
  1616. if (F_IS_HLINKED(file))
  1617. handle_skipped_hlink(file, itemizing, code, f_out);
  1618. #endif
  1619. goto cleanup;
  1620. }
  1621. fnamecmp_type = FNAMECMP_FNAME;
  1622. if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
  1623. if (delete_item(fname, sx.st.st_mode, del_opts | DEL_FOR_FILE) != 0)
  1624. goto cleanup;
  1625. statret = -1;
  1626. stat_errno = ENOENT;
  1627. }
  1628. if (statret != 0 && basis_dir[0] != NULL) {
  1629. int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
  1630. itemizing, code);
  1631. if (j == -2) {
  1632. if (remove_source_files == 1)
  1633. goto return_with_success;
  1634. goto cleanup;
  1635. }
  1636. if (j >= 0) {
  1637. fnamecmp = fnamecmpbuf;
  1638. fnamecmp_type = j;
  1639. statret = 0;
  1640. }
  1641. }
  1642. real_ret = statret;
  1643. real_sx = sx;
  1644. if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
  1645. && link_stat(partialptr, &partial_st, 0) == 0
  1646. && S_ISREG(partial_st.st_mode)) {
  1647. if (statret != 0)
  1648. goto prepare_to_open;
  1649. } else
  1650. partialptr = NULL;
  1651. if (statret != 0 && fuzzy_dirlist) {
  1652. int j = find_fuzzy(file, fuzzy_dirlist);
  1653. if (j >= 0) {
  1654. fuzzy_file = fuzzy_dirlist->files[j];
  1655. f_name(fuzzy_file, fnamecmpbuf);
  1656. if (verbose > 2) {
  1657. rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
  1658. fname, fnamecmpbuf);
  1659. }
  1660. sx.st.st_size = F_LENGTH(fuzzy_file);
  1661. statret = 0;
  1662. fnamecmp = fnamecmpbuf;
  1663. fnamecmp_type = FNAMECMP_FUZZY;
  1664. }
  1665. }
  1666. if (statret != 0) {
  1667. #ifdef SUPPORT_HARD_LINKS
  1668. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1669. cur_flist->in_progress++;
  1670. goto cleanup;
  1671. }
  1672. #endif
  1673. if (stat_errno == ENOENT)
  1674. goto notify_others;
  1675. rsyserr(FERROR_XFER, stat_errno, "recv_generator: failed to stat %s",
  1676. full_fname(fname));
  1677. goto cleanup;
  1678. }
  1679. if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
  1680. ;
  1681. else if (fnamecmp_type == FNAMECMP_FUZZY)
  1682. ;
  1683. else if (unchanged_file(fnamecmp, file, &sx.st)) {
  1684. if (partialptr) {
  1685. do_unlink(partialptr);
  1686. handle_partial_dir(partialptr, PDIR_DELETE);
  1687. }
  1688. set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
  1689. if (itemizing)
  1690. itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
  1691. #ifdef SUPPORT_HARD_LINKS
  1692. if (preserve_hard_links && F_IS_HLINKED(file))
  1693. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1694. #endif
  1695. if (remove_source_files != 1)
  1696. goto cleanup;
  1697. return_with_success:
  1698. if (!dry_run)
  1699. send_msg_int(MSG_SUCCESS, ndx);
  1700. goto cleanup;
  1701. }
  1702. if (append_mode > 0 && sx.st.st_size >= F_LENGTH(file)) {
  1703. #ifdef SUPPORT_HARD_LINKS
  1704. if (F_IS_HLINKED(file))
  1705. handle_skipped_hlink(file, itemizing, code, f_out);
  1706. #endif
  1707. goto cleanup;
  1708. }
  1709. prepare_to_open:
  1710. if (partialptr) {
  1711. sx.st = partial_st;
  1712. fnamecmp = partialptr;
  1713. fnamecmp_type = FNAMECMP_PARTIAL_DIR;
  1714. statret = 0;
  1715. }
  1716. if (!do_xfers)
  1717. goto notify_others;
  1718. if (read_batch || whole_file) {
  1719. if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
  1720. if (!(backupptr = get_backup_name(fname)))
  1721. goto cleanup;
  1722. if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
  1723. goto pretend_missing;
  1724. if (copy_file(fname, backupptr, -1, back_file->mode, 1) < 0) {
  1725. unmake_file(back_file);
  1726. back_file = NULL;
  1727. goto cleanup;
  1728. }
  1729. }
  1730. goto notify_others;
  1731. }
  1732. if (fuzzy_dirlist) {
  1733. int j = flist_find(fuzzy_dirlist, file);
  1734. if (j >= 0) /* don't use changing file as future fuzzy basis */
  1735. fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
  1736. }
  1737. /* open the file */
  1738. if ((fd = do_open(fnamecmp, O_RDONLY, 0)) < 0) {
  1739. rsyserr(FERROR, errno, "failed to open %s, continuing",
  1740. full_fname(fnamecmp));
  1741. pretend_missing:
  1742. /* pretend the file didn't exist */
  1743. #ifdef SUPPORT_HARD_LINKS
  1744. if (preserve_hard_links && F_HLINK_NOT_LAST(file)) {
  1745. cur_flist->in_progress++;
  1746. goto cleanup;
  1747. }
  1748. #endif
  1749. statret = real_ret = -1;
  1750. goto notify_others;
  1751. }
  1752. if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
  1753. if (!(backupptr = get_backup_name(fname))) {
  1754. close(fd);
  1755. goto cleanup;
  1756. }
  1757. if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
  1758. close(fd);
  1759. goto pretend_missing;
  1760. }
  1761. if (robust_unlink(backupptr) && errno != ENOENT) {
  1762. rsyserr(FERROR_XFER, errno, "unlink %s",
  1763. full_fname(backupptr));
  1764. unmake_file(back_file);
  1765. back_file = NULL;
  1766. close(fd);
  1767. goto cleanup;
  1768. }
  1769. if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
  1770. int save_errno = errno ? errno : EINVAL; /* 0 paranoia */
  1771. if (errno == ENOENT && make_bak_dir(backupptr) == 0) {
  1772. if ((f_copy = do_open(backupptr, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0)
  1773. save_errno = errno ? errno : save_errno;
  1774. else
  1775. save_errno = 0;
  1776. }
  1777. if (save_errno) {
  1778. rsyserr(FERROR_XFER, save_errno, "open %s", full_fname(backupptr));
  1779. unmake_file(back_file);
  1780. back_file = NULL;
  1781. close(fd);
  1782. goto cleanup;
  1783. }
  1784. }
  1785. fnamecmp_type = FNAMECMP_BACKUP;
  1786. }
  1787. if (verbose > 3) {
  1788. rprintf(FINFO, "gen mapped %s of size %.0f\n",
  1789. fnamecmp, (double)sx.st.st_size);
  1790. }
  1791. if (verbose > 2)
  1792. rprintf(FINFO, "generating and sending sums for %d\n", ndx);
  1793. notify_others:
  1794. if (remove_source_files && !delay_updates && !phase && !dry_run)
  1795. increment_active_files(ndx, itemizing, code);
  1796. if (inc_recurse && !dry_run)
  1797. cur_flist->in_progress++;
  1798. #ifdef SUPPORT_HARD_LINKS
  1799. if (preserve_hard_links && F_IS_HLINKED(file))
  1800. file->flags |= FLAG_FILE_SENT;
  1801. #endif
  1802. write_ndx(f_out, ndx);
  1803. if (itemizing) {
  1804. int iflags = ITEM_TRANSFER;
  1805. if (always_checksum > 0)
  1806. iflags |= ITEM_REPORT_CHANGE;
  1807. if (fnamecmp_type != FNAMECMP_FNAME)
  1808. iflags |= ITEM_BASIS_TYPE_FOLLOWS;
  1809. if (fnamecmp_type == FNAMECMP_FUZZY)
  1810. iflags |= ITEM_XNAME_FOLLOWS;
  1811. itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
  1812. fuzzy_file ? fuzzy_file->basename : NULL);
  1813. #ifdef SUPPORT_ACLS
  1814. if (preserve_acls)
  1815. free_acl(&real_sx);
  1816. #endif
  1817. #ifdef SUPPORT_XATTRS
  1818. if (preserve_xattrs)
  1819. free_xattr(&real_sx);
  1820. #endif
  1821. }
  1822. if (!do_xfers) {
  1823. #ifdef SUPPORT_HARD_LINKS
  1824. if (preserve_hard_links && F_IS_HLINKED(file))
  1825. finish_hard_link(file, fname, ndx, &sx.st, itemizing, code, -1);
  1826. #endif
  1827. goto cleanup;
  1828. }
  1829. if (read_batch)
  1830. goto cleanup;
  1831. if (statret != 0 || whole_file)
  1832. write_sum_head(f_out, NULL);
  1833. else if (sx.st.st_size <= 0) {
  1834. write_sum_head(f_out, NULL);
  1835. close(fd);
  1836. } else {
  1837. if (generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy) < 0) {
  1838. rprintf(FWARNING,
  1839. "WARNING: file is too large for checksum sending: %s\n",
  1840. fnamecmp);
  1841. write_sum_head(f_out, NULL);
  1842. }
  1843. close(fd);
  1844. }
  1845. cleanup:
  1846. if (back_file) {
  1847. int save_preserve_xattrs = preserve_xattrs;
  1848. if (f_copy >= 0)
  1849. close(f_copy);
  1850. #ifdef SUPPORT_XATTRS
  1851. if (preserve_xattrs) {
  1852. copy_xattrs(fname, backupptr);
  1853. preserve_xattrs = 0;
  1854. }
  1855. #endif
  1856. set_file_attrs(backupptr, back_file, NULL, NULL, 0);
  1857. preserve_xattrs = save_preserve_xattrs;
  1858. if (verbose > 1) {
  1859. rprintf(FINFO, "backed up %s to %s\n",
  1860. fname, backupptr);
  1861. }
  1862. unmake_file(back_file);
  1863. }
  1864. #ifdef SUPPORT_ACLS
  1865. if (preserve_acls)
  1866. free_acl(&sx);
  1867. #endif
  1868. #ifdef SUPPORT_XATTRS
  1869. if (preserve_xattrs)
  1870. free_xattr(&sx);
  1871. #endif
  1872. return;
  1873. }
  1874. #ifdef SUPPORT_HARD_LINKS
  1875. static void handle_skipped_hlink(struct file_struct *file, int itemizing,
  1876. enum logcode code, int f_out)
  1877. {
  1878. char fbuf[MAXPATHLEN];
  1879. int new_last_ndx;
  1880. struct file_list *save_flist = cur_flist;
  1881. /* If we skip the last item in a chain of links and there was a
  1882. * prior non-skipped hard-link waiting to finish, finish it now. */
  1883. if ((new_last_ndx = skip_hard_link(file, &cur_flist)) < 0)
  1884. return;
  1885. file = cur_flist->files[new_last_ndx - cur_flist->ndx_start];
  1886. cur_flist->in_progress--; /* undo prior increment */
  1887. f_name(file, fbuf);
  1888. recv_generator(fbuf, file, new_last_ndx, itemizing, code, f_out);
  1889. cur_flist = save_flist;
  1890. }
  1891. #endif
  1892. static void touch_up_dirs(struct file_list *flist, int ndx)
  1893. {
  1894. static int counter = 0;
  1895. struct file_struct *file;
  1896. char *fname;
  1897. BOOL fix_dir_perms;
  1898. int i, start, end;
  1899. if (ndx < 0) {
  1900. start = 0;
  1901. end = flist->used - 1;
  1902. } else
  1903. start = end = ndx;
  1904. /* Fix any directory permissions that were modified during the
  1905. * transfer and/or re-set any tweaked modified-time values. */
  1906. for (i = start; i <= end; i++, counter++) {
  1907. file = flist->files[i];
  1908. if (!S_ISDIR(file->mode)
  1909. || (!implied_dirs && file->flags & FLAG_IMPLIED_DIR))
  1910. continue;
  1911. if (verbose > 3) {
  1912. fname = f_name(file, NULL);
  1913. rprintf(FINFO, "touch_up_dirs: %s (%d)\n",
  1914. NS(fname), i);
  1915. }
  1916. /* Be sure not to retouch permissions with --fake-super. */
  1917. fix_dir_perms = !am_root && !(file->mode & S_IWUSR);
  1918. if (!F_IS_ACTIVE(file) || file->flags & FLAG_MISSING_DIR
  1919. || !(need_retouch_dir_times || fix_dir_perms))
  1920. continue;
  1921. fname = f_name(file, NULL);
  1922. if (fix_dir_perms)
  1923. do_chmod(fname, file->mode);
  1924. if (need_retouch_dir_times) {
  1925. STRUCT_STAT st;
  1926. if (link_stat(fname, &st, 0) == 0
  1927. && cmp_time(st.st_mtime, file->modtime) != 0)
  1928. set_modtime(fname, file->modtime, file->mode);
  1929. }
  1930. if (counter >= loopchk_limit) {
  1931. if (allowed_lull)
  1932. maybe_send_keepalive();
  1933. else
  1934. maybe_flush_socket(0);
  1935. counter = 0;
  1936. }
  1937. }
  1938. }
  1939. void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
  1940. {
  1941. struct file_struct *file;
  1942. struct file_list *flist;
  1943. char fbuf[MAXPATHLEN];
  1944. int ndx;
  1945. while (1) {
  1946. #ifdef SUPPORT_HARD_LINKS
  1947. if (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
  1948. int send_failed = (ndx == -2);
  1949. if (send_failed)
  1950. ndx = get_hlink_num();
  1951. flist = flist_for_ndx(ndx, "check_for_finished_files.1");
  1952. file = flist->files[ndx - flist->ndx_start];
  1953. assert(file->flags & FLAG_HLINKED);
  1954. if (send_failed)
  1955. handle_skipped_hlink(file, itemizing, code, sock_f_out);
  1956. else
  1957. finish_hard_link(file, f_name(file, fbuf), ndx, NULL, itemizing, code, -1);
  1958. flist->in_progress--;
  1959. continue;
  1960. }
  1961. #endif
  1962. if (check_redo && (ndx = get_redo_num()) != -1) {
  1963. csum_length = SUM_LENGTH;
  1964. max_size = -max_size;
  1965. min_size = -min_size;
  1966. ignore_existing = -ignore_existing;
  1967. ignore_non_existing = -ignore_non_existing;
  1968. update_only = -update_only;
  1969. always_checksum = -always_checksum;
  1970. size_only = -size_only;
  1971. append_mode = -append_mode;
  1972. make_backups = -make_backups; /* avoid dup backup w/inplace */
  1973. ignore_times++;
  1974. flist = cur_flist;
  1975. cur_flist = flist_for_ndx(ndx, "check_for_finished_files.2");
  1976. file = cur_flist->files[ndx - cur_flist->ndx_start];
  1977. if (solo_file)
  1978. strlcpy(fbuf, solo_file, sizeof fbuf);
  1979. else
  1980. f_name(file, fbuf);
  1981. recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
  1982. cur_flist->to_redo--;
  1983. cur_flist = flist;
  1984. csum_length = SHORT_SUM_LENGTH;
  1985. max_size = -max_size;
  1986. min_size = -min_size;
  1987. ignore_existing = -ignore_existing;
  1988. ignore_non_existing = -ignore_non_existing;
  1989. update_only = -update_only;
  1990. always_checksum = -always_checksum;
  1991. size_only = -size_only;
  1992. append_mode = -append_mode;
  1993. make_backups = -make_backups;
  1994. ignore_times--;
  1995. continue;
  1996. }
  1997. if (cur_flist == first_flist)
  1998. break;
  1999. /* We only get here if inc_recurse is enabled. */
  2000. if (first_flist->in_progress || first_flist->to_redo)
  2001. break;
  2002. write_ndx(sock_f_out, NDX_DONE);
  2003. if (!read_batch)
  2004. maybe_flush_socket(1);
  2005. if (delete_during == 2 || !dir_tweaking) {
  2006. /* Skip directory touch-up. */
  2007. } else if (first_flist->parent_ndx >= 0)
  2008. touch_up_dirs(dir_flist, first_flist->parent_ndx);
  2009. flist_free(first_flist); /* updates first_flist */
  2010. }
  2011. }
  2012. void generate_files(int f_out, const char *local_name)
  2013. {
  2014. int i, ndx, next_loopchk = 0;
  2015. char fbuf[MAXPATHLEN];
  2016. int itemizing;
  2017. enum logcode code;
  2018. int save_do_progress = do_progress;
  2019. if (protocol_version >= 29) {
  2020. itemizing = 1;
  2021. maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
  2022. code = logfile_format_has_i ? FNONE : FLOG;
  2023. } else if (am_daemon) {
  2024. itemizing = logfile_format_has_i && do_xfers;
  2025. maybe_ATTRS_REPORT = ATTRS_REPORT;
  2026. code = itemizing || !do_xfers ? FCLIENT : FINFO;
  2027. } else if (!am_server) {
  2028. itemizing = stdout_format_has_i;
  2029. maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
  2030. code = itemizing ? FNONE : FINFO;
  2031. } else {
  2032. itemizing = 0;
  2033. maybe_ATTRS_REPORT = ATTRS_REPORT;
  2034. code = FINFO;
  2035. }
  2036. solo_file = local_name;
  2037. dir_tweaking = !(list_only || solo_file || dry_run);
  2038. need_retouch_dir_times = preserve_times & PRESERVE_DIR_TIMES;
  2039. loopchk_limit = allowed_lull ? allowed_lull * 5 : 200;
  2040. symlink_timeset_failed_flags = ITEM_REPORT_TIME
  2041. | (protocol_version >= 30 || !am_server ? ITEM_REPORT_TIMEFAIL : 0);
  2042. implied_dirs_are_missing = relative_paths && !implied_dirs && protocol_version < 30;
  2043. if (verbose > 2)
  2044. rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
  2045. if (delete_before && !solo_file && cur_flist->used > 0)
  2046. do_delete_pass();
  2047. if (delete_during == 2) {
  2048. deldelay_size = BIGPATHBUFLEN * 4;
  2049. deldelay_buf = new_array(char, deldelay_size);
  2050. if (!deldelay_buf)
  2051. out_of_memory("delete-delay");
  2052. }
  2053. do_progress = 0;
  2054. if (append_mode > 0 || whole_file < 0)
  2055. whole_file = 0;
  2056. if (verbose >= 2) {
  2057. rprintf(FINFO, "delta-transmission %s\n",
  2058. whole_file
  2059. ? "disabled for local transfer or --whole-file"
  2060. : "enabled");
  2061. }
  2062. /* Since we often fill up the outgoing socket and then just sit around
  2063. * waiting for the other 2 processes to do their thing, we don't want
  2064. * to exit on a timeout. If the data stops flowing, the receiver will
  2065. * notice that and let us know via the message pipe (or its closing). */
  2066. ignore_timeout = 1;
  2067. dflt_perms = (ACCESSPERMS & ~orig_umask);
  2068. do {
  2069. #ifdef SUPPORT_HARD_LINKS
  2070. if (preserve_hard_links && inc_recurse) {
  2071. while (!flist_eof && file_total < FILECNT_LOOKAHEAD/2)
  2072. wait_for_receiver();
  2073. }
  2074. #endif
  2075. if (inc_recurse && cur_flist->parent_ndx >= 0) {
  2076. struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
  2077. if (solo_file)
  2078. strlcpy(fbuf, solo_file, sizeof fbuf);
  2079. else
  2080. f_name(fp, fbuf);
  2081. ndx = cur_flist->ndx_start - 1;
  2082. recv_generator(fbuf, fp, ndx, itemizing, code, f_out);
  2083. if (delete_during && dry_run < 2 && !list_only
  2084. && !(fp->flags & FLAG_MISSING_DIR)) {
  2085. if (fp->flags & FLAG_CONTENT_DIR) {
  2086. dev_t dirdev;
  2087. if (one_file_system) {
  2088. uint32 *devp = F_DIR_DEV_P(fp);
  2089. dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
  2090. } else
  2091. dirdev = MAKEDEV(0, 0);
  2092. delete_in_dir(fbuf, fp, &dirdev);
  2093. } else
  2094. change_local_filter_dir(fbuf, strlen(fbuf), F_DEPTH(fp));
  2095. }
  2096. }
  2097. for (i = cur_flist->low; i <= cur_flist->high; i++) {
  2098. struct file_struct *file = cur_flist->sorted[i];
  2099. if (!F_IS_ACTIVE(file))
  2100. continue;
  2101. if (unsort_ndx)
  2102. ndx = F_NDX(file);
  2103. else
  2104. ndx = i + cur_flist->ndx_start;
  2105. if (solo_file)
  2106. strlcpy(fbuf, solo_file, sizeof fbuf);
  2107. else
  2108. f_name(file, fbuf);
  2109. recv_generator(fbuf, file, ndx, itemizing, code, f_out);
  2110. check_for_finished_files(itemizing, code, 0);
  2111. if (i + cur_flist->ndx_start >= next_loopchk) {
  2112. if (allowed_lull)
  2113. maybe_send_keepalive();
  2114. else
  2115. maybe_flush_socket(0);
  2116. next_loopchk += loopchk_limit;
  2117. }
  2118. }
  2119. if (!inc_recurse) {
  2120. write_ndx(f_out, NDX_DONE);
  2121. break;
  2122. }
  2123. while (1) {
  2124. check_for_finished_files(itemizing, code, 1);
  2125. if (cur_flist->next || flist_eof)
  2126. break;
  2127. wait_for_receiver();
  2128. }
  2129. } while ((cur_flist = cur_flist->next) != NULL);
  2130. if (delete_during)
  2131. delete_in_dir(NULL, NULL, &dev_zero);
  2132. phase++;
  2133. if (verbose > 2)
  2134. rprintf(FINFO, "generate_files phase=%d\n", phase);
  2135. while (1) {
  2136. check_for_finished_files(itemizing, code, 1);
  2137. if (msgdone_cnt)
  2138. break;
  2139. wait_for_receiver();
  2140. }
  2141. phase++;
  2142. if (verbose > 2)
  2143. rprintf(FINFO, "generate_files phase=%d\n", phase);
  2144. write_ndx(f_out, NDX_DONE);
  2145. /* Reduce round-trip lag-time for a useless delay-updates phase. */
  2146. if (protocol_version >= 29 && !delay_updates)
  2147. write_ndx(f_out, NDX_DONE);
  2148. /* Read MSG_DONE for the redo phase (and any prior messages). */
  2149. while (1) {
  2150. check_for_finished_files(itemizing, code, 0);
  2151. if (msgdone_cnt > 1)
  2152. break;
  2153. wait_for_receiver();
  2154. }
  2155. if (protocol_version >= 29) {
  2156. phase++;
  2157. if (verbose > 2)
  2158. rprintf(FINFO, "generate_files phase=%d\n", phase);
  2159. if (delay_updates)
  2160. write_ndx(f_out, NDX_DONE);
  2161. /* Read MSG_DONE for delay-updates phase & prior messages. */
  2162. while (msgdone_cnt == 2)
  2163. wait_for_receiver();
  2164. }
  2165. do_progress = save_do_progress;
  2166. if (delete_during == 2)
  2167. do_delayed_deletions(fbuf);
  2168. if (delete_after && !solo_file && file_total > 0)
  2169. do_delete_pass();
  2170. if ((need_retouch_dir_perms || need_retouch_dir_times)
  2171. && dir_tweaking && (!inc_recurse || delete_during == 2))
  2172. touch_up_dirs(dir_flist, -1);
  2173. if (max_delete >= 0 && deletion_count > max_delete) {
  2174. rprintf(FWARNING,
  2175. "Deletions stopped due to --max-delete limit (%d skipped)\n",
  2176. deletion_count - max_delete);
  2177. io_error |= IOERR_DEL_LIMIT;
  2178. }
  2179. if (verbose > 2)
  2180. rprintf(FINFO, "generate_files finished\n");
  2181. }