flist.c 81 KB

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