flist.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995
  1. /*
  2. * Generate and receive file lists.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
  7. * Copyright (C) 2002-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. #include "ifuncs.h"
  24. #include "rounding.h"
  25. #include "io.h"
  26. extern int verbose;
  27. extern int am_root;
  28. extern int am_server;
  29. extern int am_daemon;
  30. extern int am_sender;
  31. extern int am_generator;
  32. extern int inc_recurse;
  33. extern int do_progress;
  34. extern int always_checksum;
  35. extern int module_id;
  36. extern int ignore_errors;
  37. extern int numeric_ids;
  38. extern int recurse;
  39. extern int use_qsort;
  40. extern int xfer_dirs;
  41. extern int filesfrom_fd;
  42. extern int one_file_system;
  43. extern int copy_dirlinks;
  44. extern int keep_dirlinks;
  45. extern int preserve_uid;
  46. extern int preserve_gid;
  47. extern int preserve_acls;
  48. extern int preserve_xattrs;
  49. extern int preserve_links;
  50. extern int preserve_hard_links;
  51. extern int preserve_devices;
  52. extern int preserve_specials;
  53. extern int uid_ndx;
  54. extern int gid_ndx;
  55. extern int eol_nulls;
  56. extern int relative_paths;
  57. extern int implied_dirs;
  58. extern int file_extra_cnt;
  59. extern int ignore_perishable;
  60. extern int non_perishable_cnt;
  61. extern int prune_empty_dirs;
  62. extern int copy_links;
  63. extern int copy_unsafe_links;
  64. extern int protocol_version;
  65. extern int sanitize_paths;
  66. extern int munge_symlinks;
  67. extern int need_unsorted_flist;
  68. extern int sender_symlink_iconv;
  69. extern int unsort_ndx;
  70. extern struct stats stats;
  71. extern char *filesfrom_host;
  72. extern char curr_dir[MAXPATHLEN];
  73. extern struct chmod_mode_struct *chmod_modes;
  74. extern struct filter_list_struct filter_list;
  75. extern struct filter_list_struct daemon_filter_list;
  76. #ifdef ICONV_OPTION
  77. extern int filesfrom_convert;
  78. extern iconv_t ic_send, ic_recv;
  79. #endif
  80. #define PTR_SIZE (sizeof (struct file_struct *))
  81. int io_error;
  82. int checksum_len;
  83. dev_t filesystem_dev; /* used to implement -x */
  84. struct file_list *cur_flist, *first_flist, *dir_flist;
  85. int send_dir_ndx = -1, send_dir_depth = -1;
  86. int flist_cnt = 0; /* how many (non-tmp) file list objects exist */
  87. int file_total = 0; /* total of all active items over all file-lists */
  88. int flist_eof = 0; /* all the file-lists are now known */
  89. #define NORMAL_NAME 0
  90. #define SLASH_ENDING_NAME 1
  91. #define DOTDIR_NAME 2
  92. /* Starting from protocol version 26, we always use 64-bit ino_t and dev_t
  93. * internally, even if this platform does not allow files to have 64-bit inums.
  94. * The only exception is if we're on a platform with no 64-bit type at all.
  95. *
  96. * Because we use read_longint() to get these off the wire, if you transfer
  97. * devices or (for protocols < 30) hardlinks with dev or inum > 2**32 to a
  98. * machine with no 64-bit types then you will get an overflow error.
  99. *
  100. * Note that if you transfer devices from a 64-bit-devt machine (say, Solaris)
  101. * to a 32-bit-devt machine (say, Linux-2.2/x86) then the device numbers will
  102. * be truncated. But it's a kind of silly thing to do anyhow. */
  103. /* The tmp_* vars are used as a cache area by make_file() to store data
  104. * that the sender doesn't need to remember in its file list. The data
  105. * will survive just long enough to be used by send_file_entry(). */
  106. static dev_t tmp_rdev;
  107. #ifdef SUPPORT_HARD_LINKS
  108. static int64 tmp_dev, tmp_ino;
  109. #endif
  110. static char tmp_sum[MAX_DIGEST_LEN];
  111. static char empty_sum[MAX_DIGEST_LEN];
  112. static int flist_count_offset; /* for --delete --progress */
  113. static int dir_count = 0;
  114. static void flist_sort_and_clean(struct file_list *flist, int strip_root);
  115. static void output_flist(struct file_list *flist);
  116. void init_flist(void)
  117. {
  118. if (verbose > 4) {
  119. rprintf(FINFO, "FILE_STRUCT_LEN=%d, EXTRA_LEN=%d\n",
  120. (int)FILE_STRUCT_LEN, (int)EXTRA_LEN);
  121. }
  122. checksum_len = protocol_version < 21 ? 2
  123. : protocol_version < 30 ? MD4_DIGEST_LEN
  124. : MD5_DIGEST_LEN;
  125. }
  126. static int show_filelist_p(void)
  127. {
  128. return verbose && xfer_dirs && !am_server && !inc_recurse;
  129. }
  130. static void start_filelist_progress(char *kind)
  131. {
  132. rprintf(FCLIENT, "%s ... ", kind);
  133. if (verbose > 1 || do_progress)
  134. rprintf(FCLIENT, "\n");
  135. rflush(FINFO);
  136. }
  137. static void emit_filelist_progress(int count)
  138. {
  139. rprintf(FCLIENT, " %d files...\r", count);
  140. }
  141. static void maybe_emit_filelist_progress(int count)
  142. {
  143. if (do_progress && show_filelist_p() && (count % 100) == 0)
  144. emit_filelist_progress(count);
  145. }
  146. static void finish_filelist_progress(const struct file_list *flist)
  147. {
  148. if (do_progress) {
  149. /* This overwrites the progress line */
  150. rprintf(FINFO, "%d file%sto consider\n",
  151. flist->used, flist->used == 1 ? " " : "s ");
  152. } else
  153. rprintf(FINFO, "done\n");
  154. }
  155. void show_flist_stats(void)
  156. {
  157. /* Nothing yet */
  158. }
  159. /* Stat either a symlink or its referent, depending on the settings of
  160. * copy_links, copy_unsafe_links, etc. Returns -1 on error, 0 on success.
  161. *
  162. * If path is the name of a symlink, then the linkbuf buffer (which must hold
  163. * MAXPATHLEN chars) will be set to the symlink's target string.
  164. *
  165. * The stat structure pointed to by stp will contain information about the
  166. * link or the referent as appropriate, if they exist. */
  167. static int readlink_stat(const char *path, STRUCT_STAT *stp, char *linkbuf)
  168. {
  169. #ifdef SUPPORT_LINKS
  170. if (link_stat(path, stp, copy_dirlinks) < 0)
  171. return -1;
  172. if (S_ISLNK(stp->st_mode)) {
  173. int llen = readlink(path, linkbuf, MAXPATHLEN - 1);
  174. if (llen < 0)
  175. return -1;
  176. linkbuf[llen] = '\0';
  177. if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) {
  178. if (verbose > 1) {
  179. rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n",
  180. path, linkbuf);
  181. }
  182. return x_stat(path, stp, NULL);
  183. }
  184. if (munge_symlinks && am_sender && llen > SYMLINK_PREFIX_LEN
  185. && strncmp(linkbuf, SYMLINK_PREFIX, SYMLINK_PREFIX_LEN) == 0) {
  186. memmove(linkbuf, linkbuf + SYMLINK_PREFIX_LEN,
  187. llen - SYMLINK_PREFIX_LEN + 1);
  188. }
  189. }
  190. return 0;
  191. #else
  192. return x_stat(path, stp, NULL);
  193. #endif
  194. }
  195. int link_stat(const char *path, STRUCT_STAT *stp, int follow_dirlinks)
  196. {
  197. #ifdef SUPPORT_LINKS
  198. if (copy_links)
  199. return x_stat(path, stp, NULL);
  200. if (x_lstat(path, stp, NULL) < 0)
  201. return -1;
  202. if (follow_dirlinks && S_ISLNK(stp->st_mode)) {
  203. STRUCT_STAT st;
  204. if (x_stat(path, &st, NULL) == 0 && S_ISDIR(st.st_mode))
  205. *stp = st;
  206. }
  207. return 0;
  208. #else
  209. return x_stat(path, stp, NULL);
  210. #endif
  211. }
  212. static inline int is_daemon_excluded(const char *fname, int is_dir)
  213. {
  214. if (daemon_filter_list.head
  215. && check_filter(&daemon_filter_list, FLOG, fname, is_dir) < 0) {
  216. errno = ENOENT;
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. static inline int path_is_daemon_excluded(char *path, int ignore_filename)
  222. {
  223. if (daemon_filter_list.head) {
  224. char *slash = path;
  225. while ((slash = strchr(slash+1, '/')) != NULL) {
  226. int ret;
  227. *slash = '\0';
  228. ret = check_filter(&daemon_filter_list, FLOG, path, 1);
  229. *slash = '/';
  230. if (ret < 0) {
  231. errno = ENOENT;
  232. return 1;
  233. }
  234. }
  235. if (!ignore_filename
  236. && check_filter(&daemon_filter_list, FLOG, path, 1) < 0) {
  237. errno = ENOENT;
  238. return 1;
  239. }
  240. }
  241. return 0;
  242. }
  243. /* This function is used to check if a file should be included/excluded
  244. * from the list of files based on its name and type etc. The value of
  245. * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */
  246. static int is_excluded(const char *fname, int is_dir, int filter_level)
  247. {
  248. #if 0 /* This currently never happens, so avoid a useless compare. */
  249. if (filter_level == NO_FILTERS)
  250. return 0;
  251. #endif
  252. if (is_daemon_excluded(fname, is_dir))
  253. return 1;
  254. if (filter_level != ALL_FILTERS)
  255. return 0;
  256. if (filter_list.head
  257. && check_filter(&filter_list, FINFO, fname, is_dir) < 0)
  258. return 1;
  259. return 0;
  260. }
  261. static void send_directory(int f, struct file_list *flist,
  262. char *fbuf, int len, int flags);
  263. static const char *pathname, *orig_dir;
  264. static int pathname_len;
  265. /* Make sure flist can hold at least flist->used + extra entries. */
  266. static void flist_expand(struct file_list *flist, int extra)
  267. {
  268. struct file_struct **new_ptr;
  269. if (flist->used + extra <= flist->malloced)
  270. return;
  271. if (flist->malloced < FLIST_START)
  272. flist->malloced = FLIST_START;
  273. else if (flist->malloced >= FLIST_LINEAR)
  274. flist->malloced += FLIST_LINEAR;
  275. else
  276. flist->malloced *= 2;
  277. /* In case count jumped or we are starting the list
  278. * with a known size just set it. */
  279. if (flist->malloced < flist->used + extra)
  280. flist->malloced = flist->used + extra;
  281. new_ptr = realloc_array(flist->files, struct file_struct *,
  282. flist->malloced);
  283. if (verbose >= 2 && flist->malloced != FLIST_START) {
  284. rprintf(FCLIENT, "[%s] expand file_list pointer array to %.0f bytes, did%s move\n",
  285. who_am_i(),
  286. (double)sizeof flist->files[0] * flist->malloced,
  287. (new_ptr == flist->files) ? " not" : "");
  288. }
  289. flist->files = new_ptr;
  290. if (!flist->files)
  291. out_of_memory("flist_expand");
  292. }
  293. static void flist_done_allocating(struct file_list *flist)
  294. {
  295. void *ptr = pool_boundary(flist->file_pool, 8*1024);
  296. if (flist->pool_boundary == ptr)
  297. flist->pool_boundary = NULL; /* list didn't use any pool memory */
  298. else
  299. flist->pool_boundary = ptr;
  300. }
  301. /* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
  302. * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
  303. * with dir == NULL taken to be the starting directory, and dirlen < 0
  304. * indicating that strdup(dir) should be called and then the -dirlen length
  305. * value checked to ensure that it is not daemon-excluded. */
  306. int change_pathname(struct file_struct *file, const char *dir, int dirlen)
  307. {
  308. if (dirlen < 0) {
  309. char *cpy = strdup(dir);
  310. if (*cpy != '/')
  311. change_dir(orig_dir, CD_SKIP_CHDIR);
  312. if (path_is_daemon_excluded(cpy, 0))
  313. goto chdir_error;
  314. dir = cpy;
  315. dirlen = -dirlen;
  316. } else {
  317. if (file) {
  318. if (pathname == F_PATHNAME(file))
  319. return 1;
  320. dir = F_PATHNAME(file);
  321. if (dir)
  322. dirlen = strlen(dir);
  323. } else if (pathname == dir)
  324. return 1;
  325. if (dir && *dir != '/')
  326. change_dir(orig_dir, CD_SKIP_CHDIR);
  327. }
  328. pathname = dir;
  329. pathname_len = dirlen;
  330. if (!dir)
  331. dir = orig_dir;
  332. if (!change_dir(dir, CD_NORMAL)) {
  333. chdir_error:
  334. io_error |= IOERR_GENERAL;
  335. rsyserr(FERROR_XFER, errno, "change_dir %s failed", full_fname(dir));
  336. if (dir != orig_dir)
  337. change_dir(orig_dir, CD_NORMAL);
  338. pathname = NULL;
  339. pathname_len = 0;
  340. return 0;
  341. }
  342. return 1;
  343. }
  344. static void send_file_entry(int f, const char *fname, struct file_struct *file,
  345. #ifdef SUPPORT_LINKS
  346. const char *symlink_name, int symlink_len,
  347. #endif
  348. int ndx, int first_ndx)
  349. {
  350. static time_t modtime;
  351. static mode_t mode;
  352. #ifdef SUPPORT_HARD_LINKS
  353. static int64 dev;
  354. #endif
  355. static dev_t rdev;
  356. static uint32 rdev_major;
  357. static uid_t uid;
  358. static gid_t gid;
  359. static const char *user_name, *group_name;
  360. static char lastname[MAXPATHLEN];
  361. int first_hlink_ndx = -1;
  362. int l1, l2;
  363. int xflags;
  364. /* Initialize starting value of xflags. */
  365. if (protocol_version >= 30 && S_ISDIR(file->mode)) {
  366. dir_count++;
  367. if (file->flags & FLAG_CONTENT_DIR)
  368. xflags = file->flags & FLAG_TOP_DIR;
  369. else if (file->flags & FLAG_IMPLIED_DIR)
  370. xflags = XMIT_TOP_DIR | XMIT_NO_CONTENT_DIR;
  371. else
  372. xflags = XMIT_NO_CONTENT_DIR;
  373. } else
  374. xflags = file->flags & FLAG_TOP_DIR; /* FLAG_TOP_DIR == XMIT_TOP_DIR */
  375. if (file->mode == mode)
  376. xflags |= XMIT_SAME_MODE;
  377. else
  378. mode = file->mode;
  379. if ((preserve_devices && IS_DEVICE(mode))
  380. || (preserve_specials && IS_SPECIAL(mode))) {
  381. if (protocol_version < 28) {
  382. if (tmp_rdev == rdev)
  383. xflags |= XMIT_SAME_RDEV_pre28;
  384. else
  385. rdev = tmp_rdev;
  386. } else {
  387. rdev = tmp_rdev;
  388. if ((uint32)major(rdev) == rdev_major)
  389. xflags |= XMIT_SAME_RDEV_MAJOR;
  390. else
  391. rdev_major = major(rdev);
  392. if (protocol_version < 30 && (uint32)minor(rdev) <= 0xFFu)
  393. xflags |= XMIT_RDEV_MINOR_8_pre30;
  394. }
  395. } else if (protocol_version < 28)
  396. rdev = MAKEDEV(0, 0);
  397. if (!preserve_uid || ((uid_t)F_OWNER(file) == uid && *lastname))
  398. xflags |= XMIT_SAME_UID;
  399. else {
  400. uid = F_OWNER(file);
  401. if (!numeric_ids) {
  402. user_name = add_uid(uid);
  403. if (inc_recurse && user_name)
  404. xflags |= XMIT_USER_NAME_FOLLOWS;
  405. }
  406. }
  407. if (!preserve_gid || ((gid_t)F_GROUP(file) == gid && *lastname))
  408. xflags |= XMIT_SAME_GID;
  409. else {
  410. gid = F_GROUP(file);
  411. if (!numeric_ids) {
  412. group_name = add_gid(gid);
  413. if (inc_recurse && group_name)
  414. xflags |= XMIT_GROUP_NAME_FOLLOWS;
  415. }
  416. }
  417. if (file->modtime == modtime)
  418. xflags |= XMIT_SAME_TIME;
  419. else
  420. modtime = file->modtime;
  421. #ifdef SUPPORT_HARD_LINKS
  422. if (tmp_dev != 0) {
  423. if (protocol_version >= 30) {
  424. struct ht_int64_node *np = idev_find(tmp_dev, tmp_ino);
  425. first_hlink_ndx = (int32)(long)np->data - 1;
  426. if (first_hlink_ndx < 0) {
  427. np->data = (void*)(long)(first_ndx + ndx + 1);
  428. xflags |= XMIT_HLINK_FIRST;
  429. }
  430. } else {
  431. if (tmp_dev == dev) {
  432. if (protocol_version >= 28)
  433. xflags |= XMIT_SAME_DEV_pre30;
  434. } else
  435. dev = tmp_dev;
  436. }
  437. xflags |= XMIT_HLINKED;
  438. }
  439. #endif
  440. for (l1 = 0;
  441. lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
  442. l1++) {}
  443. l2 = strlen(fname+l1);
  444. if (l1 > 0)
  445. xflags |= XMIT_SAME_NAME;
  446. if (l2 > 255)
  447. xflags |= XMIT_LONG_NAME;
  448. /* We must make sure we don't send a zero flag byte or the
  449. * other end will terminate the flist transfer. Note that
  450. * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
  451. * it's harmless way to add a bit to the first flag byte. */
  452. if (protocol_version >= 28) {
  453. if (!xflags && !S_ISDIR(mode))
  454. xflags |= XMIT_TOP_DIR;
  455. if ((xflags & 0xFF00) || !xflags) {
  456. xflags |= XMIT_EXTENDED_FLAGS;
  457. write_shortint(f, xflags);
  458. } else
  459. write_byte(f, xflags);
  460. } else {
  461. if (!(xflags & 0xFF))
  462. xflags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
  463. write_byte(f, xflags);
  464. }
  465. if (xflags & XMIT_SAME_NAME)
  466. write_byte(f, l1);
  467. if (xflags & XMIT_LONG_NAME)
  468. write_varint30(f, l2);
  469. else
  470. write_byte(f, l2);
  471. write_buf(f, fname + l1, l2);
  472. if (first_hlink_ndx >= 0) {
  473. write_varint(f, first_hlink_ndx);
  474. if (first_hlink_ndx >= first_ndx)
  475. goto the_end;
  476. }
  477. write_varlong30(f, F_LENGTH(file), 3);
  478. if (!(xflags & XMIT_SAME_TIME)) {
  479. if (protocol_version >= 30)
  480. write_varlong(f, modtime, 4);
  481. else
  482. write_int(f, modtime);
  483. }
  484. if (!(xflags & XMIT_SAME_MODE))
  485. write_int(f, to_wire_mode(mode));
  486. if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
  487. if (protocol_version < 30)
  488. write_int(f, uid);
  489. else {
  490. write_varint(f, uid);
  491. if (xflags & XMIT_USER_NAME_FOLLOWS) {
  492. int len = strlen(user_name);
  493. write_byte(f, len);
  494. write_buf(f, user_name, len);
  495. }
  496. }
  497. }
  498. if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
  499. if (protocol_version < 30)
  500. write_int(f, gid);
  501. else {
  502. write_varint(f, gid);
  503. if (xflags & XMIT_GROUP_NAME_FOLLOWS) {
  504. int len = strlen(group_name);
  505. write_byte(f, len);
  506. write_buf(f, group_name, len);
  507. }
  508. }
  509. }
  510. if ((preserve_devices && IS_DEVICE(mode))
  511. || (preserve_specials && IS_SPECIAL(mode))) {
  512. if (protocol_version < 28) {
  513. if (!(xflags & XMIT_SAME_RDEV_pre28))
  514. write_int(f, (int)rdev);
  515. } else {
  516. if (!(xflags & XMIT_SAME_RDEV_MAJOR))
  517. write_varint30(f, major(rdev));
  518. if (protocol_version >= 30)
  519. write_varint(f, minor(rdev));
  520. else if (xflags & XMIT_RDEV_MINOR_8_pre30)
  521. write_byte(f, minor(rdev));
  522. else
  523. write_int(f, minor(rdev));
  524. }
  525. }
  526. #ifdef SUPPORT_LINKS
  527. if (symlink_len) {
  528. write_varint30(f, symlink_len);
  529. write_buf(f, symlink_name, symlink_len);
  530. }
  531. #endif
  532. #ifdef SUPPORT_HARD_LINKS
  533. if (tmp_dev != 0 && protocol_version < 30) {
  534. if (protocol_version < 26) {
  535. /* 32-bit dev_t and ino_t */
  536. write_int(f, (int32)dev);
  537. write_int(f, (int32)tmp_ino);
  538. } else {
  539. /* 64-bit dev_t and ino_t */
  540. if (!(xflags & XMIT_SAME_DEV_pre30))
  541. write_longint(f, dev);
  542. write_longint(f, tmp_ino);
  543. }
  544. }
  545. #endif
  546. if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
  547. const char *sum;
  548. if (S_ISREG(mode))
  549. sum = tmp_sum;
  550. else {
  551. /* Prior to 28, we sent a useless set of nulls. */
  552. sum = empty_sum;
  553. }
  554. write_buf(f, sum, checksum_len);
  555. }
  556. the_end:
  557. strlcpy(lastname, fname, MAXPATHLEN);
  558. if (S_ISREG(mode) || S_ISLNK(mode))
  559. stats.total_size += F_LENGTH(file);
  560. }
  561. static struct file_struct *recv_file_entry(struct file_list *flist,
  562. int xflags, int f)
  563. {
  564. static int64 modtime;
  565. static mode_t mode;
  566. #ifdef SUPPORT_HARD_LINKS
  567. static int64 dev;
  568. #endif
  569. static dev_t rdev;
  570. static uint32 rdev_major;
  571. static uid_t uid;
  572. static gid_t gid;
  573. static uint16 gid_flags;
  574. static char lastname[MAXPATHLEN], *lastdir;
  575. static int lastdir_depth, lastdir_len = -1;
  576. static unsigned int del_hier_name_len = 0;
  577. static int in_del_hier = 0;
  578. char thisname[MAXPATHLEN];
  579. unsigned int l1 = 0, l2 = 0;
  580. int alloc_len, basename_len, linkname_len;
  581. int extra_len = file_extra_cnt * EXTRA_LEN;
  582. int first_hlink_ndx = -1;
  583. int64 file_length;
  584. const char *basename;
  585. struct file_struct *file;
  586. alloc_pool_t *pool;
  587. char *bp;
  588. if (xflags & XMIT_SAME_NAME)
  589. l1 = read_byte(f);
  590. if (xflags & XMIT_LONG_NAME)
  591. l2 = read_varint30(f);
  592. else
  593. l2 = read_byte(f);
  594. if (l2 >= MAXPATHLEN - l1) {
  595. rprintf(FERROR,
  596. "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
  597. xflags, l1, l2, lastname, who_am_i());
  598. overflow_exit("recv_file_entry");
  599. }
  600. strlcpy(thisname, lastname, l1 + 1);
  601. read_sbuf(f, &thisname[l1], l2);
  602. thisname[l1 + l2] = 0;
  603. /* Abuse basename_len for a moment... */
  604. basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
  605. #ifdef ICONV_OPTION
  606. if (ic_recv != (iconv_t)-1) {
  607. xbuf outbuf, inbuf;
  608. INIT_CONST_XBUF(outbuf, thisname);
  609. INIT_XBUF(inbuf, lastname, basename_len, -1);
  610. if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
  611. io_error |= IOERR_GENERAL;
  612. rprintf(FERROR_UTF8,
  613. "[%s] cannot convert filename: %s (%s)\n",
  614. who_am_i(), lastname, strerror(errno));
  615. outbuf.len = 0;
  616. }
  617. thisname[outbuf.len] = '\0';
  618. }
  619. #endif
  620. if (*thisname)
  621. clean_fname(thisname, 0);
  622. if (sanitize_paths)
  623. sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
  624. if ((basename = strrchr(thisname, '/')) != NULL) {
  625. int len = basename++ - thisname;
  626. if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
  627. lastdir = new_array(char, len + 1);
  628. memcpy(lastdir, thisname, len);
  629. lastdir[len] = '\0';
  630. lastdir_len = len;
  631. lastdir_depth = count_dir_elements(lastdir);
  632. }
  633. } else
  634. basename = thisname;
  635. basename_len = strlen(basename) + 1; /* count the '\0' */
  636. #ifdef SUPPORT_HARD_LINKS
  637. if (protocol_version >= 30
  638. && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
  639. first_hlink_ndx = read_varint(f);
  640. if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->ndx_start + flist->used) {
  641. rprintf(FERROR,
  642. "hard-link reference out of range: %d (%d)\n",
  643. first_hlink_ndx, flist->ndx_start + flist->used);
  644. exit_cleanup(RERR_PROTOCOL);
  645. }
  646. if (first_hlink_ndx >= flist->ndx_start) {
  647. struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
  648. file_length = F_LENGTH(first);
  649. modtime = first->modtime;
  650. mode = first->mode;
  651. if (preserve_uid)
  652. uid = F_OWNER(first);
  653. if (preserve_gid)
  654. gid = F_GROUP(first);
  655. if ((preserve_devices && IS_DEVICE(mode))
  656. || (preserve_specials && IS_SPECIAL(mode))) {
  657. uint32 *devp = F_RDEV_P(first);
  658. rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
  659. extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
  660. }
  661. if (preserve_links && S_ISLNK(mode))
  662. linkname_len = strlen(F_SYMLINK(first)) + 1;
  663. else
  664. linkname_len = 0;
  665. goto create_object;
  666. }
  667. }
  668. #endif
  669. file_length = read_varlong30(f, 3);
  670. if (!(xflags & XMIT_SAME_TIME)) {
  671. if (protocol_version >= 30) {
  672. modtime = read_varlong(f, 4);
  673. #if SIZEOF_TIME_T < SIZEOF_INT64
  674. if (!am_generator && (int64)(time_t)modtime != modtime) {
  675. rprintf(FERROR_XFER,
  676. "Time value of %s truncated on receiver.\n",
  677. lastname);
  678. }
  679. #endif
  680. } else
  681. modtime = read_int(f);
  682. }
  683. if (!(xflags & XMIT_SAME_MODE))
  684. mode = from_wire_mode(read_int(f));
  685. if (chmod_modes && !S_ISLNK(mode))
  686. mode = tweak_mode(mode, chmod_modes);
  687. if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
  688. if (protocol_version < 30)
  689. uid = (uid_t)read_int(f);
  690. else {
  691. uid = (uid_t)read_varint(f);
  692. if (xflags & XMIT_USER_NAME_FOLLOWS)
  693. uid = recv_user_name(f, uid);
  694. else if (inc_recurse && am_root && !numeric_ids)
  695. uid = match_uid(uid);
  696. }
  697. }
  698. if (preserve_gid && !(xflags & XMIT_SAME_GID)) {
  699. if (protocol_version < 30)
  700. gid = (gid_t)read_int(f);
  701. else {
  702. gid = (gid_t)read_varint(f);
  703. gid_flags = 0;
  704. if (xflags & XMIT_GROUP_NAME_FOLLOWS)
  705. gid = recv_group_name(f, gid, &gid_flags);
  706. else if (inc_recurse && (!am_root || !numeric_ids))
  707. gid = match_gid(gid, &gid_flags);
  708. }
  709. }
  710. if ((preserve_devices && IS_DEVICE(mode))
  711. || (preserve_specials && IS_SPECIAL(mode))) {
  712. if (protocol_version < 28) {
  713. if (!(xflags & XMIT_SAME_RDEV_pre28))
  714. rdev = (dev_t)read_int(f);
  715. } else {
  716. uint32 rdev_minor;
  717. if (!(xflags & XMIT_SAME_RDEV_MAJOR))
  718. rdev_major = read_varint30(f);
  719. if (protocol_version >= 30)
  720. rdev_minor = read_varint(f);
  721. else if (xflags & XMIT_RDEV_MINOR_8_pre30)
  722. rdev_minor = read_byte(f);
  723. else
  724. rdev_minor = read_int(f);
  725. rdev = MAKEDEV(rdev_major, rdev_minor);
  726. }
  727. extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
  728. file_length = 0;
  729. } else if (protocol_version < 28)
  730. rdev = MAKEDEV(0, 0);
  731. #ifdef SUPPORT_LINKS
  732. if (preserve_links && S_ISLNK(mode)) {
  733. linkname_len = read_varint30(f) + 1; /* count the '\0' */
  734. if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
  735. rprintf(FERROR, "overflow: linkname_len=%d\n",
  736. linkname_len - 1);
  737. overflow_exit("recv_file_entry");
  738. }
  739. #ifdef ICONV_OPTION
  740. /* We don't know how much extra room we need to convert
  741. * the as-yet-unread symlink data, so let's hope that a
  742. * double-size buffer is plenty. */
  743. if (sender_symlink_iconv)
  744. linkname_len *= 2;
  745. #endif
  746. if (munge_symlinks)
  747. linkname_len += SYMLINK_PREFIX_LEN;
  748. }
  749. else
  750. #endif
  751. linkname_len = 0;
  752. #ifdef SUPPORT_HARD_LINKS
  753. create_object:
  754. if (preserve_hard_links) {
  755. if (protocol_version < 28 && S_ISREG(mode))
  756. xflags |= XMIT_HLINKED;
  757. if (xflags & XMIT_HLINKED)
  758. extra_len += (inc_recurse+1) * EXTRA_LEN;
  759. }
  760. #endif
  761. #ifdef SUPPORT_ACLS
  762. /* Directories need an extra int32 for the default ACL. */
  763. if (preserve_acls && S_ISDIR(mode))
  764. extra_len += EXTRA_LEN;
  765. #endif
  766. if (always_checksum && S_ISREG(mode))
  767. extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
  768. #if SIZEOF_INT64 >= 8
  769. if (file_length > 0xFFFFFFFFu && S_ISREG(mode))
  770. extra_len += EXTRA_LEN;
  771. #endif
  772. if (file_length < 0) {
  773. rprintf(FERROR, "Offset underflow: file-length is negative\n");
  774. exit_cleanup(RERR_UNSUPPORTED);
  775. }
  776. if (inc_recurse && S_ISDIR(mode)) {
  777. if (one_file_system) {
  778. /* Room to save the dir's device for -x */
  779. extra_len += DEV_EXTRA_CNT * EXTRA_LEN;
  780. }
  781. pool = dir_flist->file_pool;
  782. } else
  783. pool = flist->file_pool;
  784. #if EXTRA_ROUNDING > 0
  785. if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
  786. extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
  787. #endif
  788. alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
  789. + linkname_len;
  790. bp = pool_alloc(pool, alloc_len, "recv_file_entry");
  791. memset(bp, 0, extra_len + FILE_STRUCT_LEN);
  792. bp += extra_len;
  793. file = (struct file_struct *)bp;
  794. bp += FILE_STRUCT_LEN;
  795. memcpy(bp, basename, basename_len);
  796. #ifdef SUPPORT_HARD_LINKS
  797. if (xflags & XMIT_HLINKED)
  798. file->flags |= FLAG_HLINKED;
  799. #endif
  800. file->modtime = (time_t)modtime;
  801. file->len32 = (uint32)file_length;
  802. #if SIZEOF_INT64 >= 8
  803. if (file_length > 0xFFFFFFFFu && S_ISREG(mode)) {
  804. #if SIZEOF_CAPITAL_OFF_T < 8
  805. rprintf(FERROR, "Offset overflow: attempted 64-bit file-length\n");
  806. exit_cleanup(RERR_UNSUPPORTED);
  807. #else
  808. file->flags |= FLAG_LENGTH64;
  809. OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
  810. #endif
  811. }
  812. #endif
  813. file->mode = mode;
  814. if (preserve_uid)
  815. F_OWNER(file) = uid;
  816. if (preserve_gid) {
  817. F_GROUP(file) = gid;
  818. file->flags |= gid_flags;
  819. }
  820. if (unsort_ndx)
  821. F_NDX(file) = flist->used + flist->ndx_start;
  822. if (basename != thisname) {
  823. file->dirname = lastdir;
  824. F_DEPTH(file) = lastdir_depth + 1;
  825. } else
  826. F_DEPTH(file) = 1;
  827. if (S_ISDIR(mode)) {
  828. if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
  829. F_DEPTH(file)--;
  830. if (protocol_version >= 30) {
  831. if (!(xflags & XMIT_NO_CONTENT_DIR)) {
  832. if (xflags & XMIT_TOP_DIR)
  833. file->flags |= FLAG_TOP_DIR;
  834. file->flags |= FLAG_CONTENT_DIR;
  835. } else if (xflags & XMIT_TOP_DIR)
  836. file->flags |= FLAG_IMPLIED_DIR;
  837. } else if (xflags & XMIT_TOP_DIR) {
  838. in_del_hier = recurse;
  839. del_hier_name_len = F_DEPTH(file) == 0 ? 0 : l1 + l2;
  840. if (relative_paths && del_hier_name_len > 2
  841. && lastname[del_hier_name_len-1] == '.'
  842. && lastname[del_hier_name_len-2] == '/')
  843. del_hier_name_len -= 2;
  844. file->flags |= FLAG_TOP_DIR | FLAG_CONTENT_DIR;
  845. } else if (in_del_hier) {
  846. if (!relative_paths || !del_hier_name_len
  847. || (l1 >= del_hier_name_len
  848. && lastname[del_hier_name_len] == '/'))
  849. file->flags |= FLAG_CONTENT_DIR;
  850. else
  851. in_del_hier = 0;
  852. }
  853. }
  854. if ((preserve_devices && IS_DEVICE(mode))
  855. || (preserve_specials && IS_SPECIAL(mode))) {
  856. uint32 *devp = F_RDEV_P(file);
  857. DEV_MAJOR(devp) = major(rdev);
  858. DEV_MINOR(devp) = minor(rdev);
  859. }
  860. #ifdef SUPPORT_LINKS
  861. if (linkname_len) {
  862. bp += basename_len;
  863. if (first_hlink_ndx >= flist->ndx_start) {
  864. struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
  865. memcpy(bp, F_SYMLINK(first), linkname_len);
  866. } else {
  867. if (munge_symlinks) {
  868. strlcpy(bp, SYMLINK_PREFIX, linkname_len);
  869. bp += SYMLINK_PREFIX_LEN;
  870. linkname_len -= SYMLINK_PREFIX_LEN;
  871. }
  872. #ifdef ICONV_OPTION
  873. if (sender_symlink_iconv) {
  874. xbuf outbuf, inbuf;
  875. alloc_len = linkname_len;
  876. linkname_len /= 2;
  877. /* Read the symlink data into the end of our double-sized
  878. * buffer and then convert it into the right spot. */
  879. INIT_XBUF(inbuf, bp + alloc_len - linkname_len,
  880. linkname_len - 1, (size_t)-1);
  881. read_sbuf(f, inbuf.buf, inbuf.len);
  882. INIT_XBUF(outbuf, bp, 0, alloc_len);
  883. if (iconvbufs(ic_recv, &inbuf, &outbuf, 0) < 0) {
  884. io_error |= IOERR_GENERAL;
  885. rprintf(FERROR_XFER,
  886. "[%s] cannot convert symlink data for: %s (%s)\n",
  887. who_am_i(), full_fname(thisname), strerror(errno));
  888. bp = (char*)file->basename;
  889. *bp++ = '\0';
  890. outbuf.len = 0;
  891. }
  892. bp[outbuf.len] = '\0';
  893. } else
  894. #endif
  895. read_sbuf(f, bp, linkname_len - 1);
  896. if (sanitize_paths && !munge_symlinks && *bp)
  897. sanitize_path(bp, bp, "", lastdir_depth, SP_DEFAULT);
  898. }
  899. }
  900. #endif
  901. #ifdef SUPPORT_HARD_LINKS
  902. if (preserve_hard_links && xflags & XMIT_HLINKED) {
  903. if (protocol_version >= 30) {
  904. if (xflags & XMIT_HLINK_FIRST) {
  905. F_HL_GNUM(file) = flist->ndx_start + flist->used;
  906. } else
  907. F_HL_GNUM(file) = first_hlink_ndx;
  908. } else {
  909. static int32 cnt = 0;
  910. struct ht_int64_node *np;
  911. int64 ino;
  912. int32 ndx;
  913. if (protocol_version < 26) {
  914. dev = read_int(f);
  915. ino = read_int(f);
  916. } else {
  917. if (!(xflags & XMIT_SAME_DEV_pre30))
  918. dev = read_longint(f);
  919. ino = read_longint(f);
  920. }
  921. np = idev_find(dev, ino);
  922. ndx = (int32)(long)np->data - 1;
  923. if (ndx < 0) {
  924. ndx = cnt++;
  925. np->data = (void*)(long)cnt;
  926. }
  927. F_HL_GNUM(file) = ndx;
  928. }
  929. }
  930. #endif
  931. if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
  932. if (S_ISREG(mode))
  933. bp = F_SUM(file);
  934. else {
  935. /* Prior to 28, we get a useless set of nulls. */
  936. bp = tmp_sum;
  937. }
  938. if (first_hlink_ndx >= flist->ndx_start) {
  939. struct file_struct *first = flist->files[first_hlink_ndx - flist->ndx_start];
  940. memcpy(bp, F_SUM(first), checksum_len);
  941. } else
  942. read_buf(f, bp, checksum_len);
  943. }
  944. #ifdef SUPPORT_ACLS
  945. if (preserve_acls && !S_ISLNK(mode))
  946. receive_acl(file, f);
  947. #endif
  948. #ifdef SUPPORT_XATTRS
  949. if (preserve_xattrs)
  950. receive_xattr(file, f );
  951. #endif
  952. if (S_ISREG(mode) || S_ISLNK(mode))
  953. stats.total_size += file_length;
  954. return file;
  955. }
  956. /* Create a file_struct for a named file by reading its stat() information
  957. * and performing extensive checks against global options.
  958. *
  959. * Returns a pointer to the new file struct, or NULL if there was an error
  960. * or this file should be excluded.
  961. *
  962. * Note: Any error (here or in send_file_name) that results in the omission of
  963. * an existent source file from the file list should set
  964. * "io_error |= IOERR_GENERAL" to avoid deletion of the file from the
  965. * destination if --delete is on. */
  966. struct file_struct *make_file(const char *fname, struct file_list *flist,
  967. STRUCT_STAT *stp, int flags, int filter_level)
  968. {
  969. static char *lastdir;
  970. static int lastdir_len = -1;
  971. struct file_struct *file;
  972. char thisname[MAXPATHLEN];
  973. char linkname[MAXPATHLEN];
  974. int alloc_len, basename_len, linkname_len;
  975. int extra_len = file_extra_cnt * EXTRA_LEN;
  976. const char *basename;
  977. alloc_pool_t *pool;
  978. STRUCT_STAT st;
  979. char *bp;
  980. if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
  981. io_error |= IOERR_GENERAL;
  982. rprintf(FERROR_XFER, "skipping overly long name: %s\n", fname);
  983. return NULL;
  984. }
  985. clean_fname(thisname, 0);
  986. if (sanitize_paths)
  987. sanitize_path(thisname, thisname, "", 0, SP_DEFAULT);
  988. if (stp && S_ISDIR(stp->st_mode)) {
  989. st = *stp; /* Needed for "symlink/." with --relative. */
  990. *linkname = '\0'; /* make IBM code checker happy */
  991. } else if (readlink_stat(thisname, &st, linkname) != 0) {
  992. int save_errno = errno;
  993. /* See if file is excluded before reporting an error. */
  994. if (filter_level != NO_FILTERS
  995. && (is_excluded(thisname, 0, filter_level)
  996. || is_excluded(thisname, 1, filter_level))) {
  997. if (ignore_perishable && save_errno != ENOENT)
  998. non_perishable_cnt++;
  999. return NULL;
  1000. }
  1001. if (save_errno == ENOENT) {
  1002. #ifdef SUPPORT_LINKS
  1003. /* When our options tell us to follow a symlink that
  1004. * points nowhere, tell the user about the symlink
  1005. * instead of giving a "vanished" message. We only
  1006. * dereference a symlink if one of the --copy*links
  1007. * options was specified, so there's no need for the
  1008. * extra lstat() if one of these options isn't on. */
  1009. if ((copy_links || copy_unsafe_links || copy_dirlinks)
  1010. && x_lstat(thisname, &st, NULL) == 0
  1011. && S_ISLNK(st.st_mode)) {
  1012. io_error |= IOERR_GENERAL;
  1013. rprintf(FERROR_XFER, "symlink has no referent: %s\n",
  1014. full_fname(thisname));
  1015. } else
  1016. #endif
  1017. {
  1018. enum logcode c = am_daemon && protocol_version < 28
  1019. ? FERROR : FWARNING;
  1020. io_error |= IOERR_VANISHED;
  1021. rprintf(c, "file has vanished: %s\n",
  1022. full_fname(thisname));
  1023. }
  1024. } else {
  1025. io_error |= IOERR_GENERAL;
  1026. rsyserr(FERROR_XFER, save_errno, "readlink_stat(%s) failed",
  1027. full_fname(thisname));
  1028. }
  1029. return NULL;
  1030. }
  1031. if (filter_level == NO_FILTERS)
  1032. goto skip_filters;
  1033. if (S_ISDIR(st.st_mode)) {
  1034. if (!xfer_dirs) {
  1035. rprintf(FINFO, "skipping directory %s\n", thisname);
  1036. return NULL;
  1037. }
  1038. /* -x only affects dirs because we need to avoid recursing
  1039. * into a mount-point directory, not to avoid copying a
  1040. * symlinked file if -L (or similar) was specified. */
  1041. if (one_file_system && st.st_dev != filesystem_dev
  1042. && BITS_SETnUNSET(flags, FLAG_CONTENT_DIR, FLAG_TOP_DIR)) {
  1043. if (one_file_system > 1) {
  1044. if (verbose > 1) {
  1045. rprintf(FINFO,
  1046. "[%s] skipping mount-point dir %s\n",
  1047. who_am_i(), thisname);
  1048. }
  1049. return NULL;
  1050. }
  1051. flags |= FLAG_MOUNT_DIR;
  1052. flags &= ~FLAG_CONTENT_DIR;
  1053. }
  1054. } else
  1055. flags &= ~FLAG_CONTENT_DIR;
  1056. if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
  1057. if (ignore_perishable)
  1058. non_perishable_cnt++;
  1059. return NULL;
  1060. }
  1061. if (lp_ignore_nonreadable(module_id)) {
  1062. #ifdef SUPPORT_LINKS
  1063. if (!S_ISLNK(st.st_mode))
  1064. #endif
  1065. if (access(thisname, R_OK) != 0)
  1066. return NULL;
  1067. }
  1068. skip_filters:
  1069. /* Only divert a directory in the main transfer. */
  1070. if (flist) {
  1071. if (flist->prev && S_ISDIR(st.st_mode)
  1072. && flags & FLAG_DIVERT_DIRS) {
  1073. /* Room for parent/sibling/next-child info. */
  1074. extra_len += DIRNODE_EXTRA_CNT * EXTRA_LEN;
  1075. if (relative_paths)
  1076. extra_len += PTR_EXTRA_CNT * EXTRA_LEN;
  1077. pool = dir_flist->file_pool;
  1078. } else
  1079. pool = flist->file_pool;
  1080. } else {
  1081. #ifdef SUPPORT_ACLS
  1082. /* Directories need an extra int32 for the default ACL. */
  1083. if (preserve_acls && S_ISDIR(st.st_mode))
  1084. extra_len += EXTRA_LEN;
  1085. #endif
  1086. pool = NULL;
  1087. }
  1088. if (verbose > 2) {
  1089. rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
  1090. who_am_i(), thisname, filter_level);
  1091. }
  1092. if ((basename = strrchr(thisname, '/')) != NULL) {
  1093. int len = basename++ - thisname;
  1094. if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
  1095. lastdir = new_array(char, len + 1);
  1096. memcpy(lastdir, thisname, len);
  1097. lastdir[len] = '\0';
  1098. lastdir_len = len;
  1099. }
  1100. } else
  1101. basename = thisname;
  1102. basename_len = strlen(basename) + 1; /* count the '\0' */
  1103. #ifdef SUPPORT_LINKS
  1104. linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0;
  1105. #else
  1106. linkname_len = 0;
  1107. #endif
  1108. #if SIZEOF_CAPITAL_OFF_T >= 8
  1109. if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode))
  1110. extra_len += EXTRA_LEN;
  1111. #endif
  1112. #if EXTRA_ROUNDING > 0
  1113. if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
  1114. extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
  1115. #endif
  1116. alloc_len = FILE_STRUCT_LEN + extra_len + basename_len
  1117. + linkname_len;
  1118. if (pool)
  1119. bp = pool_alloc(pool, alloc_len, "make_file");
  1120. else {
  1121. if (!(bp = new_array(char, alloc_len)))
  1122. out_of_memory("make_file");
  1123. }
  1124. memset(bp, 0, extra_len + FILE_STRUCT_LEN);
  1125. bp += extra_len;
  1126. file = (struct file_struct *)bp;
  1127. bp += FILE_STRUCT_LEN;
  1128. memcpy(bp, basename, basename_len);
  1129. #ifdef SUPPORT_HARD_LINKS
  1130. if (preserve_hard_links && flist && flist->prev) {
  1131. if (protocol_version >= 28
  1132. ? (!S_ISDIR(st.st_mode) && st.st_nlink > 1)
  1133. : S_ISREG(st.st_mode)) {
  1134. tmp_dev = (int64)st.st_dev + 1;
  1135. tmp_ino = (int64)st.st_ino;
  1136. } else
  1137. tmp_dev = 0;
  1138. }
  1139. #endif
  1140. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  1141. if (IS_DEVICE(st.st_mode) || IS_SPECIAL(st.st_mode)) {
  1142. tmp_rdev = st.st_rdev;
  1143. st.st_size = 0;
  1144. }
  1145. #endif
  1146. file->flags = flags;
  1147. file->modtime = st.st_mtime;
  1148. file->len32 = (uint32)st.st_size;
  1149. #if SIZEOF_CAPITAL_OFF_T >= 8
  1150. if (st.st_size > 0xFFFFFFFFu && S_ISREG(st.st_mode)) {
  1151. file->flags |= FLAG_LENGTH64;
  1152. OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
  1153. }
  1154. #endif
  1155. file->mode = st.st_mode;
  1156. if (uid_ndx) /* Check uid_ndx instead of preserve_uid for del support */
  1157. F_OWNER(file) = st.st_uid;
  1158. if (gid_ndx) /* Check gid_ndx instead of preserve_gid for del support */
  1159. F_GROUP(file) = st.st_gid;
  1160. if (basename != thisname)
  1161. file->dirname = lastdir;
  1162. #ifdef SUPPORT_LINKS
  1163. if (linkname_len)
  1164. memcpy(bp + basename_len, linkname, linkname_len);
  1165. #endif
  1166. if (always_checksum && am_sender && S_ISREG(st.st_mode))
  1167. file_checksum(thisname, tmp_sum, st.st_size);
  1168. if (am_sender)
  1169. F_PATHNAME(file) = pathname;
  1170. else if (!pool)
  1171. F_DEPTH(file) = extra_len / EXTRA_LEN;
  1172. /* This code is only used by the receiver when it is building
  1173. * a list of files for a delete pass. */
  1174. if (keep_dirlinks && linkname_len && flist) {
  1175. STRUCT_STAT st2;
  1176. int save_mode = file->mode;
  1177. file->mode = S_IFDIR; /* Find a directory with our name. */
  1178. if (flist_find(dir_flist, file) >= 0
  1179. && x_stat(thisname, &st2, NULL) == 0 && S_ISDIR(st2.st_mode)) {
  1180. file->modtime = st2.st_mtime;
  1181. file->len32 = 0;
  1182. file->mode = st2.st_mode;
  1183. if (uid_ndx)
  1184. F_OWNER(file) = st2.st_uid;
  1185. if (gid_ndx)
  1186. F_GROUP(file) = st2.st_gid;
  1187. } else
  1188. file->mode = save_mode;
  1189. }
  1190. if (basename_len == 0+1) {
  1191. if (!pool)
  1192. unmake_file(file);
  1193. return NULL;
  1194. }
  1195. if (unsort_ndx)
  1196. F_NDX(file) = dir_count;
  1197. return file;
  1198. }
  1199. /* Only called for temporary file_struct entries created by make_file(). */
  1200. void unmake_file(struct file_struct *file)
  1201. {
  1202. free(REQ_EXTRA(file, F_DEPTH(file)));
  1203. }
  1204. static struct file_struct *send_file_name(int f, struct file_list *flist,
  1205. const char *fname, STRUCT_STAT *stp,
  1206. int flags, int filter_level)
  1207. {
  1208. struct file_struct *file;
  1209. file = make_file(fname, flist, stp, flags, filter_level);
  1210. if (!file)
  1211. return NULL;
  1212. if (chmod_modes && !S_ISLNK(file->mode))
  1213. file->mode = tweak_mode(file->mode, chmod_modes);
  1214. if (f >= 0) {
  1215. char fbuf[MAXPATHLEN];
  1216. #ifdef SUPPORT_LINKS
  1217. const char *symlink_name;
  1218. int symlink_len;
  1219. #ifdef ICONV_OPTION
  1220. char symlink_buf[MAXPATHLEN];
  1221. #endif
  1222. #endif
  1223. #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
  1224. stat_x sx;
  1225. #endif
  1226. #ifdef SUPPORT_LINKS
  1227. if (preserve_links && S_ISLNK(file->mode)) {
  1228. symlink_name = F_SYMLINK(file);
  1229. symlink_len = strlen(symlink_name);
  1230. } else {
  1231. symlink_name = NULL;
  1232. symlink_len = 0;
  1233. }
  1234. #endif
  1235. #ifdef ICONV_OPTION
  1236. if (ic_send != (iconv_t)-1) {
  1237. xbuf outbuf, inbuf;
  1238. INIT_CONST_XBUF(outbuf, fbuf);
  1239. if (file->dirname) {
  1240. INIT_XBUF_STRLEN(inbuf, (char*)file->dirname);
  1241. outbuf.size -= 2; /* Reserve room for '/' & 1 more char. */
  1242. if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0)
  1243. goto convert_error;
  1244. outbuf.size += 2;
  1245. fbuf[outbuf.len++] = '/';
  1246. }
  1247. INIT_XBUF_STRLEN(inbuf, (char*)file->basename);
  1248. if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
  1249. convert_error:
  1250. io_error |= IOERR_GENERAL;
  1251. rprintf(FERROR_XFER,
  1252. "[%s] cannot convert filename: %s (%s)\n",
  1253. who_am_i(), f_name(file, fbuf), strerror(errno));
  1254. return NULL;
  1255. }
  1256. fbuf[outbuf.len] = '\0';
  1257. #ifdef SUPPORT_LINKS
  1258. if (symlink_len && sender_symlink_iconv) {
  1259. INIT_XBUF(inbuf, (char*)symlink_name, symlink_len, (size_t)-1);
  1260. INIT_CONST_XBUF(outbuf, symlink_buf);
  1261. if (iconvbufs(ic_send, &inbuf, &outbuf, 0) < 0) {
  1262. io_error |= IOERR_GENERAL;
  1263. f_name(file, fbuf);
  1264. rprintf(FERROR_XFER,
  1265. "[%s] cannot convert symlink data for: %s (%s)\n",
  1266. who_am_i(), full_fname(fbuf), strerror(errno));
  1267. return NULL;
  1268. }
  1269. symlink_buf[outbuf.len] = '\0';
  1270. symlink_name = symlink_buf;
  1271. symlink_len = outbuf.len;
  1272. }
  1273. #endif
  1274. } else
  1275. #endif
  1276. f_name(file, fbuf);
  1277. #ifdef SUPPORT_ACLS
  1278. if (preserve_acls && !S_ISLNK(file->mode)) {
  1279. sx.st.st_mode = file->mode;
  1280. sx.acc_acl = sx.def_acl = NULL;
  1281. if (get_acl(fname, &sx) < 0) {
  1282. io_error |= IOERR_GENERAL;
  1283. return NULL;
  1284. }
  1285. }
  1286. #endif
  1287. #ifdef SUPPORT_XATTRS
  1288. if (preserve_xattrs) {
  1289. sx.xattr = NULL;
  1290. if (get_xattr(fname, &sx) < 0) {
  1291. io_error |= IOERR_GENERAL;
  1292. return NULL;
  1293. }
  1294. }
  1295. #endif
  1296. send_file_entry(f, fbuf, file,
  1297. #ifdef SUPPORT_LINKS
  1298. symlink_name, symlink_len,
  1299. #endif
  1300. flist->used, flist->ndx_start);
  1301. #ifdef SUPPORT_ACLS
  1302. if (preserve_acls && !S_ISLNK(file->mode)) {
  1303. send_acl(&sx, f);
  1304. free_acl(&sx);
  1305. }
  1306. #endif
  1307. #ifdef SUPPORT_XATTRS
  1308. if (preserve_xattrs) {
  1309. F_XATTR(file) = send_xattr(&sx, f);
  1310. free_xattr(&sx);
  1311. }
  1312. #endif
  1313. }
  1314. maybe_emit_filelist_progress(flist->used + flist_count_offset);
  1315. flist_expand(flist, 1);
  1316. flist->files[flist->used++] = file;
  1317. return file;
  1318. }
  1319. static void send_if_directory(int f, struct file_list *flist,
  1320. struct file_struct *file,
  1321. char *fbuf, unsigned int ol,
  1322. int flags)
  1323. {
  1324. char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
  1325. if (S_ISDIR(file->mode)
  1326. && !(file->flags & FLAG_MOUNT_DIR) && f_name(file, fbuf)) {
  1327. void *save_filters;
  1328. unsigned int len = strlen(fbuf);
  1329. if (len > 1 && fbuf[len-1] == '/')
  1330. fbuf[--len] = '\0';
  1331. if (len >= MAXPATHLEN - 1) {
  1332. io_error |= IOERR_GENERAL;
  1333. rprintf(FERROR_XFER, "skipping long-named directory: %s\n",
  1334. full_fname(fbuf));
  1335. return;
  1336. }
  1337. save_filters = push_local_filters(fbuf, len);
  1338. send_directory(f, flist, fbuf, len, flags);
  1339. pop_local_filters(save_filters);
  1340. fbuf[ol] = '\0';
  1341. if (is_dot_dir)
  1342. fbuf[ol-1] = '.';
  1343. }
  1344. }
  1345. static int file_compare(const void *file1, const void *file2)
  1346. {
  1347. return f_name_cmp(*(struct file_struct **)file1,
  1348. *(struct file_struct **)file2);
  1349. }
  1350. /* The guts of a merge-sort algorithm. This was derived from the glibc
  1351. * version, but I (Wayne) changed the merge code to do less copying and
  1352. * to require only half the amount of temporary memory. */
  1353. static void fsort_tmp(struct file_struct **fp, size_t num,
  1354. struct file_struct **tmp)
  1355. {
  1356. struct file_struct **f1, **f2, **t;
  1357. size_t n1, n2;
  1358. n1 = num / 2;
  1359. n2 = num - n1;
  1360. f1 = fp;
  1361. f2 = fp + n1;
  1362. if (n1 > 1)
  1363. fsort_tmp(f1, n1, tmp);
  1364. if (n2 > 1)
  1365. fsort_tmp(f2, n2, tmp);
  1366. while (f_name_cmp(*f1, *f2) <= 0) {
  1367. if (!--n1)
  1368. return;
  1369. f1++;
  1370. }
  1371. t = tmp;
  1372. memcpy(t, f1, n1 * PTR_SIZE);
  1373. *f1++ = *f2++, n2--;
  1374. while (n1 > 0 && n2 > 0) {
  1375. if (f_name_cmp(*t, *f2) <= 0)
  1376. *f1++ = *t++, n1--;
  1377. else
  1378. *f1++ = *f2++, n2--;
  1379. }
  1380. if (n1 > 0)
  1381. memcpy(f1, t, n1 * PTR_SIZE);
  1382. }
  1383. /* This file-struct sorting routine makes sure that any identical names in
  1384. * the file list stay in the same order as they were in the original list.
  1385. * This is particularly vital in inc_recurse mode where we expect a sort
  1386. * on the flist to match the exact order of a sort on the dir_flist. */
  1387. static void fsort(struct file_struct **fp, size_t num)
  1388. {
  1389. if (num <= 1)
  1390. return;
  1391. if (use_qsort)
  1392. qsort(fp, num, PTR_SIZE, file_compare);
  1393. else {
  1394. struct file_struct **tmp = new_array(struct file_struct *,
  1395. (num+1) / 2);
  1396. fsort_tmp(fp, num, tmp);
  1397. free(tmp);
  1398. }
  1399. }
  1400. /* We take an entire set of sibling dirs from the sorted flist and link them
  1401. * into the tree, setting the appropriate parent/child/sibling pointers. */
  1402. static void add_dirs_to_tree(int parent_ndx, struct file_list *from_flist,
  1403. int dir_cnt)
  1404. {
  1405. int i;
  1406. int32 *dp = NULL;
  1407. int32 *parent_dp = parent_ndx < 0 ? NULL
  1408. : F_DIR_NODE_P(dir_flist->sorted[parent_ndx]);
  1409. flist_expand(dir_flist, dir_cnt);
  1410. dir_flist->sorted = dir_flist->files;
  1411. for (i = 0; dir_cnt; i++) {
  1412. struct file_struct *file = from_flist->sorted[i];
  1413. if (!S_ISDIR(file->mode))
  1414. continue;
  1415. dir_flist->files[dir_flist->used++] = file;
  1416. dir_cnt--;
  1417. if (file->basename[0] == '.' && file->basename[1] == '\0')
  1418. continue;
  1419. if (dp)
  1420. DIR_NEXT_SIBLING(dp) = dir_flist->used - 1;
  1421. else if (parent_dp)
  1422. DIR_FIRST_CHILD(parent_dp) = dir_flist->used - 1;
  1423. else
  1424. send_dir_ndx = dir_flist->used - 1;
  1425. dp = F_DIR_NODE_P(file);
  1426. DIR_PARENT(dp) = parent_ndx;
  1427. DIR_FIRST_CHILD(dp) = -1;
  1428. }
  1429. if (dp)
  1430. DIR_NEXT_SIBLING(dp) = -1;
  1431. }
  1432. /* This function is normally called by the sender, but the receiving side also
  1433. * calls it from get_dirlist() with f set to -1 so that we just construct the
  1434. * file list in memory without sending it over the wire. Also, get_dirlist()
  1435. * might call this with f set to -2, which also indicates that local filter
  1436. * rules should be ignored. */
  1437. static void send_directory(int f, struct file_list *flist, char *fbuf, int len,
  1438. int flags)
  1439. {
  1440. struct dirent *di;
  1441. unsigned remainder;
  1442. char *p;
  1443. DIR *d;
  1444. int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
  1445. int start = flist->used;
  1446. int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
  1447. assert(flist != NULL);
  1448. if (!(d = opendir(fbuf))) {
  1449. io_error |= IOERR_GENERAL;
  1450. rsyserr(FERROR_XFER, errno, "opendir %s failed", full_fname(fbuf));
  1451. return;
  1452. }
  1453. p = fbuf + len;
  1454. if (len != 1 || *fbuf != '/')
  1455. *p++ = '/';
  1456. *p = '\0';
  1457. remainder = MAXPATHLEN - (p - fbuf);
  1458. for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) {
  1459. char *dname = d_name(di);
  1460. if (dname[0] == '.' && (dname[1] == '\0'
  1461. || (dname[1] == '.' && dname[2] == '\0')))
  1462. continue;
  1463. if (strlcpy(p, dname, remainder) >= remainder) {
  1464. io_error |= IOERR_GENERAL;
  1465. rprintf(FERROR_XFER,
  1466. "cannot send long-named file %s\n",
  1467. full_fname(fbuf));
  1468. continue;
  1469. }
  1470. if (dname[0] == '\0') {
  1471. io_error |= IOERR_GENERAL;
  1472. rprintf(FERROR_XFER,
  1473. "cannot send file with empty name in %s\n",
  1474. full_fname(fbuf));
  1475. continue;
  1476. }
  1477. send_file_name(f, flist, fbuf, NULL, flags, filter_level);
  1478. }
  1479. fbuf[len] = '\0';
  1480. if (errno) {
  1481. io_error |= IOERR_GENERAL;
  1482. rsyserr(FERROR_XFER, errno, "readdir(%s)", full_fname(fbuf));
  1483. }
  1484. closedir(d);
  1485. if (f >= 0 && recurse && !divert_dirs) {
  1486. int i, end = flist->used - 1;
  1487. /* send_if_directory() bumps flist->used, so use "end". */
  1488. for (i = start; i <= end; i++)
  1489. send_if_directory(f, flist, flist->files[i], fbuf, len, flags);
  1490. }
  1491. }
  1492. static void send_implied_dirs(int f, struct file_list *flist, char *fname,
  1493. char *start, char *limit, int flags, char name_type)
  1494. {
  1495. static char lastpath[MAXPATHLEN] = "";
  1496. static int lastpath_len = 0;
  1497. static struct file_struct *lastpath_struct = NULL;
  1498. struct file_struct *file;
  1499. item_list *relname_list;
  1500. relnamecache **rnpp;
  1501. int len, need_new_dir, depth = 0;
  1502. struct filter_list_struct save_filter_list = filter_list;
  1503. flags = (flags | FLAG_IMPLIED_DIR) & ~(FLAG_TOP_DIR | FLAG_CONTENT_DIR);
  1504. filter_list.head = filter_list.tail = NULL; /* Don't filter implied dirs. */
  1505. if (inc_recurse) {
  1506. if (lastpath_struct && F_PATHNAME(lastpath_struct) == pathname
  1507. && lastpath_len == limit - fname
  1508. && strncmp(lastpath, fname, lastpath_len) == 0)
  1509. need_new_dir = 0;
  1510. else
  1511. need_new_dir = 1;
  1512. } else {
  1513. char *tp = fname, *lp = lastpath;
  1514. /* Skip any initial directories in our path that we
  1515. * have in common with lastpath. */
  1516. assert(start == fname);
  1517. for ( ; ; tp++, lp++) {
  1518. if (tp == limit) {
  1519. if (*lp == '/' || *lp == '\0')
  1520. goto done;
  1521. break;
  1522. }
  1523. if (*lp != *tp)
  1524. break;
  1525. if (*tp == '/') {
  1526. start = tp;
  1527. depth++;
  1528. }
  1529. }
  1530. need_new_dir = 1;
  1531. }
  1532. if (need_new_dir) {
  1533. int save_copy_links = copy_links;
  1534. int save_xfer_dirs = xfer_dirs;
  1535. char *slash;
  1536. copy_links = xfer_dirs = 1;
  1537. *limit = '\0';
  1538. for (slash = start; (slash = strchr(slash+1, '/')) != NULL; ) {
  1539. *slash = '\0';
  1540. file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
  1541. depth++;
  1542. if (!inc_recurse && file && S_ISDIR(file->mode))
  1543. change_local_filter_dir(fname, strlen(fname), depth);
  1544. *slash = '/';
  1545. }
  1546. file = send_file_name(f, flist, fname, NULL, flags, ALL_FILTERS);
  1547. if (inc_recurse) {
  1548. if (file && !S_ISDIR(file->mode))
  1549. file = NULL;
  1550. lastpath_struct = file;
  1551. } else if (file && S_ISDIR(file->mode))
  1552. change_local_filter_dir(fname, strlen(fname), ++depth);
  1553. strlcpy(lastpath, fname, sizeof lastpath);
  1554. lastpath_len = limit - fname;
  1555. *limit = '/';
  1556. copy_links = save_copy_links;
  1557. xfer_dirs = save_xfer_dirs;
  1558. if (!inc_recurse)
  1559. goto done;
  1560. }
  1561. if (!lastpath_struct)
  1562. goto done; /* dir must have vanished */
  1563. len = strlen(limit+1);
  1564. memcpy(&relname_list, F_DIR_RELNAMES_P(lastpath_struct), sizeof relname_list);
  1565. if (!relname_list) {
  1566. if (!(relname_list = new0(item_list)))
  1567. out_of_memory("send_implied_dirs");
  1568. memcpy(F_DIR_RELNAMES_P(lastpath_struct), &relname_list, sizeof relname_list);
  1569. }
  1570. rnpp = EXPAND_ITEM_LIST(relname_list, relnamecache *, 32);
  1571. if (!(*rnpp = (relnamecache*)new_array(char, sizeof (relnamecache) + len)))
  1572. out_of_memory("send_implied_dirs");
  1573. (*rnpp)->name_type = name_type;
  1574. strlcpy((*rnpp)->fname, limit+1, len + 1);
  1575. done:
  1576. filter_list = save_filter_list;
  1577. }
  1578. static void send1extra(int f, struct file_struct *file, struct file_list *flist)
  1579. {
  1580. char fbuf[MAXPATHLEN];
  1581. item_list *relname_list;
  1582. int len, dlen, flags = FLAG_DIVERT_DIRS | FLAG_CONTENT_DIR;
  1583. size_t j;
  1584. f_name(file, fbuf);
  1585. dlen = strlen(fbuf);
  1586. if (!change_pathname(file, NULL, 0))
  1587. exit_cleanup(RERR_FILESELECT);
  1588. change_local_filter_dir(fbuf, dlen, send_dir_depth);
  1589. if (file->flags & FLAG_CONTENT_DIR) {
  1590. if (one_file_system) {
  1591. STRUCT_STAT st;
  1592. if (link_stat(fbuf, &st, copy_dirlinks) != 0) {
  1593. io_error |= IOERR_GENERAL;
  1594. rsyserr(FERROR_XFER, errno, "link_stat %s failed",
  1595. full_fname(fbuf));
  1596. return;
  1597. }
  1598. filesystem_dev = st.st_dev;
  1599. }
  1600. send_directory(f, flist, fbuf, dlen, flags);
  1601. }
  1602. if (!relative_paths)
  1603. return;
  1604. memcpy(&relname_list, F_DIR_RELNAMES_P(file), sizeof relname_list);
  1605. if (!relname_list)
  1606. return;
  1607. for (j = 0; j < relname_list->count; j++) {
  1608. char *slash;
  1609. relnamecache *rnp = ((relnamecache**)relname_list->items)[j];
  1610. char name_type = rnp->name_type;
  1611. fbuf[dlen] = '/';
  1612. len = strlcpy(fbuf + dlen + 1, rnp->fname, sizeof fbuf - dlen - 1);
  1613. free(rnp);
  1614. if (len >= (int)sizeof fbuf)
  1615. continue; /* Impossible... */
  1616. slash = strchr(fbuf+dlen+1, '/');
  1617. if (slash) {
  1618. send_implied_dirs(f, flist, fbuf, fbuf+dlen+1, slash, flags, name_type);
  1619. continue;
  1620. }
  1621. if (name_type != NORMAL_NAME) {
  1622. STRUCT_STAT st;
  1623. if (link_stat(fbuf, &st, 1) != 0) {
  1624. io_error |= IOERR_GENERAL;
  1625. rsyserr(FERROR_XFER, errno, "link_stat %s failed",
  1626. full_fname(fbuf));
  1627. continue;
  1628. }
  1629. send_file_name(f, flist, fbuf, &st, FLAG_TOP_DIR | flags, ALL_FILTERS);
  1630. } else
  1631. send_file_name(f, flist, fbuf, NULL, FLAG_TOP_DIR | flags, ALL_FILTERS);
  1632. }
  1633. free(relname_list);
  1634. }
  1635. void send_extra_file_list(int f, int at_least)
  1636. {
  1637. struct file_list *flist;
  1638. int64 start_write;
  1639. uint16 prev_flags;
  1640. int old_cnt, save_io_error = io_error;
  1641. if (flist_eof)
  1642. return;
  1643. /* Keep sending data until we have the requested number of
  1644. * files in the upcoming file-lists. */
  1645. old_cnt = cur_flist->used;
  1646. for (flist = first_flist; flist != cur_flist; flist = flist->next)
  1647. old_cnt += flist->used;
  1648. while (file_total - old_cnt < at_least) {
  1649. struct file_struct *file = dir_flist->sorted[send_dir_ndx];
  1650. int dir_ndx, dstart = dir_count;
  1651. const char *pathname = F_PATHNAME(file);
  1652. int32 *dp;
  1653. flist = flist_new(0, "send_extra_file_list");
  1654. start_write = stats.total_written;
  1655. if (unsort_ndx)
  1656. dir_ndx = F_NDX(file);
  1657. else
  1658. dir_ndx = send_dir_ndx;
  1659. write_ndx(f, NDX_FLIST_OFFSET - dir_ndx);
  1660. flist->parent_ndx = dir_ndx;
  1661. send1extra(f, file, flist);
  1662. prev_flags = file->flags;
  1663. dp = F_DIR_NODE_P(file);
  1664. /* If there are any duplicate directory names that follow, we
  1665. * send all the dirs together in one file-list. The dir_flist
  1666. * tree links all the child subdirs onto the last dup dir. */
  1667. while ((dir_ndx = DIR_NEXT_SIBLING(dp)) >= 0
  1668. && dir_flist->sorted[dir_ndx]->flags & FLAG_DUPLICATE) {
  1669. send_dir_ndx = dir_ndx;
  1670. file = dir_flist->sorted[dir_ndx];
  1671. /* Try to avoid some duplicate scanning of identical dirs. */
  1672. if (F_PATHNAME(file) == pathname && prev_flags & FLAG_CONTENT_DIR)
  1673. file->flags &= ~FLAG_CONTENT_DIR;
  1674. send1extra(f, file, flist);
  1675. prev_flags = file->flags;
  1676. dp = F_DIR_NODE_P(file);
  1677. }
  1678. write_byte(f, 0);
  1679. if (need_unsorted_flist) {
  1680. if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
  1681. out_of_memory("send_extra_file_list");
  1682. memcpy(flist->sorted, flist->files,
  1683. flist->used * sizeof (struct file_struct*));
  1684. } else
  1685. flist->sorted = flist->files;
  1686. flist_sort_and_clean(flist, 0);
  1687. add_dirs_to_tree(send_dir_ndx, flist, dir_count - dstart);
  1688. flist_done_allocating(flist);
  1689. file_total += flist->used;
  1690. stats.flist_size += stats.total_written - start_write;
  1691. stats.num_files += flist->used;
  1692. if (verbose > 3)
  1693. output_flist(flist);
  1694. if (DIR_FIRST_CHILD(dp) >= 0) {
  1695. send_dir_ndx = DIR_FIRST_CHILD(dp);
  1696. send_dir_depth++;
  1697. } else {
  1698. while (DIR_NEXT_SIBLING(dp) < 0) {
  1699. if ((send_dir_ndx = DIR_PARENT(dp)) < 0) {
  1700. write_ndx(f, NDX_FLIST_EOF);
  1701. flist_eof = 1;
  1702. change_local_filter_dir(NULL, 0, 0);
  1703. goto finish;
  1704. }
  1705. send_dir_depth--;
  1706. file = dir_flist->sorted[send_dir_ndx];
  1707. dp = F_DIR_NODE_P(file);
  1708. }
  1709. send_dir_ndx = DIR_NEXT_SIBLING(dp);
  1710. }
  1711. }
  1712. finish:
  1713. if (io_error != save_io_error && !ignore_errors)
  1714. send_msg_int(MSG_IO_ERROR, io_error);
  1715. }
  1716. struct file_list *send_file_list(int f, int argc, char *argv[])
  1717. {
  1718. static const char *lastdir;
  1719. static int lastdir_len = -1;
  1720. int len, dirlen;
  1721. STRUCT_STAT st;
  1722. char *p, *dir;
  1723. struct file_list *flist;
  1724. struct timeval start_tv, end_tv;
  1725. int64 start_write;
  1726. int use_ff_fd = 0;
  1727. int disable_buffering;
  1728. int flags = recurse ? FLAG_CONTENT_DIR : 0;
  1729. int reading_remotely = filesfrom_host != NULL;
  1730. int rl_flags = (reading_remotely ? 0 : RL_DUMP_COMMENTS)
  1731. #ifdef ICONV_OPTION
  1732. | (filesfrom_convert ? RL_CONVERT : 0)
  1733. #endif
  1734. | (eol_nulls || reading_remotely ? RL_EOL_NULLS : 0);
  1735. int implied_dot_dir = 0;
  1736. rprintf(FLOG, "building file list\n");
  1737. if (show_filelist_p())
  1738. start_filelist_progress("building file list");
  1739. else if (inc_recurse && verbose && !am_server)
  1740. rprintf(FCLIENT, "sending incremental file list\n");
  1741. start_write = stats.total_written;
  1742. gettimeofday(&start_tv, NULL);
  1743. if (!orig_dir)
  1744. orig_dir = strdup(curr_dir);
  1745. if (relative_paths && protocol_version >= 30)
  1746. implied_dirs = 1; /* We send flagged implied dirs */
  1747. #ifdef SUPPORT_HARD_LINKS
  1748. if (preserve_hard_links && protocol_version >= 30 && !cur_flist)
  1749. init_hard_links();
  1750. #endif
  1751. flist = cur_flist = flist_new(0, "send_file_list");
  1752. if (inc_recurse) {
  1753. dir_flist = flist_new(FLIST_TEMP, "send_file_list");
  1754. flags |= FLAG_DIVERT_DIRS;
  1755. } else
  1756. dir_flist = cur_flist;
  1757. disable_buffering = io_start_buffering_out(f);
  1758. if (filesfrom_fd >= 0) {
  1759. if (argv[0] && !change_dir(argv[0], CD_NORMAL)) {
  1760. rsyserr(FERROR_XFER, errno, "change_dir %s failed",
  1761. full_fname(argv[0]));
  1762. exit_cleanup(RERR_FILESELECT);
  1763. }
  1764. use_ff_fd = 1;
  1765. }
  1766. while (1) {
  1767. char fbuf[MAXPATHLEN], *fn, name_type;
  1768. if (use_ff_fd) {
  1769. if (read_line(filesfrom_fd, fbuf, sizeof fbuf, rl_flags) == 0)
  1770. break;
  1771. sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
  1772. } else {
  1773. if (argc-- == 0)
  1774. break;
  1775. strlcpy(fbuf, *argv++, MAXPATHLEN);
  1776. if (sanitize_paths)
  1777. sanitize_path(fbuf, fbuf, "", 0, SP_KEEP_DOT_DIRS);
  1778. }
  1779. len = strlen(fbuf);
  1780. if (relative_paths) {
  1781. /* We clean up fbuf below. */
  1782. name_type = NORMAL_NAME;
  1783. } else if (!len || fbuf[len - 1] == '/') {
  1784. if (len == 2 && fbuf[0] == '.') {
  1785. /* Turn "./" into just "." rather than "./." */
  1786. fbuf[--len] = '\0';
  1787. } else {
  1788. if (len + 1 >= MAXPATHLEN)
  1789. overflow_exit("send_file_list");
  1790. fbuf[len++] = '.';
  1791. fbuf[len] = '\0';
  1792. }
  1793. name_type = DOTDIR_NAME;
  1794. } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
  1795. && (len == 2 || fbuf[len-3] == '/')) {
  1796. if (len + 2 >= MAXPATHLEN)
  1797. overflow_exit("send_file_list");
  1798. fbuf[len++] = '/';
  1799. fbuf[len++] = '.';
  1800. fbuf[len] = '\0';
  1801. name_type = DOTDIR_NAME;
  1802. } else if (fbuf[len-1] == '.' && (len == 1 || fbuf[len-2] == '/'))
  1803. name_type = DOTDIR_NAME;
  1804. else
  1805. name_type = NORMAL_NAME;
  1806. dir = NULL;
  1807. if (!relative_paths) {
  1808. p = strrchr(fbuf, '/');
  1809. if (p) {
  1810. *p = '\0';
  1811. if (p == fbuf)
  1812. dir = "/";
  1813. else
  1814. dir = fbuf;
  1815. len -= p - fbuf + 1;
  1816. fn = p + 1;
  1817. } else
  1818. fn = fbuf;
  1819. } else {
  1820. if ((p = strstr(fbuf, "/./")) != NULL) {
  1821. *p = '\0';
  1822. if (p == fbuf)
  1823. dir = "/";
  1824. else {
  1825. dir = fbuf;
  1826. clean_fname(dir, 0);
  1827. }
  1828. fn = p + 3;
  1829. while (*fn == '/')
  1830. fn++;
  1831. if (!*fn)
  1832. *--fn = '\0'; /* ensure room for '.' */
  1833. } else
  1834. fn = fbuf;
  1835. /* A leading ./ can be used in relative mode to affect
  1836. * the dest dir without its name being in the path. */
  1837. if (*fn == '.' && fn[1] == '/' && !implied_dot_dir) {
  1838. send_file_name(f, flist, ".", NULL,
  1839. (flags | FLAG_IMPLIED_DIR) & ~FLAG_CONTENT_DIR,
  1840. ALL_FILTERS);
  1841. implied_dot_dir = 1;
  1842. }
  1843. len = clean_fname(fn, CFN_KEEP_TRAILING_SLASH
  1844. | CFN_DROP_TRAILING_DOT_DIR);
  1845. if (len == 1) {
  1846. if (fn[0] == '/') {
  1847. fn = "/.";
  1848. len = 2;
  1849. name_type = DOTDIR_NAME;
  1850. } else if (fn[0] == '.')
  1851. name_type = DOTDIR_NAME;
  1852. } else if (fn[len-1] == '/') {
  1853. fn[--len] = '\0';
  1854. if (len == 1 && *fn == '.')
  1855. name_type = DOTDIR_NAME;
  1856. else
  1857. name_type = SLASH_ENDING_NAME;
  1858. }
  1859. /* Reject a ".." dir in the active part of the path. */
  1860. for (p = fn; (p = strstr(p, "..")) != NULL; p += 2) {
  1861. if ((p[2] == '/' || p[2] == '\0')
  1862. && (p == fn || p[-1] == '/')) {
  1863. rprintf(FERROR,
  1864. "found \"..\" dir in relative path: %s\n",
  1865. fn);
  1866. exit_cleanup(RERR_SYNTAX);
  1867. }
  1868. }
  1869. }
  1870. if (!*fn) {
  1871. len = 1;
  1872. fn = ".";
  1873. name_type = DOTDIR_NAME;
  1874. }
  1875. dirlen = dir ? strlen(dir) : 0;
  1876. if (dirlen != lastdir_len || memcmp(lastdir, dir, dirlen) != 0) {
  1877. if (!change_pathname(NULL, dir, -dirlen))
  1878. continue;
  1879. lastdir = pathname;
  1880. lastdir_len = pathname_len;
  1881. } else if (!change_pathname(NULL, lastdir, lastdir_len))
  1882. continue;
  1883. if (fn != fbuf)
  1884. memmove(fbuf, fn, len + 1);
  1885. if (link_stat(fbuf, &st, copy_dirlinks || name_type != NORMAL_NAME) != 0
  1886. || (name_type != DOTDIR_NAME && is_daemon_excluded(fbuf, S_ISDIR(st.st_mode)))
  1887. || (relative_paths && path_is_daemon_excluded(fbuf, 1))) {
  1888. io_error |= IOERR_GENERAL;
  1889. rsyserr(FERROR_XFER, errno, "link_stat %s failed",
  1890. full_fname(fbuf));
  1891. continue;
  1892. }
  1893. /* A dot-dir should not be excluded! */
  1894. if (name_type != DOTDIR_NAME
  1895. && is_excluded(fbuf, S_ISDIR(st.st_mode) != 0, ALL_FILTERS))
  1896. continue;
  1897. if (S_ISDIR(st.st_mode) && !xfer_dirs) {
  1898. rprintf(FINFO, "skipping directory %s\n", fbuf);
  1899. continue;
  1900. }
  1901. if (inc_recurse && relative_paths && *fbuf) {
  1902. if ((p = strchr(fbuf+1, '/')) != NULL) {
  1903. if (p - fbuf == 1 && *fbuf == '.') {
  1904. if ((fn = strchr(p+1, '/')) != NULL)
  1905. p = fn;
  1906. } else
  1907. fn = p;
  1908. send_implied_dirs(f, flist, fbuf, fbuf, p, flags, name_type);
  1909. if (fn == p)
  1910. continue;
  1911. }
  1912. } else if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
  1913. /* Send the implied directories at the start of the
  1914. * source spec, so we get their permissions right. */
  1915. send_implied_dirs(f, flist, fbuf, fbuf, p, flags, 0);
  1916. }
  1917. if (one_file_system)
  1918. filesystem_dev = st.st_dev;
  1919. if (recurse || (xfer_dirs && name_type != NORMAL_NAME)) {
  1920. struct file_struct *file;
  1921. file = send_file_name(f, flist, fbuf, &st,
  1922. FLAG_TOP_DIR | FLAG_CONTENT_DIR | flags,
  1923. NO_FILTERS);
  1924. if (!file)
  1925. continue;
  1926. if (inc_recurse) {
  1927. if (name_type == DOTDIR_NAME) {
  1928. if (send_dir_depth < 0) {
  1929. send_dir_depth = 0;
  1930. change_local_filter_dir(fbuf, len, send_dir_depth);
  1931. }
  1932. send_directory(f, flist, fbuf, len, flags);
  1933. }
  1934. } else
  1935. send_if_directory(f, flist, file, fbuf, len, flags);
  1936. } else
  1937. send_file_name(f, flist, fbuf, &st, flags, NO_FILTERS);
  1938. }
  1939. gettimeofday(&end_tv, NULL);
  1940. stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
  1941. + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
  1942. if (stats.flist_buildtime == 0)
  1943. stats.flist_buildtime = 1;
  1944. start_tv = end_tv;
  1945. write_byte(f, 0); /* Indicate end of file list */
  1946. #ifdef SUPPORT_HARD_LINKS
  1947. if (preserve_hard_links && protocol_version >= 30 && !inc_recurse)
  1948. idev_destroy();
  1949. #endif
  1950. if (show_filelist_p())
  1951. finish_filelist_progress(flist);
  1952. gettimeofday(&end_tv, NULL);
  1953. stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000
  1954. + (end_tv.tv_usec - start_tv.tv_usec) / 1000;
  1955. /* When converting names, both sides keep an unsorted file-list array
  1956. * because the names will differ on the sending and receiving sides
  1957. * (both sides will use the unsorted index number for each item). */
  1958. /* Sort the list without removing any duplicates. This allows the
  1959. * receiving side to ask for whatever name it kept. For incremental
  1960. * recursion mode, the sender marks duplicate dirs so that it can
  1961. * send them together in a single file-list. */
  1962. if (need_unsorted_flist) {
  1963. if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
  1964. out_of_memory("send_file_list");
  1965. memcpy(flist->sorted, flist->files,
  1966. flist->used * sizeof (struct file_struct*));
  1967. } else
  1968. flist->sorted = flist->files;
  1969. flist_sort_and_clean(flist, 0);
  1970. file_total += flist->used;
  1971. if (numeric_ids <= 0 && !inc_recurse)
  1972. send_id_list(f);
  1973. /* send the io_error flag */
  1974. if (protocol_version < 30)
  1975. write_int(f, ignore_errors ? 0 : io_error);
  1976. else if (io_error && !ignore_errors)
  1977. send_msg_int(MSG_IO_ERROR, io_error);
  1978. if (disable_buffering)
  1979. io_end_buffering_out();
  1980. stats.flist_size = stats.total_written - start_write;
  1981. stats.num_files = flist->used;
  1982. if (verbose > 3)
  1983. output_flist(flist);
  1984. if (verbose > 2)
  1985. rprintf(FINFO, "send_file_list done\n");
  1986. if (inc_recurse) {
  1987. send_dir_depth = 1;
  1988. add_dirs_to_tree(-1, flist, dir_count);
  1989. if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
  1990. flist->parent_ndx = -1;
  1991. flist_done_allocating(flist);
  1992. if (send_dir_ndx < 0) {
  1993. write_ndx(f, NDX_FLIST_EOF);
  1994. flist_eof = 1;
  1995. }
  1996. else if (file_total == 1) {
  1997. /* If we're creating incremental file-lists and there
  1998. * was just 1 item in the first file-list, send 1 more
  1999. * file-list to check if this is a 1-file xfer. */
  2000. send_extra_file_list(f, 1);
  2001. }
  2002. }
  2003. return flist;
  2004. }
  2005. struct file_list *recv_file_list(int f)
  2006. {
  2007. struct file_list *flist;
  2008. int dstart, flags;
  2009. int64 start_read;
  2010. if (!first_flist)
  2011. rprintf(FLOG, "receiving file list\n");
  2012. if (show_filelist_p())
  2013. start_filelist_progress("receiving file list");
  2014. else if (inc_recurse && verbose && !am_server && !first_flist)
  2015. rprintf(FCLIENT, "receiving incremental file list\n");
  2016. start_read = stats.total_read;
  2017. #ifdef SUPPORT_HARD_LINKS
  2018. if (preserve_hard_links && !first_flist)
  2019. init_hard_links();
  2020. #endif
  2021. flist = flist_new(0, "recv_file_list");
  2022. if (inc_recurse) {
  2023. if (flist->ndx_start == 1)
  2024. dir_flist = flist_new(FLIST_TEMP, "recv_file_list");
  2025. dstart = dir_flist->used;
  2026. } else {
  2027. dir_flist = flist;
  2028. dstart = 0;
  2029. }
  2030. while ((flags = read_byte(f)) != 0) {
  2031. struct file_struct *file;
  2032. flist_expand(flist, 1);
  2033. if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS))
  2034. flags |= read_byte(f) << 8;
  2035. file = recv_file_entry(flist, flags, f);
  2036. if (inc_recurse && S_ISDIR(file->mode)) {
  2037. flist_expand(dir_flist, 1);
  2038. dir_flist->files[dir_flist->used++] = file;
  2039. }
  2040. flist->files[flist->used++] = file;
  2041. maybe_emit_filelist_progress(flist->used);
  2042. if (verbose > 2) {
  2043. char *name = f_name(file, NULL);
  2044. rprintf(FINFO, "recv_file_name(%s)\n", NS(name));
  2045. }
  2046. }
  2047. file_total += flist->used;
  2048. if (verbose > 2)
  2049. rprintf(FINFO, "received %d names\n", flist->used);
  2050. if (show_filelist_p())
  2051. finish_filelist_progress(flist);
  2052. if (need_unsorted_flist) {
  2053. /* Create an extra array of index pointers that we can sort for
  2054. * the generator's use (for wading through the files in sorted
  2055. * order and for calling flist_find()). We keep the "files"
  2056. * list unsorted for our exchange of index numbers with the
  2057. * other side (since their names may not sort the same). */
  2058. if (!(flist->sorted = new_array(struct file_struct *, flist->used)))
  2059. out_of_memory("recv_file_list");
  2060. memcpy(flist->sorted, flist->files,
  2061. flist->used * sizeof (struct file_struct*));
  2062. if (inc_recurse && dir_flist->used > dstart) {
  2063. static int dir_flist_malloced = 0;
  2064. if (dir_flist_malloced < dir_flist->malloced) {
  2065. dir_flist->sorted = realloc_array(dir_flist->sorted,
  2066. struct file_struct *,
  2067. dir_flist->malloced);
  2068. dir_flist_malloced = dir_flist->malloced;
  2069. }
  2070. memcpy(dir_flist->sorted + dstart, dir_flist->files + dstart,
  2071. (dir_flist->used - dstart) * sizeof (struct file_struct*));
  2072. fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
  2073. }
  2074. } else {
  2075. flist->sorted = flist->files;
  2076. if (inc_recurse && dir_flist->used > dstart) {
  2077. dir_flist->sorted = dir_flist->files;
  2078. fsort(dir_flist->sorted + dstart, dir_flist->used - dstart);
  2079. }
  2080. }
  2081. if (inc_recurse)
  2082. flist_done_allocating(flist);
  2083. else if (f >= 0)
  2084. recv_id_list(f, flist);
  2085. flist_sort_and_clean(flist, relative_paths);
  2086. if (protocol_version < 30) {
  2087. /* Recv the io_error flag */
  2088. if (ignore_errors)
  2089. read_int(f);
  2090. else
  2091. io_error |= read_int(f);
  2092. } else if (inc_recurse && flist->ndx_start == 1) {
  2093. if (!file_total || strcmp(flist->sorted[flist->low]->basename, ".") != 0)
  2094. flist->parent_ndx = -1;
  2095. }
  2096. if (verbose > 3)
  2097. output_flist(flist);
  2098. if (verbose > 2)
  2099. rprintf(FINFO, "recv_file_list done\n");
  2100. stats.flist_size += stats.total_read - start_read;
  2101. stats.num_files += flist->used;
  2102. return flist;
  2103. }
  2104. /* This is only used once by the receiver if the very first file-list
  2105. * has exactly one item in it. */
  2106. void recv_additional_file_list(int f)
  2107. {
  2108. struct file_list *flist;
  2109. int ndx = read_ndx(f);
  2110. if (ndx == NDX_FLIST_EOF) {
  2111. flist_eof = 1;
  2112. change_local_filter_dir(NULL, 0, 0);
  2113. } else {
  2114. ndx = NDX_FLIST_OFFSET - ndx;
  2115. if (ndx < 0 || ndx >= dir_flist->used) {
  2116. ndx = NDX_FLIST_OFFSET - ndx;
  2117. rprintf(FERROR,
  2118. "[%s] Invalid dir index: %d (%d - %d)\n",
  2119. who_am_i(), ndx, NDX_FLIST_OFFSET,
  2120. NDX_FLIST_OFFSET - dir_flist->used + 1);
  2121. exit_cleanup(RERR_PROTOCOL);
  2122. }
  2123. if (verbose > 3) {
  2124. rprintf(FINFO, "[%s] receiving flist for dir %d\n",
  2125. who_am_i(), ndx);
  2126. }
  2127. flist = recv_file_list(f);
  2128. flist->parent_ndx = ndx;
  2129. }
  2130. }
  2131. /* Search for an identically-named item in the file list. Note that the
  2132. * items must agree in their directory-ness, or no match is returned. */
  2133. int flist_find(struct file_list *flist, struct file_struct *f)
  2134. {
  2135. int low = flist->low, high = flist->high;
  2136. int diff, mid, mid_up;
  2137. while (low <= high) {
  2138. mid = (low + high) / 2;
  2139. if (F_IS_ACTIVE(flist->sorted[mid]))
  2140. mid_up = mid;
  2141. else {
  2142. /* Scan for the next non-empty entry using the cached
  2143. * distance values. If the value isn't fully up-to-
  2144. * date, update it. */
  2145. mid_up = mid + F_DEPTH(flist->sorted[mid]);
  2146. if (!F_IS_ACTIVE(flist->sorted[mid_up])) {
  2147. do {
  2148. mid_up += F_DEPTH(flist->sorted[mid_up]);
  2149. } while (!F_IS_ACTIVE(flist->sorted[mid_up]));
  2150. F_DEPTH(flist->sorted[mid]) = mid_up - mid;
  2151. }
  2152. if (mid_up > high) {
  2153. /* If there's nothing left above us, set high to
  2154. * a non-empty entry below us and continue. */
  2155. high = mid - (int)flist->sorted[mid]->len32;
  2156. if (!F_IS_ACTIVE(flist->sorted[high])) {
  2157. do {
  2158. high -= (int)flist->sorted[high]->len32;
  2159. } while (!F_IS_ACTIVE(flist->sorted[high]));
  2160. flist->sorted[mid]->len32 = mid - high;
  2161. }
  2162. continue;
  2163. }
  2164. }
  2165. diff = f_name_cmp(flist->sorted[mid_up], f);
  2166. if (diff == 0) {
  2167. if (protocol_version < 29
  2168. && S_ISDIR(flist->sorted[mid_up]->mode)
  2169. != S_ISDIR(f->mode))
  2170. return -1;
  2171. return mid_up;
  2172. }
  2173. if (diff < 0)
  2174. low = mid_up + 1;
  2175. else
  2176. high = mid - 1;
  2177. }
  2178. return -1;
  2179. }
  2180. /*
  2181. * Free up any resources a file_struct has allocated
  2182. * and clear the file.
  2183. */
  2184. void clear_file(struct file_struct *file)
  2185. {
  2186. /* The +1 zeros out the first char of the basename. */
  2187. memset(file, 0, FILE_STRUCT_LEN + 1);
  2188. /* In an empty entry, F_DEPTH() is an offset to the next non-empty
  2189. * entry. Likewise for len32 in the opposite direction. We assume
  2190. * that we're alone for now since flist_find() will adjust the counts
  2191. * it runs into that aren't up-to-date. */
  2192. file->len32 = F_DEPTH(file) = 1;
  2193. }
  2194. /* Allocate a new file list. */
  2195. struct file_list *flist_new(int flags, char *msg)
  2196. {
  2197. struct file_list *flist;
  2198. if (!(flist = new0(struct file_list)))
  2199. out_of_memory(msg);
  2200. if (flags & FLIST_TEMP) {
  2201. if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
  2202. out_of_memory, POOL_INTERN)))
  2203. out_of_memory(msg);
  2204. } else {
  2205. /* This is a doubly linked list with prev looping back to
  2206. * the end of the list, but the last next pointer is NULL. */
  2207. if (!first_flist) {
  2208. flist->file_pool = pool_create(NORMAL_EXTENT, 0,
  2209. out_of_memory, POOL_INTERN);
  2210. if (!flist->file_pool)
  2211. out_of_memory(msg);
  2212. flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
  2213. first_flist = cur_flist = flist->prev = flist;
  2214. } else {
  2215. struct file_list *prev = first_flist->prev;
  2216. flist->file_pool = first_flist->file_pool;
  2217. flist->ndx_start = prev->ndx_start + prev->used + 1;
  2218. flist->flist_num = prev->flist_num + 1;
  2219. flist->prev = prev;
  2220. prev->next = first_flist->prev = flist;
  2221. }
  2222. flist->pool_boundary = pool_boundary(flist->file_pool, 0);
  2223. flist_cnt++;
  2224. }
  2225. return flist;
  2226. }
  2227. /* Free up all elements in a flist. */
  2228. void flist_free(struct file_list *flist)
  2229. {
  2230. if (!flist->prev) {
  2231. /* Was FLIST_TEMP dir-list. */
  2232. } else if (flist == flist->prev) {
  2233. first_flist = cur_flist = NULL;
  2234. file_total = 0;
  2235. flist_cnt = 0;
  2236. } else {
  2237. if (flist == cur_flist)
  2238. cur_flist = flist->next;
  2239. if (flist == first_flist)
  2240. first_flist = first_flist->next;
  2241. else {
  2242. flist->prev->next = flist->next;
  2243. if (!flist->next)
  2244. flist->next = first_flist;
  2245. }
  2246. flist->next->prev = flist->prev;
  2247. file_total -= flist->used;
  2248. flist_cnt--;
  2249. }
  2250. if (!flist->prev || !flist_cnt)
  2251. pool_destroy(flist->file_pool);
  2252. else
  2253. pool_free_old(flist->file_pool, flist->pool_boundary);
  2254. if (flist->sorted && flist->sorted != flist->files)
  2255. free(flist->sorted);
  2256. free(flist->files);
  2257. free(flist);
  2258. }
  2259. /* This routine ensures we don't have any duplicate names in our file list.
  2260. * duplicate names can cause corruption because of the pipelining. */
  2261. static void flist_sort_and_clean(struct file_list *flist, int strip_root)
  2262. {
  2263. char fbuf[MAXPATHLEN];
  2264. int i, prev_i;
  2265. if (!flist)
  2266. return;
  2267. if (flist->used == 0) {
  2268. flist->high = -1;
  2269. flist->low = 0;
  2270. return;
  2271. }
  2272. fsort(flist->sorted, flist->used);
  2273. if (!am_sender || inc_recurse) {
  2274. for (i = prev_i = 0; i < flist->used; i++) {
  2275. if (F_IS_ACTIVE(flist->sorted[i])) {
  2276. prev_i = i;
  2277. break;
  2278. }
  2279. }
  2280. flist->low = prev_i;
  2281. } else {
  2282. i = prev_i = flist->used - 1;
  2283. flist->low = 0;
  2284. }
  2285. while (++i < flist->used) {
  2286. int j;
  2287. struct file_struct *file = flist->sorted[i];
  2288. if (!F_IS_ACTIVE(file))
  2289. continue;
  2290. if (f_name_cmp(file, flist->sorted[prev_i]) == 0)
  2291. j = prev_i;
  2292. else if (protocol_version >= 29 && S_ISDIR(file->mode)) {
  2293. int save_mode = file->mode;
  2294. /* Make sure that this directory doesn't duplicate a
  2295. * non-directory earlier in the list. */
  2296. flist->high = prev_i;
  2297. file->mode = S_IFREG;
  2298. j = flist_find(flist, file);
  2299. file->mode = save_mode;
  2300. } else
  2301. j = -1;
  2302. if (j >= 0) {
  2303. int keep, drop;
  2304. /* If one is a dir and the other is not, we want to
  2305. * keep the dir because it might have contents in the
  2306. * list. Otherwise keep the first one. */
  2307. if (S_ISDIR(file->mode)) {
  2308. struct file_struct *fp = flist->sorted[j];
  2309. if (!S_ISDIR(fp->mode))
  2310. keep = i, drop = j;
  2311. else {
  2312. if (am_sender)
  2313. file->flags |= FLAG_DUPLICATE;
  2314. else { /* Make sure we merge our vital flags. */
  2315. fp->flags |= file->flags & (FLAG_TOP_DIR|FLAG_CONTENT_DIR);
  2316. fp->flags &= file->flags | ~FLAG_IMPLIED_DIR;
  2317. }
  2318. keep = j, drop = i;
  2319. }
  2320. } else
  2321. keep = j, drop = i;
  2322. if (!am_sender) {
  2323. if (verbose > 1) {
  2324. rprintf(FINFO,
  2325. "removing duplicate name %s from file list (%d)\n",
  2326. f_name(file, fbuf), drop + flist->ndx_start);
  2327. }
  2328. clear_file(flist->sorted[drop]);
  2329. }
  2330. if (keep == i) {
  2331. if (flist->low == drop) {
  2332. for (j = drop + 1;
  2333. j < i && !F_IS_ACTIVE(flist->sorted[j]);
  2334. j++) {}
  2335. flist->low = j;
  2336. }
  2337. prev_i = i;
  2338. }
  2339. } else
  2340. prev_i = i;
  2341. }
  2342. flist->high = prev_i;
  2343. if (strip_root) {
  2344. /* We need to strip off the leading slashes for relative
  2345. * paths, but this must be done _after_ the sorting phase. */
  2346. for (i = flist->low; i <= flist->high; i++) {
  2347. struct file_struct *file = flist->sorted[i];
  2348. if (!file->dirname)
  2349. continue;
  2350. while (*file->dirname == '/')
  2351. file->dirname++;
  2352. if (!*file->dirname)
  2353. file->dirname = NULL;
  2354. }
  2355. }
  2356. if (prune_empty_dirs && !am_sender) {
  2357. int j, prev_depth = 0;
  2358. prev_i = 0; /* It's OK that this isn't really true. */
  2359. for (i = flist->low; i <= flist->high; i++) {
  2360. struct file_struct *fp, *file = flist->sorted[i];
  2361. /* This temporarily abuses the F_DEPTH() value for a
  2362. * directory that is in a chain that might get pruned.
  2363. * We restore the old value if it gets a reprieve. */
  2364. if (S_ISDIR(file->mode) && F_DEPTH(file)) {
  2365. /* Dump empty dirs when coming back down. */
  2366. for (j = prev_depth; j >= F_DEPTH(file); j--) {
  2367. fp = flist->sorted[prev_i];
  2368. if (F_DEPTH(fp) >= 0)
  2369. break;
  2370. prev_i = -F_DEPTH(fp)-1;
  2371. clear_file(fp);
  2372. }
  2373. prev_depth = F_DEPTH(file);
  2374. if (is_excluded(f_name(file, fbuf), 1,
  2375. ALL_FILTERS)) {
  2376. /* Keep dirs through this dir. */
  2377. for (j = prev_depth-1; ; j--) {
  2378. fp = flist->sorted[prev_i];
  2379. if (F_DEPTH(fp) >= 0)
  2380. break;
  2381. prev_i = -F_DEPTH(fp)-1;
  2382. F_DEPTH(fp) = j;
  2383. }
  2384. } else
  2385. F_DEPTH(file) = -prev_i-1;
  2386. prev_i = i;
  2387. } else {
  2388. /* Keep dirs through this non-dir. */
  2389. for (j = prev_depth; ; j--) {
  2390. fp = flist->sorted[prev_i];
  2391. if (F_DEPTH(fp) >= 0)
  2392. break;
  2393. prev_i = -F_DEPTH(fp)-1;
  2394. F_DEPTH(fp) = j;
  2395. }
  2396. }
  2397. }
  2398. /* Dump all remaining empty dirs. */
  2399. while (1) {
  2400. struct file_struct *fp = flist->sorted[prev_i];
  2401. if (F_DEPTH(fp) >= 0)
  2402. break;
  2403. prev_i = -F_DEPTH(fp)-1;
  2404. clear_file(fp);
  2405. }
  2406. for (i = flist->low; i <= flist->high; i++) {
  2407. if (F_IS_ACTIVE(flist->sorted[i]))
  2408. break;
  2409. }
  2410. flist->low = i;
  2411. for (i = flist->high; i >= flist->low; i--) {
  2412. if (F_IS_ACTIVE(flist->sorted[i]))
  2413. break;
  2414. }
  2415. flist->high = i;
  2416. }
  2417. }
  2418. static void output_flist(struct file_list *flist)
  2419. {
  2420. char uidbuf[16], gidbuf[16], depthbuf[16];
  2421. struct file_struct *file;
  2422. const char *root, *dir, *slash, *name, *trail;
  2423. const char *who = who_am_i();
  2424. int i;
  2425. rprintf(FINFO, "[%s] flist start=%d, used=%d, low=%d, high=%d\n",
  2426. who, flist->ndx_start, flist->used, flist->low, flist->high);
  2427. for (i = 0; i < flist->used; i++) {
  2428. file = flist->files[i];
  2429. if ((am_root || am_sender) && uid_ndx) {
  2430. snprintf(uidbuf, sizeof uidbuf, " uid=%u",
  2431. F_OWNER(file));
  2432. } else
  2433. *uidbuf = '\0';
  2434. if (gid_ndx) {
  2435. static char parens[] = "(\0)\0\0\0";
  2436. char *pp = parens + (file->flags & FLAG_SKIP_GROUP ? 0 : 3);
  2437. snprintf(gidbuf, sizeof gidbuf, " gid=%s%u%s",
  2438. pp, F_GROUP(file), pp + 2);
  2439. } else
  2440. *gidbuf = '\0';
  2441. if (!am_sender)
  2442. snprintf(depthbuf, sizeof depthbuf, "%d", F_DEPTH(file));
  2443. if (F_IS_ACTIVE(file)) {
  2444. root = am_sender ? NS(F_PATHNAME(file)) : depthbuf;
  2445. if ((dir = file->dirname) == NULL)
  2446. dir = slash = "";
  2447. else
  2448. slash = "/";
  2449. name = file->basename;
  2450. trail = S_ISDIR(file->mode) ? "/" : "";
  2451. } else
  2452. root = dir = slash = name = trail = "";
  2453. rprintf(FINFO,
  2454. "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n",
  2455. who, i + flist->ndx_start,
  2456. root, dir, slash, name, trail,
  2457. (int)file->mode, (double)F_LENGTH(file),
  2458. uidbuf, gidbuf, file->flags);
  2459. }
  2460. }
  2461. enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
  2462. enum fnc_type { t_PATH, t_ITEM };
  2463. static int found_prefix;
  2464. /* Compare the names of two file_struct entities, similar to how strcmp()
  2465. * would do if it were operating on the joined strings.
  2466. *
  2467. * Some differences beginning with protocol_version 29: (1) directory names
  2468. * are compared with an assumed trailing slash so that they compare in a
  2469. * way that would cause them to sort immediately prior to any content they
  2470. * may have; (2) a directory of any name compares after a non-directory of
  2471. * any name at the same depth; (3) a directory with name "." compares prior
  2472. * to anything else. These changes mean that a directory and a non-dir
  2473. * with the same name will not compare as equal (protocol_version >= 29).
  2474. *
  2475. * The dirname component can be an empty string, but the basename component
  2476. * cannot (and never is in the current codebase). The basename component
  2477. * may be NULL (for a removed item), in which case it is considered to be
  2478. * after any existing item. */
  2479. int f_name_cmp(const struct file_struct *f1, const struct file_struct *f2)
  2480. {
  2481. int dif;
  2482. const uchar *c1, *c2;
  2483. enum fnc_state state1, state2;
  2484. enum fnc_type type1, type2;
  2485. enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM;
  2486. if (!f1 || !F_IS_ACTIVE(f1)) {
  2487. if (!f2 || !F_IS_ACTIVE(f2))
  2488. return 0;
  2489. return -1;
  2490. }
  2491. if (!f2 || !F_IS_ACTIVE(f2))
  2492. return 1;
  2493. c1 = (uchar*)f1->dirname;
  2494. c2 = (uchar*)f2->dirname;
  2495. if (c1 == c2)
  2496. c1 = c2 = NULL;
  2497. if (!c1) {
  2498. type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
  2499. c1 = (const uchar*)f1->basename;
  2500. if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
  2501. type1 = t_ITEM;
  2502. state1 = s_TRAILING;
  2503. c1 = (uchar*)"";
  2504. } else
  2505. state1 = s_BASE;
  2506. } else {
  2507. type1 = t_path;
  2508. state1 = s_DIR;
  2509. }
  2510. if (!c2) {
  2511. type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
  2512. c2 = (const uchar*)f2->basename;
  2513. if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
  2514. type2 = t_ITEM;
  2515. state2 = s_TRAILING;
  2516. c2 = (uchar*)"";
  2517. } else
  2518. state2 = s_BASE;
  2519. } else {
  2520. type2 = t_path;
  2521. state2 = s_DIR;
  2522. }
  2523. if (type1 != type2)
  2524. return type1 == t_PATH ? 1 : -1;
  2525. do {
  2526. if (!*c1) {
  2527. switch (state1) {
  2528. case s_DIR:
  2529. state1 = s_SLASH;
  2530. c1 = (uchar*)"/";
  2531. break;
  2532. case s_SLASH:
  2533. type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM;
  2534. c1 = (const uchar*)f1->basename;
  2535. if (type1 == t_PATH && *c1 == '.' && !c1[1]) {
  2536. type1 = t_ITEM;
  2537. state1 = s_TRAILING;
  2538. c1 = (uchar*)"";
  2539. } else
  2540. state1 = s_BASE;
  2541. break;
  2542. case s_BASE:
  2543. state1 = s_TRAILING;
  2544. if (type1 == t_PATH) {
  2545. c1 = (uchar*)"/";
  2546. break;
  2547. }
  2548. /* FALL THROUGH */
  2549. case s_TRAILING:
  2550. type1 = t_ITEM;
  2551. break;
  2552. }
  2553. if (*c2 && type1 != type2)
  2554. return type1 == t_PATH ? 1 : -1;
  2555. }
  2556. if (!*c2) {
  2557. switch (state2) {
  2558. case s_DIR:
  2559. state2 = s_SLASH;
  2560. c2 = (uchar*)"/";
  2561. break;
  2562. case s_SLASH:
  2563. type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM;
  2564. c2 = (const uchar*)f2->basename;
  2565. if (type2 == t_PATH && *c2 == '.' && !c2[1]) {
  2566. type2 = t_ITEM;
  2567. state2 = s_TRAILING;
  2568. c2 = (uchar*)"";
  2569. } else
  2570. state2 = s_BASE;
  2571. break;
  2572. case s_BASE:
  2573. state2 = s_TRAILING;
  2574. if (type2 == t_PATH) {
  2575. c2 = (uchar*)"/";
  2576. break;
  2577. }
  2578. /* FALL THROUGH */
  2579. case s_TRAILING:
  2580. found_prefix = 1;
  2581. if (!*c1)
  2582. return 0;
  2583. type2 = t_ITEM;
  2584. break;
  2585. }
  2586. if (type1 != type2)
  2587. return type1 == t_PATH ? 1 : -1;
  2588. }
  2589. } while ((dif = (int)*c1++ - (int)*c2++) == 0);
  2590. return dif;
  2591. }
  2592. /* Returns 1 if f1's filename has all of f2's filename as a prefix. This does
  2593. * not match if f2's basename is not an exact match of a path element in f1.
  2594. * E.g. /path/foo is not a prefix of /path/foobar/baz, but /path/foobar is. */
  2595. int f_name_has_prefix(const struct file_struct *f1, const struct file_struct *f2)
  2596. {
  2597. found_prefix = 0;
  2598. f_name_cmp(f1, f2);
  2599. return found_prefix;
  2600. }
  2601. char *f_name_buf(void)
  2602. {
  2603. static char names[5][MAXPATHLEN];
  2604. static unsigned int n;
  2605. n = (n + 1) % (sizeof names / sizeof names[0]);
  2606. return names[n];
  2607. }
  2608. /* Return a copy of the full filename of a flist entry, using the indicated
  2609. * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is
  2610. * done because we checked the size when creating the file_struct entry.
  2611. */
  2612. char *f_name(const struct file_struct *f, char *fbuf)
  2613. {
  2614. if (!f || !F_IS_ACTIVE(f))
  2615. return NULL;
  2616. if (!fbuf)
  2617. fbuf = f_name_buf();
  2618. if (f->dirname) {
  2619. int len = strlen(f->dirname);
  2620. memcpy(fbuf, f->dirname, len);
  2621. fbuf[len] = '/';
  2622. strlcpy(fbuf + len + 1, f->basename, MAXPATHLEN - (len + 1));
  2623. } else
  2624. strlcpy(fbuf, f->basename, MAXPATHLEN);
  2625. return fbuf;
  2626. }
  2627. /* Do a non-recursive scan of the named directory, possibly ignoring all
  2628. * exclude rules except for the daemon's. If "dlen" is >=0, it is the length
  2629. * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN
  2630. * buffer (the functions we call will append names onto the end, but the old
  2631. * dir value will be restored on exit). */
  2632. struct file_list *get_dirlist(char *dirname, int dlen, int ignore_filter_rules)
  2633. {
  2634. struct file_list *dirlist;
  2635. char dirbuf[MAXPATHLEN];
  2636. int save_recurse = recurse;
  2637. int save_xfer_dirs = xfer_dirs;
  2638. int save_prune_empty_dirs = prune_empty_dirs;
  2639. if (dlen < 0) {
  2640. dlen = strlcpy(dirbuf, dirname, MAXPATHLEN);
  2641. if (dlen >= MAXPATHLEN)
  2642. return NULL;
  2643. dirname = dirbuf;
  2644. }
  2645. dirlist = flist_new(FLIST_TEMP, "get_dirlist");
  2646. recurse = 0;
  2647. xfer_dirs = 1;
  2648. send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen, 0);
  2649. xfer_dirs = save_xfer_dirs;
  2650. recurse = save_recurse;
  2651. if (do_progress)
  2652. flist_count_offset += dirlist->used;
  2653. prune_empty_dirs = 0;
  2654. dirlist->sorted = dirlist->files;
  2655. flist_sort_and_clean(dirlist, 0);
  2656. prune_empty_dirs = save_prune_empty_dirs;
  2657. if (verbose > 3)
  2658. output_flist(dirlist);
  2659. return dirlist;
  2660. }