archive_write_set_format_mtree.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299
  1. /*-
  2. * Copyright (c) 2008 Joerg Sonnenberger
  3. * Copyright (c) 2009-2012 Michihiro NAKAJIMA
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h>
  29. #endif
  30. #include <errno.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "archive.h"
  34. #include "archive_digest_private.h"
  35. #include "archive_entry.h"
  36. #include "archive_entry_private.h"
  37. #include "archive_private.h"
  38. #include "archive_rb.h"
  39. #include "archive_string.h"
  40. #include "archive_write_private.h"
  41. #define INDENTNAMELEN 15
  42. #define MAXLINELEN 80
  43. #define SET_KEYS \
  44. (F_FLAGS | F_GID | F_GNAME | F_MODE | F_TYPE | F_UID | F_UNAME)
  45. struct attr_counter {
  46. struct attr_counter *prev;
  47. struct attr_counter *next;
  48. struct mtree_entry *m_entry;
  49. int count;
  50. };
  51. struct attr_counter_set {
  52. struct attr_counter *uid_list;
  53. struct attr_counter *gid_list;
  54. struct attr_counter *mode_list;
  55. struct attr_counter *flags_list;
  56. };
  57. struct mtree_chain {
  58. struct mtree_entry *first;
  59. struct mtree_entry **last;
  60. };
  61. /*
  62. * The Data only for a directory file.
  63. */
  64. struct dir_info {
  65. struct archive_rb_tree rbtree;
  66. struct mtree_chain children;
  67. struct mtree_entry *chnext;
  68. int virtual;
  69. };
  70. /*
  71. * The Data only for a regular file.
  72. */
  73. struct reg_info {
  74. int compute_sum;
  75. uint32_t crc;
  76. uint_least32_t mset_digest;
  77. struct ae_digest digest;
  78. };
  79. struct mtree_entry {
  80. struct archive_rb_node rbnode;
  81. struct mtree_entry *next;
  82. struct mtree_entry *parent;
  83. struct dir_info *dir_info;
  84. struct reg_info *reg_info;
  85. struct archive_string parentdir;
  86. struct archive_string basename;
  87. struct archive_string pathname;
  88. struct archive_string symlink;
  89. struct archive_string uname;
  90. struct archive_string gname;
  91. struct archive_string fflags_text;
  92. unsigned int nlink;
  93. mode_t filetype;
  94. mode_t mode;
  95. int64_t size;
  96. int64_t uid;
  97. int64_t gid;
  98. time_t mtime;
  99. long mtime_nsec;
  100. unsigned long fflags_set;
  101. unsigned long fflags_clear;
  102. dev_t rdevmajor;
  103. dev_t rdevminor;
  104. dev_t devmajor;
  105. dev_t devminor;
  106. int64_t ino;
  107. };
  108. struct mtree_writer {
  109. struct mtree_entry *mtree_entry;
  110. struct mtree_entry *root;
  111. struct mtree_entry *cur_dirent;
  112. struct archive_string cur_dirstr;
  113. struct mtree_chain file_list;
  114. struct archive_string ebuf;
  115. struct archive_string buf;
  116. int first;
  117. uint64_t entry_bytes_remaining;
  118. /*
  119. * Set global value.
  120. */
  121. struct {
  122. int processing;
  123. mode_t type;
  124. int keys;
  125. int64_t uid;
  126. int64_t gid;
  127. mode_t mode;
  128. unsigned long fflags_set;
  129. unsigned long fflags_clear;
  130. } set;
  131. struct attr_counter_set acs;
  132. int classic;
  133. int depth;
  134. /* check sum */
  135. int compute_sum;
  136. uint32_t crc;
  137. uint64_t crc_len;
  138. #ifdef ARCHIVE_HAS_MD5
  139. archive_md5_ctx md5ctx;
  140. #endif
  141. #ifdef ARCHIVE_HAS_RMD160
  142. archive_rmd160_ctx rmd160ctx;
  143. #endif
  144. #ifdef ARCHIVE_HAS_SHA1
  145. archive_sha1_ctx sha1ctx;
  146. #endif
  147. #ifdef ARCHIVE_HAS_SHA256
  148. archive_sha256_ctx sha256ctx;
  149. #endif
  150. #ifdef ARCHIVE_HAS_SHA384
  151. archive_sha384_ctx sha384ctx;
  152. #endif
  153. #ifdef ARCHIVE_HAS_SHA512
  154. archive_sha512_ctx sha512ctx;
  155. #endif
  156. /* Keyword options */
  157. int keys;
  158. #define F_CKSUM 0x00000001 /* checksum */
  159. #define F_DEV 0x00000002 /* device type */
  160. #define F_DONE 0x00000004 /* directory done */
  161. #define F_FLAGS 0x00000008 /* file flags */
  162. #define F_GID 0x00000010 /* gid */
  163. #define F_GNAME 0x00000020 /* group name */
  164. #define F_IGN 0x00000040 /* ignore */
  165. #define F_MAGIC 0x00000080 /* name has magic chars */
  166. #define F_MD5 0x00000100 /* MD5 digest */
  167. #define F_MODE 0x00000200 /* mode */
  168. #define F_NLINK 0x00000400 /* number of links */
  169. #define F_NOCHANGE 0x00000800 /* If owner/mode "wrong", do
  170. * not change */
  171. #define F_OPT 0x00001000 /* existence optional */
  172. #define F_RMD160 0x00002000 /* RIPEMD160 digest */
  173. #define F_SHA1 0x00004000 /* SHA-1 digest */
  174. #define F_SIZE 0x00008000 /* size */
  175. #define F_SLINK 0x00010000 /* symbolic link */
  176. #define F_TAGS 0x00020000 /* tags */
  177. #define F_TIME 0x00040000 /* modification time */
  178. #define F_TYPE 0x00080000 /* file type */
  179. #define F_UID 0x00100000 /* uid */
  180. #define F_UNAME 0x00200000 /* user name */
  181. #define F_VISIT 0x00400000 /* file visited */
  182. #define F_SHA256 0x00800000 /* SHA-256 digest */
  183. #define F_SHA384 0x01000000 /* SHA-384 digest */
  184. #define F_SHA512 0x02000000 /* SHA-512 digest */
  185. #define F_INO 0x04000000 /* inode number */
  186. #define F_RESDEV 0x08000000 /* device ID on which the
  187. * entry resides */
  188. /* Options */
  189. int dironly; /* If it is set, ignore all files except
  190. * directory files, like mtree(8) -d option. */
  191. int indent; /* If it is set, indent output data. */
  192. int output_global_set; /* If it is set, use /set keyword to set
  193. * global values. When generating mtree
  194. * classic format, it is set by default. */
  195. };
  196. #define DEFAULT_KEYS (F_DEV | F_FLAGS | F_GID | F_GNAME | F_SLINK | F_MODE\
  197. | F_NLINK | F_SIZE | F_TIME | F_TYPE | F_UID\
  198. | F_UNAME)
  199. #define attr_counter_set_reset attr_counter_set_free
  200. static void attr_counter_free(struct attr_counter **);
  201. static int attr_counter_inc(struct attr_counter **, struct attr_counter *,
  202. struct attr_counter *, struct mtree_entry *);
  203. static struct attr_counter * attr_counter_new(struct mtree_entry *,
  204. struct attr_counter *);
  205. static int attr_counter_set_collect(struct mtree_writer *,
  206. struct mtree_entry *);
  207. static void attr_counter_set_free(struct mtree_writer *);
  208. static int get_global_set_keys(struct mtree_writer *, struct mtree_entry *);
  209. static int mtree_entry_add_child_tail(struct mtree_entry *,
  210. struct mtree_entry *);
  211. static int mtree_entry_create_virtual_dir(struct archive_write *, const char *,
  212. struct mtree_entry **);
  213. static int mtree_entry_cmp_node(const struct archive_rb_node *,
  214. const struct archive_rb_node *);
  215. static int mtree_entry_cmp_key(const struct archive_rb_node *, const void *);
  216. static int mtree_entry_exchange_same_entry(struct archive_write *,
  217. struct mtree_entry *, struct mtree_entry *);
  218. static void mtree_entry_free(struct mtree_entry *);
  219. static int mtree_entry_new(struct archive_write *, struct archive_entry *,
  220. struct mtree_entry **);
  221. static void mtree_entry_register_free(struct mtree_writer *);
  222. static void mtree_entry_register_init(struct mtree_writer *);
  223. static int mtree_entry_setup_filenames(struct archive_write *,
  224. struct mtree_entry *, struct archive_entry *);
  225. static int mtree_entry_tree_add(struct archive_write *, struct mtree_entry **);
  226. static void sum_init(struct mtree_writer *);
  227. static void sum_update(struct mtree_writer *, const void *, size_t);
  228. static void sum_final(struct mtree_writer *, struct reg_info *);
  229. static void sum_write(struct archive_string *, struct reg_info *);
  230. static int write_mtree_entry(struct archive_write *, struct mtree_entry *);
  231. static int write_dot_dot_entry(struct archive_write *, struct mtree_entry *);
  232. #define COMPUTE_CRC(var, ch) (var) = (var) << 8 ^ crctab[(var) >> 24 ^ (ch)]
  233. static const uint32_t crctab[] = {
  234. 0x0,
  235. 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
  236. 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6,
  237. 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
  238. 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac,
  239. 0x5bd4b01b, 0x569796c2, 0x52568b75, 0x6a1936c8, 0x6ed82b7f,
  240. 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a,
  241. 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
  242. 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58,
  243. 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033,
  244. 0xa4ad16ea, 0xa06c0b5d, 0xd4326d90, 0xd0f37027, 0xddb056fe,
  245. 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
  246. 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4,
  247. 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
  248. 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5,
  249. 0x2ac12072, 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
  250. 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, 0x7897ab07,
  251. 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c,
  252. 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1,
  253. 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
  254. 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b,
  255. 0xbb60adfc, 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698,
  256. 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d,
  257. 0x94ea7b2a, 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
  258. 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 0xc6bcf05f,
  259. 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
  260. 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80,
  261. 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
  262. 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a,
  263. 0x58c1663d, 0x558240e4, 0x51435d53, 0x251d3b9e, 0x21dc2629,
  264. 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c,
  265. 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
  266. 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e,
  267. 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65,
  268. 0xeba91bbc, 0xef68060b, 0xd727bbb6, 0xd3e6a601, 0xdea580d8,
  269. 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
  270. 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2,
  271. 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
  272. 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74,
  273. 0x857130c3, 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
  274. 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, 0x7b827d21,
  275. 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a,
  276. 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e, 0x18197087,
  277. 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
  278. 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d,
  279. 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce,
  280. 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb,
  281. 0xdbee767c, 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
  282. 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, 0x89b8fd09,
  283. 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
  284. 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf,
  285. 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
  286. };
  287. static const unsigned char safe_char[256] = {
  288. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
  289. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
  290. /* !"$%&'()*+,-./ EXCLUSION:0x20( ) 0x23(#) */
  291. 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
  292. /* 0123456789:;<>? EXCLUSION:0x3d(=) */
  293. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
  294. /* @ABCDEFGHIJKLMNO */
  295. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
  296. /* PQRSTUVWXYZ[]^_ EXCLUSION:0x5c(\) */
  297. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, /* 50 - 5F */
  298. /* `abcdefghijklmno */
  299. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
  300. /* pqrstuvwxyz{|}~ */
  301. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
  302. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
  303. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
  304. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
  305. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
  306. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
  307. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
  308. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
  309. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
  310. };
  311. static void
  312. mtree_quote(struct archive_string *s, const char *str)
  313. {
  314. const char *start;
  315. char buf[4];
  316. unsigned char c;
  317. for (start = str; *str != '\0'; ++str) {
  318. if (safe_char[*(const unsigned char *)str])
  319. continue;
  320. if (start != str)
  321. archive_strncat(s, start, str - start);
  322. c = (unsigned char)*str;
  323. buf[0] = '\\';
  324. buf[1] = (c / 64) + '0';
  325. buf[2] = (c / 8 % 8) + '0';
  326. buf[3] = (c % 8) + '0';
  327. archive_strncat(s, buf, 4);
  328. start = str + 1;
  329. }
  330. if (start != str)
  331. archive_strncat(s, start, str - start);
  332. }
  333. /*
  334. * Indent a line as the mtree utility does so it is readable for people.
  335. */
  336. static void
  337. mtree_indent(struct mtree_writer *mtree)
  338. {
  339. int i, fn, nd, pd;
  340. const char *r, *s, *x;
  341. if (mtree->classic) {
  342. if (mtree->indent) {
  343. nd = 0;
  344. pd = mtree->depth * 4;
  345. } else {
  346. nd = mtree->depth?4:0;
  347. pd = 0;
  348. }
  349. } else
  350. nd = pd = 0;
  351. fn = 1;
  352. s = r = mtree->ebuf.s;
  353. x = NULL;
  354. while (*r == ' ')
  355. r++;
  356. while ((r = strchr(r, ' ')) != NULL) {
  357. if (fn) {
  358. fn = 0;
  359. for (i = 0; i < nd + pd; i++)
  360. archive_strappend_char(&mtree->buf, ' ');
  361. archive_strncat(&mtree->buf, s, r - s);
  362. if (nd + (r -s) > INDENTNAMELEN) {
  363. archive_strncat(&mtree->buf, " \\\n", 3);
  364. for (i = 0; i < (INDENTNAMELEN + 1 + pd); i++)
  365. archive_strappend_char(&mtree->buf, ' ');
  366. } else {
  367. for (i = (int)(r -s + nd);
  368. i < (INDENTNAMELEN + 1); i++)
  369. archive_strappend_char(&mtree->buf, ' ');
  370. }
  371. s = ++r;
  372. x = NULL;
  373. continue;
  374. }
  375. if (pd + (r - s) <= MAXLINELEN - 3 - INDENTNAMELEN)
  376. x = r++;
  377. else {
  378. if (x == NULL)
  379. x = r;
  380. archive_strncat(&mtree->buf, s, x - s);
  381. archive_strncat(&mtree->buf, " \\\n", 3);
  382. for (i = 0; i < (INDENTNAMELEN + 1 + pd); i++)
  383. archive_strappend_char(&mtree->buf, ' ');
  384. s = r = ++x;
  385. x = NULL;
  386. }
  387. }
  388. if (fn) {
  389. for (i = 0; i < nd + pd; i++)
  390. archive_strappend_char(&mtree->buf, ' ');
  391. archive_strcat(&mtree->buf, s);
  392. s += strlen(s);
  393. }
  394. if (x != NULL && pd + strlen(s) > MAXLINELEN - 3 - INDENTNAMELEN) {
  395. /* Last keyword is longer. */
  396. archive_strncat(&mtree->buf, s, x - s);
  397. archive_strncat(&mtree->buf, " \\\n", 3);
  398. for (i = 0; i < (INDENTNAMELEN + 1 + pd); i++)
  399. archive_strappend_char(&mtree->buf, ' ');
  400. s = ++x;
  401. }
  402. archive_strcat(&mtree->buf, s);
  403. archive_string_empty(&mtree->ebuf);
  404. }
  405. /*
  406. * Write /set keyword.
  407. * Set the most used value of uid, gid, mode and fflags, which are
  408. * collected by the attr_counter_set_collect() function.
  409. */
  410. static void
  411. write_global(struct mtree_writer *mtree)
  412. {
  413. struct archive_string setstr;
  414. struct archive_string unsetstr;
  415. struct attr_counter_set *acs;
  416. int keys, oldkeys, effkeys;
  417. archive_string_init(&setstr);
  418. archive_string_init(&unsetstr);
  419. keys = mtree->keys & SET_KEYS;
  420. oldkeys = mtree->set.keys;
  421. effkeys = keys;
  422. acs = &mtree->acs;
  423. if (mtree->set.processing) {
  424. /*
  425. * Check if the global data needs updating.
  426. */
  427. effkeys &= ~F_TYPE;
  428. if (acs->uid_list == NULL)
  429. effkeys &= ~(F_UNAME | F_UID);
  430. else if (oldkeys & (F_UNAME | F_UID)) {
  431. if (acs->uid_list->count < 2 ||
  432. mtree->set.uid == acs->uid_list->m_entry->uid)
  433. effkeys &= ~(F_UNAME | F_UID);
  434. }
  435. if (acs->gid_list == NULL)
  436. effkeys &= ~(F_GNAME | F_GID);
  437. else if (oldkeys & (F_GNAME | F_GID)) {
  438. if (acs->gid_list->count < 2 ||
  439. mtree->set.gid == acs->gid_list->m_entry->gid)
  440. effkeys &= ~(F_GNAME | F_GID);
  441. }
  442. if (acs->mode_list == NULL)
  443. effkeys &= ~F_MODE;
  444. else if (oldkeys & F_MODE) {
  445. if (acs->mode_list->count < 2 ||
  446. mtree->set.mode == acs->mode_list->m_entry->mode)
  447. effkeys &= ~F_MODE;
  448. }
  449. if (acs->flags_list == NULL)
  450. effkeys &= ~F_FLAGS;
  451. else if ((oldkeys & F_FLAGS) != 0) {
  452. if (acs->flags_list->count < 2 ||
  453. (acs->flags_list->m_entry->fflags_set ==
  454. mtree->set.fflags_set &&
  455. acs->flags_list->m_entry->fflags_clear ==
  456. mtree->set.fflags_clear))
  457. effkeys &= ~F_FLAGS;
  458. }
  459. } else {
  460. if (acs->uid_list == NULL)
  461. keys &= ~(F_UNAME | F_UID);
  462. if (acs->gid_list == NULL)
  463. keys &= ~(F_GNAME | F_GID);
  464. if (acs->mode_list == NULL)
  465. keys &= ~F_MODE;
  466. if (acs->flags_list == NULL)
  467. keys &= ~F_FLAGS;
  468. }
  469. if ((keys & effkeys & F_TYPE) != 0) {
  470. if (mtree->dironly) {
  471. archive_strcat(&setstr, " type=dir");
  472. mtree->set.type = AE_IFDIR;
  473. } else {
  474. archive_strcat(&setstr, " type=file");
  475. mtree->set.type = AE_IFREG;
  476. }
  477. }
  478. if ((keys & effkeys & F_UNAME) != 0) {
  479. if (archive_strlen(&(acs->uid_list->m_entry->uname)) > 0) {
  480. archive_strcat(&setstr, " uname=");
  481. mtree_quote(&setstr, acs->uid_list->m_entry->uname.s);
  482. } else {
  483. keys &= ~F_UNAME;
  484. if ((oldkeys & F_UNAME) != 0)
  485. archive_strcat(&unsetstr, " uname");
  486. }
  487. }
  488. if ((keys & effkeys & F_UID) != 0) {
  489. mtree->set.uid = acs->uid_list->m_entry->uid;
  490. archive_string_sprintf(&setstr, " uid=%jd",
  491. (intmax_t)mtree->set.uid);
  492. }
  493. if ((keys & effkeys & F_GNAME) != 0) {
  494. if (archive_strlen(&(acs->gid_list->m_entry->gname)) > 0) {
  495. archive_strcat(&setstr, " gname=");
  496. mtree_quote(&setstr, acs->gid_list->m_entry->gname.s);
  497. } else {
  498. keys &= ~F_GNAME;
  499. if ((oldkeys & F_GNAME) != 0)
  500. archive_strcat(&unsetstr, " gname");
  501. }
  502. }
  503. if ((keys & effkeys & F_GID) != 0) {
  504. mtree->set.gid = acs->gid_list->m_entry->gid;
  505. archive_string_sprintf(&setstr, " gid=%jd",
  506. (intmax_t)mtree->set.gid);
  507. }
  508. if ((keys & effkeys & F_MODE) != 0) {
  509. mtree->set.mode = acs->mode_list->m_entry->mode;
  510. archive_string_sprintf(&setstr, " mode=%o",
  511. (unsigned int)mtree->set.mode);
  512. }
  513. if ((keys & effkeys & F_FLAGS) != 0) {
  514. if (archive_strlen(
  515. &(acs->flags_list->m_entry->fflags_text)) > 0) {
  516. archive_strcat(&setstr, " flags=");
  517. mtree_quote(&setstr,
  518. acs->flags_list->m_entry->fflags_text.s);
  519. mtree->set.fflags_set =
  520. acs->flags_list->m_entry->fflags_set;
  521. mtree->set.fflags_clear =
  522. acs->flags_list->m_entry->fflags_clear;
  523. } else {
  524. keys &= ~F_FLAGS;
  525. if ((oldkeys & F_FLAGS) != 0)
  526. archive_strcat(&unsetstr, " flags");
  527. }
  528. }
  529. if (unsetstr.length > 0)
  530. archive_string_sprintf(&mtree->buf, "/unset%s\n", unsetstr.s);
  531. archive_string_free(&unsetstr);
  532. if (setstr.length > 0)
  533. archive_string_sprintf(&mtree->buf, "/set%s\n", setstr.s);
  534. archive_string_free(&setstr);
  535. mtree->set.keys = keys;
  536. mtree->set.processing = 1;
  537. }
  538. static struct attr_counter *
  539. attr_counter_new(struct mtree_entry *me, struct attr_counter *prev)
  540. {
  541. struct attr_counter *ac;
  542. ac = malloc(sizeof(*ac));
  543. if (ac != NULL) {
  544. ac->prev = prev;
  545. ac->next = NULL;
  546. ac->count = 1;
  547. ac->m_entry = me;
  548. }
  549. return (ac);
  550. }
  551. static void
  552. attr_counter_free(struct attr_counter **top)
  553. {
  554. struct attr_counter *ac, *tac;
  555. if (*top == NULL)
  556. return;
  557. ac = *top;
  558. while (ac != NULL) {
  559. tac = ac->next;
  560. free(ac);
  561. ac = tac;
  562. }
  563. *top = NULL;
  564. }
  565. static int
  566. attr_counter_inc(struct attr_counter **top, struct attr_counter *ac,
  567. struct attr_counter *last, struct mtree_entry *me)
  568. {
  569. struct attr_counter *pac;
  570. if (ac != NULL) {
  571. ac->count++;
  572. if (*top == ac || ac->prev->count >= ac->count)
  573. return (0);
  574. for (pac = ac->prev; pac; pac = pac->prev) {
  575. if (pac->count >= ac->count)
  576. break;
  577. }
  578. ac->prev->next = ac->next;
  579. if (ac->next != NULL)
  580. ac->next->prev = ac->prev;
  581. if (pac != NULL) {
  582. ac->prev = pac;
  583. ac->next = pac->next;
  584. pac->next = ac;
  585. if (ac->next != NULL)
  586. ac->next->prev = ac;
  587. } else {
  588. ac->prev = NULL;
  589. ac->next = *top;
  590. *top = ac;
  591. ac->next->prev = ac;
  592. }
  593. } else if (last != NULL) {
  594. ac = attr_counter_new(me, last);
  595. if (ac == NULL)
  596. return (-1);
  597. last->next = ac;
  598. }
  599. return (0);
  600. }
  601. /*
  602. * Tabulate uid, gid, mode and fflags of a entry in order to be used for /set.
  603. */
  604. static int
  605. attr_counter_set_collect(struct mtree_writer *mtree, struct mtree_entry *me)
  606. {
  607. struct attr_counter *ac, *last;
  608. struct attr_counter_set *acs = &mtree->acs;
  609. int keys = mtree->keys;
  610. if (keys & (F_UNAME | F_UID)) {
  611. if (acs->uid_list == NULL) {
  612. acs->uid_list = attr_counter_new(me, NULL);
  613. if (acs->uid_list == NULL)
  614. return (-1);
  615. } else {
  616. last = NULL;
  617. for (ac = acs->uid_list; ac; ac = ac->next) {
  618. if (ac->m_entry->uid == me->uid)
  619. break;
  620. last = ac;
  621. }
  622. if (attr_counter_inc(&acs->uid_list, ac, last, me) < 0)
  623. return (-1);
  624. }
  625. }
  626. if (keys & (F_GNAME | F_GID)) {
  627. if (acs->gid_list == NULL) {
  628. acs->gid_list = attr_counter_new(me, NULL);
  629. if (acs->gid_list == NULL)
  630. return (-1);
  631. } else {
  632. last = NULL;
  633. for (ac = acs->gid_list; ac; ac = ac->next) {
  634. if (ac->m_entry->gid == me->gid)
  635. break;
  636. last = ac;
  637. }
  638. if (attr_counter_inc(&acs->gid_list, ac, last, me) < 0)
  639. return (-1);
  640. }
  641. }
  642. if (keys & F_MODE) {
  643. if (acs->mode_list == NULL) {
  644. acs->mode_list = attr_counter_new(me, NULL);
  645. if (acs->mode_list == NULL)
  646. return (-1);
  647. } else {
  648. last = NULL;
  649. for (ac = acs->mode_list; ac; ac = ac->next) {
  650. if (ac->m_entry->mode == me->mode)
  651. break;
  652. last = ac;
  653. }
  654. if (attr_counter_inc(&acs->mode_list, ac, last, me) < 0)
  655. return (-1);
  656. }
  657. }
  658. if (keys & F_FLAGS) {
  659. if (acs->flags_list == NULL) {
  660. acs->flags_list = attr_counter_new(me, NULL);
  661. if (acs->flags_list == NULL)
  662. return (-1);
  663. } else {
  664. last = NULL;
  665. for (ac = acs->flags_list; ac; ac = ac->next) {
  666. if (ac->m_entry->fflags_set == me->fflags_set &&
  667. ac->m_entry->fflags_clear ==
  668. me->fflags_clear)
  669. break;
  670. last = ac;
  671. }
  672. if (attr_counter_inc(&acs->flags_list, ac, last, me) < 0)
  673. return (-1);
  674. }
  675. }
  676. return (0);
  677. }
  678. static void
  679. attr_counter_set_free(struct mtree_writer *mtree)
  680. {
  681. struct attr_counter_set *acs = &mtree->acs;
  682. attr_counter_free(&acs->uid_list);
  683. attr_counter_free(&acs->gid_list);
  684. attr_counter_free(&acs->mode_list);
  685. attr_counter_free(&acs->flags_list);
  686. }
  687. static int
  688. get_global_set_keys(struct mtree_writer *mtree, struct mtree_entry *me)
  689. {
  690. int keys;
  691. keys = mtree->keys;
  692. /*
  693. * If a keyword has been set by /set, we do not need to
  694. * output it.
  695. */
  696. if (mtree->set.keys == 0)
  697. return (keys);/* /set is not used. */
  698. if ((mtree->set.keys & (F_GNAME | F_GID)) != 0 &&
  699. mtree->set.gid == me->gid)
  700. keys &= ~(F_GNAME | F_GID);
  701. if ((mtree->set.keys & (F_UNAME | F_UID)) != 0 &&
  702. mtree->set.uid == me->uid)
  703. keys &= ~(F_UNAME | F_UID);
  704. if (mtree->set.keys & F_FLAGS) {
  705. if (mtree->set.fflags_set == me->fflags_set &&
  706. mtree->set.fflags_clear == me->fflags_clear)
  707. keys &= ~F_FLAGS;
  708. }
  709. if ((mtree->set.keys & F_MODE) != 0 && mtree->set.mode == me->mode)
  710. keys &= ~F_MODE;
  711. switch (me->filetype) {
  712. case AE_IFLNK: case AE_IFSOCK: case AE_IFCHR:
  713. case AE_IFBLK: case AE_IFIFO:
  714. break;
  715. case AE_IFDIR:
  716. if ((mtree->set.keys & F_TYPE) != 0 &&
  717. mtree->set.type == AE_IFDIR)
  718. keys &= ~F_TYPE;
  719. break;
  720. case AE_IFREG:
  721. default: /* Handle unknown file types as regular files. */
  722. if ((mtree->set.keys & F_TYPE) != 0 &&
  723. mtree->set.type == AE_IFREG)
  724. keys &= ~F_TYPE;
  725. break;
  726. }
  727. return (keys);
  728. }
  729. static int
  730. mtree_entry_new(struct archive_write *a, struct archive_entry *entry,
  731. struct mtree_entry **m_entry)
  732. {
  733. struct mtree_entry *me;
  734. const char *s;
  735. int r;
  736. static const struct archive_rb_tree_ops rb_ops = {
  737. mtree_entry_cmp_node, mtree_entry_cmp_key
  738. };
  739. me = calloc(1, sizeof(*me));
  740. if (me == NULL) {
  741. archive_set_error(&a->archive, ENOMEM,
  742. "Can't allocate memory for a mtree entry");
  743. *m_entry = NULL;
  744. return (ARCHIVE_FATAL);
  745. }
  746. r = mtree_entry_setup_filenames(a, me, entry);
  747. if (r < ARCHIVE_WARN) {
  748. mtree_entry_free(me);
  749. *m_entry = NULL;
  750. return (r);
  751. }
  752. if ((s = archive_entry_symlink(entry)) != NULL)
  753. archive_strcpy(&me->symlink, s);
  754. me->nlink = archive_entry_nlink(entry);
  755. me->filetype = archive_entry_filetype(entry);
  756. me->mode = archive_entry_mode(entry) & 07777;
  757. me->uid = archive_entry_uid(entry);
  758. me->gid = archive_entry_gid(entry);
  759. if ((s = archive_entry_uname(entry)) != NULL)
  760. archive_strcpy(&me->uname, s);
  761. if ((s = archive_entry_gname(entry)) != NULL)
  762. archive_strcpy(&me->gname, s);
  763. if ((s = archive_entry_fflags_text(entry)) != NULL)
  764. archive_strcpy(&me->fflags_text, s);
  765. archive_entry_fflags(entry, &me->fflags_set, &me->fflags_clear);
  766. me->mtime = archive_entry_mtime(entry);
  767. me->mtime_nsec = archive_entry_mtime_nsec(entry);
  768. me->rdevmajor = archive_entry_rdevmajor(entry);
  769. me->rdevminor = archive_entry_rdevminor(entry);
  770. me->devmajor = archive_entry_devmajor(entry);
  771. me->devminor = archive_entry_devminor(entry);
  772. me->ino = archive_entry_ino(entry);
  773. me->size = archive_entry_size(entry);
  774. if (me->filetype == AE_IFDIR) {
  775. me->dir_info = calloc(1, sizeof(*me->dir_info));
  776. if (me->dir_info == NULL) {
  777. mtree_entry_free(me);
  778. archive_set_error(&a->archive, ENOMEM,
  779. "Can't allocate memory for a mtree entry");
  780. *m_entry = NULL;
  781. return (ARCHIVE_FATAL);
  782. }
  783. __archive_rb_tree_init(&me->dir_info->rbtree, &rb_ops);
  784. me->dir_info->children.first = NULL;
  785. me->dir_info->children.last = &(me->dir_info->children.first);
  786. me->dir_info->chnext = NULL;
  787. } else if (me->filetype == AE_IFREG) {
  788. me->reg_info = calloc(1, sizeof(*me->reg_info));
  789. if (me->reg_info == NULL) {
  790. mtree_entry_free(me);
  791. archive_set_error(&a->archive, ENOMEM,
  792. "Can't allocate memory for a mtree entry");
  793. *m_entry = NULL;
  794. return (ARCHIVE_FATAL);
  795. }
  796. me->reg_info->compute_sum = 0;
  797. }
  798. *m_entry = me;
  799. return (ARCHIVE_OK);
  800. }
  801. static void
  802. mtree_entry_free(struct mtree_entry *me)
  803. {
  804. archive_string_free(&me->parentdir);
  805. archive_string_free(&me->basename);
  806. archive_string_free(&me->pathname);
  807. archive_string_free(&me->symlink);
  808. archive_string_free(&me->uname);
  809. archive_string_free(&me->gname);
  810. archive_string_free(&me->fflags_text);
  811. free(me->dir_info);
  812. free(me->reg_info);
  813. free(me);
  814. }
  815. static void
  816. mtree_copy_ae_digests(struct reg_info *reg, struct archive_entry *entry, int compute_sum)
  817. {
  818. reg->compute_sum = compute_sum;
  819. reg->mset_digest = entry->mset_digest;
  820. if ((reg->compute_sum & F_MD5)
  821. && (reg->mset_digest & AE_MSET_DIGEST_MD5)) {
  822. memcpy(&reg->digest.md5, entry->digest.md5,
  823. sizeof(reg->digest.md5));
  824. }
  825. if ((reg->compute_sum & F_RMD160)
  826. && (reg->mset_digest & AE_MSET_DIGEST_RMD160)) {
  827. memcpy(&reg->digest.rmd160, entry->digest.rmd160,
  828. sizeof(reg->digest.rmd160));
  829. }
  830. if ((reg->compute_sum & F_SHA1)
  831. && (reg->mset_digest & AE_MSET_DIGEST_SHA1)) {
  832. memcpy(&reg->digest.sha1, entry->digest.sha1,
  833. sizeof(reg->digest.sha1));
  834. }
  835. if ((reg->compute_sum & F_SHA256)
  836. && (reg->mset_digest & AE_MSET_DIGEST_SHA256)) {
  837. memcpy(&reg->digest.sha256, entry->digest.sha256,
  838. sizeof(reg->digest.sha256));
  839. }
  840. if ((reg->compute_sum & F_SHA384)
  841. && (reg->mset_digest & AE_MSET_DIGEST_SHA384)) {
  842. memcpy(&reg->digest.sha384, entry->digest.sha384,
  843. sizeof(reg->digest.sha384));
  844. }
  845. if ((reg->compute_sum & F_SHA512)
  846. && (reg->mset_digest & AE_MSET_DIGEST_SHA512)) {
  847. memcpy(&reg->digest.sha512, entry->digest.sha512,
  848. sizeof(reg->digest.sha512));
  849. }
  850. }
  851. static int
  852. archive_write_mtree_header(struct archive_write *a,
  853. struct archive_entry *entry)
  854. {
  855. struct mtree_writer *mtree= a->format_data;
  856. struct mtree_entry *mtree_entry;
  857. int r, r2;
  858. if (mtree->first) {
  859. mtree->first = 0;
  860. archive_strcat(&mtree->buf, "#mtree\n");
  861. if ((mtree->keys & SET_KEYS) == 0)
  862. mtree->output_global_set = 0;/* Disabled. */
  863. }
  864. mtree->entry_bytes_remaining = archive_entry_size(entry);
  865. /* While directory only mode, we do not handle non directory files. */
  866. if (mtree->dironly && archive_entry_filetype(entry) != AE_IFDIR)
  867. return (ARCHIVE_OK);
  868. r2 = mtree_entry_new(a, entry, &mtree_entry);
  869. if (r2 < ARCHIVE_WARN)
  870. return (r2);
  871. r = mtree_entry_tree_add(a, &mtree_entry);
  872. if (r < ARCHIVE_WARN) {
  873. mtree_entry_free(mtree_entry);
  874. return (r);
  875. }
  876. mtree->mtree_entry = mtree_entry;
  877. /* If the current file is a regular file, we have to
  878. * compute the sum of its content.
  879. * Initialize a bunch of checksum context. */
  880. if (mtree_entry->reg_info) {
  881. sum_init(mtree);
  882. /* honor archive_entry_set_digest() calls. These values will be
  883. * overwritten if archive_write_mtree_data() is called */
  884. mtree_copy_ae_digests(mtree_entry->reg_info, entry, mtree->compute_sum);
  885. }
  886. return (r2);
  887. }
  888. static int
  889. write_mtree_entry(struct archive_write *a, struct mtree_entry *me)
  890. {
  891. struct mtree_writer *mtree = a->format_data;
  892. struct archive_string *str;
  893. int keys, ret;
  894. if (me->dir_info) {
  895. if (mtree->classic) {
  896. /*
  897. * Output a comment line to describe the full
  898. * pathname of the entry as mtree utility does
  899. * while generating classic format.
  900. */
  901. if (!mtree->dironly)
  902. archive_strappend_char(&mtree->buf, '\n');
  903. if (me->parentdir.s)
  904. archive_string_sprintf(&mtree->buf,
  905. "# %s/%s\n",
  906. me->parentdir.s, me->basename.s);
  907. else
  908. archive_string_sprintf(&mtree->buf,
  909. "# %s\n",
  910. me->basename.s);
  911. }
  912. if (mtree->output_global_set)
  913. write_global(mtree);
  914. }
  915. archive_string_empty(&mtree->ebuf);
  916. str = (mtree->indent || mtree->classic)? &mtree->ebuf : &mtree->buf;
  917. if (!mtree->classic && me->parentdir.s) {
  918. /*
  919. * If generating format is not classic one(v1), output
  920. * a full pathname.
  921. */
  922. mtree_quote(str, me->parentdir.s);
  923. archive_strappend_char(str, '/');
  924. }
  925. mtree_quote(str, me->basename.s);
  926. keys = get_global_set_keys(mtree, me);
  927. if ((keys & F_NLINK) != 0 &&
  928. me->nlink != 1 && me->filetype != AE_IFDIR)
  929. archive_string_sprintf(str, " nlink=%u", me->nlink);
  930. if ((keys & F_GNAME) != 0 && archive_strlen(&me->gname) > 0) {
  931. archive_strcat(str, " gname=");
  932. mtree_quote(str, me->gname.s);
  933. }
  934. if ((keys & F_UNAME) != 0 && archive_strlen(&me->uname) > 0) {
  935. archive_strcat(str, " uname=");
  936. mtree_quote(str, me->uname.s);
  937. }
  938. if ((keys & F_FLAGS) != 0) {
  939. if (archive_strlen(&me->fflags_text) > 0) {
  940. archive_strcat(str, " flags=");
  941. mtree_quote(str, me->fflags_text.s);
  942. } else if (mtree->set.processing &&
  943. (mtree->set.keys & F_FLAGS) != 0)
  944. /* Overwrite the global parameter. */
  945. archive_strcat(str, " flags=none");
  946. }
  947. if ((keys & F_TIME) != 0)
  948. archive_string_sprintf(str, " time=%jd.%jd",
  949. (intmax_t)me->mtime, (intmax_t)me->mtime_nsec);
  950. if ((keys & F_MODE) != 0)
  951. archive_string_sprintf(str, " mode=%o", (unsigned int)me->mode);
  952. if ((keys & F_GID) != 0)
  953. archive_string_sprintf(str, " gid=%jd", (intmax_t)me->gid);
  954. if ((keys & F_UID) != 0)
  955. archive_string_sprintf(str, " uid=%jd", (intmax_t)me->uid);
  956. if ((keys & F_INO) != 0)
  957. archive_string_sprintf(str, " inode=%jd", (intmax_t)me->ino);
  958. if ((keys & F_RESDEV) != 0) {
  959. archive_string_sprintf(str,
  960. " resdevice=native,%ju,%ju",
  961. (uintmax_t)me->devmajor,
  962. (uintmax_t)me->devminor);
  963. }
  964. switch (me->filetype) {
  965. case AE_IFLNK:
  966. if ((keys & F_TYPE) != 0)
  967. archive_strcat(str, " type=link");
  968. if ((keys & F_SLINK) != 0) {
  969. archive_strcat(str, " link=");
  970. mtree_quote(str, me->symlink.s);
  971. }
  972. break;
  973. case AE_IFSOCK:
  974. if ((keys & F_TYPE) != 0)
  975. archive_strcat(str, " type=socket");
  976. break;
  977. case AE_IFCHR:
  978. if ((keys & F_TYPE) != 0)
  979. archive_strcat(str, " type=char");
  980. if ((keys & F_DEV) != 0) {
  981. archive_string_sprintf(str,
  982. " device=native,%ju,%ju",
  983. (uintmax_t)me->rdevmajor,
  984. (uintmax_t)me->rdevminor);
  985. }
  986. break;
  987. case AE_IFBLK:
  988. if ((keys & F_TYPE) != 0)
  989. archive_strcat(str, " type=block");
  990. if ((keys & F_DEV) != 0) {
  991. archive_string_sprintf(str,
  992. " device=native,%ju,%ju",
  993. (uintmax_t)me->rdevmajor,
  994. (uintmax_t)me->rdevminor);
  995. }
  996. break;
  997. case AE_IFDIR:
  998. if ((keys & F_TYPE) != 0)
  999. archive_strcat(str, " type=dir");
  1000. break;
  1001. case AE_IFIFO:
  1002. if ((keys & F_TYPE) != 0)
  1003. archive_strcat(str, " type=fifo");
  1004. break;
  1005. case AE_IFREG:
  1006. default: /* Handle unknown file types as regular files. */
  1007. if ((keys & F_TYPE) != 0)
  1008. archive_strcat(str, " type=file");
  1009. if ((keys & F_SIZE) != 0)
  1010. archive_string_sprintf(str, " size=%jd",
  1011. (intmax_t)me->size);
  1012. break;
  1013. }
  1014. /* Write a bunch of sum. */
  1015. if (me->reg_info)
  1016. sum_write(str, me->reg_info);
  1017. archive_strappend_char(str, '\n');
  1018. if (mtree->indent || mtree->classic)
  1019. mtree_indent(mtree);
  1020. if (mtree->buf.length > 32768) {
  1021. ret = __archive_write_output(
  1022. a, mtree->buf.s, mtree->buf.length);
  1023. archive_string_empty(&mtree->buf);
  1024. } else
  1025. ret = ARCHIVE_OK;
  1026. return (ret);
  1027. }
  1028. static int
  1029. write_dot_dot_entry(struct archive_write *a, struct mtree_entry *n)
  1030. {
  1031. struct mtree_writer *mtree = a->format_data;
  1032. int ret;
  1033. if (n->parentdir.s) {
  1034. if (mtree->indent) {
  1035. int i, pd = mtree->depth * 4;
  1036. for (i = 0; i < pd; i++)
  1037. archive_strappend_char(&mtree->buf, ' ');
  1038. }
  1039. archive_string_sprintf(&mtree->buf, "# %s/%s\n",
  1040. n->parentdir.s, n->basename.s);
  1041. }
  1042. if (mtree->indent) {
  1043. archive_string_empty(&mtree->ebuf);
  1044. archive_strncat(&mtree->ebuf, "..\n\n", (mtree->dironly)?3:4);
  1045. mtree_indent(mtree);
  1046. } else
  1047. archive_strncat(&mtree->buf, "..\n\n", (mtree->dironly)?3:4);
  1048. if (mtree->buf.length > 32768) {
  1049. ret = __archive_write_output(
  1050. a, mtree->buf.s, mtree->buf.length);
  1051. archive_string_empty(&mtree->buf);
  1052. } else
  1053. ret = ARCHIVE_OK;
  1054. return (ret);
  1055. }
  1056. /*
  1057. * Write mtree entries saved at attr_counter_set_collect() function.
  1058. */
  1059. static int
  1060. write_mtree_entry_tree(struct archive_write *a)
  1061. {
  1062. struct mtree_writer *mtree = a->format_data;
  1063. struct mtree_entry *np = mtree->root;
  1064. struct archive_rb_node *n;
  1065. int ret;
  1066. do {
  1067. if (mtree->output_global_set) {
  1068. /*
  1069. * Collect attribute information to know which value
  1070. * is frequently used among the children.
  1071. */
  1072. attr_counter_set_reset(mtree);
  1073. ARCHIVE_RB_TREE_FOREACH(n, &(np->dir_info->rbtree)) {
  1074. struct mtree_entry *e = (struct mtree_entry *)n;
  1075. if (attr_counter_set_collect(mtree, e) < 0) {
  1076. archive_set_error(&a->archive, ENOMEM,
  1077. "Can't allocate memory");
  1078. return (ARCHIVE_FATAL);
  1079. }
  1080. }
  1081. }
  1082. if (!np->dir_info->virtual || mtree->classic) {
  1083. ret = write_mtree_entry(a, np);
  1084. if (ret != ARCHIVE_OK)
  1085. return (ARCHIVE_FATAL);
  1086. } else {
  1087. /* Whenever output_global_set is enabled
  1088. * output global value(/set keywords)
  1089. * even if the directory entry is not allowed
  1090. * to be written because the global values
  1091. * can be used for the children. */
  1092. if (mtree->output_global_set)
  1093. write_global(mtree);
  1094. }
  1095. /*
  1096. * Output the attribute of all files except directory files.
  1097. */
  1098. mtree->depth++;
  1099. ARCHIVE_RB_TREE_FOREACH(n, &(np->dir_info->rbtree)) {
  1100. struct mtree_entry *e = (struct mtree_entry *)n;
  1101. if (e->dir_info)
  1102. mtree_entry_add_child_tail(np, e);
  1103. else {
  1104. ret = write_mtree_entry(a, e);
  1105. if (ret != ARCHIVE_OK)
  1106. return (ARCHIVE_FATAL);
  1107. }
  1108. }
  1109. mtree->depth--;
  1110. if (np->dir_info->children.first != NULL) {
  1111. /*
  1112. * Descend the tree.
  1113. */
  1114. np = np->dir_info->children.first;
  1115. if (mtree->indent)
  1116. mtree->depth++;
  1117. continue;
  1118. } else if (mtree->classic) {
  1119. /*
  1120. * While printing mtree classic, if there are not
  1121. * any directory files(except "." and "..") in the
  1122. * directory, output two dots ".." as returning
  1123. * the parent directory.
  1124. */
  1125. ret = write_dot_dot_entry(a, np);
  1126. if (ret != ARCHIVE_OK)
  1127. return (ARCHIVE_FATAL);
  1128. }
  1129. while (np != np->parent) {
  1130. if (np->dir_info->chnext == NULL) {
  1131. /*
  1132. * Ascend the tree; go back to the parent.
  1133. */
  1134. if (mtree->indent)
  1135. mtree->depth--;
  1136. if (mtree->classic) {
  1137. ret = write_dot_dot_entry(a,
  1138. np->parent);
  1139. if (ret != ARCHIVE_OK)
  1140. return (ARCHIVE_FATAL);
  1141. }
  1142. np = np->parent;
  1143. } else {
  1144. /*
  1145. * Switch to next mtree entry in the directory.
  1146. */
  1147. np = np->dir_info->chnext;
  1148. break;
  1149. }
  1150. }
  1151. } while (np != np->parent);
  1152. return (ARCHIVE_OK);
  1153. }
  1154. static int
  1155. archive_write_mtree_finish_entry(struct archive_write *a)
  1156. {
  1157. struct mtree_writer *mtree = a->format_data;
  1158. struct mtree_entry *me;
  1159. if ((me = mtree->mtree_entry) == NULL)
  1160. return (ARCHIVE_OK);
  1161. mtree->mtree_entry = NULL;
  1162. if (me->reg_info)
  1163. sum_final(mtree, me->reg_info);
  1164. return (ARCHIVE_OK);
  1165. }
  1166. static int
  1167. archive_write_mtree_close(struct archive_write *a)
  1168. {
  1169. struct mtree_writer *mtree= a->format_data;
  1170. int ret;
  1171. if (mtree->root != NULL) {
  1172. ret = write_mtree_entry_tree(a);
  1173. if (ret != ARCHIVE_OK)
  1174. return (ARCHIVE_FATAL);
  1175. }
  1176. archive_write_set_bytes_in_last_block(&a->archive, 1);
  1177. return __archive_write_output(a, mtree->buf.s, mtree->buf.length);
  1178. }
  1179. static ssize_t
  1180. archive_write_mtree_data(struct archive_write *a, const void *buff, size_t n)
  1181. {
  1182. struct mtree_writer *mtree= a->format_data;
  1183. if (n > mtree->entry_bytes_remaining)
  1184. n = (size_t)mtree->entry_bytes_remaining;
  1185. mtree->entry_bytes_remaining -= n;
  1186. /* We don't need to compute a regular file sum */
  1187. if (mtree->mtree_entry == NULL)
  1188. return (n);
  1189. if (mtree->mtree_entry->filetype == AE_IFREG)
  1190. sum_update(mtree, buff, n);
  1191. return (n);
  1192. }
  1193. static int
  1194. archive_write_mtree_free(struct archive_write *a)
  1195. {
  1196. struct mtree_writer *mtree= a->format_data;
  1197. if (mtree == NULL)
  1198. return (ARCHIVE_OK);
  1199. /* Make sure we do not leave any entries. */
  1200. mtree_entry_register_free(mtree);
  1201. archive_string_free(&mtree->cur_dirstr);
  1202. archive_string_free(&mtree->ebuf);
  1203. archive_string_free(&mtree->buf);
  1204. attr_counter_set_free(mtree);
  1205. free(mtree);
  1206. a->format_data = NULL;
  1207. return (ARCHIVE_OK);
  1208. }
  1209. static int
  1210. archive_write_mtree_options(struct archive_write *a, const char *key,
  1211. const char *value)
  1212. {
  1213. struct mtree_writer *mtree= a->format_data;
  1214. int keybit = 0;
  1215. switch (key[0]) {
  1216. case 'a':
  1217. if (strcmp(key, "all") == 0)
  1218. keybit = ~0;
  1219. break;
  1220. case 'c':
  1221. if (strcmp(key, "cksum") == 0)
  1222. keybit = F_CKSUM;
  1223. break;
  1224. case 'd':
  1225. if (strcmp(key, "device") == 0)
  1226. keybit = F_DEV;
  1227. else if (strcmp(key, "dironly") == 0) {
  1228. mtree->dironly = (value != NULL)? 1: 0;
  1229. return (ARCHIVE_OK);
  1230. }
  1231. break;
  1232. case 'f':
  1233. if (strcmp(key, "flags") == 0)
  1234. keybit = F_FLAGS;
  1235. break;
  1236. case 'g':
  1237. if (strcmp(key, "gid") == 0)
  1238. keybit = F_GID;
  1239. else if (strcmp(key, "gname") == 0)
  1240. keybit = F_GNAME;
  1241. break;
  1242. case 'i':
  1243. if (strcmp(key, "indent") == 0) {
  1244. mtree->indent = (value != NULL)? 1: 0;
  1245. return (ARCHIVE_OK);
  1246. } else if (strcmp(key, "inode") == 0) {
  1247. keybit = F_INO;
  1248. }
  1249. break;
  1250. case 'l':
  1251. if (strcmp(key, "link") == 0)
  1252. keybit = F_SLINK;
  1253. break;
  1254. case 'm':
  1255. if (strcmp(key, "md5") == 0 ||
  1256. strcmp(key, "md5digest") == 0)
  1257. keybit = F_MD5;
  1258. if (strcmp(key, "mode") == 0)
  1259. keybit = F_MODE;
  1260. break;
  1261. case 'n':
  1262. if (strcmp(key, "nlink") == 0)
  1263. keybit = F_NLINK;
  1264. break;
  1265. case 'r':
  1266. if (strcmp(key, "resdevice") == 0) {
  1267. keybit = F_RESDEV;
  1268. } else if (strcmp(key, "ripemd160digest") == 0 ||
  1269. strcmp(key, "rmd160") == 0 ||
  1270. strcmp(key, "rmd160digest") == 0)
  1271. keybit = F_RMD160;
  1272. break;
  1273. case 's':
  1274. if (strcmp(key, "sha1") == 0 ||
  1275. strcmp(key, "sha1digest") == 0)
  1276. keybit = F_SHA1;
  1277. if (strcmp(key, "sha256") == 0 ||
  1278. strcmp(key, "sha256digest") == 0)
  1279. keybit = F_SHA256;
  1280. if (strcmp(key, "sha384") == 0 ||
  1281. strcmp(key, "sha384digest") == 0)
  1282. keybit = F_SHA384;
  1283. if (strcmp(key, "sha512") == 0 ||
  1284. strcmp(key, "sha512digest") == 0)
  1285. keybit = F_SHA512;
  1286. if (strcmp(key, "size") == 0)
  1287. keybit = F_SIZE;
  1288. break;
  1289. case 't':
  1290. if (strcmp(key, "time") == 0)
  1291. keybit = F_TIME;
  1292. else if (strcmp(key, "type") == 0)
  1293. keybit = F_TYPE;
  1294. break;
  1295. case 'u':
  1296. if (strcmp(key, "uid") == 0)
  1297. keybit = F_UID;
  1298. else if (strcmp(key, "uname") == 0)
  1299. keybit = F_UNAME;
  1300. else if (strcmp(key, "use-set") == 0) {
  1301. mtree->output_global_set = (value != NULL)? 1: 0;
  1302. return (ARCHIVE_OK);
  1303. }
  1304. break;
  1305. }
  1306. if (keybit != 0) {
  1307. if (value != NULL)
  1308. mtree->keys |= keybit;
  1309. else
  1310. mtree->keys &= ~keybit;
  1311. return (ARCHIVE_OK);
  1312. }
  1313. /* Note: The "warn" return is just to inform the options
  1314. * supervisor that we didn't handle it. It will generate
  1315. * a suitable error if no one used this option. */
  1316. return (ARCHIVE_WARN);
  1317. }
  1318. static int
  1319. archive_write_set_format_mtree_default(struct archive *_a, const char *fn)
  1320. {
  1321. struct archive_write *a = (struct archive_write *)_a;
  1322. struct mtree_writer *mtree;
  1323. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW, fn);
  1324. if (a->format_free != NULL)
  1325. (a->format_free)(a);
  1326. if ((mtree = calloc(1, sizeof(*mtree))) == NULL) {
  1327. archive_set_error(&a->archive, ENOMEM,
  1328. "Can't allocate mtree data");
  1329. return (ARCHIVE_FATAL);
  1330. }
  1331. mtree->mtree_entry = NULL;
  1332. mtree->first = 1;
  1333. memset(&(mtree->set), 0, sizeof(mtree->set));
  1334. mtree->keys = DEFAULT_KEYS;
  1335. mtree->dironly = 0;
  1336. mtree->indent = 0;
  1337. archive_string_init(&mtree->ebuf);
  1338. archive_string_init(&mtree->buf);
  1339. mtree_entry_register_init(mtree);
  1340. a->format_data = mtree;
  1341. a->format_free = archive_write_mtree_free;
  1342. a->format_name = "mtree";
  1343. a->format_options = archive_write_mtree_options;
  1344. a->format_write_header = archive_write_mtree_header;
  1345. a->format_close = archive_write_mtree_close;
  1346. a->format_write_data = archive_write_mtree_data;
  1347. a->format_finish_entry = archive_write_mtree_finish_entry;
  1348. a->archive.archive_format = ARCHIVE_FORMAT_MTREE;
  1349. a->archive.archive_format_name = "mtree";
  1350. return (ARCHIVE_OK);
  1351. }
  1352. int
  1353. archive_write_set_format_mtree(struct archive *_a)
  1354. {
  1355. return archive_write_set_format_mtree_default(_a,
  1356. "archive_write_set_format_mtree");
  1357. }
  1358. int
  1359. archive_write_set_format_mtree_classic(struct archive *_a)
  1360. {
  1361. int r;
  1362. r = archive_write_set_format_mtree_default(_a,
  1363. "archive_write_set_format_mtree_classic");
  1364. if (r == ARCHIVE_OK) {
  1365. struct archive_write *a = (struct archive_write *)_a;
  1366. struct mtree_writer *mtree;
  1367. mtree = (struct mtree_writer *)a->format_data;
  1368. /* Set to output a mtree archive in classic format. */
  1369. mtree->classic = 1;
  1370. /* Basically, mtree classic format uses '/set' global
  1371. * value. */
  1372. mtree->output_global_set = 1;
  1373. }
  1374. return (r);
  1375. }
  1376. static void
  1377. sum_init(struct mtree_writer *mtree)
  1378. {
  1379. mtree->compute_sum = 0;
  1380. if (mtree->keys & F_CKSUM) {
  1381. mtree->compute_sum |= F_CKSUM;
  1382. mtree->crc = 0;
  1383. mtree->crc_len = 0;
  1384. }
  1385. #ifdef ARCHIVE_HAS_MD5
  1386. if (mtree->keys & F_MD5) {
  1387. if (archive_md5_init(&mtree->md5ctx) == ARCHIVE_OK)
  1388. mtree->compute_sum |= F_MD5;
  1389. else
  1390. mtree->keys &= ~F_MD5;/* Not supported. */
  1391. }
  1392. #endif
  1393. #ifdef ARCHIVE_HAS_RMD160
  1394. if (mtree->keys & F_RMD160) {
  1395. if (archive_rmd160_init(&mtree->rmd160ctx) == ARCHIVE_OK)
  1396. mtree->compute_sum |= F_RMD160;
  1397. else
  1398. mtree->keys &= ~F_RMD160;/* Not supported. */
  1399. }
  1400. #endif
  1401. #ifdef ARCHIVE_HAS_SHA1
  1402. if (mtree->keys & F_SHA1) {
  1403. if (archive_sha1_init(&mtree->sha1ctx) == ARCHIVE_OK)
  1404. mtree->compute_sum |= F_SHA1;
  1405. else
  1406. mtree->keys &= ~F_SHA1;/* Not supported. */
  1407. }
  1408. #endif
  1409. #ifdef ARCHIVE_HAS_SHA256
  1410. if (mtree->keys & F_SHA256) {
  1411. if (archive_sha256_init(&mtree->sha256ctx) == ARCHIVE_OK)
  1412. mtree->compute_sum |= F_SHA256;
  1413. else
  1414. mtree->keys &= ~F_SHA256;/* Not supported. */
  1415. }
  1416. #endif
  1417. #ifdef ARCHIVE_HAS_SHA384
  1418. if (mtree->keys & F_SHA384) {
  1419. if (archive_sha384_init(&mtree->sha384ctx) == ARCHIVE_OK)
  1420. mtree->compute_sum |= F_SHA384;
  1421. else
  1422. mtree->keys &= ~F_SHA384;/* Not supported. */
  1423. }
  1424. #endif
  1425. #ifdef ARCHIVE_HAS_SHA512
  1426. if (mtree->keys & F_SHA512) {
  1427. if (archive_sha512_init(&mtree->sha512ctx) == ARCHIVE_OK)
  1428. mtree->compute_sum |= F_SHA512;
  1429. else
  1430. mtree->keys &= ~F_SHA512;/* Not supported. */
  1431. }
  1432. #endif
  1433. }
  1434. static void
  1435. sum_update(struct mtree_writer *mtree, const void *buff, size_t n)
  1436. {
  1437. if (mtree->compute_sum & F_CKSUM) {
  1438. /*
  1439. * Compute a POSIX 1003.2 checksum
  1440. */
  1441. const unsigned char *p;
  1442. size_t nn;
  1443. for (nn = n, p = buff; nn--; ++p)
  1444. COMPUTE_CRC(mtree->crc, *p);
  1445. mtree->crc_len += n;
  1446. }
  1447. #ifdef ARCHIVE_HAS_MD5
  1448. if (mtree->compute_sum & F_MD5) {
  1449. archive_md5_update(&mtree->md5ctx, buff, n);
  1450. mtree->mtree_entry->reg_info->mset_digest &=
  1451. ~AE_MSET_DIGEST_MD5;
  1452. }
  1453. #endif
  1454. #ifdef ARCHIVE_HAS_RMD160
  1455. if (mtree->compute_sum & F_RMD160) {
  1456. archive_rmd160_update(&mtree->rmd160ctx, buff, n);
  1457. mtree->mtree_entry->reg_info->mset_digest &=
  1458. ~AE_MSET_DIGEST_RMD160;
  1459. }
  1460. #endif
  1461. #ifdef ARCHIVE_HAS_SHA1
  1462. if (mtree->compute_sum & F_SHA1) {
  1463. archive_sha1_update(&mtree->sha1ctx, buff, n);
  1464. mtree->mtree_entry->reg_info->mset_digest &=
  1465. ~AE_MSET_DIGEST_SHA1;
  1466. }
  1467. #endif
  1468. #ifdef ARCHIVE_HAS_SHA256
  1469. if (mtree->compute_sum & F_SHA256) {
  1470. archive_sha256_update(&mtree->sha256ctx, buff, n);
  1471. mtree->mtree_entry->reg_info->mset_digest &=
  1472. ~AE_MSET_DIGEST_SHA256;
  1473. }
  1474. #endif
  1475. #ifdef ARCHIVE_HAS_SHA384
  1476. if (mtree->compute_sum & F_SHA384) {
  1477. archive_sha384_update(&mtree->sha384ctx, buff, n);
  1478. mtree->mtree_entry->reg_info->mset_digest &=
  1479. ~AE_MSET_DIGEST_SHA384;
  1480. }
  1481. #endif
  1482. #ifdef ARCHIVE_HAS_SHA512
  1483. if (mtree->compute_sum & F_SHA512) {
  1484. archive_sha512_update(&mtree->sha512ctx, buff, n);
  1485. mtree->mtree_entry->reg_info->mset_digest &=
  1486. ~AE_MSET_DIGEST_SHA512;
  1487. }
  1488. #endif
  1489. }
  1490. static void
  1491. sum_final(struct mtree_writer *mtree, struct reg_info *reg)
  1492. {
  1493. if (mtree->compute_sum & F_CKSUM) {
  1494. uint64_t len;
  1495. /* Include the length of the file. */
  1496. for (len = mtree->crc_len; len != 0; len >>= 8)
  1497. COMPUTE_CRC(mtree->crc, len & 0xff);
  1498. reg->crc = ~mtree->crc;
  1499. }
  1500. #ifdef ARCHIVE_HAS_MD5
  1501. if ((mtree->compute_sum & F_MD5)
  1502. && !(reg->mset_digest & AE_MSET_DIGEST_MD5))
  1503. archive_md5_final(&mtree->md5ctx, reg->digest.md5);
  1504. #endif
  1505. #ifdef ARCHIVE_HAS_RMD160
  1506. if ((mtree->compute_sum & F_RMD160)
  1507. && !(reg->mset_digest & AE_MSET_DIGEST_RMD160))
  1508. archive_rmd160_final(&mtree->rmd160ctx, reg->digest.rmd160);
  1509. #endif
  1510. #ifdef ARCHIVE_HAS_SHA1
  1511. if ((mtree->compute_sum & F_SHA1)
  1512. && !(reg->mset_digest & AE_MSET_DIGEST_SHA1))
  1513. archive_sha1_final(&mtree->sha1ctx, reg->digest.sha1);
  1514. #endif
  1515. #ifdef ARCHIVE_HAS_SHA256
  1516. if ((mtree->compute_sum & F_SHA256)
  1517. && !(reg->mset_digest & AE_MSET_DIGEST_SHA256))
  1518. archive_sha256_final(&mtree->sha256ctx, reg->digest.sha256);
  1519. #endif
  1520. #ifdef ARCHIVE_HAS_SHA384
  1521. if ((mtree->compute_sum & F_SHA384)
  1522. && !(reg->mset_digest & AE_MSET_DIGEST_SHA384))
  1523. archive_sha384_final(&mtree->sha384ctx, reg->digest.sha384);
  1524. #endif
  1525. #ifdef ARCHIVE_HAS_SHA512
  1526. if ((mtree->compute_sum & F_SHA512)
  1527. && !(reg->mset_digest & AE_MSET_DIGEST_SHA512))
  1528. archive_sha512_final(&mtree->sha512ctx, reg->digest.sha512);
  1529. #endif
  1530. /* Save what types of sum are computed. */
  1531. reg->compute_sum = mtree->compute_sum;
  1532. }
  1533. #if defined(ARCHIVE_HAS_MD5) || defined(ARCHIVE_HAS_RMD160) || \
  1534. defined(ARCHIVE_HAS_SHA1) || defined(ARCHIVE_HAS_SHA256) || \
  1535. defined(ARCHIVE_HAS_SHA384) || defined(ARCHIVE_HAS_SHA512)
  1536. static void
  1537. strappend_bin(struct archive_string *s, const unsigned char *bin, int n)
  1538. {
  1539. static const char hex[] = "0123456789abcdef";
  1540. int i;
  1541. for (i = 0; i < n; i++) {
  1542. archive_strappend_char(s, hex[bin[i] >> 4]);
  1543. archive_strappend_char(s, hex[bin[i] & 0x0f]);
  1544. }
  1545. }
  1546. #endif
  1547. static void
  1548. sum_write(struct archive_string *str, struct reg_info *reg)
  1549. {
  1550. if (reg->compute_sum & F_CKSUM) {
  1551. archive_string_sprintf(str, " cksum=%ju",
  1552. (uintmax_t)reg->crc);
  1553. }
  1554. #define append_digest(_s, _r, _t) \
  1555. strappend_bin(_s, _r->digest._t, sizeof(_r->digest._t))
  1556. #ifdef ARCHIVE_HAS_MD5
  1557. if (reg->compute_sum & F_MD5) {
  1558. archive_strcat(str, " md5digest=");
  1559. append_digest(str, reg, md5);
  1560. }
  1561. #endif
  1562. #ifdef ARCHIVE_HAS_RMD160
  1563. if (reg->compute_sum & F_RMD160) {
  1564. archive_strcat(str, " rmd160digest=");
  1565. append_digest(str, reg, rmd160);
  1566. }
  1567. #endif
  1568. #ifdef ARCHIVE_HAS_SHA1
  1569. if (reg->compute_sum & F_SHA1) {
  1570. archive_strcat(str, " sha1digest=");
  1571. append_digest(str, reg, sha1);
  1572. }
  1573. #endif
  1574. #ifdef ARCHIVE_HAS_SHA256
  1575. if (reg->compute_sum & F_SHA256) {
  1576. archive_strcat(str, " sha256digest=");
  1577. append_digest(str, reg, sha256);
  1578. }
  1579. #endif
  1580. #ifdef ARCHIVE_HAS_SHA384
  1581. if (reg->compute_sum & F_SHA384) {
  1582. archive_strcat(str, " sha384digest=");
  1583. append_digest(str, reg, sha384);
  1584. }
  1585. #endif
  1586. #ifdef ARCHIVE_HAS_SHA512
  1587. if (reg->compute_sum & F_SHA512) {
  1588. archive_strcat(str, " sha512digest=");
  1589. append_digest(str, reg, sha512);
  1590. }
  1591. #endif
  1592. #undef append_digest
  1593. }
  1594. static int
  1595. mtree_entry_cmp_node(const struct archive_rb_node *n1,
  1596. const struct archive_rb_node *n2)
  1597. {
  1598. const struct mtree_entry *e1 = (const struct mtree_entry *)n1;
  1599. const struct mtree_entry *e2 = (const struct mtree_entry *)n2;
  1600. return (strcmp(e2->basename.s, e1->basename.s));
  1601. }
  1602. static int
  1603. mtree_entry_cmp_key(const struct archive_rb_node *n, const void *key)
  1604. {
  1605. const struct mtree_entry *e = (const struct mtree_entry *)n;
  1606. return (strcmp((const char *)key, e->basename.s));
  1607. }
  1608. #if defined(_WIN32) || defined(__CYGWIN__)
  1609. static int
  1610. cleanup_backslash_1(char *p)
  1611. {
  1612. int mb, dos;
  1613. mb = dos = 0;
  1614. while (*p) {
  1615. if (*(unsigned char *)p > 127)
  1616. mb = 1;
  1617. if (*p == '\\') {
  1618. /* If we have not met any multi-byte characters,
  1619. * we can replace '\' with '/'. */
  1620. if (!mb)
  1621. *p = '/';
  1622. dos = 1;
  1623. }
  1624. p++;
  1625. }
  1626. if (!mb || !dos)
  1627. return (0);
  1628. return (-1);
  1629. }
  1630. static void
  1631. cleanup_backslash_2(wchar_t *p)
  1632. {
  1633. /* Convert a path-separator from '\' to '/' */
  1634. while (*p != L'\0') {
  1635. if (*p == L'\\')
  1636. *p = L'/';
  1637. p++;
  1638. }
  1639. }
  1640. #endif
  1641. /*
  1642. * Generate a parent directory name and a base name from a pathname.
  1643. */
  1644. static int
  1645. mtree_entry_setup_filenames(struct archive_write *a, struct mtree_entry *file,
  1646. struct archive_entry *entry)
  1647. {
  1648. const char *pathname;
  1649. char *p, *dirname, *slash;
  1650. size_t len;
  1651. int ret = ARCHIVE_OK;
  1652. archive_strcpy(&file->pathname, archive_entry_pathname(entry));
  1653. #if defined(_WIN32) || defined(__CYGWIN__)
  1654. /*
  1655. * Convert a path-separator from '\' to '/'
  1656. */
  1657. if (cleanup_backslash_1(file->pathname.s) != 0) {
  1658. const wchar_t *wp = archive_entry_pathname_w(entry);
  1659. struct archive_wstring ws;
  1660. if (wp != NULL) {
  1661. int r;
  1662. archive_string_init(&ws);
  1663. archive_wstrcpy(&ws, wp);
  1664. cleanup_backslash_2(ws.s);
  1665. archive_string_empty(&(file->pathname));
  1666. r = archive_string_append_from_wcs(&(file->pathname),
  1667. ws.s, ws.length);
  1668. archive_wstring_free(&ws);
  1669. if (r < 0 && errno == ENOMEM) {
  1670. archive_set_error(&a->archive, ENOMEM,
  1671. "Can't allocate memory");
  1672. return (ARCHIVE_FATAL);
  1673. }
  1674. }
  1675. }
  1676. #else
  1677. (void)a; /* UNUSED */
  1678. #endif
  1679. pathname = file->pathname.s;
  1680. if (strcmp(pathname, ".") == 0) {
  1681. archive_strcpy(&file->basename, ".");
  1682. return (ARCHIVE_OK);
  1683. }
  1684. archive_strcpy(&(file->parentdir), pathname);
  1685. len = file->parentdir.length;
  1686. p = dirname = file->parentdir.s;
  1687. /*
  1688. * Remove leading '/' and '../' elements
  1689. */
  1690. while (*p) {
  1691. if (p[0] == '/') {
  1692. p++;
  1693. len--;
  1694. } else if (p[0] != '.')
  1695. break;
  1696. else if (p[1] == '.' && p[2] == '/') {
  1697. p += 3;
  1698. len -= 3;
  1699. } else
  1700. break;
  1701. }
  1702. if (p != dirname) {
  1703. memmove(dirname, p, len+1);
  1704. p = dirname;
  1705. }
  1706. /*
  1707. * Remove "/","/." and "/.." elements from tail.
  1708. */
  1709. while (len > 0) {
  1710. size_t ll = len;
  1711. if (len > 0 && p[len-1] == '/') {
  1712. p[len-1] = '\0';
  1713. len--;
  1714. }
  1715. if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
  1716. p[len-2] = '\0';
  1717. len -= 2;
  1718. }
  1719. if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
  1720. p[len-1] == '.') {
  1721. p[len-3] = '\0';
  1722. len -= 3;
  1723. }
  1724. if (ll == len)
  1725. break;
  1726. }
  1727. while (*p) {
  1728. if (p[0] == '/') {
  1729. if (p[1] == '/')
  1730. /* Convert '//' --> '/' */
  1731. memmove(p, p+1, strlen(p+1) + 1);
  1732. else if (p[1] == '.' && p[2] == '/')
  1733. /* Convert '/./' --> '/' */
  1734. memmove(p, p+2, strlen(p+2) + 1);
  1735. else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
  1736. /* Convert 'dir/dir1/../dir2/'
  1737. * --> 'dir/dir2/'
  1738. */
  1739. char *rp = p -1;
  1740. while (rp >= dirname) {
  1741. if (*rp == '/')
  1742. break;
  1743. --rp;
  1744. }
  1745. if (rp > dirname) {
  1746. strcpy(rp, p+3);
  1747. p = rp;
  1748. } else {
  1749. strcpy(dirname, p+4);
  1750. p = dirname;
  1751. }
  1752. } else
  1753. p++;
  1754. } else
  1755. p++;
  1756. }
  1757. p = dirname;
  1758. len = strlen(p);
  1759. /*
  1760. * Add "./" prefix.
  1761. * NOTE: If the pathname does not have a path separator, we have
  1762. * to add "./" to the head of the pathname because mtree reader
  1763. * will suppose that it is v1(a.k.a classic) mtree format and
  1764. * change the directory unexpectedly and so it will make a wrong
  1765. * path.
  1766. */
  1767. if (strcmp(p, ".") != 0 && strncmp(p, "./", 2) != 0) {
  1768. struct archive_string as;
  1769. archive_string_init(&as);
  1770. archive_strcpy(&as, "./");
  1771. archive_strncat(&as, p, len);
  1772. archive_string_empty(&file->parentdir);
  1773. archive_string_concat(&file->parentdir, &as);
  1774. archive_string_free(&as);
  1775. p = file->parentdir.s;
  1776. len = archive_strlen(&file->parentdir);
  1777. }
  1778. /*
  1779. * Find out the position which points the last position of
  1780. * path separator('/').
  1781. */
  1782. slash = NULL;
  1783. for (; *p != '\0'; p++) {
  1784. if (*p == '/')
  1785. slash = p;
  1786. }
  1787. if (slash == NULL) {
  1788. /* The pathname doesn't have a parent directory. */
  1789. file->parentdir.length = len;
  1790. archive_string_copy(&(file->basename), &(file->parentdir));
  1791. archive_string_empty(&(file->parentdir));
  1792. *file->parentdir.s = '\0';
  1793. return (ret);
  1794. }
  1795. /* Make a basename from file->parentdir.s and slash */
  1796. *slash = '\0';
  1797. file->parentdir.length = slash - file->parentdir.s;
  1798. archive_strcpy(&(file->basename), slash + 1);
  1799. return (ret);
  1800. }
  1801. static int
  1802. mtree_entry_create_virtual_dir(struct archive_write *a, const char *pathname,
  1803. struct mtree_entry **m_entry)
  1804. {
  1805. struct archive_entry *entry;
  1806. struct mtree_entry *file;
  1807. int r;
  1808. entry = archive_entry_new();
  1809. if (entry == NULL) {
  1810. *m_entry = NULL;
  1811. archive_set_error(&a->archive, ENOMEM,
  1812. "Can't allocate memory");
  1813. return (ARCHIVE_FATAL);
  1814. }
  1815. archive_entry_copy_pathname(entry, pathname);
  1816. archive_entry_set_mode(entry, AE_IFDIR | 0755);
  1817. archive_entry_set_mtime(entry, time(NULL), 0);
  1818. r = mtree_entry_new(a, entry, &file);
  1819. archive_entry_free(entry);
  1820. if (r < ARCHIVE_WARN) {
  1821. *m_entry = NULL;
  1822. archive_set_error(&a->archive, ENOMEM,
  1823. "Can't allocate memory");
  1824. return (ARCHIVE_FATAL);
  1825. }
  1826. file->dir_info->virtual = 1;
  1827. *m_entry = file;
  1828. return (ARCHIVE_OK);
  1829. }
  1830. static void
  1831. mtree_entry_register_add(struct mtree_writer *mtree, struct mtree_entry *file)
  1832. {
  1833. file->next = NULL;
  1834. *mtree->file_list.last = file;
  1835. mtree->file_list.last = &(file->next);
  1836. }
  1837. static void
  1838. mtree_entry_register_init(struct mtree_writer *mtree)
  1839. {
  1840. mtree->file_list.first = NULL;
  1841. mtree->file_list.last = &(mtree->file_list.first);
  1842. }
  1843. static void
  1844. mtree_entry_register_free(struct mtree_writer *mtree)
  1845. {
  1846. struct mtree_entry *file, *file_next;
  1847. file = mtree->file_list.first;
  1848. while (file != NULL) {
  1849. file_next = file->next;
  1850. mtree_entry_free(file);
  1851. file = file_next;
  1852. }
  1853. }
  1854. static int
  1855. mtree_entry_add_child_tail(struct mtree_entry *parent,
  1856. struct mtree_entry *child)
  1857. {
  1858. child->dir_info->chnext = NULL;
  1859. *parent->dir_info->children.last = child;
  1860. parent->dir_info->children.last = &(child->dir_info->chnext);
  1861. return (1);
  1862. }
  1863. /*
  1864. * Find a entry from a parent entry with the name.
  1865. */
  1866. static struct mtree_entry *
  1867. mtree_entry_find_child(struct mtree_entry *parent, const char *child_name)
  1868. {
  1869. struct mtree_entry *np;
  1870. if (parent == NULL)
  1871. return (NULL);
  1872. np = (struct mtree_entry *)__archive_rb_tree_find_node(
  1873. &(parent->dir_info->rbtree), child_name);
  1874. return (np);
  1875. }
  1876. static int
  1877. get_path_component(char *name, size_t n, const char *fn)
  1878. {
  1879. char *p;
  1880. size_t l;
  1881. p = strchr(fn, '/');
  1882. if (p == NULL) {
  1883. if ((l = strlen(fn)) == 0)
  1884. return (0);
  1885. } else
  1886. l = p - fn;
  1887. if (l > n -1)
  1888. return (-1);
  1889. memcpy(name, fn, l);
  1890. name[l] = '\0';
  1891. return ((int)l);
  1892. }
  1893. /*
  1894. * Add a new entry into the tree.
  1895. */
  1896. static int
  1897. mtree_entry_tree_add(struct archive_write *a, struct mtree_entry **filep)
  1898. {
  1899. #if defined(_WIN32) && !defined(__CYGWIN__)
  1900. char name[_MAX_FNAME];/* Included null terminator size. */
  1901. #elif defined(NAME_MAX) && NAME_MAX >= 255
  1902. char name[NAME_MAX+1];
  1903. #else
  1904. char name[256];
  1905. #endif
  1906. struct mtree_writer *mtree = (struct mtree_writer *)a->format_data;
  1907. struct mtree_entry *dent, *file, *np;
  1908. const char *fn, *p;
  1909. int l, r;
  1910. file = *filep;
  1911. if (file->parentdir.length == 0 && file->basename.length == 1 &&
  1912. file->basename.s[0] == '.') {
  1913. file->parent = file;
  1914. if (mtree->root != NULL) {
  1915. np = mtree->root;
  1916. goto same_entry;
  1917. }
  1918. mtree->root = file;
  1919. mtree_entry_register_add(mtree, file);
  1920. return (ARCHIVE_OK);
  1921. }
  1922. if (file->parentdir.length == 0) {
  1923. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1924. "Internal programming error "
  1925. "in generating canonical name for %s",
  1926. file->pathname.s);
  1927. return (ARCHIVE_FAILED);
  1928. }
  1929. fn = p = file->parentdir.s;
  1930. /*
  1931. * If the path of the parent directory of `file' entry is
  1932. * the same as the path of `cur_dirent', add `file' entry to
  1933. * `cur_dirent'.
  1934. */
  1935. if (archive_strlen(&(mtree->cur_dirstr))
  1936. == archive_strlen(&(file->parentdir)) &&
  1937. strcmp(mtree->cur_dirstr.s, fn) == 0) {
  1938. if (!__archive_rb_tree_insert_node(
  1939. &(mtree->cur_dirent->dir_info->rbtree),
  1940. (struct archive_rb_node *)file)) {
  1941. /* There is the same name in the tree. */
  1942. np = (struct mtree_entry *)__archive_rb_tree_find_node(
  1943. &(mtree->cur_dirent->dir_info->rbtree),
  1944. file->basename.s);
  1945. goto same_entry;
  1946. }
  1947. file->parent = mtree->cur_dirent;
  1948. mtree_entry_register_add(mtree, file);
  1949. return (ARCHIVE_OK);
  1950. }
  1951. dent = mtree->root;
  1952. for (;;) {
  1953. l = get_path_component(name, sizeof(name), fn);
  1954. if (l == 0) {
  1955. np = NULL;
  1956. break;
  1957. }
  1958. if (l < 0) {
  1959. archive_set_error(&a->archive,
  1960. ARCHIVE_ERRNO_MISC,
  1961. "A name buffer is too small");
  1962. return (ARCHIVE_FATAL);
  1963. }
  1964. if (l == 1 && name[0] == '.' && dent != NULL &&
  1965. dent == mtree->root) {
  1966. fn += l;
  1967. if (fn[0] == '/')
  1968. fn++;
  1969. continue;
  1970. }
  1971. np = mtree_entry_find_child(dent, name);
  1972. if (np == NULL || fn[0] == '\0')
  1973. break;
  1974. /* Find next sub directory. */
  1975. if (!np->dir_info) {
  1976. /* NOT Directory! */
  1977. archive_set_error(&a->archive,
  1978. ARCHIVE_ERRNO_MISC,
  1979. "`%s' is not directory, we cannot insert `%s' ",
  1980. np->pathname.s, file->pathname.s);
  1981. return (ARCHIVE_FAILED);
  1982. }
  1983. fn += l;
  1984. if (fn[0] == '/')
  1985. fn++;
  1986. dent = np;
  1987. }
  1988. if (np == NULL) {
  1989. /*
  1990. * Create virtual parent directories.
  1991. */
  1992. while (fn[0] != '\0') {
  1993. struct mtree_entry *vp;
  1994. struct archive_string as;
  1995. archive_string_init(&as);
  1996. archive_strncat(&as, p, fn - p + l);
  1997. if (as.s[as.length-1] == '/') {
  1998. as.s[as.length-1] = '\0';
  1999. as.length--;
  2000. }
  2001. r = mtree_entry_create_virtual_dir(a, as.s, &vp);
  2002. archive_string_free(&as);
  2003. if (r < ARCHIVE_WARN)
  2004. return (r);
  2005. if (strcmp(vp->pathname.s, ".") == 0) {
  2006. vp->parent = vp;
  2007. mtree->root = vp;
  2008. } else {
  2009. __archive_rb_tree_insert_node(
  2010. &(dent->dir_info->rbtree),
  2011. (struct archive_rb_node *)vp);
  2012. vp->parent = dent;
  2013. }
  2014. mtree_entry_register_add(mtree, vp);
  2015. np = vp;
  2016. fn += l;
  2017. if (fn[0] == '/')
  2018. fn++;
  2019. l = get_path_component(name, sizeof(name), fn);
  2020. if (l < 0) {
  2021. archive_string_free(&as);
  2022. archive_set_error(&a->archive,
  2023. ARCHIVE_ERRNO_MISC,
  2024. "A name buffer is too small");
  2025. return (ARCHIVE_FATAL);
  2026. }
  2027. dent = np;
  2028. }
  2029. /* Found out the parent directory where `file' can be
  2030. * inserted. */
  2031. mtree->cur_dirent = dent;
  2032. archive_string_empty(&(mtree->cur_dirstr));
  2033. if (archive_string_ensure(&(mtree->cur_dirstr),
  2034. archive_strlen(&(dent->parentdir)) +
  2035. archive_strlen(&(dent->basename)) + 2) == NULL) {
  2036. archive_set_error(&a->archive, ENOMEM,
  2037. "Can't allocate memory");
  2038. return (ARCHIVE_FATAL);
  2039. }
  2040. if (archive_strlen(&(dent->parentdir)) +
  2041. archive_strlen(&(dent->basename)) == 0)
  2042. mtree->cur_dirstr.s[0] = 0;
  2043. else {
  2044. if (archive_strlen(&(dent->parentdir)) > 0) {
  2045. archive_string_copy(&(mtree->cur_dirstr),
  2046. &(dent->parentdir));
  2047. archive_strappend_char(
  2048. &(mtree->cur_dirstr), '/');
  2049. }
  2050. archive_string_concat(&(mtree->cur_dirstr),
  2051. &(dent->basename));
  2052. }
  2053. if (!__archive_rb_tree_insert_node(
  2054. &(dent->dir_info->rbtree),
  2055. (struct archive_rb_node *)file)) {
  2056. np = (struct mtree_entry *)__archive_rb_tree_find_node(
  2057. &(dent->dir_info->rbtree), file->basename.s);
  2058. goto same_entry;
  2059. }
  2060. file->parent = dent;
  2061. mtree_entry_register_add(mtree, file);
  2062. return (ARCHIVE_OK);
  2063. }
  2064. same_entry:
  2065. /*
  2066. * We have already has the entry the filename of which is
  2067. * the same.
  2068. */
  2069. r = mtree_entry_exchange_same_entry(a, np, file);
  2070. if (r < ARCHIVE_WARN)
  2071. return (r);
  2072. if (np->dir_info)
  2073. np->dir_info->virtual = 0;
  2074. *filep = np;
  2075. mtree_entry_free(file);
  2076. return (ARCHIVE_WARN);
  2077. }
  2078. static int
  2079. mtree_entry_exchange_same_entry(struct archive_write *a, struct mtree_entry *np,
  2080. struct mtree_entry *file)
  2081. {
  2082. if ((np->mode & AE_IFMT) != (file->mode & AE_IFMT)) {
  2083. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2084. "Found duplicate entries `%s' and its file type is "
  2085. "different",
  2086. np->pathname.s);
  2087. return (ARCHIVE_FAILED);
  2088. }
  2089. /* Update the existent mtree entry's attributes by the new one's. */
  2090. archive_string_empty(&np->symlink);
  2091. archive_string_concat(&np->symlink, &file->symlink);
  2092. archive_string_empty(&np->uname);
  2093. archive_string_concat(&np->uname, &file->uname);
  2094. archive_string_empty(&np->gname);
  2095. archive_string_concat(&np->gname, &file->gname);
  2096. archive_string_empty(&np->fflags_text);
  2097. archive_string_concat(&np->fflags_text, &file->fflags_text);
  2098. np->nlink = file->nlink;
  2099. np->filetype = file->filetype;
  2100. np->mode = file->mode;
  2101. np->size = file->size;
  2102. np->uid = file->uid;
  2103. np->gid = file->gid;
  2104. np->fflags_set = file->fflags_set;
  2105. np->fflags_clear = file->fflags_clear;
  2106. np->mtime = file->mtime;
  2107. np->mtime_nsec = file->mtime_nsec;
  2108. np->rdevmajor = file->rdevmajor;
  2109. np->rdevminor = file->rdevminor;
  2110. np->devmajor = file->devmajor;
  2111. np->devminor = file->devminor;
  2112. np->ino = file->ino;
  2113. return (ARCHIVE_WARN);
  2114. }